/* internal helper */ struct hwa_info *Get_hw_addrs() { struct hwa_info *hwa; if ( (hwa = get_hw_addrs()) == NULL) { fprintf(stderr, "ERROR: get_hw_addrs()\n"); exit(EXIT_FAILURE); } return hwa; }
struct hwa_info * Get_hw_addrs() { struct hwa_info *hwa; if ( (hwa = get_hw_addrs()) == NULL) err_quit("get_hw_addrs error"); return(hwa); }
struct hwa_info * Get_hw_addrs() { struct hwa_info *hwa; if ( (hwa = get_hw_addrs()) == NULL) { printf("get_hw_addrs error\n"); exit(1); } return(hwa); }
void getNodeName(){ struct hwa_info *hwa, *hwahead; struct sockaddr *sa; struct hostent *hptr; for (hwahead = hwa = get_hw_addrs(); hwa != NULL; hwa = hwa->hwa_next) { if(strcmp(hwa->if_name, "eth0") == 0 || strcmp(hwa->if_name, "wlan0") == 0 ){ if ( (sa = hwa->ip_addr) != NULL) myNodeIP = Sock_ntop_host(sa, sizeof(*sa)); } } myNodeIP = Sock_ntop_host(sa, sizeof(*sa)); if(inet_aton(myNodeIP, &ip_list[0]) != 0){ if((hptr = gethostbyaddr(&ip_list[0], sizeof(struct in_addr), AF_INET)) == NULL){ err_quit ( "gethostbyaddress error for IP address: %s: %s" , myNodeIP, hstrerror(h_errno)); debug("errno: %s", strerror(errno)); } printf("The server host name is %s.\n", hptr->h_name); myNodeName =(char*) malloc(strlen(hptr->h_name)); strcpy(myNodeName,hptr->h_name); } }
int main (int argc, char **argv) { struct hwa_info *hwa, *hwahead; struct sockaddr *sa; unsigned char *ptr; int i, prflag; for (hwahead = hwa = get_hw_addrs(); hwa != NULL; hwa = hwa->hwa_next) { 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", ntop(sa)); prflag = 0; i = 0; do { if (hwa->if_haddr[i] != '\0') { prflag = 1; break; } } while (++i < IFHWADDRLEN); if (prflag) { printf(" HW addr = "); ptr = hwa->if_haddr; i = IFHWADDRLEN; do { printf("%.2x%s", *ptr++ & 0xff, (i == 1) ? " " : ":"); } while (--i > 0); } printf("\n interface index = %d\n\n", hwa->if_index); } free_hwa_info(hwahead); exit(0); }
int main(int argc, const char * argv[]) { printf("Server running\n"); struct hwa_info *hwa, *tmp; struct sockaddr *sa; int sockfd; struct sockaddr_un srvaddr; if((hwa = get_hw_addrs())==NULL) { printf("get_hw_addrs error\n"); exit(1); } int cur; int i; for (tmp = hwa; tmp != NULL; tmp = tmp->hwa_next) { if (!strcmp(tmp->if_name, "eth0") && tmp->ip_addr != NULL) { sa = tmp->ip_addr; for(i=0; i<10; i++) { if(strcmp(vms[i], Sock_ntop_host(sa, sizeof(*sa)))==0) { cur=i+1; break; } } break; } } printf("Current VM: %d\n", cur); if((sockfd=socket(AF_LOCAL, SOCK_DGRAM, 0))<0) { printf("cannot create socket\n"); exit(1); } bzero(&srvaddr, sizeof(srvaddr)); srvaddr.sun_family=AF_LOCAL; strcpy(srvaddr.sun_path, SERV_PATH); unlink(srvaddr.sun_path); if(bind(sockfd, (SA *) &srvaddr, sizeof(srvaddr))<0) { printf("bind socket error\n"); exit(1); } int recv; int src_port; char src_addr[16]; char recBuf[MSG_SIZE]; char sendBuf[MSG_SIZE]; time_t tv; while(1) { msg_recv(sockfd, recBuf, src_addr, &src_port); for(i=0; i<10; i++) { if(strcmp(vms[i], src_addr) == 0) { recv=i+1; break; } } printf("Server at VM%d received client msg from VM%d\n", cur, recv); tv=time(NULL); snprintf(sendBuf, sizeof(sendBuf), "%.24s\r\n", ctime(&tv)); msg_send(sockfd, vms[recv-1], src_port, sendBuf, 0); printf("Reply sent from VM%d to VM%d\n", cur, recv); } return 0; }
int main(int argc, char **argv){ struct hwa_info *hwa, *tmp; struct sockaddr *sa; struct sockaddr_un client_addr; int cur,i; int sockfd; int s,r; int flag; int src_port; char tempfile[MAXLINE]; char sendBuf[MSG_SIZE]="hi"; char recBuf[MSG_SIZE]; char src_addr[16]; char buf[MAXLINE]; fd_set rset; struct timeval tv; time_t ts; printf("\n====Client Service====\n\n"); if((hwa=get_hw_addrs())==NULL) err_sys("Getting interface information error!\n"); /* //get IP address except for the eth0 sa = hwa->hwa_next->ip_addr; //get node information i = 0; while((Sock_ntop_host(sa, sizeof(*sa)) != vms[i]) && i < 10){ i++; } node = i+1; printf("\nThis is vm%d \n\n",node);*/ for (tmp = hwa; tmp != NULL; tmp = tmp->hwa_next) { if (!strcmp(tmp->if_name, "eth0") && tmp->ip_addr != NULL) { sa = tmp->ip_addr; for(i=0; i<10; i++) { if(strcmp(vms[i], Sock_ntop_host(sa, sizeof(*sa)))==0) { cur=i+1; break; } } break; } } printf("Current VM: %d\n", cur); strcpy(tempfile, "fnameXXXXXX"); //get unique temp filename if(mkstemp(tempfile)<0) err_sys("Getting unique name error!\n"); //remove the file unlink(tempfile); bzero(&client_addr, sizeof(client_addr)); client_addr.sun_family=AF_LOCAL; strcpy(client_addr.sun_path, tempfile); printf("\nSun path is %s \n\n", client_addr.sun_path); //create UNIX domain socket if((sockfd=socket(AF_LOCAL, SOCK_DGRAM, 0))<0) err_sys("Creating socket error!"); if(bind(sockfd, (SA *) &client_addr, sizeof(client_addr))<0) err_sys("Bind error!\n"); while(1) { printf("Please choose VM from 1 to 10 as a server node: \n"); if(scanf("%d", &s)<0) err_sys("Getting choice error!\n"); //send request to local ODR msg_send(sockfd, vms[s-1], SERV_PORT_NO, sendBuf, 0); printf("\nclient at node vm %d sending request to server at vm %d \n\n", cur, s); //receive reply from local ODR printf("...Waiting...\n\n"); RECEIVE: //3 seconds timeout FD_ZERO(&rset); FD_SET(sockfd, &rset); tv.tv_sec=3; tv.tv_usec=0; if((flag = select(sockfd+1, &rset, NULL, NULL, &tv))<0) err_sys("Reading from local ODR error!\n"); if(flag == 0) { //timeout and retransmit printf("client at node vm %d: timeout on response from vm %d \n\n", cur, s); //set force discovery flag msg_send(sockfd, vms[s-1], SERV_PORT_NO, sendBuf, 1); printf("\nclient at node vm %d sending request to server at vm %d \n\n", cur, s); goto RECEIVE; }else{ //receive message msg_recv(sockfd, recBuf, src_addr, &src_port); //get source node index of reply for(i = 0; i < 10; i++){ if(strcmp(src_addr, vms[i])==0) { r=i+1; break; } } //get time stamp ts=time(NULL); memset(buf,0,sizeof(buf)); snprintf(buf, sizeof(buf), "%.24s\r\n", ctime(&ts)); printf("client at node vm %d: received from vm %d: <%s>\n", cur, r, buf); } } unlink(client_addr.sun_path); return 0; }
int main(int argc, char *argv[]) { //char buff[MAX_PACKET_SIZE], *payload; char buff[4096], *ptr, in_ad[20]; int int_num = 0; //Hardware addressing structure struct hwa_info *hwahead = NULL, *interface, *hwa; struct sockaddr_ll sa; struct ethhdr eth_header; arprr arpmsg; struct sockaddr_in sd; socklen_t len = sizeof(struct sockaddr_ll); int sockfd = 0; int nbytes; if(argc != 4) { printf("Usage: ARP_TEST <dest_ip> <dest_mac> <interface_id>\n"); exit(1); } memset(buff, 0, 4096); memset(&sa, 0, sizeof(struct sockaddr_ll)); memset(&arpmsg, 0, sizeof(arprr)); memset(ð_header, 0, sizeof(struct ethhdr)); int_num = atoi(argv[3]); printf("ARP: Getting Hardware Information\n"); /************ START HARDWARE INFORMATION QUERY ***************/ hwahead = get_hw_addrs(); if(hwahead != NULL) PrintAddrs(hwahead); else { printf("ARP: hwahead = NULL\n"); exit(1); } for (hwa = hwahead; hwa != NULL; hwa = hwa->hwa_next) { if(hwa->ip_alias == 0 && hwa->ip_loop == 0 && hwa->if_index == int_num) { interface = hwa; break; } } /*********************** END HARDWARE QUERY ***************/ printf("ARP: Creating socket\n"); /*********************** CREATE SOCKET ***************/ //Uncomment the following line when we are able to run this as root. //Wont work if you aren't root! sockfd = socket(PF_PACKET, SOCK_RAW, htons(GRP8_P_ARP)); //dst_addr = ether_aton("ff:ff:ff:ff:ff:ff"); //48:5b:39:89:a8:8c memset(in_ad,0,sizeof(in_ad)); strcpy(in_ad,argv[2]); memcpy(eth_header.h_dest,(void *)ether_aton(in_ad),6); memcpy(eth_header.h_source, interface->if_haddr, 6); eth_header.h_proto = htons(GRP8_P_ARP); inet_pton(AF_INET, argv[2], &(sd.sin_addr)); printf("ARP: Intializing sockaddr_ll header\n"); sa.sll_family = AF_PACKET; sa.sll_protocol = htons(GRP8_P_ARP); sa.sll_ifindex = int_num; sa.sll_hatype = 0; //gets set automattically on the receiver side sa.sll_pkttype = 0; //gets set automattically on the receiver side sa.sll_halen = 6; memcpy(sa.sll_addr, eth_header.h_dest, 6); printf("ARP: Intializing odr header\n"); //sa.sll_addr = interface->if_haddr; !DESTINATION ADDRESS arpmsg.src_ip = ((struct sockaddr_in *)interface->ip_addr)->sin_addr.s_addr; arpmsg.dest_ip = sd.sin_addr.s_addr; memcpy(arpmsg.src_mac, eth_header.h_dest, 6); arpmsg.op = OP_ARP_REQ; ptr = buff; memcpy(ptr, ð_header, sizeof(struct ethhdr)); ptr = ptr + sizeof(struct ethhdr); memcpy(ptr, &arpmsg, sizeof(arprr)); ptr = ptr + sizeof(arprr); nbytes = sendto(sockfd, buff, 1024, 0, (struct sockaddr *)&sa, len); if(nbytes < 0) { perror("sendto"); exit(1); } printf("%i bytes sent\n", nbytes); /*********************** CLEAN UP ***************/ free_hwa_info(hwahead); exit(0); }
int notmain(void) { ssize_t n; struct hwa_info *hwahead; int rawsock; socklen_t len; /* raw socket vars*/ struct sockaddr_ll raw_addr; //bcast unsigned char dst_mac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; //mine unsigned char src_mac[6] = {0x5c, 0x51, 0x4f, 0x11, 0x25, 0x65}; unsigned char dst_mac[6] = {0x5c, 0x51, 0x4f, 0x11, 0x25, 0x65}; int index = 1; //vm1 eth2 index //unsigned char src_mac[6] = {0x00, 0x0c, 0x29, 0x49, 0x3f, 0x65}; //unsigned char dst_mac[6] = {0x00, 0x0c, 0x29, 0x49, 0x3f, 0x65}; //int index = 3; //vm1 eth2 //unsigned char src_mac[6] = {0x00,0x0c,0x29,0x49,0x3f,0x6f}; //unsigned char dst_mac[6] = {0x00,0x0c,0x29,0x49,0x3f,0x6f}; //vm2 eth1 //unsigned char src_mac[6] = {0x00, 0x0c,0x29, 0xd9, 0x08, 0xf6}; //unsigned char dst_mac[6] = {0x00, 0x0c,0x29, 0xd9, 0x08, 0xf6}; //int index = 3; char* buff = malloc(ETH_FRAME_LEN); memset(buff, 0, sizeof(ETH_FRAME_LEN)); memset(route_table, 0, NUM_NODES); add_route(route_table, "192.168.1.1", src_mac, 1, 1, 1, 1); add_route(route_table, "192.168.1.2", src_mac, 1, 1, 1, 1); find_route_index(route_table, "192.168.1.1"); find_route_index(route_table, "192.168.1.2"); find_route_index(route_table, "192.168.1.3"); if ((hwahead = get_hw_addrs()) == NULL) { /* get MAC addrs of our interfaces */ fprintf(stderr, "ERROR: get_hw_addrs()\n"); exit(EXIT_FAILURE); } print_hw_addrs(hwahead); rm_eth0_lo(&hwahead); print_hw_addrs(hwahead); if(hwahead == NULL) { _ERROR("%s", "You've got no interfaces left!\n"); exit(EXIT_FAILURE); } rawsock = socket(AF_PACKET, SOCK_RAW, htons(PROTO)); if(rawsock < 0) { perror("ERROR: socket(RAW)"); exit(EXIT_FAILURE); } /* todo: sending packet for test, remove it */ _DEBUG("%s", "sending packet...\n"); craft_frame(rawsock, index, &raw_addr, buff, src_mac, dst_mac, "sup", 4); printf("sendinf over index: %d\n", raw_addr.sll_ifindex); printf("from mac: (%02X:%02X:%02X:%02X:%02X:%02X)\n", src_mac[0], src_mac[1], src_mac[2], src_mac[3], src_mac[4], src_mac[5]); printf("to mac: (%02X:%02X:%02X:%02X:%02X:%02X)\n", raw_addr.sll_addr[0], raw_addr.sll_addr[1], raw_addr.sll_addr[2], raw_addr.sll_addr[3], raw_addr.sll_addr[4], raw_addr.sll_addr[5]); n = sendto(rawsock, buff, ETH_FRAME_LEN, 0, (struct sockaddr const *) &raw_addr, sizeof(struct sockaddr_ll)); if(n < 1) { perror("sendto(RAW)"); exit(EXIT_FAILURE); } memset(&raw_addr, 0, sizeof(struct sockaddr_ll)); len = sizeof(struct sockaddr_ll); n = recvfrom(rawsock, buff, ETH_FRAME_LEN, 0, (struct sockaddr*)&raw_addr, &len); if(n < 1) { perror("error"); exit(EXIT_FAILURE); } printf("done: %s\n", buff + sizeof(struct ethhdr)); /*fixme ^^*/ return 1; }