int main(int argc, char *argv[], char *envp[]) { int soc, size; u_char buf[65535]; if (argc <= 1) { fprintf(stderr, "pcap device-name\n"); return 1; } if ((soc = InitRawSocket(argv[1], 0, 0)) == -1) { fprintf(stderr, "InitRawSocket:error:%s\n", argv[1]); return -1; } while (1) { if ((size = read(soc, buf, sizeof(buf))) <= 0) perror("read"); else AnalyzePacket(buf, size); } close(soc); return 0; }
int CXGooseCap::TestGoose() { struct pcap_pkthdr packet_header = {0}; FILE *f_gsecap = fopen("goose.cap_2", "rb"); assert(f_gsecap != NULL); u_char packet_data[1500] = {0}; u_char *test = packet_data; int nReadRet = 0; int nHeaderLen = sizeof(GSE_Ether_Header) + sizeof (GSE_MAC_Header); while (true) { memset(packet_data, 0, sizeof(packet_data)); nReadRet = fread(packet_data, 1, nHeaderLen, f_gsecap); if (nReadRet != nHeaderLen) { break; } GSE_Ether_Header *pHeader = (GSE_Ether_Header *)(packet_data + sizeof(GSE_MAC_Header)); int asduLength = (pHeader->ether_length[0] << 8) + pHeader->ether_length[1] - 8; nReadRet = fread(packet_data + nHeaderLen, 1, asduLength, f_gsecap); if (nReadRet != asduLength) { break; } AnalyzePacket(packet_data); //usleep(20 * 1000); } return J_OK; }
int Bridge() { struct pollfd targets[3]; int nready,i,size; u_char buf[2048]; // WLAN1 targets[0].fd=Device[0].soc; targets[0].events=POLLIN|POLLERR; // WLAN2 targets[1].fd=Device[1].soc; targets[1].events=POLLIN|POLLERR; // ETH1 targets[2].fd=Device[2].soc; targets[2].events=POLLIN|POLLERR; while(EndFlag==OFF) { if(ScanFlag==ON) { switch(nready=poll(targets,3,100)) { case -1: if(errno!=EINTR) { perror("poll"); } break; case 0: break; default: for(i=0; i<3; i=i+2) { if(targets[i].revents&(POLLIN|POLLERR)) { if((size=read(Device[i].soc,buf,sizeof(buf)))<=0) { perror("read"); } else { if(AnalyzePacket(i,buf,size)!=-1 && RewritePacket(i,buf,size)!=-1) { if(i==0) { if((size=write(Device[2].soc,buf,size))<=0) { //perror("write"); } } else if(i==2) { if((size=write(Device[0].soc,buf,size))<=0) { //perror("write"); } } } } } } break; } } } return(0); }
// Receive packets in loop, analyze, and print output // Will exit upon error or receipt of special 'end packet' // Returns 0 for success, -1 for failure int ReceiveAndProcessStream(SystemState *state) { Packet packet; uint32_t counter = 1; ReceiveBytes(&packet.size, sizeof(packet.size)); while (packet.size != 0) { if (packet.size > PACKET_MAX_SIZE) { FailAndTerminate("packet too large"); } state->stats->num_packets++; if (allocate(packet.size, 1, (void **)&packet.data) != 0) { FailAndTerminate("Failed allocating packet data"); } ReceiveBytes(packet.data, packet.size); packet.timestamp = counter++; packet.original_size = packet.size; packet.original_data = packet.data; AnalyzePacket(state, &packet); deallocate(packet.data, packet.size); ReceiveBytes(&packet.size, sizeof(packet.size)); } return 0; }
int main(int argc,char *argv[],char *envp[]) { int soc,size,c,i; u_char buf[65535]; if(argc<=1){ fprintf(stderr,"pcap device-name\n"); return(1); } if((soc=InitRawSocket(argv[1],0,0))==-1){ fprintf(stderr,"InitRawSocket:error:%s\n",argv[1]); return(-1); } while((c = getopt(argc,argv,"aivctudsA")) != -1){ switch(c){ case 'a': fil[7] = 1; break; case 'i': fil[6] = 1; break; case 'v': fil[5] = 1; break; case 'c': fil[4] = 1; break; case 't': fil[3] = 1; break; case 'u': fil[2] = 1; break; case 'd': fil[1] = 1; break; case 's': fil[0] = 1; break; case 'A': for(i = 0;i < 8;i++){ fil[i] = 1; } break; default: i = -1; printf(" '%s' This option is invalid. prease input any option.\n\n",argv[2]); printf("arp = a,ip = i,ipv6 = v,icmp = c\n\ntcp = t,udp = u,destination = d,source = s,all = A \n\n"); break; } printf("%d\n",fil[7]); } if(argv[2] == null) while(1){ if(i == -1){ break; } if((size=read(soc,buf,sizeof(buf)))<=0){ perror("read"); } else{ AnalyzePacket(buf,size); } } close(soc); return(0); }
// Receive entire DCAP file, analyze, and print output // Returns 0 for success, -1 for failure int ReceiveAndProcessFile(SystemState *state) { // Read file header uint32_t magic; ReceiveBytes(&magic, sizeof(magic)); if (magic != DCAP_FILE_MAGIC) { FailAndTerminate("invalid DCAP file"); } // Process file header and populate state variable ReceiveBytes(&state->stats->start_time, sizeof(state->stats->start_time)); ReceiveBytes(&state->stats->end_time, sizeof(state->stats->end_time)); ReceiveBytes(&state->stats->num_packets, sizeof(state->stats->num_packets)); if (state->stats->num_packets > DCAP_FILE_MAX_PACKETS) { FailAndTerminate("too many packets"); } ReceiveBytes(&state->stats->num_option_headers, sizeof(state->stats->num_option_headers)); if (state->stats->num_option_headers > OPTION_HEADERS_MAX_NUM) { FailAndTerminate("too many option headers"); } // Read optional headers OptionHeader *option=NULL; for (int num=0; num < state->stats->num_option_headers; num++) { if (option == NULL) { // Allocate first option header if (allocate(sizeof(OptionHeader), 1, (void **)&state->stats->option_headers) != 0) { FailAndTerminate("error allocating first option header"); } option = state->stats->option_headers; } else { // Allocate subsequent option headers if (allocate(sizeof(OptionHeader), 1, (void **)&option->next) != 0) { FailAndTerminate("error allocating option header"); } option = option->next; } bzero((void *)option, sizeof(OptionHeader)); ReceiveBytes(&option->type, sizeof(option->type)); ReceiveBytes(&option->length, sizeof(option->length)); // Allow 1 extra byte to ensure null termination if (allocate(option->length + 1, 1, (void **)&option->value)) { FailAndTerminate("failed to allocate option header value"); } bzero(option->value, option->length + 1); ReceiveBytes(option->value, option->length); } // Receive and process all packets for (int num=0; num < state->stats->num_packets; num++) { // Get meta data Packet packet; // Receive timestamp and size ReceiveBytes(&packet.timestamp, sizeof(packet.timestamp)); ReceiveBytes(&packet.size, sizeof(packet.size)); if (packet.size > PACKET_MAX_SIZE) { FailAndTerminate("packet too large"); } if (allocate(packet.size, 1, (void **)&packet.data) != 0) { FailAndTerminate("Failed allocating packet data"); } ReceiveBytes(packet.data, packet.size); packet.original_size = packet.size; packet.original_data = packet.data; AnalyzePacket(state, &packet); deallocate(packet.original_data, packet.original_size); } return 0; }