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");
193 event_enqueue_at_begin(s, e);
194 event_send_all_later(sm);
198 /* called if the overall event-sending process takes too long */
199 static void event_timeout_handler(void *eloop_data, void *user_ctx)
201 struct wps_event_ *e = user_ctx;
202 struct subscription *s = e->s;
204 assert(e == s->current_event);
206 wpa_printf(MSG_DEBUG, "WPS UPnP: Event send timeout");
211 /* event_got_response_handler -- called back when http response is received. */
212 static void event_got_response_handler(struct httpread *handle, void *cookie,
213 enum httpread_event en)
215 struct wps_event_ *e = cookie;
216 struct subscription *s = e->s;
217 struct upnp_wps_device_sm *sm = s->sm;
218 struct httpread *hread = e->hread;
221 assert(e == s->current_event);
222 eloop_cancel_timeout(event_timeout_handler, NULL, e);
224 if (en == HTTPREAD_EVENT_FILE_READY) {
225 if (httpread_hdr_type_get(hread) == HTTPREAD_HDR_TYPE_REPLY) {
226 reply_code = httpread_reply_code_get(hread);
227 if (reply_code == HTTP_OK) {
228 wpa_printf(MSG_DEBUG,
229 "WPS UPnP: Got event reply OK");
233 wpa_printf(MSG_DEBUG, "WPS UPnP: Got event "
234 "error reply code %d", reply_code);
238 wpa_printf(MSG_DEBUG, "WPS UPnP: Got bogus event "
242 wpa_printf(MSG_DEBUG, "WPS UPnP: Event response timeout/fail");
249 /* Schedule sending more if there is more to send */
251 event_send_all_later(sm);
256 * If other side doesn't like what we say, forget about them.
257 * (There is no way to tell other side that we are dropping
259 * Alternately, we could just do event_delete(e)
261 wpa_printf(MSG_DEBUG, "WPS UPnP: Deleting subscription due to errors");
262 subscription_unlink(s);
263 subscription_destroy(s);
267 /* event_send_tx_ready -- actually write event message
269 * Prequisite: subscription socket descriptor has become ready to
270 * write (because connection to subscriber has been made).
272 * It is also possible that we are called because the connect has failed;
273 * it is possible to test for this, or we can just go ahead and then
274 * the write will fail.
276 static void event_send_tx_ready(int sock, void *eloop_ctx, void *sock_ctx)
278 struct wps_event_ *e = sock_ctx;
279 struct subscription *s = e->s;
283 assert(e == s->current_event);
284 assert(e->sd == sock);
286 buf = wpabuf_alloc(1000 + wpabuf_len(e->data));
291 wpabuf_printf(buf, "NOTIFY %s HTTP/1.1\r\n", e->addr->path);
292 wpabuf_put_str(buf, "SERVER: Unspecified, UPnP/1.0, Unspecified\r\n");
293 wpabuf_printf(buf, "HOST: %s\r\n", e->addr->domain_and_port);
294 wpabuf_put_str(buf, "CONTENT-TYPE: text/xml; charset=\"utf-8\"\r\n"
296 "NTS: upnp:propchange\r\n");
297 wpabuf_put_str(buf, "SID: uuid:");
298 b = wpabuf_put(buf, 0);
299 uuid_bin2str(s->uuid, b, 80);
300 wpabuf_put(buf, os_strlen(b));
301 wpabuf_put_str(buf, "\r\n");
302 wpabuf_printf(buf, "SEQ: %u\r\n", e->subscriber_sequence);
303 wpabuf_printf(buf, "CONTENT-LENGTH: %d\r\n",
304 (int) wpabuf_len(e->data));
305 wpabuf_put_str(buf, "\r\n"); /* terminating empty line */
306 wpabuf_put_buf(buf, e->data);
308 /* Since the message size is pretty small, we should be
309 * able to get the operating system to buffer what we give it
310 * and not have to come back again later to write more...
313 /* we could: Turn blocking back on? */
314 fcntl(e->sd, F_SETFL, 0);
316 if (send_wpabuf(e->sd, buf) < 0) {
323 if (e->sd_registered) {
324 e->sd_registered = 0;
325 eloop_unregister_sock(e->sd, EVENT_TYPE_WRITE);
327 /* Set up to read the reply */
328 e->hread = httpread_create(e->sd, event_got_response_handler,
330 0 /* no data expected */,
332 if (e->hread == NULL) {
333 wpa_printf(MSG_ERROR, "WPS UPnP: httpread_create failed");
340 /* Schedule sending more if there is more to send */
342 event_send_all_later(s->sm);
347 /* event_send_start -- prepare to send a event message to subscriber
349 * This gets complicated because:
350 * -- The message is sent via TCP and we have to keep the stream open
351 * for 30 seconds to get a response... then close it.
352 * -- But we might have other event happen in the meantime...
353 * we have to queue them, if we lose them then the subscriber will
354 * be forced to unsubscribe and subscribe again.
355 * -- If multiple URLs are provided then we are supposed to try successive
356 * ones after 30 second timeout.
357 * -- The URLs might use domain names instead of dotted decimal addresses,
358 * and resolution of those may cause unwanted sleeping.
359 * -- Doing the initial TCP connect can take a while, so we have to come
360 * back after connection and then send the data.
362 * Returns nonzero on error;
364 * Prerequisite: No current event send (s->current_event == NULL)
365 * and non-empty queue.
367 static int event_send_start(struct subscription *s)
369 struct wps_event_ *e;
373 * Assume we are called ONLY with no current event and ONLY with
374 * nonempty event queue and ONLY with at least one address to send to.
376 assert(s->addr_list != NULL);
377 assert(s->current_event == NULL);
378 assert(s->event_queue != NULL);
380 s->current_event = e = event_dequeue(s);
382 /* Use address acc. to no. of retries */
383 e->addr = s->addr_list;
384 for (itry = 0; itry < e->retry; itry++)
385 e->addr = e->addr->next;
387 e->sd = socket(AF_INET, SOCK_STREAM, 0);
392 /* set non-blocking so we don't sleep waiting for connection */
393 if (fcntl(e->sd, F_SETFL, O_NONBLOCK) != 0) {
398 * Start the connect. It might succeed immediately but more likely will
399 * return errno EINPROGRESS.
401 if (connect(e->sd, (struct sockaddr *) &e->addr->saddr,
402 sizeof(e->addr->saddr))) {
403 if (errno == EINPROGRESS) {
409 /* Call back when ready for writing (or on failure...). */
410 if (eloop_register_sock(e->sd, EVENT_TYPE_WRITE, event_send_tx_ready,
415 e->sd_registered = 1;
416 /* Don't wait forever! */
417 if (eloop_register_timeout(EVENT_TIMEOUT_SEC, 0, event_timeout_handler,
426 /* event_send_all_later_handler -- actually send events as needed */
427 void event_send_all_later_handler(void *eloop_data, void *user_ctx)
429 struct upnp_wps_device_sm *sm = user_ctx;
430 struct subscription *s;
431 struct subscription *s_old;
434 sm->event_send_all_queued = 0;
435 if ((s = sm->subscriptions) == NULL)
438 if (s->addr_list == NULL) {
439 /* if we've given up on all addresses */
440 wpa_printf(MSG_DEBUG, "WPS UPnP: Removing "
441 "subscription with no addresses");
444 subscription_unlink(s_old);
445 subscription_destroy(s_old);
447 if (s->current_event == NULL /* not busy */ &&
448 s->event_queue != NULL /* more to do */) {
449 if (event_send_start(s))
454 } while (sm->subscriptions != NULL && s != sm->subscriptions);
457 /* Try again later */
458 event_send_all_later(sm);
463 /* event_send_all_later -- schedule sending events to all subscribers
465 * This avoids two problems:
466 * -- After getting a subscription, we should not send the first event
467 * until after our reply is fully queued to be sent back,
468 * -- Possible stack depth or infinite recursion issues.
470 void event_send_all_later(struct upnp_wps_device_sm *sm)
473 * The exact time in the future isn't too important. Waiting a bit
474 * might let us do several together.
476 if (sm->event_send_all_queued)
478 sm->event_send_all_queued = 1;
479 eloop_register_timeout(EVENT_DELAY_SECONDS, EVENT_DELAY_MSEC,
480 event_send_all_later_handler, NULL, sm);
484 /* event_send_stop_all -- cleanup */
485 void event_send_stop_all(struct upnp_wps_device_sm *sm)
487 if (sm->event_send_all_queued)
488 eloop_cancel_timeout(event_send_all_later_handler, NULL, sm);
489 sm->event_send_all_queued = 0;
494 * event_add - Add a new event to a queue
496 * @data: Event data (is copied; caller retains ownership)
497 * Returns: 0 on success, 1 on error
499 int event_add(struct subscription *s, const struct wpabuf *data)
501 struct wps_event_ *e;
503 if (s->n_queue >= MAX_EVENTS_QUEUED) {
504 wpa_printf(MSG_DEBUG, "WPS UPnP: Too many events queued for "
509 e = os_zalloc(sizeof(*e));
514 e->data = wpabuf_dup(data);
515 if (e->data == NULL) {
519 e->subscriber_sequence = s->next_subscriber_sequence++;
520 if (s->next_subscriber_sequence == 0)
521 s->next_subscriber_sequence++;
522 event_enqueue_at_end(s, e);
523 event_send_all_later(s->sm);