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