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.
17 #include "http_client.h"
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 */
35 /* How long to wait before sending event */
36 #define EVENT_DELAY_SECONDS 0
37 #define EVENT_DELAY_MSEC 0
40 * Event information that we send to each subscriber is remembered in this
41 * struct. The event cannot be sent by simple UDP; it has to be sent by a HTTP
42 * over TCP transaction which requires various states.. It may also need to be
43 * retried at a different address (if more than one is available).
45 * TODO: As an optimization we could share data between subscribers.
49 struct subscription *s; /* parent */
50 unsigned subscriber_sequence; /* which event for this subscription*/
51 unsigned int retry; /* which retry */
52 struct subscr_addr *addr; /* address to connect to */
53 struct wpabuf *data; /* event data to send */
54 struct http_client *http_event;
58 /* event_clean -- clean sockets etc. of event
59 * Leaves data, retry count etc. alone.
61 static void event_clean(struct wps_event_ *e)
63 if (e->s->current_event == e)
64 e->s->current_event = NULL;
65 http_client_free(e->http_event);
70 /* event_delete -- delete single unqueued event
71 * (be sure to dequeue first if need be)
73 static void event_delete(struct wps_event_ *e)
81 /* event_dequeue -- get next event from the queue
82 * Returns NULL if empty.
84 static struct wps_event_ *event_dequeue(struct subscription *s)
87 e = dl_list_first(&s->event_queue, struct wps_event_, list);
89 dl_list_del(&e->list);
94 /* event_delete_all -- delete entire event queue and current event */
95 void event_delete_all(struct subscription *s)
98 while ((e = event_dequeue(s)) != NULL)
100 if (s->current_event) {
101 event_delete(s->current_event);
102 /* will set: s->current_event = NULL; */
108 * event_retry - Called when we had a failure delivering event msg
110 * @do_next_address: skip address e.g. on connect fail
112 static void event_retry(struct wps_event_ *e, int do_next_address)
114 struct subscription *s = e->s;
115 struct upnp_wps_device_sm *sm = s->sm;
118 /* will set: s->current_event = NULL; */
122 if (e->retry >= dl_list_len(&s->addr_list)) {
123 wpa_printf(MSG_DEBUG, "WPS UPnP: Giving up on sending event "
124 "for %s", e->addr->domain_and_port);
127 dl_list_add(&s->event_queue, &e->list);
128 event_send_all_later(sm);
132 static struct wpabuf * event_build_message(struct wps_event_ *e)
137 buf = wpabuf_alloc(1000 + wpabuf_len(e->data));
140 wpabuf_printf(buf, "NOTIFY %s HTTP/1.1\r\n", e->addr->path);
141 wpabuf_put_str(buf, "SERVER: Unspecified, UPnP/1.0, Unspecified\r\n");
142 wpabuf_printf(buf, "HOST: %s\r\n", e->addr->domain_and_port);
143 wpabuf_put_str(buf, "CONTENT-TYPE: text/xml; charset=\"utf-8\"\r\n"
145 "NTS: upnp:propchange\r\n");
146 wpabuf_put_str(buf, "SID: uuid:");
147 b = wpabuf_put(buf, 0);
148 uuid_bin2str(e->s->uuid, b, 80);
149 wpabuf_put(buf, os_strlen(b));
150 wpabuf_put_str(buf, "\r\n");
151 wpabuf_printf(buf, "SEQ: %u\r\n", e->subscriber_sequence);
152 wpabuf_printf(buf, "CONTENT-LENGTH: %d\r\n",
153 (int) wpabuf_len(e->data));
154 wpabuf_put_str(buf, "\r\n"); /* terminating empty line */
155 wpabuf_put_buf(buf, e->data);
160 static void event_http_cb(void *ctx, struct http_client *c,
161 enum http_client_event event)
163 struct wps_event_ *e = ctx;
164 struct subscription *s = e->s;
168 wpa_printf(MSG_DEBUG,
169 "WPS UPnP: Got event reply OK from "
170 "%s", e->addr->domain_and_port);
173 /* Schedule sending more if there is more to send */
174 if (!dl_list_empty(&s->event_queue))
175 event_send_all_later(s->sm);
177 case HTTP_CLIENT_FAILED:
178 case HTTP_CLIENT_INVALID_REPLY:
179 wpa_printf(MSG_DEBUG, "WPS UPnP: Failed to send event to %s",
180 e->addr->domain_and_port);
183 * If other side doesn't like what we say, forget about them.
184 * (There is no way to tell other side that we are dropping
186 * Alternately, we could just do event_delete(e)
188 wpa_printf(MSG_DEBUG, "WPS UPnP: Deleting subscription due to "
190 dl_list_del(&s->list);
191 subscription_destroy(s);
193 case HTTP_CLIENT_TIMEOUT:
194 wpa_printf(MSG_DEBUG, "WPS UPnP: Event send timeout");
200 /* event_send_start -- prepare to send a event message to subscriber
202 * This gets complicated because:
203 * -- The message is sent via TCP and we have to keep the stream open
204 * for 30 seconds to get a response... then close it.
205 * -- But we might have other event happen in the meantime...
206 * we have to queue them, if we lose them then the subscriber will
207 * be forced to unsubscribe and subscribe again.
208 * -- If multiple URLs are provided then we are supposed to try successive
209 * ones after 30 second timeout.
210 * -- The URLs might use domain names instead of dotted decimal addresses,
211 * and resolution of those may cause unwanted sleeping.
212 * -- Doing the initial TCP connect can take a while, so we have to come
213 * back after connection and then send the data.
215 * Returns nonzero on error;
217 * Prerequisite: No current event send (s->current_event == NULL)
218 * and non-empty queue.
220 static int event_send_start(struct subscription *s)
222 struct wps_event_ *e;
227 * Assume we are called ONLY with no current event and ONLY with
228 * nonempty event queue and ONLY with at least one address to send to.
230 assert(!dl_list_empty(&s->addr_list));
231 assert(s->current_event == NULL);
232 assert(!dl_list_empty(&s->event_queue));
234 s->current_event = e = event_dequeue(s);
236 /* Use address according to number of retries */
238 dl_list_for_each(e->addr, &s->addr_list, struct subscr_addr, list)
239 if (itry++ == e->retry)
244 buf = event_build_message(e);
250 e->http_event = http_client_addr(&e->addr->saddr, buf, 0,
252 if (e->http_event == NULL) {
262 /* event_send_all_later_handler -- actually send events as needed */
263 static void event_send_all_later_handler(void *eloop_data, void *user_ctx)
265 struct upnp_wps_device_sm *sm = user_ctx;
266 struct subscription *s, *tmp;
269 sm->event_send_all_queued = 0;
270 dl_list_for_each_safe(s, tmp, &sm->subscriptions, struct subscription,
272 if (dl_list_empty(&s->addr_list)) {
273 /* if we've given up on all addresses */
274 wpa_printf(MSG_DEBUG, "WPS UPnP: Removing "
275 "subscription with no addresses");
276 dl_list_del(&s->list);
277 subscription_destroy(s);
279 if (s->current_event == NULL /* not busy */ &&
280 !dl_list_empty(&s->event_queue) /* more to do */) {
281 if (event_send_start(s))
288 /* Try again later */
289 event_send_all_later(sm);
294 /* event_send_all_later -- schedule sending events to all subscribers
296 * This avoids two problems:
297 * -- After getting a subscription, we should not send the first event
298 * until after our reply is fully queued to be sent back,
299 * -- Possible stack depth or infinite recursion issues.
301 void event_send_all_later(struct upnp_wps_device_sm *sm)
304 * The exact time in the future isn't too important. Waiting a bit
305 * might let us do several together.
307 if (sm->event_send_all_queued)
309 sm->event_send_all_queued = 1;
310 eloop_register_timeout(EVENT_DELAY_SECONDS, EVENT_DELAY_MSEC,
311 event_send_all_later_handler, NULL, sm);
315 /* event_send_stop_all -- cleanup */
316 void event_send_stop_all(struct upnp_wps_device_sm *sm)
318 if (sm->event_send_all_queued)
319 eloop_cancel_timeout(event_send_all_later_handler, NULL, sm);
320 sm->event_send_all_queued = 0;
325 * event_add - Add a new event to a queue
327 * @data: Event data (is copied; caller retains ownership)
328 * Returns: 0 on success, 1 on error
330 int event_add(struct subscription *s, const struct wpabuf *data)
332 struct wps_event_ *e;
334 if (dl_list_len(&s->event_queue) >= MAX_EVENTS_QUEUED) {
335 wpa_printf(MSG_DEBUG, "WPS UPnP: Too many events queued for "
340 e = os_zalloc(sizeof(*e));
343 dl_list_init(&e->list);
345 e->data = wpabuf_dup(data);
346 if (e->data == NULL) {
350 e->subscriber_sequence = s->next_subscriber_sequence++;
351 if (s->next_subscriber_sequence == 0)
352 s->next_subscriber_sequence++;
353 dl_list_add_tail(&s->event_queue, &e->list);
354 event_send_all_later(s->sm);