All pastes #1425331 Raw Edit

pcap

public c v1 · immutable
#1425331 ·published 2009-05-17 10:57 UTC
rendered paste body
void cbPacketDump(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data){	struct tm *ltime;	char timestr[16], tmp[2048];	time_t local_tv_sec;	ip_header *ih;  udp_header *uh;  u_int ip_len;  u_short sport,dport;  int i;	local_tv_sec = header->ts.tv_sec;	ltime = localtime(&local_tv_sec);	strftime(timestr, sizeof(timestr), "%H:%M:%S", ltime);  ih = (ip_header *)(pkt_data + 14);  ip_len = (ih->ver_ihl & 0xf) * 4;  uh = (udp_header *)((u_char*)ih + ip_len);  sport = ntohs(uh->sport);  dport = ntohs(uh->dport);	printf("%s,%.6d len:%d %d.%d.%d.%d.%d -> %d.%d.%d.%d.%d\r\n",         timestr, header->ts.tv_usec, header->len,        ih->saddr.byte1,ih->saddr.byte2,ih->saddr.byte3,ih->saddr.byte4,sport,        ih->daddr.byte1,ih->daddr.byte2,ih->daddr.byte3,ih->daddr.byte4,dport);  if (header->len < 2048)  {    memset(tmp, 0, 2048);    memcpy(tmp, pkt_data, header->len);    for (i=0; i<header->len; i++) if (tmp[i] == 0 || tmp[i] == 10 || tmp[i] == 13) tmp[i] = '.';    tmp[header->len-1] = 0;    printf("%s\r\n----------\r\n", tmp);    FILE *fp = fopen("log.txt", "a+");    fwrite(pkt_data, header->len, 1, fp);    fclose(fp);  } else printf("Packet too long to show here.\r\n");}int main(){  /* !--- SNIP ---! */  pcap_if_t *alldevs;  pcap_if_t *d;  int inum = 3;  pcap_t *iface;  struct bpf_program fcode;  char errbuf[PCAP_ERRBUF_SIZE];  pcap_findalldevs(&alldevs, errbuf);  for(d=alldevs; d; d=d->next) i++;  if (i == 0) return 0;  for(d=alldevs, i=0; i<inum-1;d=d->next, i++);  if ((iface = pcap_open_live(d->name, 65536, 1, 1000, errbuf)) == NULL)   {    printf("Failed to open the interface.\r\n");    pcap_freealldevs(alldevs);    return 0;  }  printf("Opened %s...\r\n", d->description);  pcap_freealldevs(alldevs);  if (pcap_compile(iface, &fcode, "src net 66.212.229.0/24", 1, 0) < 0)  {    printf("pcap_compile(): failed.\r\n");    return 0;  }  pcap_setfilter(iface, &fcode);  pcap_loop(iface, 0, cbPacketDump, NULL);  pcap_close(iface);  /* !--- SNIP ---! */  return 0;}