int main() { int sockfd = socket(AF_INET, SOCK_RAW, 1); struct sockaddr_in target; char *ip= "www.baidu.com"; char *ip1 = "11.1.1.1"; struct hostent* host = gethostbyname(ip1); target.sin_addr = *((struct in_addr *)host->h_addr); target.sin_family = AF_INET; target.sin_port=htons(0); //test(); send_v4(sockfd, &target); recv_v4(sockfd, &target); test(); proc_v4(); return 0; }
int main(int argc, char **argv) { int n, i, size, prflag; int pos = 3; int maxfd; int rt_sockfd,recv_ping_sockfd; int send_mulcst_fd, recv_mulcst_fd; const int on = 1; char buff_recv[BUFFSIZE]; char tour_array[TOURNUM][IPLEN]; char *ptr; fd_set rset; struct timeval tval, tval1, tval2, tval3, tval4; struct sockaddr_ll saddrll; Signal(SIGALRM, sig_alrm); //Signal(SIGCHLD, sig_chld); uname(&myname); if (argc == 1){ fputs("This is NOT source node\n", stdout); //_is_src_node = 0; source_flag = 0; }else{ fputs("I'm source node, start to send tour infomation \n", stdout); //_is_src_node = 1; source_flag = 1; } //if (uname(&myname) < 0) //err_sys("uname error"); for ( hwa = Get_hw_addrs(); hwa != NULL; hwa = hwa->hwa_next) { if(strcmp(hwa->if_name,"eth0")==0){ /* in eth0 we create pfsocket and bind it to hw addr, this pf socket is for sned ping info */ //pf_sockfd = socket(PF_PACKET, SOCK_RAW,htons(ETH_P_IP)); send_ping_pf_sockfd = socket(PF_PACKET, SOCK_RAW,htons(ETH_P_IP)); //send_ping_pf_sockfd use to send ping to source node saddrll.sll_family = PF_PACKET; saddrll.sll_ifindex = hwa->if_index; saddrll.sll_protocol = htons(ETH_P_IP); bind(send_ping_pf_sockfd, (struct sockaddr *) &saddrll, sizeof(saddrll)); printf("%s :%s", hwa->if_name, ((hwa->ip_alias) == IP_ALIAS) ? " (alias)\n" : "\n"); if ( (sa = hwa->ip_addr) != NULL) printf("IP addr = %s\n", Sock_ntop_host(sa, sizeof(*sa))); prflag = 0; i = 0; do { if (hwa->if_haddr[i] != '\0') { prflag = 1; break; } } while (++i < IF_HADDR); if (prflag) { printf(" HW addr = "); ptr = hwa->if_haddr; i = IF_HADDR; do { printf("%.2x%s", *ptr++ & 0xff, (i == 1) ? " " : ":"); } while (--i > 0); } printf("\n Interface Index = %d\n\n", hwa->if_index); break; } } /* creat two raw sockets for route travesal and receive ping*/ rt_sockfd = Socket(AF_INET, SOCK_RAW, 254); setsockopt(rt_sockfd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)); recv_ping_sockfd = Socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); setuid(getuid()); /* don't need special permissions any more */ size = 60 * 1024; /* OK if setsockopt fails */ setsockopt (recv_ping_sockfd, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)); if (source_flag == 1) {//this node is source struct icmp *icmp; struct ip* ip; int node_num; node_num = argc + 3; strcpy(tour_array[0], Sock_ntop_host(sa, sizeof(*sa)));//first one is source code sprintf(tour_array[1],"%d",argc - 1); sprintf(tour_array[2],"%d",node_num); sprintf(tour_array[3],"%d",4); for (i = 4; i < node_num; i++) strcpy(tour_array[i], getipbyvm(argv[i-3])); //////////////////////////before send we let the source node join the multicast send_mulcst_fd = Udp_client(MC_ADDR_LIN, MC_PORT_LIN, (void **) &sasend, &salen); recv_mulcst_fd = Socket(AF_INET, SOCK_DGRAM, 0); Setsockopt(recv_mulcst_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); sarecv = Malloc(salen); memcpy(sarecv, sasend, salen); Bind(recv_mulcst_fd, sarecv, salen); Mcast_join(recv_mulcst_fd, sasend, salen, NULL, 0); Mcast_set_loop(send_mulcst_fd, 0); /////////////////////////////////////////////// this is the send tour part rt_send(rt_sockfd, tour_array); for(;;){ if(first_mulcst_flag == 1){ printf("wait for first multicast info\n"); recv_all(recv_mulcst_fd, salen); first_mulcst_flag = 0; char buf[MAXLINE]; //sleep(1); snprintf(buf, sizeof(buf), "<<<<< Node: %s I am a member of the group.>>>>>\n", myname.nodename); send_all(send_mulcst_fd, sasend, salen, buf); }else{ for(;;){ recv_all(recv_mulcst_fd, salen); printf("Waiting for 5 seconds and exit\n"); } } } }else{//not source node pthread_t tid, tid2; char source_name[IPLEN]; for( ;; ) { FD_ZERO(&rset); FD_SET(rt_sockfd, &rset); maxfd = rt_sockfd; if(rt_recved_flag == 1){ FD_SET(recv_ping_sockfd, &rset); maxfd = max(recv_ping_sockfd, maxfd); } if (ns_first_flag == 0) { FD_SET(recv_mulcst_fd,&rset); //maxfd = (maxfd > recv_mulcst_fd) ? maxfd : recv_mulcst_fd; maxfd = max(maxfd, recv_mulcst_fd); } //printf("before select\n"); select(maxfd + 1, &rset, NULL, NULL, NULL); if (FD_ISSET(rt_sockfd, &rset)) { printf("receive route travelsal paket\n"); n = rt_recv(rt_sockfd, tour_array); memcpy(dest_ip_addr, tour_array[0], IPLEN); if (n < 0) { if (errno == EINTR) continue; else err_sys("receive tour packet error"); } get_vmname(tour_array[0], source_name); if (ns_first_flag == 1) { ns_first_flag = 0; rt_recved_flag = 1; // join the multicast first send_mulcst_fd = Udp_client(MC_ADDR_LIN, MC_PORT_LIN, (void **) &sasend, &salen); recv_mulcst_fd = Socket(AF_INET, SOCK_DGRAM, 0); Setsockopt(recv_mulcst_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); sarecv = Malloc(salen); memcpy(sarecv, sasend, salen); Bind(recv_mulcst_fd, sarecv, salen); Mcast_join(recv_mulcst_fd, sasend, salen, NULL, 0); Mcast_set_loop(send_mulcst_fd, 0); //create a thread for ping Pthread_create(&tid, NULL, &ping, NULL); } if(last_node_flag == 0){ rt_send(rt_sockfd, tour_array); }else{ //create a thread to handle last operations Pthread_create(&tid2, NULL, &ls_send_mul, &send_mulcst_fd); } } if (FD_ISSET(recv_mulcst_fd, &rset)) {//recv multicast info if (first_mulcst_flag == 1 ) { first_mulcst_flag = 0; ping_over_flag = 1;// //printf("ping_over_flag is %d\n", ping_over_flag); recv_all(recv_mulcst_fd, salen); char buf[MAXLINE]; snprintf(buf, sizeof(buf), "<<<<< Node: %s I am a member of the group.>>>>>\n", myname.nodename); send_all(send_mulcst_fd, sasend, salen, buf); //printf("gonna go to alarm with pof changed\n"); alarm(0); }else{ for(;;){ recv_all(recv_mulcst_fd, salen); } printf("Waiting for 5 seconds and exit\n"); } } if (FD_ISSET(recv_ping_sockfd, &rset)) {//recv ping reply //printf("received ping reply\n"); recvfrom(recv_ping_sockfd, buff_recv, MAXLINE, 0, NULL, NULL); Gettimeofday (&tval, NULL); proc_v4 (buff_recv, n, &tval); if (ping_over_flag == 1) alarm(0); } } } }
int main(int argc, char **argv){ const int on = 1; unsigned char ttl = 1; time_t rawtime; time_t lasttime; int maxfd; fd_set rset; ssize_t how_much_read; char msg[MAXLINE]; rt = Socket(AF_INET, SOCK_RAW, MY_IP_PROTO); Setsockopt(rt, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)); pg = Socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); request_sock = Socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP)); multicast_sock = Socket(AF_INET, SOCK_DGRAM, 0); multicast_recv = Socket(AF_INET, SOCK_DGRAM, 0); Mcast_set_loop(multicast_sock,0); Setsockopt(multicast_sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); Setsockopt(multicast_recv, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); gethostname(our_hostname, sizeof(our_hostname)); //printf("My our_hostname: %s\n", our_hostname); get_eth0_stuff(); if(argc > 1) { //DEBUG("%s is the start of the tour!\n",our_hostname); int i = 1; tour tour_pkt; // separate func bzero(&tour_pkt, sizeof(tour_pkt)); fill_buff_with_ip_of_hostname(our_hostname); //printf("fill buff ok\n"); inet_aton(ip_static_buff, &(tour_pkt.payload[0])); join_mcast(MULTICAST_ADDRESS, MULTICAST_PORT); #if 0 // tokenize by ' ' and send each char* to gethostbyname char *token, *prev_token; const char s[2] = " "; /* get the first token */ token = strtok(argv[1], s); /* walk through other tokens */ while(token != NULL) { DEBUG("token = %s\n", token ); fill_buff_with_ip_of_hostname(token); inet_aton(ip_static_buff, &tour_pkt.payload[i]); prev_token = token; token = strtok(NULL, s); if(!strcmp(prev_token, token)){ perror("The same node should not appear consequentively in the tour list – i.e., the next node on the tour cannot be the current node itself"); exit(0); } i++; } // separate func #endif while(i< argc) { //printf("argv[%d] is %s\n",i, argv[i]); fill_buff_with_ip_of_hostname(argv[i]); //printf("fill buff ok\n"); inet_aton(ip_static_buff, &(tour_pkt.payload[i])); i++; } tour_pkt.index = 0; // We're the origin tour_pkt.total = i-1; //printf("tour_pkt total %d\n", tour_pkt.total); send_raw_tour_packet(&tour_pkt); }else{ //DEBUG("We're NOT the start of the tour!"); #if 0 while(1) { int len = 0; len = Recv(rt, msg, sizeof(msg), 0); printf("received len %d\n",len); int j = 0; for(;j<len; j++) { printf("%.2x ", msg[j]); if((i-9) % 10 == 0) printf("\n"); fflush(stdout); } } #endif } #if 0 fill_buff_with_ip_of_hostname("vm2"); inet_aton(ip_static_buff, (struct in_addr *)&dest_of_echo_req); printf("destofechoreq %x\n", dest_of_echo_req.s_addr); struct sockaddr_in IPaddr; struct hwaddr HWaddr; bzero(&IPaddr, sizeof(IPaddr)); bzero(&HWaddr, sizeof(HWaddr)); IPaddr.sin_addr = dest_of_echo_req; //Areq((struct sockaddr *)&IPaddr, sizeof(struct sockaddr_in), &HWaddr); //printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",HWaddr.sll_addr[0],HWaddr.sll_addr[1],HWaddr.sll_addr[2], // HWaddr.sll_addr[3],HWaddr.sll_addr[4],HWaddr.sll_addr[5]); void sig_alrm(int signo); if(argc > 1){ Signal(SIGALRM, sig_alrm); sig_alrm(SIGALRM); } //send_icmp_request(HWaddr.sll_addr); int n = 0; struct timeval tval; while(1) { n = recv(pg, msg, sizeof(msg), 0); if(n<0) { if (errno == EINTR) continue; } printf("receive ping packet\n"); //int j = 0; //for (; j < n; j++) //{ // printf("%.2x ", msg[j]); // if((j-9) % 10 == 0) printf("\n"); // fflush(stdout); //} Gettimeofday(&tval, NULL); proc_v4(msg,n,NULL, &tval); } #endif #if 1 //act_open.sa_flags = 0; //sigemptyset(&act_open.sa_mask); //sigaddset(&act_open.sa_mask, SIGALRM); //act_open.sa_handler = terminate; Signal(SIGALRM, sig_alrm); FD_ZERO(&rset); maxfd = max(pg, multicast_recv); maxfd = max(rt, maxfd); while(1){ FD_SET(multicast_recv, &rset); FD_SET(rt, &rset); FD_SET(pg, &rset); if(select(maxfd+1, &rset, NULL, NULL, NULL)<0) { if(errno == EINTR) { continue; } } if(FD_ISSET(rt, &rset)){ //DEBUG("possibly totally wrong here\n"); //DEBUG("possibly totally wrong here\n"); //DEBUG("possibly totally wrong here\n"); alarm(0); bzero(msg, sizeof(msg)); how_much_read = Recv(rt, msg, sizeof(msg), 0); //assert(how_much_read == sizeof(sizeof(my_ip_header)+sizeof(tour))); //printf("rt receive pkt len %d\n", how_much_read); my_ip_header *head = (my_ip_header *)msg; tour *tour_pkt = (tour *)(msg+sizeof(my_ip_header)); if(ntohs(head->id) != MY_IP_ID){ //DEBUG("head->id != MY_IP_ID\n"); continue; } char *name = find_vm_name(inet_ntoa(*(struct in_addr *)&head->saddr)); time(&rawtime); printf("<%s> recieved source routing packet from <%s>\n", ctime (&rawtime), name); if(!already_here){ already_here = 1; // IP_MULTICAST_TTL - If not otherwise specified, has a default value of 1. join_mcast(inet_ntoa(tour_pkt->multicast_addr), ntohs(tour_pkt->multicast_port)); stop_pinging = 0; } // start icmp echo reqs for(i = 0; i < prev_sender_list.index; i++) { if(!memcmp(&prev_sender_list.previous_senders[i], &tour_pkt->payload[(tour_pkt->index)], sizeof(struct in_addr))) we_dont_need_to_send_echo_req = 1; else{ we_dont_need_to_send_echo_req = 0; break; } } if(!we_dont_need_to_send_echo_req){ //DEBUG("We Need To Send Echo Request\n"); prev_sender_list.previous_senders[prev_sender_list.index] = tour_pkt->payload[(tour_pkt->index)]; prev_sender_list.index++; dest_of_echo_req = tour_pkt->payload[(tour_pkt->index)]; char *name = find_vm_name(inet_ntoa(*(struct in_addr *)&tour_pkt->payload[(tour_pkt->index)])); printf("PING %s (%s): %d data bytes\n",name,inet_ntoa(dest_of_echo_req), icmpdata); sig_alrm(SIGALRM); } else { char *name = find_vm_name(inet_ntoa(*(struct in_addr *)&tour_pkt->payload[(tour_pkt->index)])); printf("We have ping %s before\n",name); } // INCREMENTING THE INDEX tour_pkt->index = (tour_pkt->index)+1; if((tour_pkt->index) == MAX_VISITS){ perror("MAX_VISITS has been reached."); exit(0); } // Now blank //bzero(&for_comparisons, sizeof(for_comparisons)); // ghiTODO this may not work ghiTODO //if(!memcmp(&tour_pkt->payload[ntohl(tour_pkt->index)], &for_comparisons, sizeof(for_comparisons))){ //printf("rt current pkt index %d, total index %d\n",tour_pkt->index, tour_pkt->total); if(tour_pkt->index == tour_pkt->total) { //printf("We're the final destination.\n"); // ping the saddr final_destination = 1; char buffer[100]; snprintf(buffer, 100, "<<<<< This is node %s. Tour has ended. Group members please identify yourselves. >>>>>",our_hostname); printf("Node %s. Sending: <%s>. ",our_hostname, buffer); send_multicast_message(buffer,strlen(buffer)+1); //printf("send mulitcast msg ok\n"); }else{ //printf("We're NOT the final destination, keep touring.\n"); // pass it along send_raw_tour_packet(tour_pkt); } }else if(FD_ISSET(multicast_recv, &rset)){ //DEBUG("Receiving multicast message.\n"); printf(""); bzero(msg, sizeof(msg)); how_much_read = Recv(multicast_recv, msg, sizeof(msg), 0); //printf("multicast msg len %d\n", how_much_read); msg[how_much_read]='\0'; if(!first_term){ printf("Node %s. Received <%s>. ",our_hostname,msg); // then should immediately stop ping //DEBUG("how_much_read is %lu\n", how_much_read); stop_pinging = 1; alarm(0); bzero(msg, sizeof(msg)); snprintf(msg, sizeof(msg), "<<<<< Node %s I am a member of the group. >>>>>", our_hostname); printf("Node %s. Sending <%s>. ", our_hostname, msg); fflush(stdout); first_term = 1; send_multicast_message(msg,strlen(msg)+1); } else { Signal(SIGALRM, terminate); alarm(5); printf(""); printf("Node %s. Received <%s>. ",our_hostname,msg); fflush(stdout); } //send_multicast_message(msg); }else if(FD_ISSET(pg, &rset)){ //DEBUG("Receiving echo reply message.\n"); struct timeval tval; bzero(msg, sizeof(msg)); how_much_read = Recv(pg, msg, sizeof(msg), 0); Gettimeofday(&tval, NULL); proc_v4(msg,how_much_read,NULL, &tval); //assert(how_much_read == sizeof(sizeof(my_eth_header)+sizeof(my_ip_header)+sizeof(my_icmp_header))); //DEBUG("how_much_read is %lu\n", how_much_read); //DEBUG("Node %s recieved %s\n", our_hostname, msg); // my_eth_header *eh = (my_eth_header *)msg; // my_ip_header *iph = (my_ip_header *)msg+sizeof(my_eth_header); // my_icmp_header *icmph = (my_icmp_header *)msg+sizeof(my_eth_header)+sizeof(my_ip_header); //my_ip_header *iph = (my_ip_header *)(msg+sizeof(my_eth_header)); //if(!memcmp(&iph->saddr, &dest_of_echo_req, sizeof(dest_of_echo_req))){ // DEBUG("ghiTODO\n"); // count_of_replies++; // DEBUG("ghiTODO\n"); //DEBUG("ghiTODO\n"); //DEBUG("ghiTODO\n"); // if(final_destination && count_of_replies >= 5){ // stop_pinging = 1; // bzero(msg, sizeof(msg)); /// snprintf(msg, sizeof(msg), "<<<<< This is node %s Tour has ended. Group members please identify yourselves.>>>>>", our_hostname); // printf("Node %s sending %s", our_hostname, msg); // send_multicast_message(msg, strlen(msg)+1); // alarm(5); // } //}else{ //DEBUG("The following is FALSE !memcmp(&iph->saddr, &dest_of_echo_req, sizeof(dest_of_echo_req))"); //} } #if 0 time(&rawtime); if(lasttime+1 <= rawtime){ if(!stop_pinging){ struct sockaddr_in IPaddr; struct hwaddr HWaddr; bzero(&IPaddr, sizeof(IPaddr)); bzero(&HWaddr, sizeof(HWaddr)); IPaddr.sin_addr = dest_of_echo_req; Areq((struct sockaddr *)&IPaddr, sizeof(struct sockaddr_in), &HWaddr); send_raw_echo_request_message(HWaddr.sll_addr); } }else{ lasttime = rawtime; } #endif } #endif }
void monitor_sockets() { fd_set set_fd; fd_set set_sel; int mx_fd; struct sockaddr_in multicast_socket; struct timeval ping_timeout; FD_ZERO(&set_fd); struct tour_packet packet; char buf[100]; struct in_addr dest_ip; strcpy(buf,"This is Multicast message \n"); while(1) { FD_SET(rt_fd,&set_fd); mx_fd = rt_fd; if (am_i_member_multicast == 1) { FD_SET(udp_fd,&set_fd); mx_fd = max(rt_fd,udp_fd); } if (ping_enabled == 1 || exit_flag == 1) { FD_SET(pg_fd,&set_fd); mx_fd = max(mx_fd,pg_fd); ping_timeout.tv_sec = 1; ping_timeout.tv_usec = 0; if (exit_flag == 1) { ping_timeout.tv_sec = 5; ping_timeout.tv_usec = 0; ping_enabled = 1; } } mx_fd = mx_fd +1; int s = select(mx_fd,&set_fd,NULL,NULL,ping_enabled ? &ping_timeout : NULL); if (s == 0) { if (exit_flag == 1) { exit(0); } if (tour_ended == 1) { if (ping_count >= 0) ping_count--; } if (ping_count == 0) { ping_enabled = 0; end_multicast(); } else { int i; //send_ping_request(pf_fd,host_ip_struct,dest_ip); for (i = 1; i< 11;i++) { if (ping_status[i] == 1) { dest_ip.s_addr = ping_add[i].s_addr; send_ping_request(pf_fd,host_ip_struct,dest_ip); } } } } else if (FD_ISSET(rt_fd,&set_fd)) { int len = recvfrom(rt_fd,&packet,sizeof(struct tour_packet),0,NULL,NULL); enable_multicating(ip_multicast,port_multicast); int ping_index = packet.index - 1; dest_ip.s_addr = packet.vm_list[ping_index].s_addr; printf(" \n %s Received Source Routing Packet from %s \n",print_time(),getVMbyIPaddr(dest_ip)); ping_enabled = 1; char *vm_id = getVMbyIPaddr(dest_ip); vm_id++; vm_id++; int node_no = atoi(vm_id); ping_status[node_no] = 1; ping_add[node_no].s_addr = dest_ip.s_addr; send_ping_request(pf_fd,host_ip_struct,dest_ip); if (packet.index == packet.end_index) { tour_ended = 1; continue; } send_to_next_node(&packet,count); } else if (FD_ISSET(udp_fd,&set_fd)) { char buf[100]; int len = recvfrom(udp_fd,buf,100,0,NULL,NULL); printf("\n Node %s Received %s \n",getVMbyIPaddr(ip_hw_map[0].ip),buf); if (multicast_message_sent == 0) { multicast_socket.sin_family = AF_INET; multicast_socket.sin_addr.s_addr = ip_multicast.s_addr; multicast_socket.sin_port = htons(PORT_MULTICAST); memset(buf,'\0',100); sprintf(buf,"This is multicast message from %s",getVMbyIPaddr(ip_hw_map[0].ip)); printf("\n Node %s ,i am member of group \n",getVMbyIPaddr(ip_hw_map[0].ip)); printf("\n Node %s Sending %s \n",getVMbyIPaddr(ip_hw_map[0].ip),buf); int s = sendto(udp_fd,(void *)buf,100,0,(struct sockaddr *)&multicast_socket,sizeof(struct sockaddr)); multicast_message_sent = 1; tour_ended = 1; ping_enabled = 0; exit_flag = 1; //printf("Multicast packet sent %d",s); if (s<=0) perror("multicast error"); continue; } } else if (FD_ISSET(pg_fd,&set_fd)) { //printf("\n PING reply received \n "); proc_v4(pg_fd); } } }