wps_upnp_i.h 6.4 KB

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