xref: /minix/external/bsd/dhcp/dist/omapip/test.c (revision bb9622b5)
1 /*	$NetBSD: test.c,v 1.1.1.2 2014/07/12 11:58:00 spz Exp $	*/
2 /* test.c
3 
4    Test code for omapip... */
5 
6 /*
7  * Copyright (c) 2009-2010,2013-2014 by Internet Systems Consortium, Inc. ("ISC")
8  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
9  * Copyright (c) 1999-2003 by Internet Software Consortium
10  *
11  * Permission to use, copy, modify, and distribute this software for any
12  * purpose with or without fee is hereby granted, provided that the above
13  * copyright notice and this permission notice appear in all copies.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
16  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
18  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  *   Internet Systems Consortium, Inc.
24  *   950 Charter Street
25  *   Redwood City, CA 94063
26  *   <info@isc.org>
27  *   https://www.isc.org/
28  *
29  */
30 
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: test.c,v 1.1.1.2 2014/07/12 11:58:00 spz Exp $");
33 
34 #include "config.h"
35 
36 #include <time.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <stdarg.h>
40 #include <string.h>
41 #include <omapip/result.h>
42 #include <sys/time.h>
43 #include <omapip/omapip.h>
44 #include <omapip/isclib.h>
45 
46 int main (int argc, char **argv)
47 {
48 	omapi_object_t *listener = (omapi_object_t*)0;
49 	omapi_object_t *connection = (omapi_object_t*)0;
50 	isc_result_t status;
51 
52 	status = dhcp_context_create(DHCP_CONTEXT_PRE_DB | DHCP_CONTEXT_POST_DB,
53 				     NULL, NULL);
54 	if (status != ISC_R_SUCCESS) {
55 		fprintf(stderr, "Can't initialize context: %s\n",
56 			isc_result_totext(status));
57 		exit(1);
58 	}
59 
60 	omapi_init ();
61 
62 	if (argc > 1 && !strcmp (argv [1], "listen")) {
63 		if (argc < 3) {
64 			fprintf (stderr, "Usage: test listen port\n");
65 			exit (1);
66 		}
67 		status = omapi_generic_new (&listener, MDL);
68 		if (status != ISC_R_SUCCESS) {
69 			fprintf (stderr, "omapi_generic_new: %s\n",
70 				 isc_result_totext (status));
71 			exit (1);
72 		}
73 		status = omapi_protocol_listen (listener,
74 						(unsigned)atoi (argv [2]), 1);
75 		if (status != ISC_R_SUCCESS) {
76 			fprintf (stderr, "omapi_listen: %s\n",
77 				 isc_result_totext (status));
78 			exit (1);
79 		}
80 		omapi_dispatch (0);
81 	} else if (argc > 1 && !strcmp (argv [1], "connect")) {
82 		if (argc < 4) {
83 			fprintf (stderr, "Usage: test listen address port\n");
84 			exit (1);
85 		}
86 		status = omapi_generic_new (&connection, MDL);
87 		if (status != ISC_R_SUCCESS) {
88 			fprintf (stderr, "omapi_generic_new: %s\n",
89 				 isc_result_totext (status));
90 			exit (1);
91 		}
92 		status = omapi_protocol_connect (connection,
93 						 argv [2],
94 						 (unsigned)atoi (argv [3]), 0);
95 		fprintf (stderr, "connect: %s\n", isc_result_totext (status));
96 		if (status != ISC_R_SUCCESS)
97 			exit (1);
98 		status = omapi_wait_for_completion (connection, 0);
99 		fprintf (stderr, "completion: %s\n",
100 			 isc_result_totext (status));
101 		if (status != ISC_R_SUCCESS)
102 			exit (1);
103 		/* ... */
104 	} else {
105 		fprintf (stderr, "Usage: test [listen | connect] ...\n");
106 		exit (1);
107 	}
108 
109 	return 0;
110 }
111