dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / hal / addons / network-devices / common.c
blob1ee31e8e0bb8bb4ae6bc17240475e6edf6c632fd
1 /*
2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
4 * Licensed under the Academic Free License version 2.1
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <signal.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <sys/ioctl.h>
15 #include <sys/sockio.h>
16 #include <net/if.h>
17 #include <net/if_arp.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <netdb.h>
22 #include <libhal.h>
23 #include <logger.h>
25 #include <glib.h>
27 #include "network-discovery.h"
28 #define NP(x) (x?x:"NULL")
30 extern int snmp_printer_info(char *hostname, char *community,
31 char **manufacturer, char **model, char **description,
32 char **serial_no, char ***command_set, char **uri);
34 void
35 network_device_name_to_udi(char *udi, size_t size, ...)
37 va_list ap;
38 char *element;
39 int i;
41 udi[0] = '\0';
42 va_start(ap, size);
43 while ((element = va_arg(ap, char *)) != NULL) {
44 if (element[0] != '/')
45 strlcat(udi, "/", size);
46 strlcat(udi, element, size);
48 va_end(ap);
50 for (i = 0; udi[i] != '\0'; i++)
51 if (udi[i] == '.')
52 udi[i] = '_';
55 static void nop(int sig) {}
57 static int
58 test_socket_access(struct in6_addr *addr, int port)
60 int sd, rc;
61 struct sockaddr_in6 sin6;
62 void (*hndlr)(int);
64 memset(&sin6, 0, sizeof (sin6));
65 sin6.sin6_family = AF_INET6;
66 memcpy(&sin6.sin6_addr, addr, sizeof (*addr));
67 sin6.sin6_port = htons(port);
69 sd = socket(AF_INET6, SOCK_STREAM, 0);
70 hndlr = signal(SIGALRM, nop);
71 alarm(1);
72 rc = connect(sd, (struct sockaddr *)&sin6, sizeof (sin6));
73 alarm(0);
74 if (hndlr != NULL)
75 signal(SIGALRM, hndlr);
76 close(sd);
78 return ((rc < 0) ? 1 : 0);
81 int
82 is_listening(char *hostname, int port)
84 char *uri = NULL, addr_string[INET6_ADDRSTRLEN];
85 struct in6_addr ipv6addr[1];
86 int errnum;
87 struct hostent *hp;
89 hp = getipnodebyname(hostname, AF_INET6,
90 AI_ALL | AI_ADDRCONFIG | AI_V4MAPPED, &errnum);
91 if (hp != NULL) {
92 (void) memcpy(&ipv6addr, hp->h_addr_list[0], hp->h_length);
93 } else
94 return (-1);
96 return (test_socket_access(ipv6addr, port));
99 static char *
100 addr_to_string(char *prefix, uchar_t *mac, int mac_len, char *buf, int buf_len)
102 int i, n = 0;
104 buf[0] = '\0';
105 if (prefix != NULL)
106 n = sprintf(buf, prefix);
107 for (i = 0; ((i < (mac_len)) && (n < buf_len)); i++)
108 n += sprintf(buf + n, "%2.2X", *mac++);
110 return (buf);
113 static char *
114 pseudo_serialno_from_addr(char *name)
116 int sd, rc, errnum;
117 char buf[128];
118 struct hostent *hp;
119 struct xarpreq ar;
121 if (name == NULL)
122 return (NULL);
124 memset(&ar, 0, sizeof (ar));
126 hp = getipnodebyname(name, AF_INET6, AI_ADDRCONFIG, &errnum);
127 if (hp != NULL) {
128 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ar.xarp_pa;
130 sin6->sin6_family = AF_INET6;
131 (void) memcpy(&sin6->sin6_addr, hp->h_addr_list[0],
132 hp->h_length);
133 } else {
134 struct sockaddr_in *sin = (struct sockaddr_in *)&ar.xarp_pa;
136 sin->sin_family = AF_INET;
137 sin->sin_addr.s_addr = inet_addr(name);
140 sd = socket(AF_INET, SOCK_DGRAM, 0);
142 ar.xarp_ha.sdl_family = AF_LINK;
143 rc = ioctl(sd, SIOCGXARP, (caddr_t)&ar);
145 close(sd);
147 if (ar.xarp_flags & ATF_COM) { /* use the MAC address */
148 uchar_t *ea = (uchar_t *)LLADDR(&ar.xarp_ha);
150 addr_to_string("LLADDR-", ea, ar.xarp_ha.sdl_alen,
151 buf, sizeof (buf));
153 } else if (hp != NULL) { /* use the IPv6 address */
154 addr_to_string("IPV6ADDR-", (uchar_t *)&hp->h_addr_list[0],
155 hp->h_length, buf, sizeof (buf));
156 } else { /* use the IPv4 address */
157 struct sockaddr_in *sin = (struct sockaddr_in *)&ar.xarp_pa;
159 addr_to_string("IPV4ADDR-", (uchar_t *)&sin->sin_addr.s_addr, 4,
160 buf, sizeof (buf));
163 return (strdup(buf));
167 add_network_printer(LibHalContext *ctx, char *base, char *hostaddr,
168 char *device, char *community)
170 DBusError error;
171 int rc = -1;
172 char udi[128];
173 char *tmp_udi = NULL;
174 static char *parent = NULL;
175 char *manufacturer = NULL, *model = NULL, *description = NULL,
176 *uri = NULL, *sn, *serial;
178 sn = serial = pseudo_serialno_from_addr(hostaddr);
180 if (parent == NULL)
181 parent = getenv("UDI");
183 dbus_error_init(&error);
185 network_device_name_to_udi(udi, sizeof (udi), base, serial, NULL);
187 if (libhal_device_exists(ctx, udi, &error) == TRUE)
188 goto out;
190 if ((tmp_udi = libhal_new_device(ctx, &error)) == NULL)
191 goto out;
193 snmp_printer_info(hostaddr, community, &manufacturer, &model,
194 &description, &serial, NULL, &uri);
196 libhal_device_set_property_string(ctx, tmp_udi,
197 "info.parent", parent, &error);
199 libhal_device_set_property_string(ctx, tmp_udi,
200 "info.category", "printer", &error);
202 libhal_device_property_strlist_append(ctx, tmp_udi,
203 "info.capabilities", "printer", &error);
204 libhal_device_property_strlist_append(ctx, tmp_udi,
205 "info.capabilities", "network_device", &error);
207 libhal_device_set_property_string(ctx, tmp_udi,
208 "network_device.address", hostaddr, &error);
210 if ((community != NULL) && (strcasecmp(community, "public") != 0))
211 libhal_device_set_property_string(ctx, tmp_udi,
212 "network_device.snmp_community", community, &error);
214 if ((uri != NULL) || (device != NULL))
215 libhal_device_set_property_string(ctx, tmp_udi,
216 "printer.device", (uri ? uri : device), &error);
218 if (serial != NULL)
219 libhal_device_set_property_string(ctx, tmp_udi,
220 "printer.serial", serial, &error);
222 if (manufacturer != NULL)
223 libhal_device_set_property_string(ctx, tmp_udi,
224 "printer.vendor", manufacturer, &error);
226 if (model != NULL)
227 libhal_device_set_property_string(ctx, tmp_udi,
228 "printer.product", model, &error);
230 if (description != NULL)
231 libhal_device_set_property_string(ctx, tmp_udi,
232 "printer.description", description, &error);
234 /* commit the changes to the new UDI */
235 rc = libhal_device_commit_to_gdl(ctx, tmp_udi, udi, &error);
237 out:
238 HAL_DEBUG(("result: %s (%s): %s, %s, %s, %s, %s", hostaddr, udi,
239 NP(manufacturer), NP(model), NP(description), NP(serial),
240 NP(uri)));
242 free(tmp_udi);
243 free(manufacturer);
244 free(model);
245 free(description);
246 free(uri);
247 free(sn);
249 if (dbus_error_is_set(&error)) {
250 HAL_WARNING(("%s: %s", error.name, error.message));
251 dbus_error_free(&error);
254 HAL_DEBUG(("add: %s (%s)", hostaddr, udi));
256 return (rc);
259 static int
260 number_of_interfaces(int s)
262 int rc = -1;
263 struct lifnum n;
265 memset(&n, 0 , sizeof (n));
266 n.lifn_family = AF_INET;
267 if (ioctl(s, SIOCGLIFNUM, (char *)&n) == 0)
268 rc = n.lifn_count;
270 return (rc);
273 static char *
274 broadcast_address(int s, char *ifname)
276 char *result = NULL;
277 struct lifreq r;
279 memset(&r, 0, sizeof (r));
280 strlcpy(r.lifr_name, ifname, sizeof (r.lifr_name));
281 if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&r) < 0) {
282 HAL_DEBUG(("broadcast_address: ioctl(SIOCGLIFFLAGS) failed."));
283 return (NULL);
285 if ((r.lifr_flags & (IFF_UP | IFF_LOOPBACK)) != IFF_UP) {
286 return (NULL);
288 if (ioctl(s, SIOCGLIFBRDADDR, (char *)&r) >= 0) {
289 char buf[INET_ADDRSTRLEN];
290 struct sockaddr_in *s =
291 (struct sockaddr_in *)&r.lifr_broadaddr;
292 result = (char *)inet_ntop(AF_INET,
293 &s->sin_addr, buf, sizeof (buf));
294 if (result != NULL)
295 result = strdup(result);
298 return (result);
301 GList *
302 broadcast_addresses()
304 GList *result = NULL;
305 int s;
306 struct lifconf c;
307 int count;
309 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
310 return (NULL);
312 count = number_of_interfaces(s);
314 memset(&c, 0, sizeof (c));
315 c.lifc_family = AF_INET;
316 c.lifc_flags = 0;
317 c.lifc_buf = calloc(count, sizeof (struct lifreq));
318 c.lifc_len = (count * sizeof (struct lifreq));
320 if (ioctl(s, SIOCGLIFCONF, (char *)&c) == 0) {
321 struct lifreq *r = c.lifc_req;
323 for (count = c.lifc_len / sizeof (struct lifreq);
324 count > 0; count--, r++) {
325 char *address = broadcast_address(s, r->lifr_name);
327 if (address != NULL) /* add it to the list */
328 result = g_list_append(result, address);
331 free(c.lifc_buf);
332 close(s);
334 return (result);