1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
//A TCP server asking for every clients
//on it's network to send their
//logfiles
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <time.h>
#define MSGSIZE 256
#define UDPPORT 12346
#define TCPPORT 12347
main(int argc, char **argv){
int sockfd, ok, addr_in_size, optval = 1, timeout = 1*60, cc, pid;
u_short portnum = UDPPORT;
struct sockaddr_in *to, *from; //target/source
struct hostent *toinfo;
char ask[] = "ENQ\0", go[] = "ACK\0", msg[MSGSIZE];//Enquiring all the clients (ENQ)/clear to send (ACK)
char* htoname = "255.255.255.255";//broadcasting (initially)
u_long toaddr, fromaddr;
time_t end;
to = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
from = (struct sockaddr_in *)malloc(addr_in_size);
if((toinfo = gethostbyname(htoname)) == NULL){
fprintf(stderr,"Error %d in gethostbyname: %s\n",
errno,sys_errlist[errno]);
exit(errno);
};
toaddr = *(u_long *)toinfo->h_addr_list[0];
addr_in_size = sizeof(struct sockaddr_in);
memset((char *)to,(char)0,addr_in_size);
to->sin_family = AF_INET;
to->sin_addr.s_addr = INADDR_BROADCAST;
to->sin_port = htons(portnum);
if((sockfd = socket (PF_INET, SOCK_DGRAM, 0)) == -1){
fprintf(stderr,"Error %d in socket: %s\n",errno,sys_errlist[errno]);
exit(errno);
};
if(setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (char*)&optval, sizeof(optval))!=0){
fprintf(stderr,"Error %d in setting socket option: %s\n",errno,sys_errlist[errno]);
exit(errno);
}
ok = 1;
printf("Broadcasting logfiles query\n");
if(sendto(sockfd,ask,strlen(ask),0,(struct sockaddr *)to,
addr_in_size) == -1){
fprintf(stderr,"Error %d in sendto: %s\n",errno,sys_errlist[errno]);
exit(errno);
}
printf("Waiting for answers for %d seconds...\n", timeout);
end = time(NULL)+timeout;
while(time(NULL) < end){
if((cc = recvfrom (sockfd,msg,MSGSIZE,0,(struct sockaddr *)from,
&addr_in_size)) == -1){
fprintf(stderr,"Error %d in recvfrom: %s\n",
errno,sys_errlist[errno]);
exit(errno);
}
fromaddr = from->sin_addr.s_addr;
msg[cc] = '\0';
if(strcmp(msg, go) == 0){
pid = fork();
switch(pid){
case 0:
//we are ready to get the file
printf("%s clear to send\n", gethostbyaddr((char *)&fromaddr,
sizeof(fromaddr),
AF_INET));
if(sendto(sockfd,ask,strlen(ask),0,(struct sockaddr *)to,
addr_in_size) == -1){
fprintf(stderr,"Error %d in sendto: %s\n",errno,sys_errlist[errno]);
exit(errno);
}
//Client ready for sending
getFileFromClient(gethostbyaddr((char *)&fromaddr, sizeof(fromaddr), AF_INET));
break;
case -1:
fprintf(stderr,"Error %d in fork: %s\n",errno,sys_errlist[errno]);
exit(errno);
break;
default:
break;
}
}else {
fprintf(stderr, "Error in client answer: %s answered %s, expecting %s\n", gethostbyaddr((char *)&fromaddr,
sizeof(fromaddr),
AF_INET), msg, go);
}
fprintf(stdout,"From %s port %d: %s\n",
(gethostbyaddr((char *)&fromaddr,
sizeof(fromaddr),
AF_INET))->h_name,
from->sin_port,msg);
}
}
int getFileFromClient(const char* from){
int listenfd,connfd,n;
struct sockaddr_in servaddr,cliaddr;
socklen_t clilen;
char mesg[MSGSIZE];
listenfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(32000);
bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
listen(listenfd, TCPPORT);
//Got one client connected
clilen=sizeof(cliaddr);
connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&clilen);
close (listenfd);
for(;;)
{
n = recvfrom(connfd,mesg,1000,0,(struct sockaddr *)&cliaddr,&clilen);
sendto(connfd,mesg,n,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
printf("-------------------------------------------------------\n");
mesg[n] = 0;
printf("Received the following:\n");
printf("%s",mesg);
printf("-------------------------------------------------------\n");
}
close(connfd);
return 0;
} |
Partager