Beispiel #1
0
void metaserver_keepalive(int sockfd, const SA *servaddr, socklen_t servlen)
{
    char         mesg[MAXLINE];
    char        *mesg_ptr;
    uint32_t     handshake=0, command=0;
    SA           addr;
    socklen_t    addrlen;
    unsigned int packet_size;

    packet_size = 0;
    mesg_ptr = pack_uint32(SKEEP_ALIVE, mesg, &packet_size);
    Sendto(sockfd, mesg, packet_size, 0, servaddr, servlen);

    addrlen = sizeof(addr);
    Recvfrom(sockfd, mesg, MAXLINE, 0, &addr, &addrlen);
    mesg_ptr = unpack_uint32(&command, mesg);

    if(command == HANDSHAKE)
    {
        mesg_ptr = unpack_uint32(&handshake, mesg_ptr);
        printf("Server contacted successfully.\n");

        packet_size = 0;
        mesg_ptr = pack_uint32(SERVERSHAKE, mesg, &packet_size);
        mesg_ptr = pack_uint32(handshake, mesg_ptr, &packet_size);

        Sendto(sockfd, mesg, packet_size, 0, servaddr, servlen);
    }
}
void recv_fp(int sockfd) {
	ssize_t		n;
	char		buf[MAXLINE];
	char *res = "new";
	char fp[FP_LEN+1];
	char filename[MAXLINE];
	fp[FP_LEN] = '\0';

	char	recvline[MAXLINE];
	struct sockaddr_in	cliaddr;
	socklen_t	cliaddrlen = sizeof(cliaddr);


	memset(recvline, 0, sizeof(recvline));
	n = Recvfrom(sockfd, recvline, MAXLINE, 0,  (SA *) &cliaddr, &cliaddrlen);

	memcpy(fp, recvline, FP_LEN);
	memcpy(filename, &recvline[FP_LEN], n - FP_LEN);
	filename[n - FP_LEN] = '\0';
	printf("**File FP: %s, File name: %s**\n", fp, filename);

	// look up this fp for dedu in the hash table
	int exists = isExistsFP(fp);
	// printf("recv_fp: isExistsFP=%d\n", exists);
	if(exists){
		res = "duplicated";
		Sendto(sockfd, res, strlen(res), 0, (SA *) &cliaddr, cliaddrlen);
		// addFilename2FP(fp, filename);
	}else{
		Sendto(sockfd, res, strlen(res), 0, (SA *) &cliaddr, cliaddrlen);
		// addFilename2FP(fp, filename);
		insertFP(fp); // just 1 to indicate
	}
}
void transfer_file(char *fp_filename, char *filename){
	char	recvline[MAXLINE];
	char buf[CHUNK_SIZE];
	char *sendline = "file chunk data";
	int fd, res;


	int chunk_count = 4, i = 0;
	FILE *fs = fopen(filename, "r");

	// 1. transfer fp+file name
	printf("-------%s\n", fp_filename);
	Sendto(sockfd2server, fp_filename,strlen(fp_filename), 0,  (SA *) &servaddr, sizeof(servaddr));
	// Write(sockfd, fp_filename, strlen(fp_filename));
	// fp+file name should be a separate packet
	// if (Read(sockfd, recvline, MAXLINE) == 0)
	// 		err_quit("transfer_file: server terminated prematurely");

	// 2. then transfer the whole file chunkly
	while((res = fread(buf, sizeof(char), CHUNK_SIZE, fs)) > 0){
		Sendto(sockfd2server, buf, res, 0,  (SA *) &servaddr, sizeof(servaddr));
		// we DO NOT need reply
		memset(buf, 0, CHUNK_SIZE);
	}

	// 3. eof it
	sendline = "exit";
	Sendto(sockfd2server, sendline, strlen(sendline), 0, (SA *) &servaddr, sizeof(servaddr));

	fclose(fs);
}
Beispiel #4
0
void
sendto_01()
{
	int		sockfd, n;
	socklen_t	len;

	sockfd = TcpSockByAddr("140.252.13.34", 7);		/* echo server */

	/*
	 * This also verifies that we can call sendto() on a TCP socket
	 * if we don't specify a destination address.
	 */

	Sendto(sockfd, "hello", 5, 0, NULL, NULL);

	if ( (n = Recvfrom(sockfd, buff, sizeof(buff), 0, NULL, NULL)) != 5)
		err_quit("! Recvfrom expected 5");

	/*
	 * Now see what happens when we ask for the server's address.
	 * Berkeley-derived implementations do not return this (p. 517, tcpipiv2)
	 * while Solaris does.
	 */

	Sendto(sockfd, "world", 5, 0, NULL, NULL);

	len = sizeof(servaddr) * 2;	/* that's a lie */
	if ( (n = Recvfrom(sockfd, buff, sizeof(buff), 0,
					   (SA *) &servaddr, &len)) != 5)
		err_quit("! Recvfrom expected 5");
	if (len != 0) {
		err_msg("+ recvfrom on TCP socket returns len = %d for sender's addr",
				len);
		if (len == sizeof(servaddr))
			printf("  recvfrom from %s, port %d\n",
				   inet_ntoa(servaddr.sin_addr), ntohs(servaddr.sin_port));
	}

	Close(sockfd);

	/*
	 * Now try and specify a destination address for sendto() on
	 * a TCP socket.
	 */

	sockfd = TcpSockByAddr("140.252.13.34", 7);		/* echo server */

		/* should not work with destination address specified */
	n = sendto(sockfd, "hello1", 6, 0, (SA *) &servaddr, sizeof(servaddr));
	if (n < 0)
		err_ret("sendto on TCP socket specifying dest addr returns error");
	else if (n == 6)
#ifdef	MSG_EOF		/* defined only if T/TCP supported */
		err_msg("+ sendto on TCP socket specifying dest addr OK (T/TCP supported)");
#else
		err_msg("+ sendto on TCP socket specifying dest addr OK");
#endif
	else
Beispiel #5
0
// 等待握手
int WaitShakeHands(char *message, FILE **fp)
{
    printf("Waiting request ...\n");
    Packet fnpack;
    Recvfrom(server_socket, (char *)&fnpack, sizeof(Packet), 0, (struct sockaddr *)&client_addr, &clen);
    bzero(message, FILE_NAME_MAX_SIZE+1);
    strncpy(message, fnpack.data, fnpack.dataLength>FILE_NAME_MAX_SIZE?FILE_NAME_MAX_SIZE:fnpack.dataLength);
    // dataID==-1 表示下载请求
    if (fnpack.dataID == -1)
    {
        printf("Request File name: %s\n",message);
        *fp = fopen(message, "rb");
        if (NULL == *fp)
        {
            printf("File:\t %s is not found.\n",message);
            fnpack.dataID = -5;
            fnpack.dataLength = 0;
            strncpy(fnpack.data, NULL, 0);
            Sendto(server_socket, (char *)&fnpack, sizeof(Packet), 0, (struct sockaddr *)&client_addr, clen);
            return -5;
        }
        printf("File:\t %s Open.\n",message);
        fnpack.dataID = -1;
        fnpack.dataLength = 0;
        strncpy(fnpack.data, NULL, 0);
        Sendto(server_socket, (char *)&fnpack, sizeof(Packet), 0, (struct sockaddr *)&client_addr, clen);
        return 1;
    }// dataID == -2 表示显示目录请求
    else if( fnpack.dataID == -2)
    {
        Sendto(server_socket, (char *)&fnpack, sizeof(Packet), 0, (struct sockaddr *)&client_addr, clen);
        return 2;
    }// dataID == -3 表示上传请求
    else if( fnpack.dataID == -3)
    {
        // 先验证md5,若存在同md5文件,则启动秒传
        if (check_md5(message)==1) 
        {
            fnpack.dataID = -5;
            fnpack.dataLength = 0;
            strncpy(fnpack.data, NULL, 0);
            Sendto(server_socket, (char *)&fnpack, sizeof(Packet), 0, (struct sockaddr *)&client_addr, clen);
            return -5;
        }
        else{
            fnpack.dataID = 0; // Wait for packet 0
            fnpack.dataLength = 0;
            fnpack.flag = -1;
            strncpy(fnpack.data,NULL, 0);
            Sendto(server_socket, (char *)&fnpack, sizeof(Packet), 0, (struct sockaddr *)&client_addr, clen);
            return 3;
        }
    }
}
Beispiel #6
0
int send_hpvcmem(int fd , char * str ,  struct sockaddr * ppeeraddr , socklen_t addrlen)
{
		struct node_t * p = hpvc_usr.mem_list.head;

		Sendto(fd , "----------HPVCMEM---------- :\n" , 30 , 0 , ppeeraddr , addrlen);
		while(NULL != p)
		{
				sprintf(str , "p : 0x%.8x , avfd : 0x%.8x , next : 0x%.8x \n", p , *((int*)p->data) , p->next);
				Sendto(fd , str , strlen(str)  , 0 , ppeeraddr , addrlen);
				p = p->next;
		}
		return 0; 
}
Beispiel #7
0
void
dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
	int	n;
	char	sendline[MAXLINE], recvline[MAXLINE + 1];
	socklen_t		len;
	struct sockaddr	*preply_addr;

	preply_addr = Malloc(servlen);

	while (Fgets(sendline, MAXLINE, fp) != NULL) {

		Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);

		len = servlen;
		n = Recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len);
		if (len != servlen || memcmp(pservaddr, preply_addr, len) != 0) {
			printf("reply from %s (ignored)\n",
					Sock_ntop(preply_addr, len));
			continue;
		}

		recvline[n] = 0;	/* null terminate */
		Fputs(recvline, stdout);
	}
}
void exer08_dg_echo(int sockfd, SA *pcliaddr, socklen_t clilen)
{
    int n, ret;
    socklen_t len;
    char mesg[MAXLINE];

    len = sizeof(n);
    ret = getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &n, &len);
    if (ret == -1)
        err_sys("getsockopt error");
    printf("SO_RCVBUF = %d\n", n);

    n = 70000;
    ret = setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n));
    if (ret == -1)
        err_sys("setsockopt error");

    ret = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &n, &len);
    if (ret == -1)
        err_sys("getsockopt error");
    printf("SO_SNDBUF = %d\n", n);

    for (;;) {
        len = clilen;
        n = Recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);
        printf("received %d bytes\n", n);
        Sendto(sockfd, mesg, n, 0, pcliaddr, len);
        printf("sent %d bytes\n", n);
    }
}
Beispiel #9
0
void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
	int				n;
	const int		on = 1;
	char			sendline[MAXLINE], recvline[MAXLINE + 1];
	socklen_t		len;
	struct sockaddr	*preply_addr;
	preply_addr = Malloc(servlen);
	Setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)); //告诉内核广播
	Signal(SIGALRM, recvfrom_alarm);
	while (Fgets(sendline, MAXLINE, fp) != NULL) {
		Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);
		alarm(5);
		for ( ; ; ) {
			len = servlen;
			n = recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len);
			if (n < 0) {
				if (errno == EINTR)
					break;		/* waited long enough for replies */
				else
					err_sys("recvfrom error");
			} else {
				recvline[n] = 0;	/* null terminate */
				printf("from %s: %s",
						Sock_ntop_host(preply_addr, len), recvline);
			}
		}
	}
	free(preply_addr);
}
Beispiel #10
0
int main(int argc, char **argv) {
    int sockfd;
    ssize_t n;
    char buff[MAXLINE];
    time_t ticks;
    socklen_t len;
    struct sockaddr_storage cliaddr;

    if (argc == 2)
        sockfd = Udp_server(NULL, argv[1], NULL);
    else if (argc == 3) 
        sockfd = Udp_server(argv[1], argv[2], NULL);
    else
        err_quit("usage: daytimeupdsrv [ host ] <service or port>");

    for ( ; ; ) {
        len = sizeof(cliaddr);
        n = Recvfrom(sockfd, buff, MAXLINE, 0, (SA *)&cliaddr, &len);
        printf("datagram from %s\n", Sock_ntop((SA *)&cliaddr, len));

        ticks = time(NULL);
        snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
        Sendto(sockfd, buff, strlen(buff), 0, (SA *)&cliaddr, len);
    }
}
int
main(int argc, char **argv)
{
	int					sockfd;
	struct sockaddr_in	servaddr, cliaddr;
	int			n;
	socklen_t len;
	char		mesg[MAXLINE];
	sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
	SA *pcliaddr;
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family      = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port        = htons(2345);

	Bind(sockfd, (SA *) &servaddr, sizeof(servaddr));
	len=sizeof(cliaddr);

	pcliaddr= (SA *)&cliaddr;
	
	n = Recvfrom(sockfd, mesg, MAXLINE, 0,  pcliaddr, &len);
	
	Fputs("Init req from client\n",stdout);
	Fputs("####",stdout);
	Fputs(mesg,stdout);
	sprintf(mesg,"Init reply from server");
	Sendto(sockfd, mesg, sizeof(mesg), 0, pcliaddr, len);
	Fputs("\nStarting File transfer\n",stdout);

	//------------------Method for server-------------------------------------------
	dg_echofun(stdin,sockfd, (SA *) &cliaddr, len);
	return 0;
}
void 
msg_send(int fsockfd,char *fdest_ip,int fdest_port,char *fmsgbody,int fflag)
{
  socklen_t odrlen;
  struct sockaddr_un	odraddr;
  int i;
  char tempstr[100];
  
  bzero(&odraddr, sizeof(odraddr));	// fill in ODR's address 
  odraddr.sun_family = AF_LOCAL;
  strcpy(odraddr.sun_path, ODR_PATH);
  
  odrlen=sizeof(odraddr);       
 
  strcpy(tempstr,fdest_ip);
  memcpy (tempstr+15,&fdest_port,sizeof(int));
  memset(tempstr+20,2,1);   //Set byte 20 to 2 for msg type = 2
  strcpy(tempstr+21,fmsgbody);
  //strcpy(fmsgbody,tempstr);
  
  printf("\n Dest IP is %s",tempstr); 
  printf("\n Dest Port is %d",*( (int *) (tempstr+15) ) );
  printf("\n Msg type = %d",tempstr[20]);
  printf("\n Timestamp=%s",tempstr+21);
  
 
  Sendto(fsockfd, tempstr, 100, 0, (SA *) &odraddr, odrlen); 
  printf("\n Response sent to ODR service. Waiting for incoming messages...");
  
  
}            
void Master::server_notify(pair <int, int> srv, bool extension) //notify server of server fail/chain extension
{
    Server *prev = Search_Prev_Server(srv, !extension);
    cal_next(srv.first);
    if (prev == NULL)
    {
        printf("Head server fails, the successor automatically becomes the new head\n");
        return;
    }
    else
    {
        
        //printf("previous server is %d\n", prev->GetserverName().second);
        socklen_t len = sizeof(sockaddr_in);
        char send_msg[MAXLINE];
        Server *next = Search_Next_Server(prev);
        if (next == NULL)
        {
            srv.second = -1;
        }
        else
        {
            srv.second = next->GetserverName().second;
        }
        packetize(send_msg, srv, false, extension);
        Sendto(sockfd_udp, send_msg, MAXLINE, 0, (SA*)&(prev->Getsockaddr()), len);
        if (extension)
            printf("New server extended to the tail of chain %d\n%s\n", srv.first, seperator);
        else if (srv.second == -1)
            printf("Tail server fails, server notification sent out to its precessor: 127.0.0.1:%d\n", prev->GetserverName().second);
        else
            printf("Internal server fails, server notification sent out to its precessor: 127.0.0.1:%d, new next server is 127.0.0.1:%d\n", prev->GetserverName().second, srv.second);
    }
}
Beispiel #14
0
int sendExecuteAck(int sockfd, struct sockaddr_in sockaddr, Packet *packet){
   Packet *packet_reply;
   packet_reply = (Packet *)malloc(sizeof(Packet)); //Packet with Execute_Serv type Data

   packet_reply = genExecuteAck(packet); // msg to be sent back
   return Sendto(sockfd, packet_reply, sizeof(Packet), MSG_NOSIGNAL, sockaddr, sizeof(sockaddr));
}
int
main(int argc, char **argv)
{
	int				sockfd;
	ssize_t			n;
	char			buff[MAXLINE];
	time_t			ticks;
	socklen_t		addrlen, len;
	struct sockaddr	*cliaddr;

	if (argc == 2)
		sockfd = Udp_server_reuseaddr(NULL, argv[1], &addrlen);
	else if (argc == 3)
		sockfd = Udp_server_reuseaddr(argv[1], argv[2], &addrlen);
	else
		err_quit("usage: daytimeudpsrv [ <host> ] <service or port>");

	cliaddr = Malloc(addrlen);

	for ( ; ; ) {
		len = addrlen;
		n = Recvfrom(sockfd, buff, MAXLINE, 0, cliaddr, &len);
		printf("datagram from %s\n", Sock_ntop(cliaddr, len));

        ticks = time(NULL);
        snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
        Sendto(sockfd, buff, strlen(buff), 0, cliaddr, len);
	}
}
void start_backup(){
	char recvline[MAXLINE];
	char *msg = "##start_backup##";
	Sendto(sockfd2serverfp, msg, strlen(msg), 0, (SA *) &servaddrfp, sizeof(servaddrfp));
	if (Read(sockfd2serverfp, recvline, MAXLINE) == 0)
		err_quit("transfer_file: server terminated prematurely");
}
/*
 *	return:
 *		1 -	file exists , complete
 *		-1 - 	file new , POX
 *		-2 -	file new , server
 */
int isExistsFile_1(char *fp_filename, char *filename) {
	char	recvline[MAXLINE];
	struct sockaddr_in	cliaddr;
	socklen_t	cliaddrlen = sizeof(cliaddr);
	printf("is exists file ?\n");

	// WE NEED THE REPLY 

		Sendto(sockfd2serverfp, fp_filename, strlen(fp_filename), 0, (SA *) &servaddrfp, sizeof(servaddrfp));
		// Write(sockfd2serverfp, fp_filename, strlen(fp_filename));	
		printf("sendto server fp ----\n");
		/* recv quick reply from POX with timeout */
		// struct timeval tv;
		// tv.tv_sec = 2;
		// tv.tv_usec = 0;
		// setsockopt(sockfd4pox, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
		memset(recvline, 0, sizeof(recvline));
		if(sdn_switch){
			Recvfrom(sockfd4pox, recvline, MAXLINE, 0,  (SA *) &cliaddr, &cliaddrlen);
			// printf("=============%s\n", recvline);
		}
		if(strcmp(recvline, "new_file") == 0){ // Bloom filter ,Quick decision
			printf("**File(%s) new, so start transfer(POX).**\n", filename);
			// File is new, transfer file to server
			// But , also need update the bloom filter in server
			return -1;
		}else if(strcmp(recvline, "duplicated") == 0){// cache hit, Quick decision
			printf("**File(%s) duplicated, so transfer complete(POX)..**\n", filename);
			return 1;
		}else{// go to server , wait for the reply from server
			
			int n = 0;
			memset(recvline, 0, sizeof(recvline));
			n = recvfrom(sockfd2serverfp, recvline, MAXLINE, 0,  NULL, NULL);
			
			
			// // int n = Read(sockfd2serverfp, recvline, MAXLINE);
			if(n < 0){
				if(errno == EWOULDBLOCK || errno == EAGAIN){
					fprintf(stderr, "socket recvfrom timeout\n");
					printf("Not get answer from server , fake it new \n");
					return -2;
					//continue;// re send 
				}else err_sys("recvfrom error!");
			}
			// if (Recvfrom(sockfd2serverfp, recvline, MAXLINE, 0,  (SA *) &cliaddr, &cliaddrlen)==0)
			// 	err_quit("client: server terminated prematurely");
			printf("get answer from server\n");
			// printf("=============%s\n", recvline);
			if(strcmp(recvline, "duplicated") == 0){
				printf("**File duplicated(%s), so transfer complete..**\n", filename);
				return 1;
			}else{
				printf("**File new(%s), so start transfer..**\n", filename);
				return -2;
			}
		} // end go to server

	return -1; // cannot get here
}
Beispiel #18
0
int main(int argc, char *argv[]) {

uint16_t		tport_n; //= 1500;
struct      sockaddr_in	saddr;
char        *msg;
int 			  s;
int         result;
int         len;
int         recv_size;
char        datagram[SIZE_MAX_BUFF];


  prog_name = argv[0];

  /*creazione della socket(famiglia,tipo,protocollo)*/
  s = Socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  trace ( err_msg("(%s) socket created",prog_name) );

  /*controllo immissione e.g. ./client_udp 'indirizzo' 'porta' 'datagram'*/
  if (argc!=4)
  		err_quit ("wrong number of parameters e.g. ./client 'address' 'port' 'datagram' ", prog_name);
  		//salviamo in result ciò che ritorna la inet_aton (1 se ok, 0 se errore) e ci gestiamo l'errore
  		result=inet_aton(argv[1],&(saddr.sin_addr));
  			if ( result == 0 )
  			{
  			err_sys ("(%s) error - connect() failed", prog_name);
  			}
  		tport_n=atoi(argv[2]); 	//conversione_porta
      msg = argv[3];
      len = strlen(argv[3]);  //prendo la lunghezza di datagram e la controllo perchè non deve superare 31
      if( len > 31 )
      {
        err_sys ("(%s) datagram maggiore di 31 caratteri", prog_name);
      }

/*inizializzazione di famiglia,porta e indirizzo_destinatario(saddr)*/
saddr.sin_family 	= AF_INET;
saddr.sin_port   	= htons(tport_n);
trace ( err_msg("(%s) destination is %s:%u", prog_name, inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)) );

    /*send_to verso il server*/
    Sendto (s,msg,len,0,(struct sockaddr*)&saddr,sizeof(saddr));
    trace ( err_msg("(%s) data has been sent: '%s'", prog_name,msg) );

    /*dopo 5 sec chiude se ci sono problemi */
    struct timeval tv;
    tv.tv_sec = 5;
    tv.tv_usec = 0;
    if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0) {
        perror("Error");
    }

    /*recv_from dal server*/
    socklen_t fromlen = sizeof(saddr);
    recv_size = Recvfrom (s,datagram,SIZE_MAX_BUFF,0,(struct sockaddr*)&saddr,&fromlen);
    datagram[recv_size] = '\0';
    trace ( err_msg("(%s) receive string: '%s'",prog_name,datagram) );

  return 0;
}
Beispiel #19
0
void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
	int	icmpfd, maxfdp1;
	char	sendline[MAXLINE], recvline[MAXLINE + 1];
	fd_set	rset;
	ssize_t	n;
	struct timeval tv;
	struct icmpd_err icmpd_err;
	struct sockaddr_un sun;

	Sock_bind_wild(sockfd, pservaddr->sa_family);
	icmpfd = Socket(AF_LOCAL, SOCK_STREAM, 0);
	sun.sun_family = AF_LOCAL;
	strcpy(sun.sun_path, ICMPD_PATH);
	Connect(icmpfd, (SA *)&sun, sizeof(sun));
	Write_fd(icmpfd, "1", 1, sockfd);
	n = Read(icmpfd, recvline, 1);
	if (n != 1 || recvline[0] != '1')
		err_quit("error creating icmp socket, n = %d, char = %c",
				n, recvline[0]);
	FD_ZERO(&rset);
	maxfdp1 = max(sockfd, icmpfd) + 1;

	while (Fgets(sendline, MAXLINE, fp) != NULL) {
		Sendto(sockfd, sendline, strlen(sendline), 0, 
				pservaddr, servlen);

		tv.tv_sec = 5;
		tv.tv_usec = 0;
		FD_SET(sockfd, &rset);
		FD_SET(icmpfd, &rset);
		if ((n = Select(maxfdp1, &rset, NULL, NULL, &tv)) == 0) {
			fprintf(stderr, "socket timeout\n");
			continue;
		}

		if (FD_ISSET(sockfd, &rset)) {
			n = Recvfrom(sockfd, recvline, MAXLINE, 0, 
					NULL, NULL);
			recvline[n] = 0;
			Fputs(recvline, stdout);
		}

		if (FD_ISSET(icmpfd, &rset)) {
			if ((n = Read(icmpfd, &icmpd_err, 
					sizeof(icmpd_err))) == 0)
				err_quit("ICMP daemon terminate");
			else if (n != sizeof(icmpd_err))
				err_quit("n = %d, expected %d",
					n, sizeof(icmpd_err));
			printf("ICMP error: dest = %s, %s, type = %d,"
				" code = %d\n",
				Sock_ntop((SA *)&icmpd_err.icmpd_dest,
					icmpd_err.icmpd_len),
				strerror(icmpd_err.icmpd_errno),
				icmpd_err.icmpd_type, 
				icmpd_err.icmpd_code);
		}
	}
}
Beispiel #20
0
void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
	int n;
	const int on = 1;
	char sendline[MAXLINE], recvline[MAXLINE + 1];
	socklen_t len;
	struct sockaddr *preply_addr;

	preply_addr = (struct sockaddr *)Malloc(servlen);

	Setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));

	Signal(SIGALRM, recvfrom_alarm);

	while (Fgets(sendline, MAXLINE, fp) != NULL)
	{
		Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);
		alarm(5);
		for ( ; ; )
		{
			if (sigsetjmp(jmpbuf, 1) != 0)
				break;
			len = servlen;
			n = Recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len);
			recvline[n] = 0;
			printf("from %s: %s", (char *)Sock_ntop_host(preply_addr, len), recvline);
		}
	}
	free(preply_addr);
}
Beispiel #21
0
void
dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
    int	n;
    char	sendline[MAXLINE], recvline[MAXLINE + 1];

    Signal(SIGALRM, sig_alrm);

    while (Fgets(sendline, MAXLINE, fp) != NULL)
    {

        Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);

        alarm(5);
        if ( (n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL)) < 0)
        {
            if (errno == EINTR)
                fprintf(stderr, "socket timeout\n");
            else
                err_sys("recvfrom error");
        }
        else
        {
            alarm(0);
            recvline[n] = 0;	/* null terminate */
            Fputs(recvline, stdout);
        }
    }
}
Beispiel #22
0
// 发起三次握手
int ShakeHands(char *file_name, FILE **fpp)
{
    Packet fnpack;
    int filesize = 0;
    // 上传请求信号 dataID = -3
    fnpack.dataID = -3;
    FILE *fp = *fpp;
    fp = fopen(file_name, "rb");
    if (NULL == fp){
        printf("File:\t %s can not open to read.\n",file_name);exit(1);
    }
    char message[BUFFER_SIZE],md5sum[33]={'\0'};
    md5(&filesize,md5sum,&fp);                              // 计算文件大小和md5
    bzero(&message,sizeof(message));
    sprintf(message,"%s\t%d\t%s",file_name,filesize,md5sum);// 发送message包括文件名,文件大小和md5
    strncpy(fnpack.data, message, strlen(message));
    fnpack.dataLength = strlen(message);
    fnpack.flag = -1;
    
    printf("Sending request ...\n");
    Sendto(client_socket,(char *)&fnpack, sizeof(Packet),0, (struct sockaddr *)&server_addr, slen);
    
    Packet ack;
    Recvfrom(client_socket, (char *)&ack, sizeof(Packet), 0, (struct sockaddr *)&server_addr, &slen);
    if (ack.dataID == -5){
        // 服务器反馈 -5,说明秒传命中
        printf("File Exist on Server. Using fast copy.\n");
        return -5;
    }
    return 1;
}
Beispiel #23
0
void msg_send(int sockfd, SA *servaddr, socklen_t servlen, char *ip_src, char *ip_dest, int port_dest, char *message, int flag)
{
	//printf("SRC IP    : %s\n", ip_src);
	//printf("DEST IP   : %s\n", ip_dest);
	//printf("DEST PORT : %d\n", port_dest);
	//printf("MESSAGE   : %s\n", message);
	//printf("FLAG      : %d\n", flag);
	
	int type=2;
	int hop_cnt=0;
	int len=32;
	
	unsigned short port_src=1033;
	
	void *send_packet=(void *)Malloc(SIZE_SENDMESSAGE+4);

	memcpy((void *)send_packet, (void *)&type, SIZE_TYPE);
	memcpy((void *)(send_packet+4), (void *)ip_src, SIZE_IP);

	memcpy((void *)(send_packet+20), (void *)&port_src, SIZE_PORT);
	
	memcpy((void *)(send_packet+22), (void *)ip_dest, SIZE_IP);
	memcpy((void *)(send_packet+38), (void *)&port_dest, SIZE_PORT);
	memcpy((void *)(send_packet+40), (void *)&hop_cnt, SIZE_HOP);
	memcpy((void *)(send_packet+44), (void *)&len, SIZE_LEN);
	memcpy((void *)(send_packet+48), (void *)message, 32);
	memcpy((void *)(send_packet+80), (void *)&flag, 4);
	
	Sendto(sockfd, send_packet, SIZE_SENDMESSAGE+4, 0, servaddr, servlen);
}
Beispiel #24
0
void dg_cli(FILE* fp, int sockfd, const SA* pservaddr, socklen_t servlen)
{
    int n;
    char sendline[MAXLINE], recvline[MAXLINE + 1];
    socklen_t len;
    struct sockaddr* preply_addr;

    preply_addr = Malloc(servlen);

    Signal(SIGALRM, recvfrom_alarm);

    while (Fgets(sendline, MAXLINE, fp) != NULL) {
        Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);

        alarm(5);
        for (;;) {
            if (sigsetjmp(jmpbuf, 1) != 0) break;
            len = servlen;
            n = Recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len);
            recvline[n] = 0; /* null terminate */
            printf("from %s: %s", Sock_ntop_host(preply_addr, servlen),
                   recvline);
        }
    }
}
Beispiel #25
0
/*
 * When a peer sends `auth: CAN' it asks for communication with another peer.
 * The other peer must send back `auth: OFC' to accept the connection.
 */
