diff options
Diffstat (limited to 'src/wps/wps_upnp_ssdp.c')
-rw-r--r-- | src/wps/wps_upnp_ssdp.c | 125 |
1 files changed, 64 insertions, 61 deletions
diff --git a/src/wps/wps_upnp_ssdp.c b/src/wps/wps_upnp_ssdp.c index c1dc99d..8505d05 100644 --- a/src/wps/wps_upnp_ssdp.c +++ b/src/wps/wps_upnp_ssdp.c @@ -24,7 +24,6 @@ #define UPNP_CACHE_SEC (UPNP_CACHE_SEC_MIN + 1) /* cache time we use */ #define UPNP_CACHE_SEC_MIN 1800 /* min cachable time per UPnP standard */ #define UPNP_ADVERTISE_REPEAT 2 /* no more than 3 */ -#define MULTICAST_MAX_READ 1600 /* max bytes we'll read for UPD request */ #define MAX_MSEARCH 20 /* max simultaneous M-SEARCH replies ongoing */ #define SSDP_TARGET "239.0.0.0" #define SSDP_NETMASK "255.0.0.0" @@ -131,14 +130,15 @@ static int str_starts(const char *str, const char *start) * Note: next_advertisement is shared code with msearchreply_* functions */ static struct wpabuf * -next_advertisement(struct advertisement_state_machine *a, int *islast) +next_advertisement(struct upnp_wps_device_sm *sm, + struct advertisement_state_machine *a, int *islast) { struct wpabuf *msg; char *NTString = ""; char uuid_string[80]; *islast = 0; - uuid_bin2str(a->sm->wps->uuid, uuid_string, sizeof(uuid_string)); + uuid_bin2str(sm->wps->uuid, uuid_string, sizeof(uuid_string)); msg = wpabuf_alloc(800); /* more than big enough */ if (msg == NULL) goto fail; @@ -172,7 +172,7 @@ next_advertisement(struct advertisement_state_machine *a, int *islast) if (a->type != ADVERTISE_DOWN) { /* Where others may get our XML files from */ wpabuf_printf(msg, "LOCATION: http://%s:%d/%s\r\n", - a->sm->ip_addr_text, a->sm->web_port, + sm->ip_addr_text, sm->web_port, UPNP_WPS_DEVICE_XML_FILE); } @@ -245,7 +245,6 @@ void advertisement_state_machine_stop(struct upnp_wps_device_sm *sm, a->type = ADVERTISE_DOWN; a->state = 0; - a->sm = sm; os_memset(&dest, 0, sizeof(dest)); dest.sin_family = AF_INET; @@ -253,7 +252,7 @@ void advertisement_state_machine_stop(struct upnp_wps_device_sm *sm, dest.sin_port = htons(UPNP_MULTICAST_PORT); while (!islast) { - msg = next_advertisement(a, &islast); + msg = next_advertisement(sm, a, &islast); if (msg == NULL) break; if (sendto(sm->multicast_sd, wpabuf_head(msg), wpabuf_len(msg), @@ -292,7 +291,7 @@ static void advertisement_state_machine_handler(void *eloop_data, */ wpa_printf(MSG_MSGDUMP, "WPS UPnP: Advertisement state=%d", a->state); - msg = next_advertisement(a, &islast); + msg = next_advertisement(sm, a, &islast); if (msg == NULL) return; @@ -357,7 +356,6 @@ int advertisement_state_machine_start(struct upnp_wps_device_sm *sm) */ a->type = ADVERTISE_DOWN; a->state = 0; - a->sm = sm; /* (other fields not used here) */ /* First timeout should be random interval < 100 msec */ @@ -376,28 +374,15 @@ int advertisement_state_machine_start(struct upnp_wps_device_sm *sm) * They are sent in response to a UDP M-SEARCH packet. **************************************************************************/ -static void msearchreply_state_machine_handler(void *eloop_data, - void *user_ctx); - - /** * msearchreply_state_machine_stop - Stop M-SEARCH reply state machine * @a: Selected advertisement/reply state */ void msearchreply_state_machine_stop(struct advertisement_state_machine *a) { - struct upnp_wps_device_sm *sm = a->sm; wpa_printf(MSG_DEBUG, "WPS UPnP: M-SEARCH stop"); - if (a->next == a) { - sm->msearch_replies = NULL; - } else { - if (sm->msearch_replies == a) - sm->msearch_replies = a->next; - a->next->prev = a->prev; - a->prev->next = a->next; - } + dl_list_del(&a->list); os_free(a); - sm->n_msearch_replies--; } @@ -405,7 +390,7 @@ static void msearchreply_state_machine_handler(void *eloop_data, void *user_ctx) { struct advertisement_state_machine *a = user_ctx; - struct upnp_wps_device_sm *sm = a->sm; + struct upnp_wps_device_sm *sm = eloop_data; struct wpabuf *msg; int next_timeout_msec = 100; int next_timeout_sec = 0; @@ -423,7 +408,7 @@ static void msearchreply_state_machine_handler(void *eloop_data, wpa_printf(MSG_MSGDUMP, "WPS UPnP: M-SEARCH reply state=%d (%s:%d)", a->state, inet_ntoa(a->client.sin_addr), ntohs(a->client.sin_port)); - msg = next_advertisement(a, &islast); + msg = next_advertisement(sm, a, &islast); if (msg == NULL) return; @@ -477,10 +462,12 @@ static void msearchreply_state_machine_start(struct upnp_wps_device_sm *sm, struct advertisement_state_machine *a; int next_timeout_sec; int next_timeout_msec; + int replies; + replies = dl_list_len(&sm->msearch_replies); wpa_printf(MSG_DEBUG, "WPS UPnP: M-SEARCH reply start (%d " - "outstanding)", sm->n_msearch_replies); - if (sm->n_msearch_replies >= MAX_MSEARCH) { + "outstanding)", replies); + if (replies >= MAX_MSEARCH) { wpa_printf(MSG_INFO, "WPS UPnP: Too many outstanding " "M-SEARCH replies"); return; @@ -491,7 +478,6 @@ static void msearchreply_state_machine_start(struct upnp_wps_device_sm *sm, return; a->type = MSEARCH_REPLY; a->state = 0; - a->sm = sm; os_memcpy(&a->client, client, sizeof(*client)); /* Wait time depending on MX value */ next_timeout_msec = (1000 * mx * (os_random() & 0xFF)) >> 8; @@ -504,15 +490,7 @@ static void msearchreply_state_machine_start(struct upnp_wps_device_sm *sm, goto fail; } /* Remember for future cleanup */ - if (sm->msearch_replies) { - a->next = sm->msearch_replies; - a->prev = a->next->prev; - a->prev->next = a; - a->next->prev = a; - } else { - sm->msearch_replies = a->next = a->prev = a; - } - sm->n_msearch_replies++; + dl_list_add(&sm->msearch_replies, &a->list); return; fail: @@ -546,7 +524,9 @@ fail: static void ssdp_parse_msearch(struct upnp_wps_device_sm *sm, struct sockaddr_in *client, const char *data) { +#ifndef CONFIG_NO_STDOUT_DEBUG const char *start = data; +#endif /* CONFIG_NO_STDOUT_DEBUG */ const char *end; int got_host = 0; int got_st = 0, st_match = 0; @@ -688,7 +668,7 @@ bad: * ssdp_listener_stop - Stop SSDP listered * @sm: WPS UPnP state machine from upnp_wps_device_init() * - * This function stops the SSDP listerner that was started by calling + * This function stops the SSDP listener that was started by calling * ssdp_listener_start(). */ void ssdp_listener_stop(struct upnp_wps_device_sm *sm) @@ -750,23 +730,16 @@ static void ssdp_listener_handler(int sd, void *eloop_ctx, void *sock_ctx) } -/** - * ssdp_listener_start - Set up for receiving discovery (UDP) packets - * @sm: WPS UPnP state machine from upnp_wps_device_init() - * Returns: 0 on success, -1 on failure - * - * The SSDP listerner is stopped by calling ssdp_listener_stop(). - */ -int ssdp_listener_start(struct upnp_wps_device_sm *sm) +int ssdp_listener_open(void) { - int sd = -1; struct sockaddr_in addr; struct ip_mreq mcast_addr; int on = 1; /* per UPnP spec, keep IP packet time to live (TTL) small */ unsigned char ttl = 4; + int sd; - sm->ssdp_sd = sd = socket(AF_INET, SOCK_DGRAM, 0); + sd = socket(AF_INET, SOCK_DGRAM, 0); if (sd < 0) goto fail; if (fcntl(sd, F_SETFL, O_NONBLOCK) != 0) @@ -788,8 +761,29 @@ int ssdp_listener_start(struct upnp_wps_device_sm *sm) if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl))) goto fail; - if (eloop_register_sock(sd, EVENT_TYPE_READ, ssdp_listener_handler, - NULL, sm)) + + return sd; + +fail: + if (sd >= 0) + close(sd); + return -1; +} + + +/** + * ssdp_listener_start - Set up for receiving discovery (UDP) packets + * @sm: WPS UPnP state machine from upnp_wps_device_init() + * Returns: 0 on success, -1 on failure + * + * The SSDP listener is stopped by calling ssdp_listener_stop(). + */ +int ssdp_listener_start(struct upnp_wps_device_sm *sm) +{ + sm->ssdp_sd = ssdp_listener_open(); + + if (eloop_register_sock(sm->ssdp_sd, EVENT_TYPE_READ, + ssdp_listener_handler, NULL, sm)) goto fail; sm->ssdp_sd_registered = 1; return 0; @@ -813,7 +807,7 @@ fail: * once after booting up, but it does not hurt to call this more frequently * "to be safe". */ -int add_ssdp_network(char *net_if) +int add_ssdp_network(const char *net_if) { #ifdef __linux__ int ret = -1; @@ -829,7 +823,7 @@ int add_ssdp_network(char *net_if) if (sock < 0) goto fail; - rt.rt_dev = net_if; + rt.rt_dev = (char *) net_if; sin = aliasing_hide_typecast(&rt.rt_dst, struct sockaddr_in); sin->sin_family = AF_INET; sin->sin_port = 0; @@ -864,19 +858,14 @@ fail: } -/** - * ssdp_open_multicast - Open socket for sending multicast SSDP messages - * @sm: WPS UPnP state machine from upnp_wps_device_init() - * Returns: 0 on success, -1 on failure - */ -int ssdp_open_multicast(struct upnp_wps_device_sm *sm) +int ssdp_open_multicast_sock(u32 ip_addr) { - int sd = -1; + int sd; /* per UPnP-arch-DeviceArchitecture, 1. Discovery, keep IP packet * time to live (TTL) small */ unsigned char ttl = 4; - sm->multicast_sd = sd = socket(AF_INET, SOCK_DGRAM, 0); + sd = socket(AF_INET, SOCK_DGRAM, 0); if (sd < 0) return -1; @@ -886,7 +875,7 @@ int ssdp_open_multicast(struct upnp_wps_device_sm *sm) #endif if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, - &sm->ip_addr, sizeof(sm->ip_addr))) + &ip_addr, sizeof(ip_addr))) return -1; if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl))) @@ -896,7 +885,7 @@ int ssdp_open_multicast(struct upnp_wps_device_sm *sm) { struct ip_mreq mreq; mreq.imr_multiaddr.s_addr = inet_addr(UPNP_MULTICAST_ADDRESS); - mreq.imr_interface.s_addr = sm->ip_addr; + mreq.imr_interface.s_addr = ip_addr; wpa_printf(MSG_DEBUG, "WPS UPnP: Multicast addr 0x%x if addr " "0x%x", mreq.imr_multiaddr.s_addr, @@ -917,5 +906,19 @@ int ssdp_open_multicast(struct upnp_wps_device_sm *sm) * which aids debugging I suppose but isn't really necessary? */ + return sd; +} + + +/** + * ssdp_open_multicast - Open socket for sending multicast SSDP messages + * @sm: WPS UPnP state machine from upnp_wps_device_init() + * Returns: 0 on success, -1 on failure + */ +int ssdp_open_multicast(struct upnp_wps_device_sm *sm) +{ + sm->multicast_sd = ssdp_open_multicast_sock(sm->ip_addr); + if (sm->multicast_sd < 0) + return -1; return 0; } |