2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
4 * Licensed under the Academic Free License version 2.1
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <sys/ioctl.h>
15 #include <sys/sockio.h>
17 #include <net/if_arp.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.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
);
35 network_device_name_to_udi(char *udi
, size_t size
, ...)
43 while ((element
= va_arg(ap
, char *)) != NULL
) {
44 if (element
[0] != '/')
45 strlcat(udi
, "/", size
);
46 strlcat(udi
, element
, size
);
50 for (i
= 0; udi
[i
] != '\0'; i
++)
55 static void nop(int sig
) {}
58 test_socket_access(struct in6_addr
*addr
, int port
)
61 struct sockaddr_in6 sin6
;
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
);
72 rc
= connect(sd
, (struct sockaddr
*)&sin6
, sizeof (sin6
));
75 signal(SIGALRM
, hndlr
);
78 return ((rc
< 0) ? 1 : 0);
82 is_listening(char *hostname
, int port
)
84 char *uri
= NULL
, addr_string
[INET6_ADDRSTRLEN
];
85 struct in6_addr ipv6addr
[1];
89 hp
= getipnodebyname(hostname
, AF_INET6
,
90 AI_ALL
| AI_ADDRCONFIG
| AI_V4MAPPED
, &errnum
);
92 (void) memcpy(&ipv6addr
, hp
->h_addr_list
[0], hp
->h_length
);
96 return (test_socket_access(ipv6addr
, port
));
100 addr_to_string(char *prefix
, uchar_t
*mac
, int mac_len
, char *buf
, int buf_len
)
106 n
= sprintf(buf
, prefix
);
107 for (i
= 0; ((i
< (mac_len
)) && (n
< buf_len
)); i
++)
108 n
+= sprintf(buf
+ n
, "%2.2X", *mac
++);
114 pseudo_serialno_from_addr(char *name
)
124 memset(&ar
, 0, sizeof (ar
));
126 hp
= getipnodebyname(name
, AF_INET6
, AI_ADDRCONFIG
, &errnum
);
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],
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
);
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
,
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,
163 return (strdup(buf
));
167 add_network_printer(LibHalContext
*ctx
, char *base
, char *hostaddr
,
168 char *device
, char *community
)
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
);
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
)
190 if ((tmp_udi
= libhal_new_device(ctx
, &error
)) == NULL
)
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
);
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
);
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
);
238 HAL_DEBUG(("result: %s (%s): %s, %s, %s, %s, %s", hostaddr
, udi
,
239 NP(manufacturer
), NP(model
), NP(description
), NP(serial
),
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
));
260 number_of_interfaces(int s
)
265 memset(&n
, 0 , sizeof (n
));
266 n
.lifn_family
= AF_INET
;
267 if (ioctl(s
, SIOCGLIFNUM
, (char *)&n
) == 0)
274 broadcast_address(int s
, char *ifname
)
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."));
285 if ((r
.lifr_flags
& (IFF_UP
| IFF_LOOPBACK
)) != IFF_UP
) {
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
));
295 result
= strdup(result
);
302 broadcast_addresses()
304 GList
*result
= NULL
;
309 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
312 count
= number_of_interfaces(s
);
314 memset(&c
, 0, sizeof (c
));
315 c
.lifc_family
= AF_INET
;
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
);