2 * UPnP WPS Device - Event processing
3 * Copyright (c) 2000-2003 Intel Corporation
4 * Copyright (c) 2006-2007 Sony Corporation
5 * Copyright (c) 2008-2009 Atheros Communications
6 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
8 * See wps_upnp.c for more details on licensing and code history.
20 #include "wps_upnp_i.h"
23 * Event message generation (to subscribers)
25 * We make a separate copy for each message for each subscriber. This memory
26 * wasted could be limited (adding code complexity) by sharing copies, keeping
27 * a usage count and freeing when zero.
29 * Sending a message requires using a HTTP over TCP NOTIFY
30 * (like a PUT) which requires a number of states..
33 #define MAX_EVENTS_QUEUED 20 /* How far behind queued events */
34 #define EVENT_TIMEOUT_SEC 30 /* Drop sending event after timeout */
36 /* How long to wait before sending event */
37 #define EVENT_DELAY_SECONDS 0
38 #define EVENT_DELAY_MSEC 0
41 * Event information that we send to each subscriber is remembered in this
42 * struct. The event cannot be sent by simple UDP; it has to be sent by a HTTP
43 * over TCP transaction which requires various states.. It may also need to be
44 * retried at a different address (if more than one is available).
46 * TODO: As an optimization we could share data between subscribers.
49 struct wps_event_ *next;
50 struct wps_event_ *prev; /* double linked list */
51 struct subscription *s; /* parent */
52 unsigned subscriber_sequence; /* which event for this subscription*/
53 int retry; /* which retry */
54 struct subscr_addr *addr; /* address to connect to */
55 struct wpabuf *data; /* event data to send */
56 /* The following apply while we are sending an event message. */
57 int sd; /* -1 or socket descriptor for open connection */
58 int sd_registered; /* nonzero if we must cancel registration */
59 struct httpread *hread; /* NULL or open connection for event msg */
63 static void event_timeout_handler(void *eloop_data, void *user_ctx);
65 /* event_clean -- clean sockets etc. of event
66 * Leaves data, retry count etc. alone.
68 static void event_clean(struct wps_event_ *e)
70 if (e->s->current_event == e) {
71 eloop_cancel_timeout(event_timeout_handler, NULL, e);
72 e->s->current_event = NULL;
74 if (e->sd_registered) {
75 eloop_unregister_sock(e->sd, EVENT_TYPE_WRITE);
83 httpread_destroy(e->hread);
88 /* event_delete -- delete single unqueued event
89 * (be sure to dequeue first if need be)
91 void event_delete(struct wps_event_ *e)
99 /* event_dequeue -- get next event from the queue
100 * Returns NULL if empty.
102 static struct wps_event_ *event_dequeue(struct subscription *s)
104 struct wps_event_ **event_head = &s->event_queue;
105 struct wps_event_ *e = *event_head;
108 e->next->prev = e->prev;
109 e->prev->next = e->next;
110 if (*event_head == e) {
115 *event_head = e->next;
119 e->next = e->prev = NULL;
120 /* but parent "s" is still valid */
125 /* event_enqueue_at_end -- add event to end of queue */
126 static void event_enqueue_at_end(struct subscription *s, struct wps_event_ *e)
128 struct wps_event_ **event_head = &s->event_queue;
129 if (*event_head == NULL) {
130 *event_head = e->next = e->prev = e;
132 e->next = *event_head;
133 e->prev = e->next->prev;
141 /* event_enqueue_at_begin -- add event to begin of queue
142 * (appropriate for retrying event only)
144 static void event_enqueue_at_begin(struct subscription *s,
145 struct wps_event_ *e)
147 struct wps_event_ **event_head = &s->event_queue;
148 if (*event_head == NULL) {
149 *event_head = e->next = e->prev = e;
151 e->prev = *event_head;
152 e->next = e->prev->next;
161 /* event_delete_all -- delete entire event queue and current event */
162 void event_delete_all(struct subscription *s)
164 struct wps_event_ *e;
165 while ((e = event_dequeue(s)) != NULL)
167 if (s->current_event) {
168 event_delete(s->current_event);
169 /* will set: s->current_event = NULL; */
175 * event_retry - Called when we had a failure delivering event msg
177 * @do_next_address: skip address e.g. on connect fail
179 static void event_retry(struct wps_event_ *e, int do_next_address)
181 struct subscription *s = e->s;
182 struct upnp_wps_device_sm *sm = s->sm;
185 /* will set: s->current_event = NULL; */
189 if (e->retry >= s->n_addr) {
190 wpa_printf(MSG_DEBUG, "WPS UPnP: Giving up on sending event "
191 "for %s", e->addr->domain_and_port);
194 event_enqueue_at_begin(s, e);
195 event_send_all_later(sm);
199 /* called if the overall event-sending process takes too long */
200 static void event_timeout_handler(void *eloop_data, void *user_ctx)
202 struct wps_event_ *e = user_ctx;
203 struct subscription *s = e->s;
205 assert(e == s->current_event);
207 wpa_printf(MSG_DEBUG, "WPS UPnP: Event send timeout");
212 /* event_got_response_handler -- called back when http response is received. */
213 static void event_got_response_handler(struct httpread *handle, void *cookie,
214 enum httpread_event en)
216 struct wps_event_ *e = cookie;
217 struct subscription *s = e->s;
218 struct upnp_wps_device_sm *sm = s->sm;
219 struct httpread *hread = e->hread;
222 assert(e == s->current_event);
223 eloop_cancel_timeout(event_timeout_handler, NULL, e);
225 if (en == HTTPREAD_EVENT_FILE_READY) {
226 if (httpread_hdr_type_get(hread) == HTTPREAD_HDR_TYPE_REPLY) {
227 reply_code = httpread_reply_code_get(hread);
228 if (reply_code == HTTP_OK) {
229 wpa_printf(MSG_DEBUG,
230 "WPS UPnP: Got event reply OK from "
231 "%s", e->addr->domain_and_port);
235 wpa_printf(MSG_DEBUG, "WPS UPnP: Got event "
236 "error reply code %d from %s",
238 e->addr->domain_and_port);
242 wpa_printf(MSG_DEBUG, "WPS UPnP: Got bogus event "
243 "response %d from %s", en,
244 e->addr->domain_and_port);
247 wpa_printf(MSG_DEBUG, "WPS UPnP: Event response timeout/fail "
248 "for %s", e->addr->domain_and_port);
255 /* Schedule sending more if there is more to send */
257 event_send_all_later(sm);
262 * If other side doesn't like what we say, forget about them.
263 * (There is no way to tell other side that we are dropping
265 * Alternately, we could just do event_delete(e)
267 wpa_printf(MSG_DEBUG, "WPS UPnP: Deleting subscription due to errors");
268 subscription_unlink(s);
269 subscription_destroy(s);
273 /* event_send_tx_ready -- actually write event message
275 * Prequisite: subscription socket descriptor has become ready to
276 * write (because connection to subscriber has been made).
278 * It is also possible that we are called because the connect has failed;
279 * it is possible to test for this, or we can just go ahead and then
280 * the write will fail.
282 static void event_send_tx_ready(int sock, void *eloop_ctx, void *sock_ctx)
284 struct wps_event_ *e = sock_ctx;
285 struct subscription *s = e->s;
289 assert(e == s->current_event);
290 assert(e->sd == sock);
292 buf = wpabuf_alloc(1000 + wpabuf_len(e->data));
297 wpabuf_printf(buf, "NOTIFY %s HTTP/1.1\r\n", e->addr->path);
298 wpabuf_put_str(buf, "SERVER: Unspecified, UPnP/1.0, Unspecified\r\n");
299 wpabuf_printf(buf, "HOST: %s\r\n", e->addr->domain_and_port);
300 wpabuf_put_str(buf, "CONTENT-TYPE: text/xml; charset=\"utf-8\"\r\n"
302 "NTS: upnp:propchange\r\n");
303 wpabuf_put_str(buf, "SID: uuid:");
304 b = wpabuf_put(buf, 0);
305 uuid_bin2str(s->uuid, b, 80);
306 wpabuf_put(buf, os_strlen(b));
307 wpabuf_put_str(buf, "\r\n");
308 wpabuf_printf(buf, "SEQ: %u\r\n", e->subscriber_sequence);
309 wpabuf_printf(buf, "CONTENT-LENGTH: %d\r\n",
310 (int) wpabuf_len(e->data));
311 wpabuf_put_str(buf, "\r\n"); /* terminating empty line */
312 wpabuf_put_buf(buf, e->data);
314 /* Since the message size is pretty small, we should be
315 * able to get the operating system to buffer what we give it
316 * and not have to come back again later to write more...
319 /* we could: Turn blocking back on? */
320 fcntl(e->sd, F_SETFL, 0);
322 wpa_printf(MSG_DEBUG, "WPS UPnP: Sending event to %s",
323 e->addr->domain_and_port);
324 if (send_wpabuf(e->sd, buf) < 0) {
331 if (e->sd_registered) {
332 e->sd_registered = 0;
333 eloop_unregister_sock(e->sd, EVENT_TYPE_WRITE);
335 /* Set up to read the reply */
336 e->hread = httpread_create(e->sd, event_got_response_handler,
338 0 /* no data expected */,
340 if (e->hread == NULL) {
341 wpa_printf(MSG_ERROR, "WPS UPnP: httpread_create failed");
348 /* Schedule sending more if there is more to send */
350 event_send_all_later(s->sm);
355 /* event_send_start -- prepare to send a event message to subscriber
357 * This gets complicated because:
358 * -- The message is sent via TCP and we have to keep the stream open
359 * for 30 seconds to get a response... then close it.
360 * -- But we might have other event happen in the meantime...
361 * we have to queue them, if we lose them then the subscriber will
362 * be forced to unsubscribe and subscribe again.
363 * -- If multiple URLs are provided then we are supposed to try successive
364 * ones after 30 second timeout.
365 * -- The URLs might use domain names instead of dotted decimal addresses,
366 * and resolution of those may cause unwanted sleeping.
367 * -- Doing the initial TCP connect can take a while, so we have to come
368 * back after connection and then send the data.
370 * Returns nonzero on error;
372 * Prerequisite: No current event send (s->current_event == NULL)
373 * and non-empty queue.
375 static int event_send_start(struct subscription *s)
377 struct wps_event_ *e;
381 * Assume we are called ONLY with no current event and ONLY with
382 * nonempty event queue and ONLY with at least one address to send to.
384 assert(s->addr_list != NULL);
385 assert(s->current_event == NULL);
386 assert(s->event_queue != NULL);
388 s->current_event = e = event_dequeue(s);
390 /* Use address acc. to no. of retries */
391 e->addr = s->addr_list;
392 for (itry = 0; itry < e->retry; itry++)
393 e->addr = e->addr->next;
395 e->sd = socket(AF_INET, SOCK_STREAM, 0);
400 /* set non-blocking so we don't sleep waiting for connection */
401 if (fcntl(e->sd, F_SETFL, O_NONBLOCK) != 0) {
406 * Start the connect. It might succeed immediately but more likely will
407 * return errno EINPROGRESS.
409 if (connect(e->sd, (struct sockaddr *) &e->addr->saddr,
410 sizeof(e->addr->saddr))) {
411 if (errno != EINPROGRESS) {
416 /* Call back when ready for writing (or on failure...). */
417 if (eloop_register_sock(e->sd, EVENT_TYPE_WRITE, event_send_tx_ready,
422 e->sd_registered = 1;
423 /* Don't wait forever! */
424 if (eloop_register_timeout(EVENT_TIMEOUT_SEC, 0, event_timeout_handler,
433 /* event_send_all_later_handler -- actually send events as needed */
434 void event_send_all_later_handler(void *eloop_data, void *user_ctx)
436 struct upnp_wps_device_sm *sm = user_ctx;
437 struct subscription *s;
438 struct subscription *s_old;
441 sm->event_send_all_queued = 0;
442 s = sm->subscriptions;
446 if (s->addr_list == NULL) {
447 /* if we've given up on all addresses */
448 wpa_printf(MSG_DEBUG, "WPS UPnP: Removing "
449 "subscription with no addresses");
452 subscription_unlink(s_old);
453 subscription_destroy(s_old);
455 if (s->current_event == NULL /* not busy */ &&
456 s->event_queue != NULL /* more to do */) {
457 if (event_send_start(s))
462 } while (sm->subscriptions != NULL && s != sm->subscriptions);
465 /* Try again later */
466 event_send_all_later(sm);
471 /* event_send_all_later -- schedule sending events to all subscribers
473 * This avoids two problems:
474 * -- After getting a subscription, we should not send the first event
475 * until after our reply is fully queued to be sent back,
476 * -- Possible stack depth or infinite recursion issues.
478 void event_send_all_later(struct upnp_wps_device_sm *sm)
481 * The exact time in the future isn't too important. Waiting a bit
482 * might let us do several together.
484 if (sm->event_send_all_queued)
486 sm->event_send_all_queued = 1;
487 eloop_register_timeout(EVENT_DELAY_SECONDS, EVENT_DELAY_MSEC,
488 event_send_all_later_handler, NULL, sm);
492 /* event_send_stop_all -- cleanup */
493 void event_send_stop_all(struct upnp_wps_device_sm *sm)
495 if (sm->event_send_all_queued)
496 eloop_cancel_timeout(event_send_all_later_handler, NULL, sm);
497 sm->event_send_all_queued = 0;
502 * event_add - Add a new event to a queue
504 * @data: Event data (is copied; caller retains ownership)
505 * Returns: 0 on success, 1 on error
507 int event_add(struct subscription *s, const struct wpabuf *data)
509 struct wps_event_ *e;
511 if (s->n_queue >= MAX_EVENTS_QUEUED) {
512 wpa_printf(MSG_DEBUG, "WPS UPnP: Too many events queued for "
517 e = os_zalloc(sizeof(*e));
522 e->data = wpabuf_dup(data);
523 if (e->data == NULL) {
527 e->subscriber_sequence = s->next_subscriber_sequence++;
528 if (s->next_subscriber_sequence == 0)
529 s->next_subscriber_sequence++;
530 event_enqueue_at_end(s, e);
531 event_send_all_later(s->sm);