dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / cmd-inet / sbin / dhcpinfo / dhcpinfo.c
blobd28286f95927129debbfaf13ebd8ad0438f0b356
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <sys/types.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <string.h>
33 #include <dhcpagent_ipc.h>
34 #include <dhcp_inittab.h>
35 #include <dhcp_symbol.h>
37 #define DHCP_INFO_VENDOR_START_V4 256
38 #define DHCP_INFO_VENDOR_START_V6 65536
40 static void
41 usage(const char *program)
43 (void) fprintf(stderr,
44 "usage: %s [-c] [-i interface] [-n limit] [-v {4|6}] code\n"
45 " %s [-c] [-i interface] [-n limit] [-v {4|6}] identifier\n",
46 program, program);
48 exit(DHCP_EXIT_BADARGS);
51 int
52 main(int argc, char **argv)
54 ssize_t max_lines = -1;
55 size_t gran, n_spaces = 0;
56 dhcp_optnum_t optnum;
57 dhcp_ipc_request_t *request;
58 dhcp_ipc_reply_t *reply;
59 int c, error, i;
60 char *ifname = "";
61 char *value, *valuep;
62 dhcp_symbol_t *entry;
63 DHCP_OPT *opt;
64 size_t opt_len;
65 boolean_t is_canonical = B_FALSE;
66 long version = 4;
67 boolean_t isv6;
68 uint8_t *valptr;
70 while ((c = getopt(argc, argv, "ci:n:v:")) != EOF) {
72 switch (c) {
74 case 'c':
75 is_canonical = B_TRUE;
76 break;
78 case 'i':
79 ifname = optarg;
80 break;
82 case 'n':
83 max_lines = strtoul(optarg, NULL, 0);
84 break;
86 case 'v':
87 version = strtol(optarg, NULL, 0);
88 if (version != 4 && version != 6)
89 usage(argv[0]);
90 break;
92 case '?':
93 usage(argv[0]);
95 default:
96 break;
100 if (argc - optind != 1)
101 usage(argv[0]);
104 * we either have a code or an identifer. if we have a code,
105 * then values over 256 indicate a vendor option. if we have
106 * an identifier, then use inittab_getbyname() to turn the
107 * identifier into a code, then send the request over the wire.
110 isv6 = (version == 6);
112 if (isalpha(*argv[optind])) {
114 entry = inittab_getbyname(ITAB_CAT_SITE | ITAB_CAT_STANDARD |
115 ITAB_CAT_VENDOR | ITAB_CAT_FIELD |
116 (isv6 ? ITAB_CAT_V6 : 0), ITAB_CONS_INFO,
117 argv[optind]);
119 if (entry == NULL) {
120 (void) fprintf(stderr, "%s: unknown identifier `%s'\n",
121 argv[0], argv[optind]);
122 return (DHCP_EXIT_BADARGS);
125 optnum.code = entry->ds_code;
126 optnum.category = entry->ds_category;
128 } else {
129 ulong_t start;
131 optnum.code = strtoul(argv[optind], 0, 0);
132 optnum.category = ITAB_CAT_STANDARD | ITAB_CAT_SITE;
135 * sigh. this is a hack, but it's needed for backward
136 * compatibility with the CA dhcpinfo program.
139 start = isv6 ? DHCP_INFO_VENDOR_START_V6 :
140 DHCP_INFO_VENDOR_START_V4;
141 if (optnum.code > start) {
142 optnum.code -= start;
143 optnum.category = ITAB_CAT_VENDOR;
146 if (isv6)
147 optnum.category |= ITAB_CAT_V6;
149 entry = inittab_getbycode(optnum.category, ITAB_CONS_INFO,
150 optnum.code);
152 if (entry == NULL) {
153 (void) fprintf(stderr, "%s: unknown code `%s'\n",
154 argv[0], argv[optind]);
155 return (DHCP_EXIT_BADARGS);
157 optnum.category = entry->ds_category;
160 optnum.size = entry->ds_max * inittab_type_to_size(entry);
163 * send the request to the agent and reap the reply
166 request = dhcp_ipc_alloc_request(DHCP_GET_TAG | (isv6 ? DHCP_V6 : 0),
167 ifname, &optnum, sizeof (dhcp_optnum_t), DHCP_TYPE_OPTNUM);
169 if (request == NULL)
170 return (DHCP_EXIT_SYSTEM);
172 error = dhcp_ipc_make_request(request, &reply, DHCP_IPC_WAIT_DEFAULT);
173 if (error != 0 || reply->return_code != 0) {
175 if (error == 0)
176 error = reply->return_code;
178 (void) fprintf(stderr, "%s: %s\n", argv[0],
179 dhcp_ipc_strerror(error));
181 if (error == DHCP_IPC_E_TIMEOUT)
182 return (DHCP_EXIT_TIMEOUT);
184 return (DHCP_EXIT_FAILURE);
187 opt = dhcp_ipc_get_data(reply, &opt_len, NULL);
190 * no data means that the client has an ACK but has no information
191 * about the specified option; return success
194 if (opt_len == 0)
195 return (DHCP_EXIT_SUCCESS);
198 * check for protocol error
201 if (isv6) {
202 dhcpv6_option_t d6o;
204 if (opt_len < sizeof (d6o))
205 return (DHCP_EXIT_FAILURE);
206 (void) memcpy(&d6o, opt, sizeof (d6o));
207 if (opt_len != ntohs(d6o.d6o_len) + sizeof (d6o))
208 return (DHCP_EXIT_FAILURE);
209 valptr = (uint8_t *)opt + sizeof (d6o);
210 opt_len -= sizeof (d6o);
211 } else {
212 if (opt_len < 2 || (opt_len - 2 != opt->len))
213 return (DHCP_EXIT_FAILURE);
214 opt_len -= 2;
215 valptr = opt->value;
218 if (is_canonical) {
220 value = malloc(opt_len * (sizeof ("0xNN") + 1));
221 if (value == NULL) {
222 (void) fprintf(stderr, "%s: out of memory\n", argv[0]);
223 return (DHCP_EXIT_FAILURE);
226 for (i = 0, valuep = value; i < opt_len; i++)
227 valuep += sprintf(valuep, "0x%02X ", valptr[i]);
229 valuep[-1] = '\0';
230 gran = 1;
232 } else {
234 value = inittab_decode(entry, valptr, opt_len, B_TRUE);
235 if (value == NULL) {
236 (void) fprintf(stderr, "%s: cannot decode agent's "
237 "reply\n", argv[0]);
238 return (DHCP_EXIT_FAILURE);
241 gran = entry->ds_gran;
245 * now display `gran' items per line, printing at most `max_lines'.
248 for (i = 0; value[i] != '\0'; i++) {
249 if (value[i] == ' ') {
250 if ((++n_spaces % gran) == 0) {
251 value[i] = '\n';
252 if (max_lines != -1 && --max_lines == 0) {
253 value[i] = '\0';
254 break;
260 (void) printf("%s\n", value);
262 return (DHCP_EXIT_SUCCESS);