68782c0545f04768f6a0fb38625b9a416468af6c
[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 "utils/list.h"
15 #include "http.h"
16
17 #define UPNP_MULTICAST_ADDRESS  "239.255.255.250" /* for UPnP multicasting */
18 #define UPNP_MULTICAST_PORT 1900 /* UDP port to monitor for UPnP */
19
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)
24
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"
30
31 #define MULTICAST_MAX_READ 1600 /* max bytes we'll read for UPD request */
32
33
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         struct dl_list list;
53         enum advertisement_type_enum type;
54         int state;
55         int nerrors;
56         struct sockaddr_in client; /* for M-SEARCH replies */
57 };
58
59
60 /*
61  * An address of a subscriber (who may have multiple addresses). We are
62  * supposed to send (via TCP) updates to each subscriber, trying each address
63  * for a subscriber until we find one that seems to work.
64  */
65 struct subscr_addr {
66         struct dl_list list;
67         char *domain_and_port; /* domain and port part of url */
68         char *path; /* "filepath" part of url (from "mem") */
69         struct sockaddr_in saddr; /* address for doing connect */
70         unsigned num_failures;
71 };
72
73
74 /*
75  * Subscribers to our events are recorded in this struct. This includes a max
76  * of one outgoing connection (sending an "event message") per subscriber. We
77  * also have to age out subscribers unless they renew.
78  */
79 struct subscription {
80         struct dl_list list;
81         struct upnp_wps_device_sm *sm; /* parent */
82         time_t timeout_time; /* when to age out the subscription */
83         unsigned next_subscriber_sequence; /* number our messages */
84         /*
85          * This uuid identifies the subscription and is randomly generated by
86          * us and given to the subscriber when the subscription is accepted;
87          * and is then included with each event sent to the subscriber.
88          */
89         u8 uuid[UUID_LEN];
90         /* Linked list of address alternatives (rotate through on failure) */
91         struct dl_list addr_list;
92         struct dl_list event_queue; /* Queued event messages. */
93         struct wps_event_ *current_event; /* non-NULL if being sent (not in q)
94                                            */
95
96         /* Information from SetSelectedRegistrar action */
97         u8 selected_registrar;
98         u16 dev_password_id;
99         u16 config_methods;
100         u8 authorized_macs[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
101         struct wps_registrar *reg;
102 };
103
104
105 /*
106  * Our instance data corresponding to one WiFi network interface
107  * (multiple might share the same wired network interface!).
108  *
109  * This is known as an opaque struct declaration to users of the WPS UPnP code.
110  */
111 struct upnp_wps_device_sm {
112         struct upnp_wps_device_ctx *ctx; /* callback table */
113         struct wps_context *wps;
114         void *priv;
115         char *root_dir;
116         char *desc_url;
117         int started; /* nonzero if we are active */
118         u8 mac_addr[ETH_ALEN]; /* mac addr of network i.f. we use */
119         char *ip_addr_text; /* IP address of network i.f. we use */
120         unsigned ip_addr; /* IP address of network i.f. we use (host order) */
121         int multicast_sd; /* send multicast messages over this socket */
122         int ssdp_sd; /* receive discovery UPD packets on socket */
123         int ssdp_sd_registered; /* nonzero if we must unregister */
124         unsigned advertise_count; /* how many advertisements done */
125         struct advertisement_state_machine advertisement;
126         struct dl_list msearch_replies;
127         int web_port; /* our port that others get xml files from */
128         struct http_server *web_srv;
129         /* Note: subscriptions are kept in expiry order */
130         struct dl_list subscriptions;
131         int event_send_all_queued; /* if we are scheduled to send events soon
132                                     */
133
134         char *wlanevent; /* the last WLANEvent data */
135
136         /* FIX: maintain separate structures for each UPnP peer */
137         struct upnp_wps_peer peer;
138 };
139
140 /* wps_upnp.c */
141 void format_date(struct wpabuf *buf);
142 struct subscription * subscription_start(struct upnp_wps_device_sm *sm,
143                                          const char *callback_urls);
144 struct subscription * subscription_renew(struct upnp_wps_device_sm *sm,
145                                          const u8 uuid[UUID_LEN]);
146 void subscription_destroy(struct subscription *s);
147 struct subscription * subscription_find(struct upnp_wps_device_sm *sm,
148                                         const u8 uuid[UUID_LEN]);
149 void subscr_addr_delete(struct subscr_addr *a);
150 int send_wpabuf(int fd, struct wpabuf *buf);
151 int get_netif_info(const char *net_if, unsigned *ip_addr, char **ip_addr_text,
152                    u8 mac[ETH_ALEN]);
153
154 /* wps_upnp_ssdp.c */
155 void msearchreply_state_machine_stop(struct advertisement_state_machine *a);
156 int advertisement_state_machine_start(struct upnp_wps_device_sm *sm);
157 void advertisement_state_machine_stop(struct upnp_wps_device_sm *sm,
158                                       int send_byebye);
159 void ssdp_listener_stop(struct upnp_wps_device_sm *sm);
160 int ssdp_listener_start(struct upnp_wps_device_sm *sm);
161 int ssdp_listener_open(void);
162 int add_ssdp_network(const char *net_if);
163 int ssdp_open_multicast_sock(u32 ip_addr);
164 int ssdp_open_multicast(struct upnp_wps_device_sm *sm);
165
166 /* wps_upnp_web.c */
167 int web_listener_start(struct upnp_wps_device_sm *sm);
168 void web_listener_stop(struct upnp_wps_device_sm *sm);
169
170 /* wps_upnp_event.c */
171 int event_add(struct subscription *s, const struct wpabuf *data);
172 void event_delete_all(struct subscription *s);
173 void event_send_all_later(struct upnp_wps_device_sm *sm);
174 void event_send_stop_all(struct upnp_wps_device_sm *sm);
175
176 /* wps_upnp_ap.c */
177 int upnp_er_set_selected_registrar(struct wps_registrar *reg,
178                                    struct subscription *s,
179                                    const struct wpabuf *msg);
180 void upnp_er_remove_notification(struct subscription *s);
181
182 #endif /* WPS_UPNP_I_H */