remove @EAP_LDFLAGS@, no longer exists
[mech_eap.orig] / libeap / src / wps / wps_er_ssdp.c
1 /*
2  * Wi-Fi Protected Setup - External Registrar (SSDP)
3  * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
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"
24
25
26 static void wps_er_ssdp_rx(int sd, void *eloop_ctx, void *sock_ctx)
27 {
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];
37
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';
44         if (er->filter_addr.s_addr &&
45             er->filter_addr.s_addr != addr.sin_addr.s_addr)
46                 return;
47
48         wpa_printf(MSG_DEBUG, "WPS ER: Received SSDP from %s",
49                    inet_ntoa(addr.sin_addr));
50         wpa_hexdump_ascii(MSG_MSGDUMP, "WPS ER: Received SSDP contents",
51                           (u8 *) buf, nread);
52
53         if (sd == er->multicast_sd) {
54                 /* Reply to M-SEARCH */
55                 if (os_strncmp(buf, "HTTP/1.1 200 OK", 15) != 0)
56                         return; /* unexpected response header */
57         } else {
58                 /* Unsolicited message (likely NOTIFY or M-SEARCH) */
59                 if (os_strncmp(buf, "NOTIFY ", 7) != 0)
60                         return; /* only process notifications */
61         }
62
63         os_memset(uuid, 0, sizeof(uuid));
64
65         for (start = buf; start && *start; start = pos) {
66                 pos = os_strchr(start, '\n');
67                 if (pos) {
68                         if (pos[-1] == '\r')
69                                 pos[-1] = '\0';
70                         *pos++ = '\0';
71                 }
72                 if (os_strstr(start, "schemas-wifialliance-org:device:"
73                               "WFADevice:1"))
74                         wfa = 1;
75                 if (os_strstr(start, "schemas-wifialliance-org:service:"
76                               "WFAWLANConfig:1"))
77                         wfa = 1;
78                 if (os_strncasecmp(start, "LOCATION:", 9) == 0) {
79                         start += 9;
80                         while (*start == ' ')
81                                 start++;
82                         location = start;
83                 } else if (os_strncasecmp(start, "NTS:", 4) == 0) {
84                         if (os_strstr(start, "ssdp:byebye"))
85                                 byebye = 1;
86                 } else if (os_strncasecmp(start, "CACHE-CONTROL:", 14) == 0) {
87                         start += 9;
88                         while (*start == ' ')
89                                 start++;
90                         pos2 = os_strstr(start, "max-age=");
91                         if (pos2 == NULL)
92                                 continue;
93                         pos2 += 8;
94                         max_age = atoi(pos2);
95                 } else if (os_strncasecmp(start, "USN:", 4) == 0) {
96                         start += 4;
97                         pos2 = os_strstr(start, "uuid:");
98                         if (pos2) {
99                                 pos2 += 5;
100                                 while (*pos2 == ' ')
101                                         pos2++;
102                                 if (uuid_str2bin(pos2, uuid) < 0) {
103                                         wpa_printf(MSG_DEBUG, "WPS ER: "
104                                                    "Invalid UUID in USN: %s",
105                                                    pos2);
106                                         return;
107                                 }
108                         }
109                 }
110         }
111
112         if (!wfa)
113                 return; /* Not WPS advertisement/reply */
114
115         if (byebye) {
116                 wps_er_ap_remove(er, &addr.sin_addr);
117                 return;
118         }
119
120         if (!location)
121                 return; /* Unknown location */
122
123         if (max_age < 1)
124                 return; /* No max-age reported */
125
126         wpa_printf(MSG_DEBUG, "WPS ER: AP discovered: %s "
127                    "(packet source: %s  max-age: %d)",
128                    location, inet_ntoa(addr.sin_addr), max_age);
129
130         wps_er_ap_add(er, uuid, &addr.sin_addr, location, max_age);
131 }
132
133
134 void wps_er_send_ssdp_msearch(struct wps_er *er)
135 {
136         struct wpabuf *msg;
137         struct sockaddr_in dest;
138
139         msg = wpabuf_alloc(500);
140         if (msg == NULL)
141                 return;
142
143         wpabuf_put_str(msg,
144                        "M-SEARCH * HTTP/1.1\r\n"
145                        "HOST: 239.255.255.250:1900\r\n"
146                        "MAN: \"ssdp:discover\"\r\n"
147                        "MX: 3\r\n"
148                        "ST: urn:schemas-wifialliance-org:device:WFADevice:1"
149                        "\r\n"
150                        "\r\n");
151
152         os_memset(&dest, 0, sizeof(dest));
153         dest.sin_family = AF_INET;
154         dest.sin_addr.s_addr = inet_addr(UPNP_MULTICAST_ADDRESS);
155         dest.sin_port = htons(UPNP_MULTICAST_PORT);
156
157         if (sendto(er->multicast_sd, wpabuf_head(msg), wpabuf_len(msg), 0,
158                    (struct sockaddr *) &dest, sizeof(dest)) < 0)
159                 wpa_printf(MSG_DEBUG, "WPS ER: M-SEARCH sendto failed: "
160                            "%d (%s)", errno, strerror(errno));
161
162         wpabuf_free(msg);
163 }
164
165
166 int wps_er_ssdp_init(struct wps_er *er)
167 {
168         if (add_ssdp_network(er->ifname)) {
169                 wpa_printf(MSG_INFO, "WPS ER: Failed to add routing entry for "
170                            "SSDP");
171                 return -1;
172         }
173
174         er->multicast_sd = ssdp_open_multicast_sock(er->ip_addr);
175         if (er->multicast_sd < 0) {
176                 wpa_printf(MSG_INFO, "WPS ER: Failed to open multicast socket "
177                            "for SSDP");
178                 return -1;
179         }
180
181         er->ssdp_sd = ssdp_listener_open();
182         if (er->ssdp_sd < 0) {
183                 wpa_printf(MSG_INFO, "WPS ER: Failed to open SSDP listener "
184                            "socket");
185                 return -1;
186         }
187
188         if (eloop_register_sock(er->multicast_sd, EVENT_TYPE_READ,
189                                 wps_er_ssdp_rx, er, NULL) ||
190             eloop_register_sock(er->ssdp_sd, EVENT_TYPE_READ,
191                                 wps_er_ssdp_rx, er, NULL))
192                 return -1;
193
194         wps_er_send_ssdp_msearch(er);
195
196         return 0;
197 }
198
199
200 void wps_er_ssdp_deinit(struct wps_er *er)
201 {
202         if (er->multicast_sd >= 0) {
203                 eloop_unregister_sock(er->multicast_sd, EVENT_TYPE_READ);
204                 close(er->multicast_sd);
205         }
206         if (er->ssdp_sd >= 0) {
207                 eloop_unregister_sock(er->ssdp_sd, EVENT_TYPE_READ);
208                 close(er->ssdp_sd);
209         }
210 }