Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 94 lines (80 sloc) 2.27 kb
ef8388a @ice799 Initial commit
ice799 authored
1 /*
2 * Copyright 2012, Boundary
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <errno.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <pcap/pcap.h>
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26
27 #include "khial.h"
28
29 void usage(void)
30 {
31 fprintf(stderr, "./output_packets pcap_file_name\n");
32 return;
33 }
34
35 int main(int argc, char *argv[])
36 {
37 size_t count = 0;
38 char errbuf[PCAP_ERRBUF_SIZE];
39 struct pcap_pkthdr header;
40 const u_char *packet;
41 pcap_t *pf;
42 int fd = 0;
43 FILE *file;
44 int direction = 0;
45
46 if (argc != 2) {
47 usage();
48 exit(1);
49 }
50
51 /* open a socket to the character device testpackets0.
52 * this device corresponds to the test0 interface.
53 */
54 fd = open("/dev/char/testpackets0", O_WRONLY);
55 if (fd < 0) {
56 perror("open failed");
57 exit(1);
58 }
59 file = fdopen(fd, "w+");
60
61 /* set the direction as TX */
62 direction = KHIAL_DIRECTION_TX;
63 ioctl(fd, KHIAL_SET_DIRECTION, &direction);
64
65 pf = pcap_open_offline(argv[1], errbuf);
66 if (!pf) {
67 fprintf(stderr, "Error opening pcap file %s: %s\n", argv[1], errbuf);
68 exit(1);
69 }
70
71 while (packet = pcap_next(pf, &header)) {
72 size_t bufsz = sizeof(header) + header.caplen;
73 char *buffer = malloc(bufsz);
74 if (!buffer) {
75 fprintf(stderr, "not enough memory!\n");
76 exit(1);
77 }
78
79 /* copy the header and the packet data into a buffer */
80 memcpy(buffer, &header, sizeof(header));
81 memcpy(buffer+sizeof(header), packet, header.caplen);
82
83 /* write the blob to the character device */
84 write(fd, buffer, bufsz);
85 fflush(file);
86 count++;
87 }
88
89 fprintf(stderr, "size of header: %zd\n", sizeof(header));
90 fprintf(stderr, "packet count: %zd\n", count);
91 close(fd);
92 return 0;
93 }
Something went wrong with that request. Please try again.