xref: /netbsd-src/external/bsd/libpcap/dist/testprogs/reactivatetest.c (revision 748408ed59e7aef1b0dd2f652356b3c051bb3440)
1*9185e895Schristos /*
2*9185e895Schristos  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3*9185e895Schristos  *	The Regents of the University of California.  All rights reserved.
4*9185e895Schristos  *
5*9185e895Schristos  * Redistribution and use in source and binary forms, with or without
6*9185e895Schristos  * modification, are permitted provided that: (1) source code distributions
7*9185e895Schristos  * retain the above copyright notice and this paragraph in its entirety, (2)
8*9185e895Schristos  * distributions including binary code include the above copyright notice and
9*9185e895Schristos  * this paragraph in its entirety in the documentation or other materials
10*9185e895Schristos  * provided with the distribution, and (3) all advertising materials mentioning
11*9185e895Schristos  * features or use of this software display the following acknowledgement:
12*9185e895Schristos  * ``This product includes software developed by the University of California,
13*9185e895Schristos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*9185e895Schristos  * the University nor the names of its contributors may be used to endorse
15*9185e895Schristos  * or promote products derived from this software without specific prior
16*9185e895Schristos  * written permission.
17*9185e895Schristos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*9185e895Schristos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*9185e895Schristos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*9185e895Schristos  */
21*9185e895Schristos 
22*9185e895Schristos #include "varattrs.h"
23*9185e895Schristos 
24*9185e895Schristos #ifndef lint
25*9185e895Schristos static const char copyright[] _U_ =
26*9185e895Schristos     "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
27*9185e895Schristos The Regents of the University of California.  All rights reserved.\n";
28*9185e895Schristos #endif
29*9185e895Schristos 
30*9185e895Schristos #include <pcap.h>
31*9185e895Schristos #include <stdio.h>
32*9185e895Schristos #include <stdlib.h>
33*9185e895Schristos #include <string.h>
34*9185e895Schristos #include <stdarg.h>
35*9185e895Schristos 
36*9185e895Schristos #include "pcap/funcattrs.h"
37*9185e895Schristos 
38*9185e895Schristos /* Forwards */
39*9185e895Schristos static void PCAP_NORETURN error(PCAP_FORMAT_STRING(const char *), ...) PCAP_PRINTFLIKE(1,2);
40*9185e895Schristos 
41*9185e895Schristos int
main(void)42*9185e895Schristos main(void)
43*9185e895Schristos {
44*9185e895Schristos 	char ebuf[PCAP_ERRBUF_SIZE];
45*9185e895Schristos 	pcap_t *pd;
46*9185e895Schristos 	int status = 0;
47*9185e895Schristos 
48*9185e895Schristos 	pd = pcap_open_live("lo0", 65535, 0, 1000, ebuf);
49*9185e895Schristos 	if (pd == NULL) {
50*9185e895Schristos 		pd = pcap_open_live("lo", 65535, 0, 1000, ebuf);
51*9185e895Schristos 		if (pd == NULL) {
52*9185e895Schristos 			error("Neither lo0 nor lo could be opened: %s",
53*9185e895Schristos 			    ebuf);
54*9185e895Schristos 		}
55*9185e895Schristos 	}
56*9185e895Schristos 	status = pcap_activate(pd);
57*9185e895Schristos 	if (status != PCAP_ERROR_ACTIVATED) {
58*9185e895Schristos 		if (status == 0)
59*9185e895Schristos 			error("pcap_activate() of opened pcap_t succeeded");
60*9185e895Schristos 		else if (status == PCAP_ERROR)
61*9185e895Schristos 			error("pcap_activate() of opened pcap_t failed with %s, not PCAP_ERROR_ACTIVATED",
62*9185e895Schristos 			    pcap_geterr(pd));
63*9185e895Schristos 		else
64*9185e895Schristos 			error("pcap_activate() of opened pcap_t failed with %s, not PCAP_ERROR_ACTIVATED",
65*9185e895Schristos 			    pcap_statustostr(status));
66*9185e895Schristos 	}
67*9185e895Schristos 	return 0;
68*9185e895Schristos }
69*9185e895Schristos 
70*9185e895Schristos /* VARARGS */
71*9185e895Schristos static void
error(const char * fmt,...)72*9185e895Schristos error(const char *fmt, ...)
73*9185e895Schristos {
74*9185e895Schristos 	va_list ap;
75*9185e895Schristos 
76*9185e895Schristos 	(void)fprintf(stderr, "reactivatetest: ");
77*9185e895Schristos 	va_start(ap, fmt);
78*9185e895Schristos 	(void)vfprintf(stderr, fmt, ap);
79*9185e895Schristos 	va_end(ap);
80*9185e895Schristos 	if (*fmt) {
81*9185e895Schristos 		fmt += strlen(fmt);
82*9185e895Schristos 		if (fmt[-1] != '\n')
83*9185e895Schristos 			(void)fputc('\n', stderr);
84*9185e895Schristos 	}
85*9185e895Schristos 	exit(1);
86*9185e895Schristos 	/* NOTREACHED */
87*9185e895Schristos }
88