WPS: Cleanup subscription URL list handling
[libeap.git] / src / wps / wps_upnp_i.h
1 /*
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>
7  *
8  * See wps_upnp.c for more details on licensing and code history.
9  */
10
11 #ifndef WPS_UPNP_I_H
12 #define WPS_UPNP_I_H
13
14 #include "http.h"
15
16 #define UPNP_MULTICAST_ADDRESS  "239.255.255.250" /* for UPnP multicasting */
17 #define UPNP_MULTICAST_PORT 1900 /* UDP port to monitor for UPnP */
18
19 /* min subscribe time per UPnP standard */
20 #define UPNP_SUBSCRIBE_SEC_MIN 1800
21 /* subscribe time we use */
22 #define UPNP_SUBSCRIBE_SEC (UPNP_SUBSCRIBE_SEC_MIN + 1)
23
24 /* "filenames" used in URLs that we service via our "web server": */
25 #define UPNP_WPS_DEVICE_XML_FILE "wps_device.xml"
26 #define UPNP_WPS_SCPD_XML_FILE   "wps_scpd.xml"
27 #define UPNP_WPS_DEVICE_CONTROL_FILE "wps_control"
28 #define UPNP_WPS_DEVICE_EVENT_FILE "wps_event"
29
30 #define MULTICAST_MAX_READ 1600 /* max bytes we'll read for UPD request */
31
32
33 struct subscription;
34 struct upnp_wps_device_sm;
35
36
37 enum advertisement_type_enum {
38         ADVERTISE_UP = 0,
39         ADVERTISE_DOWN = 1,
40         MSEARCH_REPLY = 2
41 };
42
43 /*
44  * Advertisements are broadcast via UDP NOTIFYs, and are also the essence of
45  * the reply to UDP M-SEARCH requests. This struct handles both cases.
46  *
47  * A state machine is needed because a number of variant forms must be sent in
48  * separate packets and spread out in time to avoid congestion.
49  */
50 struct advertisement_state_machine {
51         /* double-linked list */
52         struct advertisement_state_machine *next;
53         struct advertisement_state_machine *prev;
54         struct upnp_wps_device_sm *sm; /* parent */
55         enum advertisement_type_enum type;
56         int state;
57         int nerrors;
58         struct sockaddr_in client; /* for M-SEARCH replies */
59 };
60
61
62 /*
63  * An address of a subscriber (who may have multiple addresses). We are
64  * supposed to send (via TCP) updates to each subscriber, trying each address
65  * for a subscriber until we find one that seems to work.
66  */
67 struct subscr_addr {
68         /* double linked list */
69         struct subscr_addr *next;
70         struct subscr_addr *prev;
71         struct subscription *s; /* parent */
72         char *domain_and_port; /* domain and port part of url */
73         char *path; /* "filepath" part of url (from "mem") */
74         struct sockaddr_in saddr; /* address for doing connect */
75 };
76
77
78 /*
79  * Subscribers to our events are recorded in this struct. This includes a max
80  * of one outgoing connection (sending an "event message") per subscriber. We
81  * also have to age out subscribers unless they renew.
82  */
83 struct subscription {
84         /* double linked list */
85         struct subscription *next;
86         struct subscription *prev;
87         struct upnp_wps_device_sm *sm; /* parent */
88         time_t timeout_time; /* when to age out the subscription */
89         unsigned next_subscriber_sequence; /* number our messages */
90         /*
91          * This uuid identifies the subscription and is randomly generated by
92          * us and given to the subscriber when the subscription is accepted;
93          * and is then included with each event sent to the subscriber.
94          */
95         u8 uuid[UUID_LEN];
96         /* Linked list of address alternatives (rotate through on failure) */
97         struct subscr_addr *addr_list;
98         int n_addr; /* Number of addresses in list */
99         struct wps_event_ *event_queue; /* Queued event messages. */
100         int n_queue; /* How many events are queued */
101         struct wps_event_ *current_event; /* non-NULL if being sent (not in q)
102                                            */
103 };
104
105
106 /*
107  * Our instance data corresponding to one WiFi network interface
108  * (multiple might share the same wired network interface!).
109  *
110  * This is known as an opaque struct declaration to users of the WPS UPnP code.
111  */
112 struct upnp_wps_device_sm {
113         struct upnp_wps_device_ctx *ctx; /* callback table */
114         struct wps_context *wps;
115         void *priv;
116         char *root_dir;
117         char *desc_url;
118         int started; /* nonzero if we are active */
119         char *net_if; /* network interface we use */
120         char *mac_addr_text; /* mac addr of network i.f. we use */
121         u8 mac_addr[ETH_ALEN]; /* mac addr of network i.f. we use */
122         char *ip_addr_text; /* IP address of network i.f. we use */
123         unsigned ip_addr; /* IP address of network i.f. we use (host order) */
124         int multicast_sd; /* send multicast messages over this socket */
125         int ssdp_sd; /* receive discovery UPD packets on socket */
126         int ssdp_sd_registered; /* nonzero if we must unregister */
127         unsigned advertise_count; /* how many advertisements done */
128         struct advertisement_state_machine advertisement;
129         struct advertisement_state_machine *msearch_replies;
130         int n_msearch_replies; /* no. of pending M-SEARCH replies */
131         int web_port; /* our port that others get xml files from */
132         struct http_server *web_srv;
133         /* Note: subscriptions are kept in expiry order */
134         struct subscription *subscriptions; /* linked list */
135         int n_subscriptions; /* no of current subscriptions */
136         int event_send_all_queued; /* if we are scheduled to send events soon
137                                     */
138
139         char *wlanevent; /* the last WLANEvent data */
140
141         /* FIX: maintain separate structures for each UPnP peer */
142         struct upnp_wps_peer peer;
143 };
144
145 /* wps_upnp.c */
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_unlink(struct subscription *s);
152 void subscription_destroy(struct subscription *s);
153 struct subscription * subscription_find(struct upnp_wps_device_sm *sm,
154                                         const u8 uuid[UUID_LEN]);
155 int send_wpabuf(int fd, struct wpabuf *buf);
156 int get_netif_info(const char *net_if, unsigned *ip_addr, char **ip_addr_text,
157                    u8 mac[ETH_ALEN], char **mac_addr_text);
158
159 /* wps_upnp_ssdp.c */
160 void msearchreply_state_machine_stop(struct advertisement_state_machine *a);
161 int advertisement_state_machine_start(struct upnp_wps_device_sm *sm);
162 void advertisement_state_machine_stop(struct upnp_wps_device_sm *sm,
163                                       int send_byebye);
164 void ssdp_listener_stop(struct upnp_wps_device_sm *sm);
165 int ssdp_listener_start(struct upnp_wps_device_sm *sm);
166 int ssdp_listener_open(void);
167 int add_ssdp_network(const char *net_if);
168 int ssdp_open_multicast_sock(u32 ip_addr);
169 int ssdp_open_multicast(struct upnp_wps_device_sm *sm);
170
171 /* wps_upnp_web.c */
172 int web_listener_start(struct upnp_wps_device_sm *sm);
173 void web_listener_stop(struct upnp_wps_device_sm *sm);
174
175 /* wps_upnp_event.c */
176 int event_add(struct subscription *s, const struct wpabuf *data);
177 void event_delete_all(struct subscription *s);
178 void event_send_all_later(struct upnp_wps_device_sm *sm);
179 void event_send_stop_all(struct upnp_wps_device_sm *sm);
180
181 #endif /* WPS_UPNP_I_H */