Print this page
8485 Remove set but unused variables in usr/src/cmd
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/hal/addons/network-devices/common.c
+++ new/usr/src/cmd/hal/addons/network-devices/common.c
1 1 /*
2 + * Copyright 2017 Gary Mills
2 3 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 4 *
4 5 * Licensed under the Academic Free License version 2.1
5 6 */
6 7
7 8 #include <stdio.h>
8 9 #include <stdlib.h>
9 10 #include <unistd.h>
10 11 #include <signal.h>
11 12 #include <string.h>
12 13 #include <sys/types.h>
13 14 #include <sys/socket.h>
14 15 #include <sys/ioctl.h>
15 16 #include <sys/sockio.h>
16 17 #include <net/if.h>
17 18 #include <net/if_arp.h>
18 19 #include <netinet/in.h>
19 20 #include <arpa/inet.h>
20 21 #include <netdb.h>
21 22
22 23 #include <libhal.h>
23 24 #include <logger.h>
24 25
25 26 #include <glib.h>
26 27
27 28 #include "network-discovery.h"
28 29 #define NP(x) (x?x:"NULL")
29 30
30 31 extern int snmp_printer_info(char *hostname, char *community,
31 32 char **manufacturer, char **model, char **description,
32 33 char **serial_no, char ***command_set, char **uri);
33 34
34 35 void
35 36 network_device_name_to_udi(char *udi, size_t size, ...)
36 37 {
37 38 va_list ap;
38 39 char *element;
39 40 int i;
40 41
41 42 udi[0] = '\0';
42 43 va_start(ap, size);
43 44 while ((element = va_arg(ap, char *)) != NULL) {
44 45 if (element[0] != '/')
45 46 strlcat(udi, "/", size);
46 47 strlcat(udi, element, size);
47 48 }
48 49 va_end(ap);
49 50
50 51 for (i = 0; udi[i] != NULL; i++)
51 52 if (udi[i] == '.')
52 53 udi[i] = '_';
53 54 }
54 55
55 56 static void nop(int sig) {}
56 57
57 58 static int
58 59 test_socket_access(struct in6_addr *addr, int port)
59 60 {
60 61 int sd, rc;
61 62 struct sockaddr_in6 sin6;
62 63 void (*hndlr)(int);
63 64
64 65 memset(&sin6, 0, sizeof (sin6));
65 66 sin6.sin6_family = AF_INET6;
66 67 memcpy(&sin6.sin6_addr, addr, sizeof (*addr));
67 68 sin6.sin6_port = htons(port);
68 69
69 70 sd = socket(AF_INET6, SOCK_STREAM, 0);
70 71 hndlr = signal(SIGALRM, nop);
71 72 alarm(1);
72 73 rc = connect(sd, (struct sockaddr *)&sin6, sizeof (sin6));
73 74 alarm(0);
74 75 if (hndlr != NULL)
75 76 signal(SIGALRM, hndlr);
76 77 close(sd);
77 78
78 79 return ((rc < 0) ? 1 : 0);
79 80 }
80 81
81 82 int
82 83 is_listening(char *hostname, int port)
83 84 {
84 85 char *uri = NULL, addr_string[INET6_ADDRSTRLEN];
85 86 struct in6_addr ipv6addr[1];
86 87 int errnum;
87 88 struct hostent *hp;
88 89
89 90 hp = getipnodebyname(hostname, AF_INET6,
90 91 AI_ALL | AI_ADDRCONFIG | AI_V4MAPPED, &errnum);
91 92 if (hp != NULL) {
92 93 (void) memcpy(&ipv6addr, hp->h_addr_list[0], hp->h_length);
93 94 } else
94 95 return (-1);
95 96
96 97 return (test_socket_access(ipv6addr, port));
97 98 }
98 99
99 100 static char *
100 101 addr_to_string(char *prefix, uchar_t *mac, int mac_len, char *buf, int buf_len)
101 102 {
102 103 int i, n = 0;
103 104
104 105 buf[0] = '\0';
105 106 if (prefix != NULL)
↓ open down ↓ |
94 lines elided |
↑ open up ↑ |
106 107 n = sprintf(buf, prefix);
107 108 for (i = 0; ((i < (mac_len)) && (n < buf_len)); i++)
108 109 n += sprintf(buf + n, "%2.2X", *mac++);
109 110
110 111 return (buf);
111 112 }
112 113
113 114 static char *
114 115 pseudo_serialno_from_addr(char *name)
115 116 {
116 - int sd, rc, errnum;
117 + int sd, errnum;
117 118 char buf[128];
118 119 struct hostent *hp;
119 120 struct xarpreq ar;
120 121
121 122 if (name == NULL)
122 123 return (NULL);
123 124
124 125 memset(&ar, 0, sizeof (ar));
125 126
126 127 hp = getipnodebyname(name, AF_INET6, AI_ADDRCONFIG, &errnum);
127 128 if (hp != NULL) {
128 129 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ar.xarp_pa;
129 130
130 131 sin6->sin6_family = AF_INET6;
131 132 (void) memcpy(&sin6->sin6_addr, hp->h_addr_list[0],
132 133 hp->h_length);
↓ open down ↓ |
6 lines elided |
↑ open up ↑ |
133 134 } else {
134 135 struct sockaddr_in *sin = (struct sockaddr_in *)&ar.xarp_pa;
135 136
136 137 sin->sin_family = AF_INET;
137 138 sin->sin_addr.s_addr = inet_addr(name);
138 139 }
139 140
140 141 sd = socket(AF_INET, SOCK_DGRAM, 0);
141 142
142 143 ar.xarp_ha.sdl_family = AF_LINK;
143 - rc = ioctl(sd, SIOCGXARP, (caddr_t)&ar);
144 + (void) ioctl(sd, SIOCGXARP, (caddr_t)&ar);
144 145
145 146 close(sd);
146 147
147 148 if (ar.xarp_flags & ATF_COM) { /* use the MAC address */
148 149 uchar_t *ea = (uchar_t *)LLADDR(&ar.xarp_ha);
149 150
150 151 addr_to_string("LLADDR-", ea, ar.xarp_ha.sdl_alen,
151 152 buf, sizeof (buf));
152 153
153 154 } else if (hp != NULL) { /* use the IPv6 address */
154 155 addr_to_string("IPV6ADDR-", (uchar_t *)&hp->h_addr_list[0],
155 156 hp->h_length, buf, sizeof (buf));
156 157 } else { /* use the IPv4 address */
157 158 struct sockaddr_in *sin = (struct sockaddr_in *)&ar.xarp_pa;
158 159
159 160 addr_to_string("IPV4ADDR-", (uchar_t *)&sin->sin_addr.s_addr, 4,
160 161 buf, sizeof (buf));
161 162 }
162 163
163 164 return (strdup(buf));
164 165 }
165 166
166 167 int
167 168 add_network_printer(LibHalContext *ctx, char *base, char *hostaddr,
168 169 char *device, char *community)
169 170 {
170 171 DBusError error;
171 172 int rc = -1;
172 173 char udi[128];
173 174 char *tmp_udi = NULL;
174 175 static char *parent = NULL;
175 176 char *manufacturer = NULL, *model = NULL, *description = NULL,
176 177 *uri = NULL, *sn, *serial;
177 178
178 179 sn = serial = pseudo_serialno_from_addr(hostaddr);
179 180
180 181 if (parent == NULL)
181 182 parent = getenv("UDI");
182 183
183 184 dbus_error_init(&error);
184 185
185 186 network_device_name_to_udi(udi, sizeof (udi), base, serial, NULL);
186 187
187 188 if (libhal_device_exists(ctx, udi, &error) == TRUE)
188 189 goto out;
189 190
190 191 if ((tmp_udi = libhal_new_device(ctx, &error)) == NULL)
191 192 goto out;
192 193
193 194 snmp_printer_info(hostaddr, community, &manufacturer, &model,
194 195 &description, &serial, NULL, &uri);
195 196
196 197 libhal_device_set_property_string(ctx, tmp_udi,
197 198 "info.parent", parent, &error);
198 199
199 200 libhal_device_set_property_string(ctx, tmp_udi,
200 201 "info.category", "printer", &error);
201 202
202 203 libhal_device_property_strlist_append(ctx, tmp_udi,
203 204 "info.capabilities", "printer", &error);
204 205 libhal_device_property_strlist_append(ctx, tmp_udi,
205 206 "info.capabilities", "network_device", &error);
206 207
207 208 libhal_device_set_property_string(ctx, tmp_udi,
208 209 "network_device.address", hostaddr, &error);
209 210
210 211 if ((community != NULL) && (strcasecmp(community, "public") != 0))
211 212 libhal_device_set_property_string(ctx, tmp_udi,
212 213 "network_device.snmp_community", community, &error);
213 214
214 215 if ((uri != NULL) || (device != NULL))
215 216 libhal_device_set_property_string(ctx, tmp_udi,
216 217 "printer.device", (uri ? uri : device), &error);
217 218
218 219 if (serial != NULL)
219 220 libhal_device_set_property_string(ctx, tmp_udi,
220 221 "printer.serial", serial, &error);
221 222
222 223 if (manufacturer != NULL)
223 224 libhal_device_set_property_string(ctx, tmp_udi,
224 225 "printer.vendor", manufacturer, &error);
225 226
226 227 if (model != NULL)
227 228 libhal_device_set_property_string(ctx, tmp_udi,
228 229 "printer.product", model, &error);
229 230
230 231 if (description != NULL)
231 232 libhal_device_set_property_string(ctx, tmp_udi,
232 233 "printer.description", description, &error);
233 234
234 235 /* commit the changes to the new UDI */
235 236 rc = libhal_device_commit_to_gdl(ctx, tmp_udi, udi, &error);
236 237
237 238 out:
238 239 HAL_DEBUG(("result: %s (%s): %s, %s, %s, %s, %s", hostaddr, udi,
239 240 NP(manufacturer), NP(model), NP(description), NP(serial),
240 241 NP(uri)));
241 242
242 243 if (tmp_udi != NULL)
243 244 free(tmp_udi);
244 245 if (manufacturer != NULL)
245 246 free(manufacturer);
246 247 if (model != NULL)
247 248 free(model);
248 249 if (description != NULL)
249 250 free(description);
250 251 if (uri != NULL)
251 252 free(uri);
252 253 if (sn != NULL)
253 254 free(sn);
254 255
255 256 if (dbus_error_is_set(&error)) {
256 257 HAL_WARNING(("%s: %s", error.name, error.message));
257 258 dbus_error_free(&error);
258 259 }
259 260
260 261 HAL_DEBUG(("add: %s (%s)", hostaddr, udi));
261 262
262 263 return (rc);
263 264 }
264 265
265 266 static int
266 267 number_of_interfaces(int s)
267 268 {
268 269 int rc = -1;
269 270 struct lifnum n;
270 271
271 272 memset(&n, 0 , sizeof (n));
272 273 n.lifn_family = AF_INET;
273 274 if (ioctl(s, SIOCGLIFNUM, (char *)&n) == 0)
274 275 rc = n.lifn_count;
275 276
276 277 return (rc);
277 278 }
278 279
279 280 static char *
280 281 broadcast_address(int s, char *ifname)
281 282 {
282 283 char *result = NULL;
283 284 struct lifreq r;
284 285
285 286 memset(&r, 0, sizeof (r));
286 287 strlcpy(r.lifr_name, ifname, sizeof (r.lifr_name));
287 288 if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&r) < 0) {
288 289 HAL_DEBUG(("broadcast_address: ioctl(SIOCGLIFFLAGS) failed."));
289 290 return (NULL);
290 291 }
291 292 if ((r.lifr_flags & (IFF_UP | IFF_LOOPBACK)) != IFF_UP) {
292 293 return (NULL);
293 294 }
294 295 if (ioctl(s, SIOCGLIFBRDADDR, (char *)&r) >= 0) {
295 296 char buf[INET_ADDRSTRLEN];
296 297 struct sockaddr_in *s =
297 298 (struct sockaddr_in *)&r.lifr_broadaddr;
298 299 result = (char *)inet_ntop(AF_INET,
299 300 &s->sin_addr, buf, sizeof (buf));
300 301 if (result != NULL)
301 302 result = strdup(result);
302 303 }
303 304
304 305 return (result);
305 306 }
306 307
307 308 GList *
308 309 broadcast_addresses()
309 310 {
310 311 GList *result = NULL;
311 312 int s;
312 313 struct lifconf c;
313 314 int count;
314 315
315 316 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
316 317 return (NULL);
317 318
318 319 count = number_of_interfaces(s);
319 320
320 321 memset(&c, 0, sizeof (c));
321 322 c.lifc_family = AF_INET;
322 323 c.lifc_flags = 0;
323 324 c.lifc_buf = calloc(count, sizeof (struct lifreq));
324 325 c.lifc_len = (count * sizeof (struct lifreq));
325 326
326 327 if (ioctl(s, SIOCGLIFCONF, (char *)&c) == 0) {
327 328 struct lifreq *r = c.lifc_req;
328 329
329 330 for (count = c.lifc_len / sizeof (struct lifreq);
330 331 count > 0; count--, r++) {
331 332 char *address = broadcast_address(s, r->lifr_name);
332 333
333 334 if (address != NULL) /* add it to the list */
334 335 result = g_list_append(result, address);
335 336 }
336 337 }
337 338 free(c.lifc_buf);
338 339 close(s);
339 340
340 341 return (result);
341 342 }
↓ open down ↓ |
188 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX