File: pcap2skb.c

package info (click to toggle)
openswan 1%3A2.4.6%2Bdfsg.2-1.1%2Betch2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 25,000 kB
  • ctags: 16,877
  • sloc: ansic: 121,112; sh: 19,782; xml: 9,699; asm: 4,422; perl: 4,087; makefile: 3,367; tcl: 713; exp: 657; yacc: 396; pascal: 328; lex: 289; sed: 265; awk: 124; lisp: 3
file content (87 lines) | stat: -rw-r--r-- 1,780 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * this program converts a pcap file to a C source file for use
 * by testing code.
 *
 */

#include <pcap.h>
#include <ctype.h>
#include <stdlib.h>

static int packnum = 0;

void pcap_skbuff(u_char *user,
		 const struct pcap_pkthdr *h,
		 const u_char *bytes)
{
  FILE *out = (FILE *)user;
  char line[81];
  int pos;
  int i;
  
  packnum++;
  fprintf(out, "const unsigned char packet%d_len=%d;\n", packnum, h->caplen);
  fprintf(out, "const unsigned char packet%d[]={\n", packnum);

  memset(line, ' ', sizeof(line));
  line[53]='/';
  line[54]='*';
  line[65]='*';
  line[66]='/';
  line[67]='\n';
  line[68]='\0';
  pos=0;

  for(i=0; i<h->caplen; i++) {
    if(pos==8) {
      fputs(line, out);
      memset(line, ' ', sizeof(line));
      line[53]='/';
      line[54]='*';
      line[65]='*';
      line[66]='/';
      line[67]='\n';
      line[68]='\0';
      pos=0;
    }
      
    /* line looks like:
     *  0xXX, 0xYY, 0xZZ, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE,  /+ ........ +/
     *        1         2         3         4         5         6
     * 3456789012345678901234567890123456789012345678901234567890123456789
     */
    snprintf(line+(pos*6)+4, 6, "0x%02x,", bytes[i]);
    line[(pos*6)+4+5]=' ';
    if(isprint(bytes[i])) {
      line[pos+56]=bytes[i];
    } else {
      line[pos+56]='.';
    }
    pos++;
  }
  
  fputs(line, out);
  fprintf(out, "    0};\n\n");
}

int main(int argc, char *argv[])
{
  char *f;
  char errbuf[PCAP_ERRBUF_SIZE];
  pcap_t *pc;
  FILE *out = stdout;

  while(--argc>0) {
    f = *++argv;

    pc = pcap_open_offline(f, errbuf);
    if(pc == NULL) {
      fprintf(stderr, "pcap_open_offline(%s): %s\n",
	      f, errbuf);
      exit(10);
    }

    pcap_dispatch(pc, -1, pcap_skbuff, (u_char *)out);
    pcap_close(pc);
  }
}