Annotation of embedaddon/dhcp/dhcpctl/cltest.c, revision 1.1
1.1 ! misho 1: /* cltest.c
! 2:
! 3: Example program that uses the dhcpctl library. */
! 4:
! 5: /*
! 6: * Copyright (c) 2004,2007,2009 by Internet Systems Consortium, Inc. ("ISC")
! 7: * Copyright (c) 2000-2003 by Internet Software Consortium
! 8: *
! 9: * Permission to use, copy, modify, and distribute this software for any
! 10: * purpose with or without fee is hereby granted, provided that the above
! 11: * copyright notice and this permission notice appear in all copies.
! 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 <time.h>
! 32: #include <sys/time.h>
! 33: #include <stdio.h>
! 34: #include <stdlib.h>
! 35: #include <string.h>
! 36: #include <stdarg.h>
! 37: #include <isc-dhcp/result.h>
! 38: #include "dhcpctl.h"
! 39:
! 40: int main (int, char **);
! 41:
! 42: enum modes { up, down, undefined };
! 43:
! 44: static void usage (char *s) {
! 45: fprintf (stderr,
! 46: "Usage: %s [-n <username>] [-p <password>] [-a <algorithm>]"
! 47: "(-u | -d) <if>\n", s);
! 48: exit (1);
! 49: }
! 50:
! 51: int main (argc, argv)
! 52: int argc;
! 53: char **argv;
! 54: {
! 55: isc_result_t status, waitstatus;
! 56: dhcpctl_handle authenticator;
! 57: dhcpctl_handle connection;
! 58: dhcpctl_handle interface_handle;
! 59: dhcpctl_data_string result;
! 60: int i;
! 61: int mode = undefined;
! 62: const char *interface = 0;
! 63: const char *action;
! 64:
! 65: for (i = 1; i < argc; i++) {
! 66: if (!strcmp (argv[i], "-u")) {
! 67: mode = up;
! 68: } else if (!strcmp (argv [i], "-d")) {
! 69: mode = down;
! 70: } else if (argv[i][0] == '-') {
! 71: usage(argv[0]);
! 72: } else {
! 73: interface = argv[i];
! 74: }
! 75: }
! 76:
! 77: if (!interface)
! 78: usage(argv[0]);
! 79: if (mode == undefined)
! 80: usage(argv[0]);
! 81:
! 82: status = dhcpctl_initialize ();
! 83: if (status != ISC_R_SUCCESS) {
! 84: fprintf (stderr, "dhcpctl_initialize: %s\n",
! 85: isc_result_totext (status));
! 86: exit (1);
! 87: }
! 88:
! 89: authenticator = dhcpctl_null_handle;
! 90: connection = dhcpctl_null_handle;
! 91:
! 92: status = dhcpctl_connect (&connection, "127.0.0.1", 7911,
! 93: authenticator);
! 94: if (status != ISC_R_SUCCESS) {
! 95: fprintf (stderr, "dhcpctl_connect: %s\n",
! 96: isc_result_totext (status));
! 97: exit (1);
! 98: }
! 99:
! 100: interface_handle = dhcpctl_null_handle;
! 101: status = dhcpctl_new_object (&interface_handle,
! 102: connection, "interface");
! 103: if (status != ISC_R_SUCCESS) {
! 104: fprintf (stderr, "dhcpctl_new_object: %s\n",
! 105: isc_result_totext (status));
! 106: exit (1);
! 107: }
! 108:
! 109: status = dhcpctl_set_string_value (interface_handle,
! 110: interface, "name");
! 111: if (status != ISC_R_SUCCESS) {
! 112: fprintf (stderr, "dhcpctl_set_value: %s\n",
! 113: isc_result_totext (status));
! 114: exit (1);
! 115: }
! 116:
! 117: if (mode == up) {
! 118: /* "up" the interface */
! 119: printf ("upping interface %s\n", interface);
! 120: action = "create";
! 121: status = dhcpctl_open_object (interface_handle, connection,
! 122: DHCPCTL_CREATE | DHCPCTL_EXCL);
! 123: if (status != ISC_R_SUCCESS) {
! 124: fprintf (stderr, "dhcpctl_open_object: %s\n",
! 125: isc_result_totext (status));
! 126: exit (1);
! 127: }
! 128: } else {
! 129: /* down the interface */
! 130: printf ("downing interface %s\n", interface);
! 131: action = "remove";
! 132: status = dhcpctl_open_object (interface_handle, connection, 0);
! 133: if (status != ISC_R_SUCCESS) {
! 134: fprintf (stderr, "dhcpctl_open_object: %s\n",
! 135: isc_result_totext (status));
! 136: exit (1);
! 137: }
! 138: status = dhcpctl_wait_for_completion (interface_handle,
! 139: &waitstatus);
! 140: if (status != ISC_R_SUCCESS) {
! 141: fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
! 142: isc_result_totext (status));
! 143: exit (1);
! 144: }
! 145: if (waitstatus != ISC_R_SUCCESS) {
! 146: fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
! 147: isc_result_totext (waitstatus));
! 148: exit (1);
! 149: }
! 150: status = dhcpctl_object_remove (connection, interface_handle);
! 151: if (status != ISC_R_SUCCESS) {
! 152: fprintf (stderr, "dhcpctl_open_object: %s\n",
! 153: isc_result_totext (status));
! 154: exit (1);
! 155: }
! 156: }
! 157:
! 158: status = dhcpctl_wait_for_completion (interface_handle, &waitstatus);
! 159: if (status != ISC_R_SUCCESS) {
! 160: fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
! 161: isc_result_totext (status));
! 162: exit (1);
! 163: }
! 164: if (waitstatus != ISC_R_SUCCESS) {
! 165: fprintf (stderr, "interface object %s: %s\n", action,
! 166: isc_result_totext (waitstatus));
! 167: exit (1);
! 168: }
! 169:
! 170: memset (&result, 0, sizeof result);
! 171: status = dhcpctl_get_value (&result, interface_handle, "state");
! 172: if (status != ISC_R_SUCCESS) {
! 173: fprintf (stderr, "dhcpctl_get_value: %s\n",
! 174: isc_result_totext (status));
! 175: exit (1);
! 176: }
! 177:
! 178: exit (0);
! 179: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>