void got_packet (u_char *args, const struct pcap_pkthdr *header, const u_char *packet) { static int count = 1; int etype=0, protocol=0; int size_ip, size_tcp, size_total; const struct sniff_ethernet * ethernet; const struct sniff_ip * ip; const struct sniff_tcp *tcp; ethernet = (struct sniff_ethernet*)(packet); ip = (struct sniff_ip*)(packet + 14); size_ip = IP_HL(ip); tcp = (struct sniff_tcp*)(packet + 14 + size_ip); size_tcp = TH_OFF(tcp)*4; size_total = ntohs(ip->ip_len) + 14; printf("\ncount : %d\n", count); count++; printf("------------------------------\n"); etype = show_addr(packet); if (etype == IPV4) { protocol = show_ipv4_ip(packet); if (protocol == TCP) { show_port(packet); if (size_total != (size_ip + size_tcp + 14)) { printf("------------------------------\n"); show_data(args, header, packet, *(packet+size_total), size_total); printf("------------------------------\n"); } } else if (protocol == UDP) { show_port(packet); printf("------------------------------\n"); show_data(args, header, packet, 42, size_total); printf("------------------------------\n"); } printf("------------------------------\n"); } else if (etype == ARP) { show_ark_ip(packet); } }
int main(int argc, char **argv) { int retval = 0; struct switch_dev *dev; struct switch_attr *a; struct switch_val val; int err; int i; int cmd = CMD_NONE; char *cdev = NULL; int cport = -1; int cvlan = -1; char *ckey = NULL; char *cvalue = NULL; if(argc < 4) print_usage(); if(strcmp(argv[1], "dev")) print_usage(); cdev = argv[2]; for(i = 3; i < argc; i++) { char *arg = argv[i]; if (cmd != CMD_NONE) { print_usage(); } else if (!strcmp(arg, "port") && i+1 < argc) { cport = atoi(argv[++i]); } else if (!strcmp(arg, "vlan") && i+1 < argc) { cvlan = atoi(argv[++i]); } else if (!strcmp(arg, "help")) { cmd = CMD_HELP; } else if (!strcmp(arg, "set") && i+1 < argc) { cmd = CMD_SET; ckey = argv[++i]; if (i+1 < argc) cvalue = argv[++i]; } else if (!strcmp(arg, "get") && i+1 < argc) { cmd = CMD_GET; ckey = argv[++i]; } else if (!strcmp(arg, "load") && i+1 < argc) { if ((cport >= 0) || (cvlan >= 0)) print_usage(); cmd = CMD_LOAD; ckey = argv[++i]; } else if (!strcmp(arg, "show")) { cmd = CMD_SHOW; } else { print_usage(); } } if (cmd == CMD_NONE) print_usage(); if (cport > -1 && cvlan > -1) print_usage(); dev = swlib_connect(cdev); if (!dev) { fprintf(stderr, "Failed to connect to the switch\n"); return 1; } swlib_scan(dev); if (cmd == CMD_GET || cmd == CMD_SET) { if(cport > -1) a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_PORT, ckey); else if(cvlan > -1) a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_VLAN, ckey); else a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_GLOBAL, ckey); if(!a) { fprintf(stderr, "Unknown attribute \"%s\"\n", ckey); goto out; } } switch(cmd) { case CMD_SET: if ((a->type != SWITCH_TYPE_NOVAL) && (cvalue == NULL)) print_usage(); if(cvlan > -1) cport = cvlan; if(swlib_set_attr_string(dev, a, cport, cvalue) < 0) { fprintf(stderr, "failed\n"); retval = -1; goto out; } break; case CMD_GET: if(cvlan > -1) val.port_vlan = cvlan; if(cport > -1) val.port_vlan = cport; if(swlib_get_attr(dev, a, &val) < 0) { fprintf(stderr, "failed\n"); retval = -1; goto out; } print_attr_val(a, &val); putchar('\n'); break; case CMD_LOAD: swconfig_load_uci(dev, ckey); break; case CMD_HELP: list_attributes(dev); break; case CMD_SHOW: if (cport >= 0 || cvlan >= 0) { if (cport >= 0) show_port(dev, cport); else show_vlan(dev, cvlan, false); } else { show_global(dev); for (i=0; i < dev->ports; i++) show_port(dev, i); for (i=0; i < dev->vlans; i++) show_vlan(dev, i, true); } break; } out: swlib_free_all(dev); return 0; }