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