2 * Wi-Fi Protected Setup - External Registrar (SSDP)
3 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
22 #include "wps_upnp_i.h"
26 static void wps_er_ssdp_rx(int sd
, void *eloop_ctx
, void *sock_ctx
)
28 struct wps_er
*er
= eloop_ctx
;
29 struct sockaddr_in addr
; /* client address */
32 char buf
[MULTICAST_MAX_READ
], *pos
, *pos2
, *start
;
33 int wfa
= 0, byebye
= 0;
35 char *location
= NULL
;
36 u8 uuid
[WPS_UUID_LEN
];
38 addr_len
= sizeof(addr
);
39 nread
= recvfrom(sd
, buf
, sizeof(buf
) - 1, 0,
40 (struct sockaddr
*) &addr
, &addr_len
);
45 wpa_printf(MSG_DEBUG
, "WPS ER: Received SSDP from %s",
46 inet_ntoa(addr
.sin_addr
));
47 wpa_hexdump_ascii(MSG_MSGDUMP
, "WPS ER: Received SSDP contents",
50 if (sd
== er
->multicast_sd
) {
51 /* Reply to M-SEARCH */
52 if (os_strncmp(buf
, "HTTP/1.1 200 OK", 15) != 0)
53 return; /* unexpected response header */
55 /* Unsolicited message (likely NOTIFY or M-SEARCH) */
56 if (os_strncmp(buf
, "NOTIFY ", 7) != 0)
57 return; /* only process notifications */
60 os_memset(uuid
, 0, sizeof(uuid
));
62 for (start
= buf
; start
&& *start
; start
= pos
) {
63 pos
= os_strchr(start
, '\n');
69 if (os_strstr(start
, "schemas-wifialliance-org:device:"
72 if (os_strstr(start
, "schemas-wifialliance-org:service:"
75 if (os_strncasecmp(start
, "LOCATION:", 9) == 0) {
80 } else if (os_strncasecmp(start
, "NTS:", 4) == 0) {
81 if (os_strstr(start
, "ssdp:byebye"))
83 } else if (os_strncasecmp(start
, "CACHE-CONTROL:", 14) == 0) {
87 pos2
= os_strstr(start
, "max-age=");
92 } else if (os_strncasecmp(start
, "USN:", 4) == 0) {
94 pos2
= os_strstr(start
, "uuid:");
99 if (uuid_str2bin(pos2
, uuid
) < 0) {
100 wpa_printf(MSG_DEBUG
, "WPS ER: "
101 "Invalid UUID in USN: %s",
110 return; /* Not WPS advertisement/reply */
113 wps_er_ap_remove(er
, &addr
.sin_addr
);
118 return; /* Unknown location */
121 return; /* No max-age reported */
123 wpa_printf(MSG_DEBUG
, "WPS ER: AP discovered: %s "
124 "(packet source: %s max-age: %d)",
125 location
, inet_ntoa(addr
.sin_addr
), max_age
);
127 wps_er_ap_add(er
, uuid
, &addr
.sin_addr
, location
, max_age
);
131 void wps_er_send_ssdp_msearch(struct wps_er
*er
)
134 struct sockaddr_in dest
;
136 msg
= wpabuf_alloc(500);
141 "M-SEARCH * HTTP/1.1\r\n"
142 "HOST: 239.255.255.250:1900\r\n"
143 "MAN: \"ssdp:discover\"\r\n"
145 "ST: urn:schemas-wifialliance-org:device:WFADevice:1"
149 os_memset(&dest
, 0, sizeof(dest
));
150 dest
.sin_family
= AF_INET
;
151 dest
.sin_addr
.s_addr
= inet_addr(UPNP_MULTICAST_ADDRESS
);
152 dest
.sin_port
= htons(UPNP_MULTICAST_PORT
);
154 if (sendto(er
->multicast_sd
, wpabuf_head(msg
), wpabuf_len(msg
), 0,
155 (struct sockaddr
*) &dest
, sizeof(dest
)) < 0)
156 wpa_printf(MSG_DEBUG
, "WPS ER: M-SEARCH sendto failed: "
157 "%d (%s)", errno
, strerror(errno
));
163 int wps_er_ssdp_init(struct wps_er
*er
)
165 if (add_ssdp_network(er
->ifname
))
168 er
->multicast_sd
= ssdp_open_multicast_sock(er
->ip_addr
);
169 if (er
->multicast_sd
< 0)
172 er
->ssdp_sd
= ssdp_listener_open();
176 if (eloop_register_sock(er
->multicast_sd
, EVENT_TYPE_READ
,
177 wps_er_ssdp_rx
, er
, NULL
) ||
178 eloop_register_sock(er
->ssdp_sd
, EVENT_TYPE_READ
,
179 wps_er_ssdp_rx
, er
, NULL
))
182 wps_er_send_ssdp_msearch(er
);
188 void wps_er_ssdp_deinit(struct wps_er
*er
)
190 if (er
->multicast_sd
>= 0) {
191 eloop_unregister_sock(er
->multicast_sd
, EVENT_TYPE_READ
);
192 close(er
->multicast_sd
);
194 if (er
->ssdp_sd
>= 0) {
195 eloop_unregister_sock(er
->ssdp_sd
, EVENT_TYPE_READ
);