2 * UPnP for WPS / internal definitions
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.
14 #include "utils/list.h"
17 #define UPNP_MULTICAST_ADDRESS "239.255.255.250" /* for UPnP multicasting */
18 #define UPNP_MULTICAST_PORT 1900 /* UDP port to monitor for UPnP */
20 /* min subscribe time per UPnP standard */
21 #define UPNP_SUBSCRIBE_SEC_MIN 1800
22 /* subscribe time we use */
23 #define UPNP_SUBSCRIBE_SEC (UPNP_SUBSCRIBE_SEC_MIN + 1)
25 /* "filenames" used in URLs that we service via our "web server": */
26 #define UPNP_WPS_DEVICE_XML_FILE "wps_device.xml"
27 #define UPNP_WPS_SCPD_XML_FILE "wps_scpd.xml"
28 #define UPNP_WPS_DEVICE_CONTROL_FILE "wps_control"
29 #define UPNP_WPS_DEVICE_EVENT_FILE "wps_event"
31 #define MULTICAST_MAX_READ 1600 /* max bytes we'll read for UPD request */
35 struct upnp_wps_device_sm;
39 enum advertisement_type_enum {
46 * Advertisements are broadcast via UDP NOTIFYs, and are also the essence of
47 * the reply to UDP M-SEARCH requests. This struct handles both cases.
49 * A state machine is needed because a number of variant forms must be sent in
50 * separate packets and spread out in time to avoid congestion.
52 struct advertisement_state_machine {
53 /* double-linked list */
54 struct advertisement_state_machine *next;
55 struct advertisement_state_machine *prev;
56 struct upnp_wps_device_sm *sm; /* parent */
57 enum advertisement_type_enum type;
60 struct sockaddr_in client; /* for M-SEARCH replies */
65 * An address of a subscriber (who may have multiple addresses). We are
66 * supposed to send (via TCP) updates to each subscriber, trying each address
67 * for a subscriber until we find one that seems to work.
71 char *domain_and_port; /* domain and port part of url */
72 char *path; /* "filepath" part of url (from "mem") */
73 struct sockaddr_in saddr; /* address for doing connect */
78 * Subscribers to our events are recorded in this struct. This includes a max
79 * of one outgoing connection (sending an "event message") per subscriber. We
80 * also have to age out subscribers unless they renew.
84 struct upnp_wps_device_sm *sm; /* parent */
85 time_t timeout_time; /* when to age out the subscription */
86 unsigned next_subscriber_sequence; /* number our messages */
88 * This uuid identifies the subscription and is randomly generated by
89 * us and given to the subscriber when the subscription is accepted;
90 * and is then included with each event sent to the subscriber.
93 /* Linked list of address alternatives (rotate through on failure) */
94 struct dl_list addr_list;
95 struct dl_list event_queue; /* Queued event messages. */
96 struct wps_event_ *current_event; /* non-NULL if being sent (not in q)
99 /* Information from SetSelectedRegistrar action */
100 u8 selected_registrar;
103 struct wps_registrar *reg;
108 * Our instance data corresponding to one WiFi network interface
109 * (multiple might share the same wired network interface!).
111 * This is known as an opaque struct declaration to users of the WPS UPnP code.
113 struct upnp_wps_device_sm {
114 struct upnp_wps_device_ctx *ctx; /* callback table */
115 struct wps_context *wps;
119 int started; /* nonzero if we are active */
120 char *net_if; /* network interface we use */
121 char *mac_addr_text; /* mac addr of network i.f. we use */
122 u8 mac_addr[ETH_ALEN]; /* mac addr of network i.f. we use */
123 char *ip_addr_text; /* IP address of network i.f. we use */
124 unsigned ip_addr; /* IP address of network i.f. we use (host order) */
125 int multicast_sd; /* send multicast messages over this socket */
126 int ssdp_sd; /* receive discovery UPD packets on socket */
127 int ssdp_sd_registered; /* nonzero if we must unregister */
128 unsigned advertise_count; /* how many advertisements done */
129 struct advertisement_state_machine advertisement;
130 struct advertisement_state_machine *msearch_replies;
131 int n_msearch_replies; /* no. of pending M-SEARCH replies */
132 int web_port; /* our port that others get xml files from */
133 struct http_server *web_srv;
134 /* Note: subscriptions are kept in expiry order */
135 struct dl_list subscriptions;
136 int event_send_all_queued; /* if we are scheduled to send events soon
139 char *wlanevent; /* the last WLANEvent data */
141 /* FIX: maintain separate structures for each UPnP peer */
142 struct upnp_wps_peer peer;
146 void format_date(struct wpabuf *buf);
147 struct subscription * subscription_start(struct upnp_wps_device_sm *sm,
148 const char *callback_urls);
149 struct subscription * subscription_renew(struct upnp_wps_device_sm *sm,
150 const u8 uuid[UUID_LEN]);
151 void subscription_destroy(struct subscription *s);
152 struct subscription * subscription_find(struct upnp_wps_device_sm *sm,
153 const u8 uuid[UUID_LEN]);
154 int send_wpabuf(int fd, struct wpabuf *buf);
155 int get_netif_info(const char *net_if, unsigned *ip_addr, char **ip_addr_text,
156 u8 mac[ETH_ALEN], char **mac_addr_text);
158 /* wps_upnp_ssdp.c */
159 void msearchreply_state_machine_stop(struct advertisement_state_machine *a);
160 int advertisement_state_machine_start(struct upnp_wps_device_sm *sm);
161 void advertisement_state_machine_stop(struct upnp_wps_device_sm *sm,
163 void ssdp_listener_stop(struct upnp_wps_device_sm *sm);
164 int ssdp_listener_start(struct upnp_wps_device_sm *sm);
165 int ssdp_listener_open(void);
166 int add_ssdp_network(const char *net_if);
167 int ssdp_open_multicast_sock(u32 ip_addr);
168 int ssdp_open_multicast(struct upnp_wps_device_sm *sm);
171 int web_listener_start(struct upnp_wps_device_sm *sm);
172 void web_listener_stop(struct upnp_wps_device_sm *sm);
174 /* wps_upnp_event.c */
175 int event_add(struct subscription *s, const struct wpabuf *data);
176 void event_delete_all(struct subscription *s);
177 void event_send_all_later(struct upnp_wps_device_sm *sm);
178 void event_send_stop_all(struct upnp_wps_device_sm *sm);
181 int upnp_er_set_selected_registrar(struct wps_registrar *reg,
182 struct subscription *s,
183 const struct wpabuf *msg);
184 void upnp_er_remove_notification(struct subscription *s);
186 #endif /* WPS_UPNP_I_H */