void printEtherHeader(u_char *buf) { struct ether_header *eth; eth = (struct ether_header *)buf; printf("----------- ETHERNET -----------\n"); printf("Dst MAC addr : %17s \n", mac_ntoa(eth->ether_dhost)); printf("Src MAC addr : %17s \n", mac_ntoa(eth->ether_shost)); int type = ntohs(eth->ether_type); printf("Ethernet Type : 0x%04x\n", ntohs(eth->ether_type));//2バイト以上扱う時はntoh- }
void parse_arp_request ( const Ether_pkg *pkg ) { unsigned char *pchar; pchar = (unsigned char *)pkg->arp_tpa; printf ( "rcv dst MAC=[%s] IP=[%d.%d.%d.%d]\n",mac_ntoa ( pkg->arp_tha ), pchar[0], pchar[1], pchar[2], pchar[3]); // printf ( "rcv dst MAC=[%s] IP=[%s]\n",mac_ntoa ( pkg->arp_tha ),inet_ntoa ( * ( struct in_addr * ) pkg->arp_tpa ) ); pchar = (unsigned char *)pkg->arp_spa; // printf ( "IP=[%d.%d.%d.%d]\n", pchar[0], pchar[1], pchar[2], pchar[3]); printf ( "rcv src MAC=[%s] IP=[%d.%d.%d.%d]\n",mac_ntoa ( pkg->arp_sha ), pchar[0], pchar[1], pchar[2], pchar[3]); // printf ( "rcv src MAC=[%s] IP=[%s]\n",mac_ntoa ( pkg->arp_sha ),inet_ntoa ( * ( struct in_addr * ) pkg->arp_spa ) ); // printf("\n"); }
void printArp(u_char *buf) { struct ether_arp *arp; arp = (struct ether_arp *)buf; printf("----------- ARP ----------\n"); printf("arp_hrd=%u\n", ntohs(arp->arp_hrd)); printf("arp_pro=%u\n", ntohs(arp->arp_pro)); printf("arp_hln=%u\n", arp->arp_hln); printf("arp_pln=%u\n", arp->arp_pln); printf("arp_op=%u\n", ntohs(arp->arp_op)); printf("arp_sha=%s\n", mac_ntoa(arp->arp_sha)); printf("arp_spa=%s\n", ip_ntoa2(arp->arp_spa)); printf("arp_tha=%s\n", mac_ntoa(arp->arp_tha)); printf("arp_tpa=%s\n", ip_ntoa2(arp->arp_tpa)); // printf("arp_tpa=%s\n",ip_ntoa(*((u_int32_t *)arp->arp_tpa))); }
int main ( int argc, char **argv ) { struct timeval tvafter, tvpre; struct timezone tz; gettimeofday ( &tvpre , &tz ); unsigned char mac[7]; unsigned char ip[5]; char dest[16] = {0}; unsigned char broad_mac[7] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00}; memset ( mac, 0, sizeof ( mac ) ); memset ( ip, 0, sizeof ( ip ) ); if ( GetLocalMac ( "eth0", mac, ip ) == -1 ) return ( -1 ); printf ( "Local Mac=[%s] Ip=[%s]\n", mac_ntoa ( mac ), inet_ntoa ( * ( struct in_addr * ) ip ) ); //if ( argc==1 ) return ( -1 ); int i = 0; for(i=1;i<256;i++) { sprintf ( dest,"172.16.0.%d",i ); //sprintf ( dest, "255.255.255.255", i ); sendpkg ( mac, broad_mac, ip, dest ); } gettimeofday ( &tvafter , &tz ); printf ( "\nProgramm finish:%dms\n", ( tvafter.tv_sec - tvpre.tv_sec ) *1000 + ( tvafter.tv_usec - tvpre.tv_usec ) / 1000 ); return 0; }
int main ( int argc,char **argv ) { struct timeval tvafter,tvpre; struct timezone tz; gettimeofday ( &tvpre , &tz ); unsigned char mac[7]; unsigned char ip[5]; //char dip[16]={0}; char dest[16]={0}; unsigned char broad_mac[7]={0xff,0xff,0xff,0xff,0xff,0xff,0x00}; //以太网帧首部的硬件地址填FF:FF:FF:FF:FF:FF表示广播 memset ( mac,0,sizeof ( mac ) ); memset ( ip,0,sizeof ( ip ) ); if ( GetLocalMac ( "eth0",mac,ip ) ==-1 ) return ( -1 ); printf ( "本地 Mac=[%s] Ip=[%s]\n", mac_ntoa ( mac ),inet_ntoa ( * ( struct in_addr * ) ip ) ); //if ( argc==1 ) return ( -1 ); /*sprintf ( dest,"192.168.0.%d",64 ); uint32_t dip= inet_addr("192.168.0.66"); sendpkg (mac,broad_mac,(char *)&dip,dest); /*/ int i=0; for(i=10;i<76;i++){ sprintf ( dest,"202.194.201.%d",i ); sendpkg ( mac,broad_mac,ip,dest ); } /**/ gettimeofday ( &tvafter , &tz ); printf ( "\n程序执行完毕:%d毫秒\n", ( tvafter.tv_sec-tvpre.tv_sec ) *1000+ ( tvafter.tv_usec-tvpre.tv_usec ) /1000 ); return 0; }
void make_arp_reply(char *mac, Ether_pkg *pkg) { unsigned char *pchar; unsigned char ip_temp[4]; unsigned char mac_temp[6]; /************* 以太网帧 **************/ /* ARP回复目的MAC */ memcpy((char *)pkg->ether_dhost, pkg->ether_shost, 6); /* ARP回复源MAC */ memcpy(mac_temp, pkg->ether_shost, 6); memcpy((char *)pkg->ether_shost, mac, 6); // pkg->ether_type = htons(ETHERTYPE_ARP); /************* ARP报文 **************/ /* 操作代码 */ pkg->ar_op = htons(ARPOP_REPLY); /* 源MAC地址 */ memcpy(pkg->arp_sha, mac, 6); /* 源IP地址 */ memcpy(ip_temp, pkg->arp_spa, 4); memcpy(pkg->arp_spa, pkg->arp_tpa, 4); /* 目的MAC地址 */ memcpy(pkg->arp_tha, mac_temp, 6); /* 目的IP */ memcpy(pkg->arp_tpa, ip_temp, 4); /* 打印调试信息 */ printf("arp reply:\n"); printf ( "dst MAC=[%s]\n",mac_ntoa ( pkg->ether_dhost )); printf ( "src MAC=[%s]\n",mac_ntoa ( pkg->ether_shost )); printf ( "dst MAC=[%s] IP=[%d.%d.%d.%d]\n",mac_ntoa ( pkg->arp_tha ), pkg->arp_tpa[0], pkg->arp_tpa[1], pkg->arp_tpa[2], pkg->arp_tpa[3]); printf ( "src MAC=[%s] IP=[%d.%d.%d.%d]\n",mac_ntoa ( pkg->arp_sha ), pkg->arp_spa[0], pkg->arp_spa[1], pkg->arp_spa[2], pkg->arp_spa[3]); // printf ( "dst MAC=[%s] IP=[%d.%d.%d.%d]\n",mac_ntoa ( pkg->arp_sha ), pchar[0], pchar[1], pchar[2], pchar[3]); }
static void ebt_mac_ret_call(char *mod, struct mac_addr *mac, int add) { char str[128]; int ret; snprintf(str, sizeof(str), EBTABLES " %s ARP_LIMIT_TLCHECK %s --source %s -j DROP", mod, add ? "2" : "", mac_ntoa(mac)); ret = system(str); if (ret) fprintf(stderr, "%i: Calling ebtables for TL failed with status %i\n", clock, ret); }
static void ebt_mac_limit_call(char *mod, struct mac_addr *mac) { char str[128]; int ret; snprintf(str, sizeof(str), EBTABLES " %s ARP_LIMIT_TLCHECK --source %s --limit 6/min --limit-burst 50 -j RETURN", mod, mac_ntoa(mac)); ret = system(str); if (ret) fprintf(stderr, "%i: Calling ebtables for TL failed with status %i\n", clock, ret); }
int main ( int argc,char **argv ) { struct timeval tvafter,tvpre; struct timezone tz; gettimeofday ( &tvpre , &tz ); unsigned char mac[7]; unsigned char ip[5]; char dest[16]={0}; unsigned char broad_mac[7]={0xff,0xff,0xff,0xff,0xff,0xff,0x00}; //以太网帧首部的硬件地址填FF:FF:FF:FF:FF:FF表示广播 memset ( mac,0,sizeof ( mac ) ); memset ( ip,0,sizeof ( ip ) ); if ( GetLocalMac ( ETH_DEVICE, mac,ip ) ==-1 ) return ( -1 ); //源IP地址设置为网关 // inet_aton ( "202.194.201.254", ( struct in_addr * ) ip ); printf ( "local Mac=[%s] Ip=[%s]\n", mac_ntoa ( mac ),inet_ntoa ( * ( struct in_addr * ) ip ) ); printf("\n"); int i=0; sprintf ( dest,"202.194.201.%d",i ); arp_attack(mac,broad_mac,ip,dest); // int i=0; // while(1) { for(i=200;i<=200;i++) { // getchar(); sprintf ( dest,"202.194.201.%d",i ); /* 源mac * 目的mac * 源ip * 目的ip */ sendpkg ( mac,broad_mac,ip,dest ); // sleep(1); } } gettimeofday ( &tvafter , &tz ); printf ( "\n%dms\n", ( tvafter.tv_sec-tvpre.tv_sec ) *1000+ ( tvafter.tv_usec-tvpre.tv_usec ) /1000 ); return 0; }
void parse_ether_package ( const Ether_pkg *pkg ) { printf ( "SourceIP=[%s] MAC=[%s]\n", inet_ntoa ( * ( struct in_addr * ) pkg->arp_spa ), mac_ntoa ( pkg->arp_sha ) ); printf ( "TargetIP=[%s] MAC=[%s]\n", inet_ntoa ( * ( struct in_addr * ) pkg->arp_tpa ), mac_ntoa ( pkg->arp_tha ) ); }
void Config::load(aJsonObject * json){ //loads values from json. Values not in json are not touched byte tmpmac[6]; char debug_out[32]; IPAddress tmpip; int json_strlen; aJsonObject* cnfobj = aJson.getObjectItem(json, "use_dhcp"); if (cnfobj) { if (strcmp(cnfobj->valuestring, "0") == 0 || cnfobj->valueint == 0) { use_dhcp = 0; } else { use_dhcp = 1; } } cnfobj = aJson.getObjectItem(json, "mac"); if (cnfobj && cnfobj->type == aJson_String && mac_aton(cnfobj->valuestring, tmpmac) == 1) { mac_aton(cnfobj->valuestring, mac); Serial.print(F("mac ")); mac_ntoa(mac, debug_out); Serial.println(debug_out); } if (use_dhcp == 0) { // Static IP config cnfobj = aJson.getObjectItem(json, "ip"); if (cnfobj && cnfobj->type == aJson_String && inet_aton(cnfobj->valuestring, tmpip) == 1) { inet_aton(cnfobj->valuestring, ip); Serial.print(F("ip ")); inet_ntoa(ip, debug_out); Serial.println(debug_out); } cnfobj = aJson.getObjectItem(json, "netmask"); if (cnfobj && cnfobj->type == aJson_String && inet_aton(cnfobj->valuestring, tmpip) == 1) { inet_aton(cnfobj->valuestring, netmask); Serial.print(F("netmask ")); inet_ntoa(netmask, debug_out); Serial.println(debug_out); } cnfobj = aJson.getObjectItem(json, "gateway"); if (cnfobj && cnfobj->type == aJson_String && inet_aton(cnfobj->valuestring, tmpip) == 1) { inet_aton(cnfobj->valuestring, gateway); Serial.print(F("gateway ")); inet_ntoa(gateway, debug_out); Serial.println(debug_out); } } cnfobj = aJson.getObjectItem(json, "smtp"); if (cnfobj && cnfobj->type == aJson_String && inet_aton(cnfobj->valuestring, tmpip) == 1) { inet_aton(cnfobj->valuestring, smtp); Serial.print(F("smtp ")); inet_ntoa(smtp, debug_out); Serial.println(debug_out); } cnfobj = aJson.getObjectItem(json, "smtp_port"); if (cnfobj) { sscanf(cnfobj->valuestring, "%d", &smtp_port); } else { smtp_port = 25; } cnfobj = aJson.getObjectItem(json, "mail_from"); if (cnfobj->type == aJson_String) { if (mail_from != NULL) { free(mail_from); mail_from = NULL; } json_strlen = strnlen(cnfobj->valuestring, 31); mail_from = (char *) malloc(json_strlen + 1); if (mail_from == NULL) { Serial.println(F("OOM on config load (mail_from)")); } else { strlcpy(mail_from, cnfobj->valuestring, json_strlen + 1); } } cnfobj = aJson.getObjectItem(json, "ntp"); if (cnfobj && cnfobj->type == aJson_String && inet_aton(cnfobj->valuestring, tmpip) == 1) { inet_aton(cnfobj->valuestring, ntp); Serial.print(F("ntp ")); inet_ntoa(ntp, debug_out); Serial.println(debug_out); } cnfobj = aJson.getObjectItem(json, "sys_name"); if (cnfobj->type == aJson_String) { if (sys_name != NULL) { free(sys_name); sys_name = NULL; } json_strlen = strnlen(cnfobj->valuestring, 31); sys_name = (char *) malloc(json_strlen + 1); if (sys_name == NULL) { Serial.println(F("OOM on config load (sys_name)")); } else { strlcpy(sys_name, cnfobj->valuestring, json_strlen + 1); } } cnfobj = aJson.getObjectItem(json, "time_zone"); if (cnfobj) { sscanf(cnfobj->valuestring, "%d", &time_zone); } else { time_zone = 2; } cnfobj = aJson.getObjectItem(json, "ups_trigger_level"); if (cnfobj) { sscanf(cnfobj->valuestring, "%d", &ups_trigger_level); } else { ups_trigger_level = 255; } #ifdef USE_CO2_SENSOR cnfobj = aJson.getObjectItem(json, "co2_400"); if (cnfobj) { sscanf(cnfobj->valuestring, "%d", &co2_400); } else { co2_400 = CO2_400; } cnfobj = aJson.getObjectItem(json, "co2_40k"); if (cnfobj) { sscanf(cnfobj->valuestring, "%d", &co2_40k); } else { co2_40k = CO2_40k; } #endif #ifdef USE_PH_SENSOR cnfobj = aJson.getObjectItem(json, "ph_4"); if (cnfobj) { sscanf(cnfobj->valuestring, "%d", &ph_4); } else { ph_4 = PH_4; } cnfobj = aJson.getObjectItem(json, "ph_7"); if (cnfobj) { sscanf(cnfobj->valuestring, "%d", &ph_7); } else { ph_7 = PH_7; } #endif #ifdef USE_EC_SENSOR cnfobj = aJson.getObjectItem(json, "ec_low_ion"); if (cnfobj) { sscanf(cnfobj->valuestring, "%d", &ec_low_ion); } else { ec_low_ion = EC_LOW_ION; } cnfobj = aJson.getObjectItem(json, "ec_high_ion"); if (cnfobj) { sscanf(cnfobj->valuestring, "%d", &ec_high_ion); } else { ec_high_ion = EC_HIGH_ION; } #endif }
int Config::save(){ char buffer[20]; file_for_write("", "config.jso", &sd_file); sd_file.print(F("{")); sd_file.print(F("\"use_dhcp\":")); sd_file.print(use_dhcp); sd_file.print(F(",")); mac_ntoa(mac, buffer); sd_file.print(F("\"mac\":\"")); sd_file.print(buffer); sd_file.print(F("\",")); inet_ntoa(ip, buffer); sd_file.print(F("\"ip\":\"")); sd_file.print(buffer); sd_file.print(F("\",")); inet_ntoa(netmask, buffer); sd_file.print(F("\"netmask\":\"")); sd_file.print(buffer); sd_file.print(F("\",")); inet_ntoa(gateway, buffer); sd_file.print(F("\"gateway\":\"")); sd_file.print(buffer); sd_file.print(F("\",")); inet_ntoa(ntp, buffer); sd_file.print(F("\"ntp\":\"")); sd_file.print(buffer); sd_file.print(F("\",")); inet_ntoa(smtp, buffer); sd_file.print(F("\"smtp\":\"")); sd_file.print(buffer); sd_file.print(F("\",")); sd_file.print(F("\"mail_from\":\"")); sd_file.print(mail_from); sd_file.print(F("\",")); sd_file.print(F("\"sys_name\":\"")); sd_file.print(sys_name); sd_file.print(F("\",")); sd_file.print(F("\"smtp_port\":\"")); sd_file.print(smtp_port); sd_file.print(F("\",")); sd_file.print(F("\"time_zone\":\"")); sd_file.print(time_zone); sd_file.print(F("\",")); sd_file.print(F("\"ups_trigger_level\":\"")); sd_file.print(ups_trigger_level); sd_file.print(F("\",")); #ifdef USE_CO2_SENSOR sd_file.print(F("\"co2_400\":\"")); sd_file.print(co2_400); sd_file.print(F("\",")); sd_file.print(F("\"co2_40k\":\"")); sd_file.print(co2_40k); sd_file.print(F("\",")); #endif #ifdef USE_PH_SENSOR sd_file.print(F("\"ph_4\":\"")); sd_file.print(ph_4); sd_file.print(F("\",")); sd_file.print(F("\"ph_7\":\"")); sd_file.print(ph_7); sd_file.print(F("\",")); #endif #ifdef USE_EC_SENSOR sd_file.print(F("\"ec_low_ion\":\"")); sd_file.print(ec_low_ion); sd_file.print(F("\",")); sd_file.print(F("\"ec_high_ion\":\"")); sd_file.print(ec_high_ion); sd_file.print(F("\"")); #endif sd_file.print(F("}")); sd_file.close(); return 1; }
char *addr_mac_ntoa(void *addr) { return mac_ntoa((struct mac_addr *)addr); }