void print_package(unsigned int *vlanid, struct tpacket_hdr *h, struct ether_header *eth, struct iphdr *ip, struct tcphdr *tcp, void *data) { printf("from: "); printmac(eth->ether_shost); printf(" to: "); printmac(eth->ether_dhost); printf(" len:%4d",h->tp_len); if (*vlanid > 0) { printf(" (vlan:%4d)",*vlanid); } else printf(" "); printf(" %6s (0x%04x)",tok2str(str_ethertype_values,"n.k.", ntohs(eth->ether_type)),ntohs(eth->ether_type)); if (eth->ether_type == htons(ETHERTYPE_IP)) { printf(" %4s", tok2str(str_proto_values,"n.k.", ip->protocol) ); printf(" %-15s:%-6d -->",inet_ntoa(*(struct in_addr *)&ip->saddr),ntohs(tcp->source)); printf(" %-15s:%-6d",inet_ntoa(*(struct in_addr *)&ip->daddr), ntohs(tcp->dest)); } else if (eth->ether_type == htons(ETHERTYPE_ARP)) { struct ether_arp * arph = (struct ether_arp *)ip; printf(" arp %-7s", tok2str(str_arpop_values,"n.k.", ntohs(arph->arp_op))); printf(" %-15s -->",inet_ntoa(*(struct in_addr *)arph->arp_spa)); printf(" %-15s",inet_ntoa(*(struct in_addr *)arph->arp_tpa)); printf(" from: "); printmac(arph->arp_sha); printf(" to: "); printmac(arph->arp_tha); } printf("\n"); return; }
static void ICACHE_FLASH_ATTR promisc_cb(uint8 *buf, uint16 len) { os_printf("-> %3d: %d", wifi_get_channel(), len); printmac(buf, 4); printmac(buf, 10); printmac(buf, 16); os_printf("\n"); }
static int target_get_mac(target_context_t *tc) { char buf [LINE_LENGTH]; /* read MAC address from target */ msg("getting target MAC\n"); target_write_command(tc, "mac"); target_gets(tc, buf, sizeof buf); if(target_confirm_response(tc) == -1){ return -1; } /* and parse it */ msgf("target-provided remote MAC: %s", buf); if (parsemac(tc->remote_mac, buf)){ panicf("can't parse target-provided remote MAC: %s", buf); return -1; } if (opt_verbose) { msg("target-provided remote MAC (parsed): "); printmac(stdout, tc->remote_mac); msg("\n"); } return 0; }
/** Record packet to log (if dbg_logPackets was activated) */ void logPkt(bool incoming, unsigned char* data , int size) { int i,j,k=0; if (! dbg_logPackets) return; errorf("\r\n %s, length: %d\r\n", (incoming ? "IN" : "OUT"), size); //errorf(" Target MAC : %02X:%02X:%02X:%02X:%02X:%02X\n", data[k+=1],data[k+=1],data[k+=1],data[k+=1],data[k+=1],data[k+=1]); errorf(" Source MAC : "); printmac(errorf, (data+MAC_LEN)); errorf("\n Target MAC : "); printmac(errorf, data); k += (2 * MAC_LEN); //errorf(" Source MAC : %02X:%02X:%02X:%02X:%02X:%02X\n", data[k+=1],data[k+=1],data[k+=1],data[k+=1],data[k+=1],data[k+=1]); errorf("\n Ether-Type : %02X %02X\n", data[k++],data[k++]); errorf(" Command : %02X\n\n", data[k++]); errorf(" Data :\n"); for(i= 0; i < size-k; i++) { if(i != 0 && i%16 == 0) // one line of hex printing is complete... { errorf(" ", i); for(j= i-16+k; j < i+k; j++) { if(data[j] >= 32 && data[j] <= 128) errorf("%c",(unsigned char)data[j]); //if its a number or alphabet else errorf("."); //otherwise print a dot } errorf("\n"); } if(i%16 == 0) errorf(" "); errorf(" %02X",(unsigned int)data[i+k]); if(i == size-k-1) //print the last spaces { for(j=0; j < 15-i%16; j++) errorf(" "); //extra spaces errorf(" "); for(j= i+k-i%16; j <= i+k; j++) if (data[j] >= 32 && data[j] <= 128) errorf("%c", (unsigned char) data[j]); else errorf("."); errorf("\n"); } } }
void printJson() { pthread_rwlock_rdlock(&rwlock); if (strlen(path_tmp) < 5) return; FILE *fout = fopen(path_tmp, "w"); if (!fout) return; fprintf(fout, "{\n"); struct timeval tv; gettimeofday(&tv, NULL); long long current_time = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000; fprintf(fout, "\"time\": %lld,\n", current_time); fprintf(fout, "\"maclist\": ["); int i; int hasmac = 0; for (i = 0; i < 256; ++i) { struct Maclist *maclist = maclists[i]; while (maclist) { if (hasmac) fprintf(fout, ","); fprintf(fout, "\n {\n"); fprintf(fout, " \"macaddr\": \""); printmac(fout, maclist->addr); fprintf(fout, "\",\n"); fprintf(fout, " \"ip\": ["); struct Record* record = maclist->records; int hasrecord = 0; while (record) { if (hasrecord) fprintf(fout, ","); hasrecord = 1; fprintf(fout, "\n {\n"); fprintf(fout, " \"ipaddr\": \""); if (record->addrlen == 4) printipv4(fout, record->addr); else { char str[INET6_ADDRSTRLEN]; inet_ntop(AF_INET6, record->addr, str, INET6_ADDRSTRLEN); fprintf(fout, "%s", str); } fprintf(fout, "\",\n"); fprintf(fout, " \"upstream-bytes\": %lld,\n", record->bytes_up); fprintf(fout, " \"downstream-bytes\": %lld\n", record->bytes_down); fprintf(fout, " }"); record = record->next; } fprintf(fout, "]\n }"); hasmac = 1; maclist = maclist->next; } } fprintf(fout, "\n]}\n"); fclose(fout); char cmd[1000] = {0}; sprintf(cmd, "mv %s %s", path_tmp, path); system(cmd); pthread_rwlock_unlock(&rwlock); }
static void dump(struct Maclist* mac, struct Record* record) { printmac(stdout, mac->addr); printf(" "); if (record->addrlen == 4) printipv4(stdout, record->addr); else printipv6(record->addr); printf(" "); printf("%lld %lld\n", record->bytes_up, record->bytes_down); }
/** Initialize the specified adapter, and get the relative MAC address. */ void initAdapter(char* ifname) { int fd; struct ifreq ifr; fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL)); /* open socket */ strcpy(ifr.ifr_name, ifname); ioctl(fd, SIOCGIFHWADDR, &ifr); /* retrieve MAC address */ close(fd); memcpy(tc_eth_localMac, ifr.ifr_hwaddr.sa_data, 6); debugf("Got MAC address of '%s': ", ifname); printmac(debugf, tc_eth_localMac); debugf("\n"); // %02X:%02X:%02X:%02X:%02X:%02X\n", ifname, tc_eth_localMac[0], // tc_eth_localMac[1], tc_eth_localMac[2],tc_eth_localMac[3],tc_eth_localMac[4],tc_eth_localMac[5]); }
void GetAddr() { struct ifreq ifr; int sock; char nic[16] = ""; sock = socket(AF_INET, SOCK_DGRAM, 0); if(sock < 0) { fprintf(stderr, "sock create error\n"); exit(1); } strcpy(ifr.ifr_name, nic); if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) { fprintf(stderr, "ioctl create error\n"); exit(1); } memcpy(ServerMac, ifr.ifr_hwaddr.sa_data, 6); printmac(ServerMac); }
/****************************************************************************** * FunctionName : user_init * Description : entry of user application, init user function here * Parameters : none * Returns : none *******************************************************************************/ void user_init(void) { #ifdef DOUBLE_CLK_FREQ system_update_cpu_freq(160); #endif #ifdef DEV //system_uart_swap(); #endif uart_init_new(BAUD, uart_rx); DBG("WILOC-MASTER"); printf("SDK version:%s\n", system_get_sdk_version()); unsigned char mac[6]; wifi_get_macaddr(STATION_IF, mac); printf("MAC-STA:"); printmac(mac, 0); printf("\n"); wifi_get_macaddr(SOFTAP_IF, mac); printf("MAC- AP:"); printmac(mac, 0); printf("\n"); DBG(" ---- set opmode"); if (!wifi_set_opmode(STATIONAP_MODE)) { DBG(" ---- > failed to set opmode"); } DBG(" ---- done"); // wifi_softap_set_config(&softapConf); os_delay_us(300); char *ssid = SSID; char *password = PASSWORD; struct station_config stationConf; stationConf.bssid_set = 0; //need not check MAC address of AP memcpy(&stationConf.ssid, ssid, strlen(ssid) + 1); memcpy(&stationConf.password, password, strlen(password) + 1); wifi_station_set_config(&stationConf); wifi_station_set_reconnect_policy(true); wifi_station_connect(); DBG(" --- trying to connect to AP"); tx_queue = xQueueCreate(1, sizeof(int)); ringbuf_mutex = xSemaphoreCreateMutex(); ringbuf_init(&ringbuf_m, tx_buffer_m, sizeof(tx_buffer_m)); ringbuf_init(&ringbuf_t, tx_buffer_t, sizeof(tx_buffer_t)); synchronize_dev_id(); // wait until wifi is connected user_wifi_init(connect_to_server); }
/** @main Application entry-point. */ int main(int argc, char* argv[]) { int c; strcpy(initcmd, DEF_CMD_INIT); while (true) { static struct option long_options[] = { {"adapter", required_argument, 0, 'a'}, {"remote-adapter", required_argument, 0, 'b'}, {"close", no_argument, 0, 'c'}, {"blink-on-receive", no_argument, 0, 'k'}, {"log-packets", no_argument, 0, 'p'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; /* getopt_long stores the option index here. */ int option_index = 0; c = getopt_long (argc, argv, "a:b:ckpvV?", long_options, &option_index); /* Detect the end of the options. */ if (c == -1) break; switch (c) { case 'a': tc_eth_adapter = optarg; break; case 'b': tc_eth_remoteAdapter = optarg; break; case 'c': exitReason = 0; break; case 'p': dbg_logPackets = true; break; case 'v': dbg_debugOut = true; break; case 'k': dbg_blinkOnReceive = true; break; case 'V': printf("Asus-TCCI (TrendChip Command Interpreter), version %s, build %s\n", VERSION, VERSION_BUILD); printf(" by %s. Sources and updates: %s\n", AUTHOR, URL); return EC_NORMAL; case 'h': /* getopt_long already printed an error message. */ printf("usage: %s -V|--version\n", argv[0]); printf("or: %s [-a|--adapter=\"%s\"] [-b|--remote-adapter=\"%s\"] [-c|--close] [-p|--log-packets] [-v|--verbose] [-k|--blink-on-receive] [<%s>]\n", argv[0], tc_eth_adapter, tc_eth_remoteAdapter, initcmd); return EC_NORMAL; default: return 9; } } /* Print any remaining command line arguments (not options). */ if (optind < argc) { initcmd[0] = '\0'; // Clear string while (optind < argc) strncat(initcmd, argv[optind++], sizeof(initcmd)); } debugf("-- Starting...\n"); // Initialize in-socket tc_eth_sockIn = socket(AF_INET, SOCK_DGRAM, 0); if (tc_eth_sockIn < 0) { errorf("-- E: Input socket init failed!\n"); return EC_APIFAIL; } /*debugf("-- dbg_debugOut = %d, dbg_logPackets = %d, tc_eth_adapter = \"%s\", initcmd = \"%s\"\n", dbg_debugOut, dbg_logPackets, tc_eth_adapter, initcmd);*/ // Initialize adapter eth_adapterId = if_nametoindex(tc_eth_adapter); initAdapter(tc_eth_adapter); // Initialize out-socket tc_eth_sockOut = socket(AF_PACKET,SOCK_RAW, htons(ETH_P_ALL)); if (tc_eth_sockOut < 0) { errorf("-- E: Output socket init failed!\n"); return EC_APIFAIL; } // Acquired MACs debugf("Remote (TrendChip) MAC: "); printmac(debugf, tc_eth_remoteMac); debugf("\nLocal (terminal) MAC: "); printmac(debugf, tc_eth_localMac); // Initial command execution debugf("\n-- Phase 2 - Perform init-command (%s)\n", initcmd); if (!tc_exec(initcmd)) return EC_NETFAIL; // Interactive shell char inbuff[TC_CMD_MAX_LEN]; fd_set rfds; struct timeval tv; int retval; while(exitReason < 0) { FD_ZERO(&rfds); FD_SET(0, &rfds); tv.tv_sec = 0; tv.tv_usec = 100000; //100ms retval = select(1, &rfds, NULL, NULL, &tv); if (retval == -1){ perror("-- select(stdin)"); return EC_APIFAIL; } else if (retval){ debugf("-- Gathering input... "); char* cmd = fgets(inbuff, TC_CMD_MAX_LEN, stdin); if (cmd[strlen(cmd)-1] == '\n') cmd[strlen(cmd)-1] = '\0'; // Trim the trailing '\n' char. debugf("Got '%s'\n", cmd); if (cmd[0] == '\0') {} // Do nothing on blank lines. else if (strcmp(cmd, "!q") == 0 || strcmp(cmd, "!quit") == 0) exitReason = EC_NORMAL; else if (strcmp(cmd, "!v") == 0 || strcmp(cmd, "!verbose") == 0) errorf("-- Verbose output is now %s.\n", (dbg_debugOut = !dbg_debugOut) ? "ON" : "OFF"); else if (strcmp(cmd, "!p") == 0 || strcmp(cmd, "!logpackets") == 0) errorf("-- Packet logging is now %s.\n", (dbg_logPackets = !dbg_logPackets) ? "ON" : "OFF"); else if (strcmp(cmd, "!k") == 0 || strcmp(cmd, "!blink") == 0) errorf("-- Blink-on-receive is now %s.\n", (dbg_blinkOnReceive = !dbg_blinkOnReceive) ? "ON" : "OFF"); else if (strcmp(cmd, "!h") == 0 || strcmp(cmd, "!help") == 0) errorf("-- Valid client commands: \n-- ![blin]k, !h[elp], ![log]p[ackets], !q[uit], !v[erbose].\n"); else if (cmd[0] == '!') errorf("-- Unrecognized client command. Please try \"!help\" if you aren't sure.\n"); else if (!tc_exec(cmd)) errorf("-- W: Command request fail!\n"); //exitReason = EC_NETFAIL; // I/O error? } if (!tc_listen()) exitReason = EC_NETFAIL; } if (tc_eth_sockIn) close(tc_eth_sockIn); else debugf("-- W: Input socket already closed!"); if (tc_eth_sockOut) close(tc_eth_sockOut); else debugf("-- W: Output socket already closed!"); debugf("-- Connection closed, reason = %d (-1).\n", exitReason); return exitReason-1; }