ISC DHCP  4.4.2b1
A reference DHCPv4 and DHCPv6 implementation
cltest.c
Go to the documentation of this file.
1 /* cltest.c
2 
3  Example program that uses the dhcpctl library. */
4 
5 /*
6  * Copyright (c) 2004-2017 by Internet Systems Consortium, Inc. ("ISC")
7  * Copyright (c) 2000-2003 by Internet Software Consortium
8  *
9  * This Source Code Form is subject to the terms of the Mozilla Public
10  * License, v. 2.0. If a copy of the MPL was not distributed with this
11  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  *
21  * Internet Systems Consortium, Inc.
22  * 950 Charter Street
23  * Redwood City, CA 94063
24  * <info@isc.org>
25  * https://www.isc.org/
26  *
27  * This software was contributed to Internet Systems Consortium
28  * by Brian Murrell.
29  */
30 
31 #include "config.h"
32 
33 #include <time.h>
34 #include <sys/time.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdarg.h>
39 #include "omapip/result.h"
40 #include "dhcpctl.h"
41 #include "dhcpd.h"
42 
43 /* Fixups */
44 isc_result_t find_class (struct class **c, const char *n, const char *f, int l)
45 {
46  return 0;
47 }
48 int parse_allow_deny (struct option_cache **oc, struct parse *cfile, int flag)
49 {
50  return 0;
51 }
52 void dhcp (struct packet *packet) { }
53 void bootp (struct packet *packet) { }
54 
55 #ifdef DHCPv6
56 /* XXX: should we warn or something here? */
57 void dhcpv6(struct packet *packet) { }
58 #ifdef DHCP4o6
59 isc_result_t dhcpv4o6_handler(omapi_object_t *h)
60 {
61  return ISC_R_NOTIMPLEMENTED;
62 }
63 #endif /* DHCP4o6 */
64 #endif /* DHCPv6 */
65 
66 int check_collection (struct packet *p, struct lease *l, struct collection *c)
67 {
68  return 0;
69 }
70 void classify (struct packet *packet, struct class *class) { }
71 
73  control_object_state_t newstate)
74 {
75  return ISC_R_SUCCESS;
76 }
77 
78 int main (int, char **);
79 
80 enum modes { up, down, undefined };
81 
82 static void usage (char *s) {
83  fprintf (stderr,
84  "Usage: %s [-n <username>] [-p <password>] [-a <algorithm>]"
85  "(-u | -d) <if>\n", s);
86  exit (1);
87 }
88 
89 int main (argc, argv)
90  int argc;
91  char **argv;
92 {
93  isc_result_t status, waitstatus;
94  dhcpctl_handle authenticator;
95  dhcpctl_handle connection;
96  dhcpctl_handle interface_handle;
97  dhcpctl_data_string result;
98  int i;
99  int mode = undefined;
100  const char *interface = 0;
101  const char *action;
102 
103  for (i = 1; i < argc; i++) {
104  if (!strcmp (argv[i], "-u")) {
105  mode = up;
106  } else if (!strcmp (argv [i], "-d")) {
107  mode = down;
108  } else if (argv[i][0] == '-') {
109  usage(argv[0]);
110  } else {
111  interface = argv[i];
112  }
113  }
114 
115  if (!interface)
116  usage(argv[0]);
117  if (mode == undefined)
118  usage(argv[0]);
119 
120  status = dhcpctl_initialize ();
121  if (status != ISC_R_SUCCESS) {
122  fprintf (stderr, "dhcpctl_initialize: %s\n",
123  isc_result_totext (status));
124  exit (1);
125  }
126 
127  authenticator = dhcpctl_null_handle;
128  connection = dhcpctl_null_handle;
129 
130  status = dhcpctl_connect (&connection, "127.0.0.1", 7911,
131  authenticator);
132  if (status != ISC_R_SUCCESS) {
133  fprintf (stderr, "dhcpctl_connect: %s\n",
134  isc_result_totext (status));
135  exit (1);
136  }
137 
138  interface_handle = dhcpctl_null_handle;
139  status = dhcpctl_new_object (&interface_handle,
140  connection, "interface");
141  if (status != ISC_R_SUCCESS) {
142  fprintf (stderr, "dhcpctl_new_object: %s\n",
143  isc_result_totext (status));
144  exit (1);
145  }
146 
147  status = dhcpctl_set_string_value (interface_handle,
148  interface, "name");
149  if (status != ISC_R_SUCCESS) {
150  fprintf (stderr, "dhcpctl_set_value: %s\n",
151  isc_result_totext (status));
152  exit (1);
153  }
154 
155  if (mode == up) {
156  /* "up" the interface */
157  printf ("upping interface %s\n", interface);
158  action = "create";
159  status = dhcpctl_open_object (interface_handle, connection,
161  if (status != ISC_R_SUCCESS) {
162  fprintf (stderr, "dhcpctl_open_object: %s\n",
163  isc_result_totext (status));
164  exit (1);
165  }
166  } else {
167  /* down the interface */
168  printf ("downing interface %s\n", interface);
169  action = "remove";
170  status = dhcpctl_open_object (interface_handle, connection, 0);
171  if (status != ISC_R_SUCCESS) {
172  fprintf (stderr, "dhcpctl_open_object: %s\n",
173  isc_result_totext (status));
174  exit (1);
175  }
176  status = dhcpctl_wait_for_completion (interface_handle,
177  &waitstatus);
178  if (status != ISC_R_SUCCESS) {
179  fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
180  isc_result_totext (status));
181  exit (1);
182  }
183  if (waitstatus != ISC_R_SUCCESS) {
184  fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
185  isc_result_totext (waitstatus));
186  exit (1);
187  }
188  status = dhcpctl_object_remove (connection, interface_handle);
189  if (status != ISC_R_SUCCESS) {
190  fprintf (stderr, "dhcpctl_open_object: %s\n",
191  isc_result_totext (status));
192  exit (1);
193  }
194  }
195 
196  status = dhcpctl_wait_for_completion (interface_handle, &waitstatus);
197  if (status != ISC_R_SUCCESS) {
198  fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
199  isc_result_totext (status));
200  exit (1);
201  }
202  if (waitstatus != ISC_R_SUCCESS) {
203  fprintf (stderr, "interface object %s: %s\n", action,
204  isc_result_totext (waitstatus));
205  exit (1);
206  }
207 
208  memset (&result, 0, sizeof result);
209  status = dhcpctl_get_value (&result, interface_handle, "state");
210  if (status != ISC_R_SUCCESS) {
211  fprintf (stderr, "dhcpctl_get_value: %s\n",
212  isc_result_totext (status));
213  exit (1);
214  }
215 
216  exit (0);
217 }
void bootp(struct packet *packet)
Definition: cltest.c:53
void dhcp(struct packet *packet)
Definition: cltest.c:52
int main(int, char **)
Definition: cltest.c:89
int parse_allow_deny(struct option_cache **oc, struct parse *cfile, int flag)
Definition: cltest.c:48
int check_collection(struct packet *p, struct lease *l, struct collection *c)
Definition: cltest.c:66
modes
Definition: cltest.c:80
@ down
Definition: cltest.c:80
@ up
Definition: cltest.c:80
@ undefined
Definition: cltest.c:80
void classify(struct packet *packet, struct class *class)
Definition: cltest.c:70
isc_result_t dhcp_set_control_state(control_object_state_t oldstate, control_object_state_t newstate)
Definition: cltest.c:72
isc_result_t find_class(struct class **c, const char *n, const char *f, int l)
Definition: cltest.c:44
dhcpctl_status dhcpctl_wait_for_completion(dhcpctl_handle h, dhcpctl_status *s)
Definition: dhcpctl.c:137
dhcpctl_status dhcpctl_connect(dhcpctl_handle *connection, const char *server_name, int port, dhcpctl_handle authinfo)
Definition: dhcpctl.c:95
dhcpctl_status dhcpctl_object_remove(dhcpctl_handle connection, dhcpctl_handle h)
Definition: dhcpctl.c:537
dhcpctl_status dhcpctl_initialize()
Definition: dhcpctl.c:40
dhcpctl_status dhcpctl_set_string_value(dhcpctl_handle h, const char *value, const char *value_name)
Definition: dhcpctl.c:288
dhcpctl_status dhcpctl_get_value(dhcpctl_data_string *result, dhcpctl_handle h, const char *value_name)
Definition: dhcpctl.c:164
#define DHCPCTL_CREATE
Definition: dhcpctl.h:40
dhcpctl_status dhcpctl_open_object(dhcpctl_handle, dhcpctl_handle, int)
Definition: remote.c:171
dhcpctl_status dhcpctl_new_object(dhcpctl_handle *, dhcpctl_handle, const char *)
Definition: remote.c:106
#define DHCPCTL_EXCL
Definition: dhcpctl.h:42
#define dhcpctl_null_handle
Definition: dhcpctl.h:38
control_object_state_t
Definition: dhcpd.h:522
void dhcpv6(struct packet *)
#define ISC_R_NOTIMPLEMENTED
#define ISC_R_SUCCESS
Definition: dhcpd.h:560
Definition: dhcpd.h:405
Definition: dhcpd.h:288