void auth_accept(int sockfd, const SA *peeraddr, socklen_t peeraddr_len)
{
    char auth_ofc[4 + strlen(AUTH_OFC) + 1];
    create_send_msg_static(AUTH_OFC, auth_ofc);

    Sendto(sockfd, auth_ofc, strlen(auth_ofc), 0, peeraddr, peeraddr_len);
}
Beispiel #26
0
int
main(int argc, char **argv)
{
    int		sockfd, n, nq;
    char	recvline[MAXLINE + 1];
    socklen_t	salen;
    struct sockaddr	*sa;

    if (argc != 3)
        err_quit("usage: a.out <hostname or IPaddress> <service or port#>");

    sockfd = Udp_client(argv[1], argv[2], (void **) &sa, &salen);

    printf("sending to %s\n", Sock_ntop_host(sa, salen));

    Sendto(sockfd, "", 1, 0, sa, salen);	/* send 1-byte datagram */

    n = Recvfrom(sockfd, recvline, MAXLINE, MSG_PEEK, NULL, NULL);

    Ioctl(sockfd, FIONREAD, &nq);	/* check FIONREAD support */
    printf("%d bytes from PEEK, %d bytes pending\n", n, nq);

    n = Recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL);
    recvline[n] = 0;	/* null terminate */
    Fputs(recvline, stdout);

    exit(0);
}
Beispiel #27
0
void
dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
	int				n;
	char			sendline[MAXLINE], recvline[MAXLINE + 1];
	struct timeval	tv;

	tv.tv_sec = 5;
	tv.tv_usec = 0;
	Setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));

	while (Fgets(sendline, MAXLINE, fp) != NULL) {

		Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);

		n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL);
		if (n < 0) {
			if (errno == EWOULDBLOCK) {
				fprintf(stderr, "socket timeout\n");
				continue;
			} else
				err_sys("recvfrom error");
		}

		recvline[n] = 0;	/* null terminate */
		Fputs(recvline, stdout);
	}
}
Beispiel #28
0
void metaserver_terminate(int sockfd, const SA *servaddr, socklen_t servlen)
{
    char         mesg[MAXLINE];
    unsigned int packet_size=0;

    pack_uint32(TERMINATE, mesg, &packet_size);
    Sendto(sockfd, mesg, packet_size, 0, servaddr, servlen);
}
void transfer_file_udp(char *fp_filename, char *filename, int updatebloom){
	char	recvline[MAXLINE];
	char buf[CHUNK_SIZE];
	char *sendline = "file chunk data";
	int fd, res;
	char firstpkt[MAXLINE]={0};


	int chunk_count = 4, i = 0;
	FILE *fs = fopen(filename, "r");

	// 1. transfer fp+file name, update bloom flag
	
	if(updatebloom){
		strcat(firstpkt, "#");
	}
	strcat(firstpkt, fp_filename);
	Sendto(sockfd2server, firstpkt,strlen(firstpkt), 0,  (SA *) &servaddr, sizeof(servaddr));
	printf("----client first unit sent\n");
	if (Read(sockfd2server, recvline, MAXLINE) == 0)
		err_quit("transfer_file: server terminated prematurely");
	printf("----client first unit ACK\n");
	// Write(sockfd, fp_filename, strlen(fp_filename));
	// fp+file name should be a separate packet
	// 
	// 2. then transfer the whole file chunkly
	while((res = fread(buf, sizeof(char), CHUNK_SIZE, fs)) > 0){
		Sendto(sockfd2server, buf, res, 0,  (SA *) &servaddr, sizeof(servaddr));
		printf("----client chunk unit sent\n");
		Read(sockfd2server, recvline, MAXLINE);

		// we DO NOT need reply
		memset(buf, 0, CHUNK_SIZE);
	}

	// 3. eof it
	sendline = "##file_eof##";
	Sendto(sockfd2server, sendline, strlen(sendline), 0, (SA *) &servaddr, sizeof(servaddr));

	// 4. ACK for this file transmission
	if (Read(sockfd2server, recvline, MAXLINE) == 0)
		err_quit("transfer_file: server terminated prematurely");


	fclose(fs);
}
/*
 * Send bloom filter vector to POX 
 * I tried , BUT cannot match 
 * so here I just fake it by write to a local file
 * trigger the POX to update the bloom filter by read this fp file
 */
void sync_bloom_to_pox(){
	char *s = "bloom filter";
	// latest fp file close
	fflush(fpfile_latest);
	fclose(fpfile_latest);

	Sendto(sockfdbloom, s,strlen(s), 0,  (SA *) &fakeaddr, sizeof(fakeaddr));
}