in_addr_t anubis_lookup_ip_address(const char *expression) { //get param char *buffer = anubis_parse_param("lookup_ip_address", expression); if(!buffer) return 0; if(!strcasecmp(buffer, "ff:ff:ff:ff:ff:ff") || !strcasecmp(buffer, "Broadcast")) { return INADDR_BROADCAST; }//end if else { arp_t *handle = arp_open(); struct arp_entry arp_entry = {0}; if(!handle) { anubis_perror("arp_open()"); return 0; }//end if addr_pton(buffer, &arp_entry.arp_ha); int ret = arp_loop(handle, arp_lookup_ip_address_callback, (void *)&arp_entry); if(ret == 1) { char *ip_address = addr_ntoa(&arp_entry.arp_pa); arp_close(handle); return anubis_ip_aton(ip_address); }//end if else { anubis_err("lookup_ip_address(): \"%s\" is not found\n", buffer); arp_close(handle); return 0; }//end else }//end else }//end anubis_lookup_ip_address
static void arpd_exit(int status) { eth_close(arpd_eth); pcap_close(arpd_pcap); arp_close(arpd_arp); closelog(); unlink(PIDFILE); exit(status); }
arp_t * arp_open(void) { arp_t *a; if ((a = calloc(1, sizeof(*a))) != NULL) { #ifdef HAVE_STREAMS_MIB2 if ((a->fd = open(IP_DEV_NAME, O_RDWR)) < 0) #elif defined(HAVE_STREAMS_ROUTE) if ((a->fd = open("/dev/route", O_WRONLY, 0)) < 0) #else if ((a->fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) #endif return (arp_close(a)); #ifdef HAVE_ARPREQ_ARP_DEV if ((a->intf = intf_open()) == NULL) return (arp_close(a)); #endif } return (a); }
static int fragroute_close(void) { if (ctx.tun != NULL) tun_close(ctx.tun); if (ctx.route != NULL) route_close(ctx.route); if (ctx.intf != NULL) intf_close(ctx.intf); if (ctx.eth != NULL) eth_close(ctx.eth); if (ctx.arp != NULL) arp_close(ctx.arp); if (ctx.dfile != NULL) pcap_dump_close(ctx.dfile); #ifdef WIN32 WSACleanup(); #endif return (-1); }
arp_t * arp_open(void) { arp_t *arp; if ((arp = calloc(1, sizeof(*arp))) != NULL) { #ifdef HAVE_STREAMS_ROUTE if ((arp->fd = open("/dev/route", O_RDWR, 0)) < 0) #else if ((arp->fd = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) #endif return (arp_close(arp)); } return (arp); }
u_int8_t *anubis_lookup_mac_address(const char *expression, const char *device) { //get param char *buffer = anubis_parse_param("lookup_mac_address", expression); if(!buffer) return NULL; if(!strcasecmp(buffer, "255.255.255.255") || !strcasecmp(buffer, "Broadcast")) { return (u_int8_t *)"\xff\xff\xff\xff\xff\xff"; }//end if else if(!strncasecmp(buffer, "224.", 4) || !strncasecmp(buffer, "239.", 4)) { char mac_address[MAC_ADDRSTRLEN] = {0}; int byte1 = 1, byte2 = 0, byte3 = 0, byte4 = 0; if(sscanf(buffer, "%d.%d.%d.%d", &byte1, &byte2, &byte3, &byte4) != 4) { anubis_err("lookup_mac_address(): \"%s\" is not found\n", buffer); return NULL; }//end if snprintf(mac_address, sizeof(mac_address), "01:00:5e:%02x:%02x:%02x", byte2, byte3, byte4); return anubis_mac_aton(mac_address); }//end if multicast else { arp_t *handle = arp_open(); struct arp_entry arp_entry = {0}; int arping = 0; if(!handle) { anubis_perror("arp_open()"); return NULL; }//end if addr_pton(buffer, &arp_entry.arp_pa); AGAIN: if(arp_get(handle, &arp_entry) == 0) { char *mac_address = addr_ntoa(&arp_entry.arp_ha); arp_close(handle); if(arping) { anubis_verbose("Found \"%s\" at \"%s\"\n", buffer, mac_address); }//end if found return anubis_mac_aton(mac_address); }//end if else { if(arping) { anubis_err("lookup_mac_address(): \"%s\" is not found\n", buffer); arp_close(handle); return NULL; }//end if else { //try to arping anubis_verbose("MAC address of \"%s\" is not found in the ARP cache, trying to arping \"%s\"\n", buffer, buffer); pid_t pid = fork(); if(pid == 0) { FILE *temp_out_stream = out_stream; FILE *temp_err_stream = err_stream; int out_fail = 0; int err_fail = 0; out_stream = anubis_null_stream(); err_stream = anubis_null_stream(); if(!out_stream) { out_stream = temp_out_stream; out_fail = 1; }//end if if(!err_stream) { err_stream = temp_err_stream; err_fail = 1; }//end if //arping 3 three times for(int i = 0 ; i < 3 ; i++) anubis_arping(device, buffer); anubis_wait_microsecond(800000); //wait 800 ms //restore if(!out_fail) { fclose(out_stream); out_stream = temp_out_stream; }//end if if(!err_fail) { fclose(err_stream); err_stream = temp_err_stream; }//end if exit(0); }//end if else if(pid < 0) { anubis_perror("fork()"); }//end if #ifdef __CYGWIN__ wait(0); #else wait(NULL); #endif //wait fork finish arping = 1; goto AGAIN; } }//end else }//end else }//end anubis_lookup_mac_address