dhcpctl/cltest.c

Go to the documentation of this file.
00001 /* cltest.c
00002 
00003    Example program that uses the dhcpctl library. */
00004 
00005 /*
00006  * Copyright (c) 2004,2007,2009 by Internet Systems Consortium, Inc. ("ISC")
00007  * Copyright (c) 2000-2003 by Internet Software Consortium
00008  *
00009  * Permission to use, copy, modify, and distribute this software for any
00010  * purpose with or without fee is hereby granted, provided that the above
00011  * copyright notice and this permission notice appear in all copies.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
00014  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00015  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
00016  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00017  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00018  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
00019  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00020  *
00021  *   Internet Systems Consortium, Inc.
00022  *   950 Charter Street
00023  *   Redwood City, CA 94063
00024  *   <info@isc.org>
00025  *   https://www.isc.org/
00026  *
00027  * This software was contributed to Internet Systems Consortium
00028  * by Brian Murrell.
00029  */
00030 
00031 #include "config.h"
00032 
00033 #include <time.h>
00034 #include <sys/time.h>
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <string.h>
00038 #include <stdarg.h>
00039 #include "omapip/result.h"
00040 #include "dhcpctl.h"
00041 
00042 int main (int, char **);
00043 
00044 enum modes { up, down, undefined };
00045 
00046 static void usage (char *s) {
00047         fprintf (stderr,
00048                  "Usage: %s [-n <username>] [-p <password>] [-a <algorithm>]"
00049                  "(-u | -d) <if>\n", s);
00050         exit (1);
00051 }
00052 
00053 int main (argc, argv)
00054         int argc;
00055         char **argv;
00056 {
00057         isc_result_t status, waitstatus;
00058         dhcpctl_handle authenticator;
00059         dhcpctl_handle connection;
00060         dhcpctl_handle interface_handle;
00061         dhcpctl_data_string result;
00062         int i;
00063         int mode = undefined;
00064         const char *interface = 0;
00065         const char *action;
00066         
00067         for (i = 1; i < argc; i++) {
00068                 if (!strcmp (argv[i], "-u")) {
00069                         mode = up;
00070                 } else if (!strcmp (argv [i], "-d")) {
00071                         mode = down;
00072                 } else if (argv[i][0] == '-') {
00073                         usage(argv[0]);
00074                 } else {
00075                         interface = argv[i];
00076                 }
00077         }
00078 
00079         if (!interface)
00080                 usage(argv[0]);
00081         if (mode == undefined)
00082                 usage(argv[0]);
00083 
00084         status = dhcpctl_initialize ();
00085         if (status != ISC_R_SUCCESS) {
00086                 fprintf (stderr, "dhcpctl_initialize: %s\n",
00087                          isc_result_totext (status));
00088                 exit (1);
00089         }
00090 
00091         authenticator = dhcpctl_null_handle;
00092         connection = dhcpctl_null_handle;
00093 
00094         status = dhcpctl_connect (&connection, "127.0.0.1", 7911,
00095                                   authenticator);
00096         if (status != ISC_R_SUCCESS) {
00097                 fprintf (stderr, "dhcpctl_connect: %s\n",
00098                          isc_result_totext (status));
00099                 exit (1);
00100         }
00101 
00102         interface_handle = dhcpctl_null_handle;
00103         status = dhcpctl_new_object (&interface_handle,
00104                                      connection, "interface");
00105         if (status != ISC_R_SUCCESS) {
00106                 fprintf (stderr, "dhcpctl_new_object: %s\n",
00107                          isc_result_totext (status));
00108                 exit (1);
00109         }
00110 
00111         status = dhcpctl_set_string_value (interface_handle,
00112                                            interface, "name");
00113         if (status != ISC_R_SUCCESS) {
00114                 fprintf (stderr, "dhcpctl_set_value: %s\n",
00115                          isc_result_totext (status));
00116                 exit (1);
00117         }
00118 
00119         if (mode == up) {
00120                 /* "up" the interface */
00121                 printf ("upping interface %s\n", interface);
00122                 action = "create";
00123                 status = dhcpctl_open_object (interface_handle, connection,
00124                                               DHCPCTL_CREATE | DHCPCTL_EXCL);
00125                 if (status != ISC_R_SUCCESS) {
00126                         fprintf (stderr, "dhcpctl_open_object: %s\n",
00127                                  isc_result_totext (status));
00128                         exit (1);
00129                 }
00130         } else {
00131                 /* down the interface */
00132                 printf ("downing interface %s\n", interface);
00133                 action = "remove";
00134                 status = dhcpctl_open_object (interface_handle, connection, 0);
00135                 if (status != ISC_R_SUCCESS) {
00136                         fprintf (stderr, "dhcpctl_open_object: %s\n",
00137                                  isc_result_totext (status));
00138                         exit (1);
00139                 }
00140                 status = dhcpctl_wait_for_completion (interface_handle,
00141                                                       &waitstatus);
00142                 if (status != ISC_R_SUCCESS) {
00143                         fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
00144                                  isc_result_totext (status));
00145                         exit (1);
00146                 }
00147                 if (waitstatus != ISC_R_SUCCESS) {
00148                         fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
00149                                  isc_result_totext (waitstatus));
00150                         exit (1);
00151                 }
00152                 status = dhcpctl_object_remove (connection, interface_handle);
00153                 if (status != ISC_R_SUCCESS) {
00154                         fprintf (stderr, "dhcpctl_open_object: %s\n",
00155                                  isc_result_totext (status));
00156                         exit (1);
00157                 }
00158         }
00159 
00160         status = dhcpctl_wait_for_completion (interface_handle, &waitstatus);
00161         if (status != ISC_R_SUCCESS) {
00162                 fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
00163                          isc_result_totext (status));
00164                 exit (1);
00165         }
00166         if (waitstatus != ISC_R_SUCCESS) {
00167                 fprintf (stderr, "interface object %s: %s\n", action,
00168                          isc_result_totext (waitstatus));
00169                 exit (1);
00170         }
00171 
00172         memset (&result, 0, sizeof result);
00173         status = dhcpctl_get_value (&result, interface_handle, "state");
00174         if (status != ISC_R_SUCCESS) {
00175                 fprintf (stderr, "dhcpctl_get_value: %s\n",
00176                          isc_result_totext (status));
00177                 exit (1);
00178         }
00179 
00180         exit (0);
00181 }

Generated on 5 Apr 2014 for ISC DHCP by  doxygen 1.6.1