int main(void *arg) { int ret = -1; int sock = -1; struct Device_Info device; unsigned char *ptr = NULL; struct sockaddr_in local_addr; struct sockaddr_in from_addr; int from_len = sizeof(struct sockaddr_in); int count = -1; fd_set readfd; char buff[BUFFER_LEN]; struct timeval timeout; timeout.tv_sec = 2; timeout.tv_usec = 0; memset(device.device_ip, 0, sizeof(device.device_ip)); memset(device.device_mac, 0, sizeof(device.device_mac)); memset(device.device_num, 0, sizeof(device.device_num)); memset(device.info, 0, sizeof(device.info)); memcpy(device.device_ip, get_local_ip(), 20); memcpy(device.device_mac, get_local_mac(), 20); memcpy(device.device_num, "AG172", sizeof("AG172")); memcpy(device.info, MATCH_INFO, sizeof(MATCH_INFO)); sock = socket(AF_INET, SOCK_DGRAM, 0); if( sock < 0 ) { printf("HandleIPFound: socket init error!\n"); return 0; } memset((void*)&local_addr, 0, sizeof(struct sockaddr_in)); local_addr.sin_family = AF_INET; local_addr.sin_addr.s_addr = htonl(INADDR_ANY); local_addr.sin_port = htons(MCAST_PORT); printf("local IP is %s\n",inet_ntoa(local_addr.sin_addr)); ret = bind(sock, (struct sockaddr*)&local_addr, sizeof(local_addr)); if(ret != 0) { printf("HandleIPFound:bind error...\n"); return 0; } while(1) { FD_ZERO(&readfd); FD_SET(sock, &readfd); ret = select(sock+1, &readfd, NULL, NULL, &timeout); switch(ret) { case -1: break; case 0: break; default: if( FD_ISSET( sock, &readfd )) { memset(buff, 0, BUFFER_LEN); count = recvfrom(sock, buff, BUFFER_LEN, 0, (struct sockaddr*)&local_addr, &from_len); printf("Recv msg is %s,count=%d\n", buff, count); if( strstr(buff, IP_FOUND)) { // memcpy(buff, (char *)&device, strlen(device)); printf("Now buff is %s\n", buff); printf("local ip is %s\n", device.device_ip); printf("local mac is: %s\n", device.device_mac); // ptr = (unsigned char *)&device; count = sendto(sock, (struct Device_Info *)&device, sizeof(device), 0, (struct sockaddr*)&local_addr, from_len); //count = sendto(sock, ptr, sizeof(device), 0, (struct sockaddr*)&local_addr, from_len); printf("count = %d\n",count); } } } } return 0; }
/** @brief Library initalization routine * * This sets up the library to begin operation with a device. It * returns the context pointer needed for almost all operations. * * @param device the name of the ethernet device if raw ethernet is used, otherwise NULL * @param ethernet one of LIBFB_ETHERNET_OFF or LIBFB_ETHERNET_ON * @param errstr A buffer of at least length LIBFB_ERRBUF_SIZE to store an error message * @return The initalized context pointer, or NULL is an error occurs */ libfb_t * libfb_init (char *device, int ethernet, char *errstr) { libfb_t *f; bpf_u_int32 mask; /* The netmask of our sniffing device */ bpf_u_int32 net; /* The IP of our sniffing device */ f = malloc (sizeof (libfb_t)); if (f == NULL) { strncpy (errstr, "Fatal error, could not allocate memory!\n", LIBFB_ERRBUF_SIZE); return NULL; } memset (f, 0, sizeof (libfb_t)); f->udp_socket = -1; if ((f->ether_on = ethernet) == LIBFB_ETHERNET_ON) { if (device == NULL) { strncpy (errstr, "Fatal error, no device specified!\n", LIBFB_ERRBUF_SIZE); goto init_error_out_1; } f->s_mac = get_local_mac (device); if (f->s_mac == NULL) { strncpy (errstr, "Unable to lookup local MAC address!\n", LIBFB_ERRBUF_SIZE); goto init_error_out_1; } f->l = libnet_init (LIBNET_LINK, device, errstr); if (f->l == NULL) goto init_error_out_2; /* libnet/libpcap will fill out errstr from here on in */ /* look up netmask */ if (pcap_lookupnet (device, &net, &mask, errstr) == -1) { fprintf (stderr, "[Warning] Can't get netmask for device %s\n", device); net = 0; mask = 0; } /* open pcap */ f->p = pcap_open_live (device, 512, 0, 0, errstr); if (f->p == NULL) goto init_error_out_3; /* set up pcap filter */ if (set_filter (f, mask) == -1) { strncpy (errstr, pcap_geterr (f->p), LIBFB_ERRBUF_SIZE); goto init_error_out_4; } } /* end of ethernet enabling functions */ f->crc_en = 0; f->device = device; return f; /* Error handling, free up things we've allocated */ init_error_out_4: pcap_close (f->p); init_error_out_3: libnet_destroy (f->l); init_error_out_2: free (f->s_mac); init_error_out_1: free (f); return NULL; }