nl80211: Fix a typo
[hostap-gosc2009.git] / src / wps / wps_er_ssdp.c
blob83879db22f391c4f1914c2d39e657c0b9aac8a13
1 /*
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
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
18 #include "uuid.h"
19 #include "eloop.h"
20 #include "wps_i.h"
21 #include "wps_upnp.h"
22 #include "wps_upnp_i.h"
23 #include "wps_er.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 */
30 socklen_t addr_len;
31 int nread;
32 char buf[MULTICAST_MAX_READ], *pos, *pos2, *start;
33 int wfa = 0, byebye = 0;
34 int max_age = -1;
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);
41 if (nread <= 0)
42 return;
43 buf[nread] = '\0';
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",
48 (u8 *) buf, nread);
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 */
54 } else {
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');
64 if (pos) {
65 if (pos[-1] == '\r')
66 pos[-1] = '\0';
67 *pos++ = '\0';
69 if (os_strstr(start, "schemas-wifialliance-org:device:"
70 "WFADevice:1"))
71 wfa = 1;
72 if (os_strstr(start, "schemas-wifialliance-org:service:"
73 "WFAWLANConfig:1"))
74 wfa = 1;
75 if (os_strncasecmp(start, "LOCATION:", 9) == 0) {
76 start += 9;
77 while (*start == ' ')
78 start++;
79 location = start;
80 } else if (os_strncasecmp(start, "NTS:", 4) == 0) {
81 if (os_strstr(start, "ssdp:byebye"))
82 byebye = 1;
83 } else if (os_strncasecmp(start, "CACHE-CONTROL:", 14) == 0) {
84 start += 9;
85 while (*start == ' ')
86 start++;
87 pos2 = os_strstr(start, "max-age=");
88 if (pos2 == NULL)
89 continue;
90 pos2 += 8;
91 max_age = atoi(pos2);
92 } else if (os_strncasecmp(start, "USN:", 4) == 0) {
93 start += 4;
94 pos2 = os_strstr(start, "uuid:");
95 if (pos2) {
96 pos2 += 5;
97 while (*pos2 == ' ')
98 pos2++;
99 uuid_str2bin(pos2, uuid);
104 if (!wfa)
105 return; /* Not WPS advertisement/reply */
107 if (byebye) {
108 wps_er_ap_remove(er, &addr.sin_addr);
109 return;
112 if (!location)
113 return; /* Unknown location */
115 if (max_age < 1)
116 return; /* No max-age reported */
118 wpa_printf(MSG_DEBUG, "WPS ER: AP discovered: %s "
119 "(packet source: %s max-age: %d)",
120 location, inet_ntoa(addr.sin_addr), max_age);
122 wps_er_ap_add(er, uuid, &addr.sin_addr, location, max_age);
126 void wps_er_send_ssdp_msearch(struct wps_er *er)
128 struct wpabuf *msg;
129 struct sockaddr_in dest;
131 msg = wpabuf_alloc(500);
132 if (msg == NULL)
133 return;
135 wpabuf_put_str(msg,
136 "M-SEARCH * HTTP/1.1\r\n"
137 "HOST: 239.255.255.250:1900\r\n"
138 "MAN: \"ssdp:discover\"\r\n"
139 "MX: 3\r\n"
140 "ST: urn:schemas-wifialliance-org:device:WFADevice:1"
141 "\r\n"
142 "\r\n");
144 os_memset(&dest, 0, sizeof(dest));
145 dest.sin_family = AF_INET;
146 dest.sin_addr.s_addr = inet_addr(UPNP_MULTICAST_ADDRESS);
147 dest.sin_port = htons(UPNP_MULTICAST_PORT);
149 if (sendto(er->multicast_sd, wpabuf_head(msg), wpabuf_len(msg), 0,
150 (struct sockaddr *) &dest, sizeof(dest)) < 0)
151 wpa_printf(MSG_DEBUG, "WPS ER: M-SEARCH sendto failed: "
152 "%d (%s)", errno, strerror(errno));
154 wpabuf_free(msg);
158 int wps_er_ssdp_init(struct wps_er *er)
160 if (add_ssdp_network(er->ifname))
161 return -1;
163 er->multicast_sd = ssdp_open_multicast_sock(er->ip_addr);
164 if (er->multicast_sd < 0)
165 return -1;
167 er->ssdp_sd = ssdp_listener_open();
168 if (er->ssdp_sd < 0)
169 return -1;
171 if (eloop_register_sock(er->multicast_sd, EVENT_TYPE_READ,
172 wps_er_ssdp_rx, er, NULL) ||
173 eloop_register_sock(er->ssdp_sd, EVENT_TYPE_READ,
174 wps_er_ssdp_rx, er, NULL))
175 return -1;
177 wps_er_send_ssdp_msearch(er);
179 return 0;
183 void wps_er_ssdp_deinit(struct wps_er *er)
185 if (er->multicast_sd >= 0) {
186 eloop_unregister_sock(er->multicast_sd, EVENT_TYPE_READ);
187 close(er->multicast_sd);
189 if (er->ssdp_sd >= 0) {
190 eloop_unregister_sock(er->ssdp_sd, EVENT_TYPE_READ);
191 close(er->ssdp_sd);