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