]>
The Tcpdump Group git mirrors - libpcap/blob - seltest.c
539f0d0f66cf4f98e2365f3f05eb5b30ad4464a9
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 static const char copyright
[] =
24 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
25 The Regents of the University of California. All rights reserved.\n";
35 #include <sys/types.h>
40 static void printme(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
41 static void usage(void) __attribute__((noreturn
));
42 static void error(const char *, ...);
43 static void warning(const char *, ...);
44 static char *copy_argv(char **);
53 main(int argc
, char **argv
)
56 bpf_u_int32 localnet
, netmask
;
57 register char *cp
, *cmdbuf
, *device
;
58 int doselect
, dotimeout
, dononblock
;
59 struct bpf_program fcode
;
60 char ebuf
[PCAP_ERRBUF_SIZE
];
67 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
68 program_name
= cp
+ 1;
70 program_name
= argv
[0];
73 while ((op
= getopt(argc
, argv
, "i:stn")) != -1)
98 device
= pcap_lookupdev(ebuf
);
103 pd
= pcap_open_live(device
, 65535, 0, 1000, ebuf
);
108 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
113 cmdbuf
= copy_argv(&argv
[optind
]);
115 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, netmask
) < 0)
116 error("%s", pcap_geterr(pd
));
118 if (pcap_setfilter(pd
, &fcode
) < 0)
119 error("%s", pcap_geterr(pd
));
120 if (pcap_get_selectable_fd(pd
) == -1)
121 error("pcap_get_selectable_fd() fails");
123 if (pcap_setnonblock(pd
, 1, ebuf
) == -1)
124 error("pcap_setnonblock failed: %s", ebuf
);
129 struct timeval timeout
;
132 FD_SET(pcap_fileno(pd
), &set1
);
135 timeout
.tv_usec
= 1000;
136 select(pcap_fileno(pd
) + 1, &set1
, NULL
, NULL
,
139 select(pcap_fileno(pd
) + 1, &set1
, NULL
, NULL
, NULL
);
140 status
= pcap_dispatch(pd
, -1, printme
, NULL
);
143 else if (status
== 0)
144 printf("No packets seen after select returns\n");
146 printf("%d packets seen after select returns\n",
150 status
= pcap_loop(pd
, -1, printme
, NULL
);
153 * We got interrupted, so perhaps we didn't
154 * manage to finish a line we were printing.
155 * Print an extra newline, just in case.
159 (void)fflush(stdout
);
164 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
165 program_name
, pcap_geterr(pd
));
168 exit(status
== -1 ? 1 : 0);
172 printme(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
174 printf("Saw a packet\n");
180 (void)fprintf(stderr
, "Usage: %s [ -stn ] [ -i interface ] [expression]\n",
187 error(const char *fmt
, ...)
191 (void)fprintf(stderr
, "%s: ", program_name
);
193 (void)vfprintf(stderr
, fmt
, ap
);
198 (void)fputc('\n', stderr
);
206 warning(const char *fmt
, ...)
210 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
212 (void)vfprintf(stderr
, fmt
, ap
);
217 (void)fputc('\n', stderr
);
222 * Copy arg vector into a new buffer, concatenating arguments with spaces.
225 copy_argv(register char **argv
)
228 register u_int len
= 0;
237 len
+= strlen(*p
++) + 1;
239 buf
= (char *)malloc(len
);
241 error("copy_argv: malloc");
245 while ((src
= *p
++) != NULL
) {
246 while ((*dst
++ = *src
++) != '\0')