/** * test case for get_ipaddr() functions */ void TestEthernet::tcGet_ipaddr(void) { cout << "获取IP地址:" << endl; char* interface_name = NULL; char* strRetValue = NULL; if(0 != get_ipaddr(interface_name, strRetValue)) { cout << "获取IP地址成功!" << endl; cout << "interface_name : " << interface_name << endl; cout << "strRetValue : " << strRetValue << endl; } else { cout << "获取IP地址失败!" << endl; } char interface_name1[] = "eth0"; char *strRetValue1 = NULL; if(0 != get_ipaddr(interface_name1, strRetValue1)) { cout << "获取IP地址成功!" << endl; cout << "interface_name : " << interface_name1 << endl; cout << "strRetValue : " << strRetValue1 << endl; } else { cout << "获取IP地址失败!" << endl; } char interface_name2[] = "abc"; char *strRetValue2 = NULL; if(0 != get_ipaddr(interface_name2, strRetValue2)) { cout << "获取IP地址成功!" << endl; cout << "interface_name : " << interface_name2 << endl; cout << "strRetValue : " << strRetValue2 << endl; } else { cout << "获取IP地址失败!" << endl; } }
int main(int argc, char *argv[]) { int i; init(); /* Init some variables (like malloc timestamp string, encrypt text string, etc.) */ check_par(argc, argv); /* Check command arguments number */ open_config(argv); /* Open config file and check if it failed */ open_log(argv); /* Open log file and check if it failed */ get_ipaddr(); /* Get server IP address */ create_socket(); /* Create a socket */ bind_socket(); /* Bind the socket */ listen_socket(); /* Listen at the socket */ print_server_info(); /* Print server information */ while (TRUE) { /* Read until the end of file */ if (read_flag) { if (fscanf(fcfg, "%s", enc_txt) == EOF) { finish_flag = 1; break; } else { fscanf(fcfg, "%s", dec_txt); } } read_flag = 0; init_select(); /* Select function */ if (select_func() == -1) break; for (i = 0; i < max_fds + 1; i++) { if (FD_ISSET(i, &rfds)) { if (i == sockfd) { /* If have a new client connect */ if (accept_new_cli() == -1) break; /* Try to accept new client */ if (check_connect() == -1) break; /* Check connect message from client */ if (print_client_info() == -1) break; /* Print the information of client side */ store_client_ip(); /* Store the client ip address */ break; } else { /* If have new message from client side */ client_ip = get_host_by_sockfd(i); /* Get the client ip address by socket */ recv_socket_msg(i, recv_mark); /* Get the message from socket */ handle_client_msg(i); /* Handle client message (SUCCESS_MSG, FAILURE_MSG, DISPATCH_MSG, etc.) */ break; } } if (main_flag == EXIT_FAILURE) break; } if (main_flag == EXIT_FAILURE) break; } remained_cli = ask_clients_quit(); /* Ask clients quit and count the remain clients number */ wait_clients_quit(); /* Wait for all clients quit */ quit_server(); /* Clean up and quit server, also print the message to log */ return main_flag; }
t_stat udp_parse_remote (int32 link, char *premote) { // This routine will parse a remote address string in any of these forms - // // llll:w.x.y.z:rrrr // llll:name.domain.com:rrrr // llll::rrrr // w.x.y.z:rrrr // name.domain.com:rrrr // // In all examples, "llll" is the local port number that we use for listening, // and "rrrr" is the remote port number that we use for transmitting. The // local port is optional and may be omitted, in which case it defaults to the // same as the remote port. This works fine if the other IMP is actually on // a different host, but don't try that with localhost - you'll be talking to // yourself!! In both cases, "w.x.y.z" is a dotted IP for the remote machine // and "name.domain.com" is its name (which will be looked up to get the IP). // If the host name/IP is omitted then it defaults to "localhost". char *end, *colon; int32 port; struct hostent *he; if (*premote == '\0') return SCPE_2FARG; // Look for the local port number. If it's not there, set rxport to zero for now. port = strtoul(premote, &end, 10); udp_links[link].rxport = 0; if ((*end == ':') && (port > 0)) { udp_links[link].rxport = port; premote = end+1; } // Look for "name:port" and extract the remote port... if ((colon = strchr(premote, ':')) == NULL) return SCPE_ARG; *colon++ = '\0'; port = strtoul(colon, &end, 10); if ((*end != '\0') || (port == 0)) return SCPE_ARG; udp_links[link].txport = port; if (udp_links[link].rxport == 0) udp_links[link].rxport = port; // Now try to parse the host as a dotted IP address ... if (get_ipaddr(premote, &udp_links[link].ipremote, NULL) == SCPE_OK) return SCPE_OK; // Special kludge - allow just ":port" to mean "localhost:port" ... if(*premote == '\0') { if (udp_links[link].rxport == udp_links[link].txport) fprintf(stderr,"WARNING - use different transmit and receive ports!\n"); premote = "localhost"; } // Not a dotted IP - try to lookup a host name ... if ((he = gethostbyname(premote)) == NULL) return SCPE_OPENERR; udp_links[link].ipremote = * (unsigned long *) he->h_addr_list[0]; if (udp_links[link].ipremote == INADDR_NONE) { fprintf(stderr,"WARNING - unable to resolve \"%s\"\n", premote); return SCPE_OPENERR; } udp_links[link].ipremote = ntohl(udp_links[link].ipremote); return SCPE_OK; }
void * time_stamper() { time_t now; while(1){ sleep(1); time(&now); if(sleeping && now - sleep_time > 5){ syslog(LOG_DEBUG, "i slept"); struct ether_addr hwaddr; struct in_addr ipaddr; get_hwaddr(&hwaddr); get_ipaddr(&ipaddr); send_poison_packet(&ipaddr,&hwaddr, NULL); initialize(); sleeping = 0; //i slept } time(&sleep_time); } }
int send_run(int sock) #endif { log_debug("send", "thread started"); pthread_mutex_lock(&send_mutex); #ifdef ZMAP_PCAP_INJECT /* Using pcap, mirror the linux SOCK_RAW behaviour as closely as possible */ unsigned char mac[ETHER_ADDR_LEN]; struct in_addr src_ip = {0}; //pcap_t *pc = get_pcap_t(); /* We don't need the index; we have a pcap handle to the proper interface */ get_hwaddr(mac); get_ipaddr(&src_ip); #else //int sock = get_socket(); struct sockaddr_ll sockaddr; // get source interface index struct ifreq if_idx; memset(&if_idx, 0, sizeof(struct ifreq)); if (strlen(zconf.iface) >= IFNAMSIZ) { log_error("send", "device interface name (%s) too long\n", zconf.iface); return -1; } strncpy(if_idx.ifr_name, zconf.iface, IFNAMSIZ-2); if (ioctl(sock, SIOCGIFINDEX, &if_idx) < 0) { perror("SIOCGIFINDEX"); return -1; } int ifindex = if_idx.ifr_ifindex; // get source interface mac struct ifreq if_mac; memset(&if_mac, 0, sizeof(struct ifreq)); strncpy(if_mac.ifr_name, zconf.iface, IFNAMSIZ-1); if (ioctl(sock, SIOCGIFHWADDR, &if_mac) < 0) { perror("SIOCGIFHWADDR"); return -1; } // find source IP address associated with the dev from which we're sending. // while we won't use this address for sending packets, we need the address // to set certain socket options and it's easiest to just use the primary // address the OS believes is associated. struct ifreq if_ip; memset(&if_ip, 0, sizeof(struct ifreq)); strncpy(if_ip.ifr_name, zconf.iface, IFNAMSIZ-1); if (ioctl(sock, SIOCGIFADDR, &if_ip) < 0) { perror("SIOCGIFADDR"); return -1; } // wbk TODO: gateway MAC. // destination address for the socket memset((void*) &sockaddr, 0, sizeof(struct sockaddr_ll)); sockaddr.sll_ifindex = ifindex; sockaddr.sll_halen = ETH_ALEN; memcpy(sockaddr.sll_addr, zconf.gw_mac, ETH_ALEN); #endif /* not ZMAP_PCAP_INJECT */ /* may move down... TODO wbk */ char buf[MAX_PACKET_SIZE]; memset(buf, 0, MAX_PACKET_SIZE); zconf.probe_module->thread_initialize(buf, #ifdef ZMAP_PCAP_INJECT mac, #else (unsigned char *)if_mac.ifr_hwaddr.sa_data, #endif zconf.gw_mac, zconf.target_port); pthread_mutex_unlock(&send_mutex); // adaptive timing to hit target rate uint32_t count = 0; uint32_t last_count = count; double last_time = now(); uint32_t delay = 0; int interval = 0; volatile int vi; if (zconf.rate > 0) { // estimate initial rate delay = 10000; for (vi = delay; vi--; ) ; delay *= 1 / (now() - last_time) / (zconf.rate / zconf.senders); interval = (zconf.rate / zconf.senders) / 20; last_time = now(); } while (1) { // adaptive timing delay if (delay > 0) { count++; for (vi = delay; vi--; ) ; if (!interval || (count % interval == 0)) { double t = now(); delay *= (double)(count - last_count) / (t - last_time) / (zconf.rate / zconf.senders); if (delay < 1) delay = 1; last_count = count; last_time = t; } } // generate next ip from cyclic group and update global state // (everything locked happens here) pthread_mutex_lock(&send_mutex); if (zsend.complete) { pthread_mutex_unlock(&send_mutex); break; } if (zsend.sent >= zconf.max_targets) { zsend.complete = 1; zsend.finish = now(); pthread_mutex_unlock(&send_mutex); break; } if (zconf.max_runtime && zconf.max_runtime <= now() - zsend.start) { zsend.complete = 1; zsend.finish = now(); pthread_mutex_unlock(&send_mutex); break; } uint32_t curr = cyclic_get_next_ip(); if (curr == zsend.first_scanned) { zsend.complete = 1; zsend.finish = now(); } zsend.sent++; pthread_mutex_unlock(&send_mutex); for (int i=0; i < zconf.packet_streams; i++) { uint32_t src_ip = get_src_ip(curr, i); uint32_t validation[VALIDATE_BYTES/sizeof(uint32_t)]; validate_gen(src_ip, curr, (uint8_t *)validation); zconf.probe_module->make_packet(buf, src_ip, curr, validation, i); if (zconf.dryrun) { zconf.probe_module->print_packet(stdout, buf); } else { int l = zconf.probe_module->packet_length; #ifdef ZMAP_PCAP_INJECT int rc = pcap_inject(pc, buf, (size_t)l); if (rc == -1) { struct in_addr addr; addr.s_addr = curr; log_fatal("send", "pcap_inject() failed for %s. %s", /* TODO: make log_debug */ inet_ntoa(addr), strerror(errno)); pthread_mutex_lock(&send_mutex); zsend.sendto_failures++; pthread_mutex_unlock(&send_mutex); } #else /* TODO: error handling can be shared. */ int rc = sendto(sock, buf + zconf.send_ip_pkts*sizeof(struct ethhdr), l, 0, (struct sockaddr *)&sockaddr, sizeof(struct sockaddr_ll)); if (rc < 0) { struct in_addr addr; addr.s_addr = curr; log_debug("send", "sendto failed for %s. %s", inet_ntoa(addr), strerror(errno)); pthread_mutex_lock(&send_mutex); zsend.sendto_failures++; pthread_mutex_unlock(&send_mutex); } #endif } } } log_debug("send", "thread finished"); return EXIT_SUCCESS; }
int main(int argc, char **argv) { struct sockaddr_in salocal; struct sockaddr_in saremote; struct servent *svp; u_short svc_port; char username[MAXPWNAM]; char passwd[MAXPASS]; char *ptr; char authstring[MAXLINE]; const char *cfname = NULL; int err = 0; socklen_t salen; int c; while ((c = getopt(argc, argv, "h:p:f:w:i:t:")) != -1) { switch(c) { case 'f': cfname = optarg; break; case 'h': strcpy(server, optarg); break; case 'p': strcpy(svc_name, optarg); break; case 'w': strcpy(secretkey, optarg); break; case 'i': strcpy(identifier, optarg); break; case 't': retries = atoi(optarg); break; } } /* make standard output line buffered */ if (setvbuf(stdout, NULL, _IOLBF, 0) != 0) return 1; if (cfname) { if (rad_auth_config(cfname) < 0) { fprintf(stderr, "%s: can't open configuration file '%s'.\n", argv[0], cfname); exit(1); } } if (!*server) { fprintf(stderr, "%s: Server not specified\n", argv[0]); exit(1); } if (!*secretkey) { fprintf(stderr, "%s: Shared secret not specified\n", argv[0]); exit(1); } /* * Open a connection to the server. */ svp = getservbyname(svc_name, "udp"); if (svp != NULL) svc_port = ntohs((u_short) svp->s_port); else svc_port = atoi(svc_name); if (svc_port == 0) svc_port = PW_AUTH_UDP_PORT; /* Get the IP address of the authentication server */ if ((auth_ipaddr = get_ipaddr(server)) == 0) { fprintf(stderr, "Couldn't find host %s\n", server); exit(1); } sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) { perror("socket"); exit(1); } memset(&saremote, 0, sizeof(saremote)); saremote.sin_family = AF_INET; saremote.sin_addr.s_addr = htonl(auth_ipaddr); saremote.sin_port = htons(svc_port); if (connect(sockfd, (struct sockaddr *) &saremote, sizeof(saremote)) < 0) { perror("connect"); exit(1); } salen = sizeof(salocal); if (getsockname(sockfd, (struct sockaddr *) &salocal, &salen) < 0) { perror("getsockname"); exit(1); } #ifdef O_NONBLOCK fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL, 0) | O_NONBLOCK); #endif nas_ipaddr = ntohl(salocal.sin_addr.s_addr); while (fgets(authstring, MAXLINE, stdin) != NULL) { char *end; /* protect me form to long lines */ if ((end = strchr(authstring, '\n')) == NULL) { err = 1; continue; } if (err) { printf("ERR\n"); err = 0; continue; } if (strlen(authstring) > MAXLINE) { printf("ERR\n"); continue; } /* Strip off the trailing newline */ *end = '\0'; /* Parse out the username and password */ ptr = authstring; while (isspace(*ptr)) ptr++; if ((end = strchr(ptr, ' ')) == NULL) { printf("ERR\n"); /* No password */ continue; } *end = '\0'; urldecode(username, ptr, MAXPWNAM); ptr = end + 1; while (isspace(*ptr)) ptr++; urldecode(passwd, ptr, MAXPASS); if (authenticate(sockfd, username, passwd)) printf("OK\n"); else printf("ERR\n"); } close(sockfd); exit(1); }
void cmd_parser(unsigned long data) { char *curr_pos = &user_string[0]; int ret; while(1) { while(cmd_in_progress) os_TaskDelay(10); DBG_P(( DBG_L0 "\r\n> ")); cmd_parser_read_line(); if(strlen(user_string) == 0) { /* Do Nothing. */ } else if(!memcmp(user_string,"iwlist",6)){ userif_prepare_scan_cmd(0); } else if(!memcmp(user_string,"iwconf",6)){ curr_pos = &user_string[get_next_word(user_string)]; if(!memcmp(curr_pos,"essid",5)) { curr_pos = &curr_pos[get_next_word(curr_pos)]; specificSSID.Len = strlen(curr_pos); memcpy((void *)specificSSID.SsId, curr_pos, strlen(curr_pos)); if(link_present) { if(currbss_type != BSS_INDEPENDENT) userif_prepare_deauth_cmd(); else userif_prepare_adhoc_stop_cmd(); } else { userif_prepare_scan_cmd(1); } link_present = 0; } else if (!memcmp(curr_pos,"mode",4)) { curr_pos = &curr_pos[get_next_word(curr_pos)]; if(!memcmp(curr_pos,"ad-hoc",6)) { bss_type = BSS_INDEPENDENT; } else if(!memcmp(curr_pos,"manage",6)) { bss_type = BSS_INFRASTRUCTURE; } else { bss_type = BSS_ANY; } } else if (!memcmp(curr_pos,"ap",2)) { curr_pos = &curr_pos[get_next_word(curr_pos)]; get_macaddr(curr_pos,(char *)specificBSSID); if (FindBSSIDinList()) { userif_prepare_auth_cmd(); } else { if(link_present) { link_present = 0; userif_prepare_deauth_cmd(); } else { userif_prepare_scan_cmd(2); } } } } else if(!memcmp(user_string,"econfi",6)){ unsigned int ip; unsigned int nm; unsigned int gw; curr_pos = &user_string[get_next_word(user_string)]; get_ipaddr(curr_pos, (char *)&ip); curr_pos = &curr_pos[get_next_word(curr_pos)]; get_ipaddr(curr_pos, (char *)&nm); curr_pos = &curr_pos[get_next_word(curr_pos)]; get_ipaddr(curr_pos, (char *)&gw); sys_tcpip_init(ip, nm); } else if(!memcmp(user_string, "printip", 7)){ DBG_P(( DBG_L0 "%d.%d.%d.%d,", ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3] )); DBG_P(( DBG_L0 "%d.%d.%d.%d,", net_mask[0], net_mask[1], net_mask[2], net_mask[3] )); DBG_P(( DBG_L0 "%d.%d.%d.%d\r\n", def_gtwy[0], def_gtwy[1], def_gtwy[2], def_gtwy[3] )); } else if(!memcmp(user_string, "printmac", 8)){ char mac[6]; GetMACAddr(NULL, mac); DBG_P(( DBG_L0 "%02x:%02x:%02x:%02x:%02x:%02x\r\n", \ mac[0], mac[1], mac[2], mac[3], mac[4], mac[5])); } else if(!memcmp(user_string,"ping",4)){ curr_pos = &user_string[get_next_word(user_string)]; if(!memcmp(curr_pos,"stop",4)) { send_ping = 0; } else { get_ipaddr(curr_pos, ping_ipaddr); #ifdef EMBEDDED_TCPIP userif_prepare_open_raw_socket(); #else userif_prepare_macaddr_get_cmd(); #endif send_ping = 1; } } /* Link-local address manager. */ else if(!memcmp(user_string, "linklocal", 9)) { curr_pos = &user_string[get_next_word(user_string)]; if(!memcmp(curr_pos, "start", 5)) { /* Launch the link local manager */ ret = ll_init(); if(ret) DBG_P(( DBG_L0 "Error launching link local: %d.\r\n", ret)); } else if(!memcmp(curr_pos, "stop", 4)) { /* Kill the link local manager. */ ret = ll_shutdown(); if(ret) DBG_P(( DBG_L0 "Error killing link local: %d.\r\n", ret)); } } /* mDNS responder */ else if(!memcmp(user_string, "mdns", 4)) { curr_pos = &user_string[get_next_word(user_string)]; if(!memcmp(curr_pos, "start", 5)) { /* launch the mDNS responder */ ret = mdns_responder_init(); if(ret) DBG_P(( DBG_L0 "Error launching mDNS responder: %d.\r\n",ret)); } else if(!memcmp(curr_pos, "stop", 4)) { /* stop the mDNS responder */ ret = mdns_responder_shutdown(); if(ret) DBG_P(( DBG_L0 "Error stopping mDNS responder: %d.\r\n", ret)); } } else if(!memcmp(user_string, "mcast", 5 )){ curr_pos = &user_string[get_next_word(user_string)]; if(!memcmp(curr_pos, "get", 3 )) { userif_prepare_mcast_cmd(); } else if(!memcmp(curr_pos, "set", 3 )) { userif_prepare_mcast_add_cmd(); } } /* httpd interface */ else if(!memcmp(user_string, "httpd", 5)) { curr_pos = &user_string[get_next_word(user_string)]; if(!memcmp(curr_pos, "start", 5)) { ret = httpd_init(); if(ret) DBG_P(( DBG_L0 "Error launching httpd: %d.\r\n",ret)); } else if(!memcmp(curr_pos, "stop", 4)) { ret = httpd_shutdown(); if(ret) DBG_P(( DBG_L0 "Error stopping httpd: %d.\r\n", ret)); } else { DBG_P(( DBG_L0 "No such httpd command: %s.\r\n", curr_pos)); } } /* log interface */ else if(!memcmp(user_string, "log", 3)) { curr_pos = &user_string[get_next_word(user_string)]; if(!memcmp(curr_pos, "init", 5)) { ret = log_init(); if(ret) DBG_P(( DBG_L0 "Error launching logger: %d.\r\n",ret)); } else if(!memcmp(curr_pos, "shutdown", 8)) { ret = log_shutdown(); if(ret) DBG_P(( DBG_L0 "Error stopping logger: %d.\r\n", ret)); } else if(!memcmp(curr_pos, "dump", 4)) { ret = log_dump(); if(ret) DBG_P(( DBG_L0 "Error dumping log: %d.\r\n", ret)); } else if(!memcmp(curr_pos, "purge", 5)) { ret = log_purge(); if(ret) DBG_P(( DBG_L0 "Error purging log: %d.\r\n", ret)); } else if(!memcmp(curr_pos, "write", 5)) { curr_pos = &curr_pos[get_next_word(curr_pos)]; ret = log(curr_pos); if(ret) DBG_P(( DBG_L0 "Failed to write log: %d.\r\n", ret)); } else { DBG_P(( DBG_L0 "No such log command: %s.\r\n", curr_pos)); } } else if(!memcmp(user_string, "help", 4)) { print_usage(); } else { #ifdef UART_DRV DBG_P(( DBG_L0 "Unknown command.\r\n")); #endif } } }