Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

upnphandler.h

Go to the documentation of this file.
00001 
00002 /*
00003 ** Copyright (C) 2004 Russell Leighton
00004 **
00005 ** This program is free software; you can redistribute it and/or modify
00006 ** it under the terms of the GNU General Public License as published by
00007 ** the Free Software Foundation; either version 2 of the License, or
00008 ** (at your option) any later version.
00009 **
00010 ** This program is distributed in the hope that it will be useful,
00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 ** GNU General Public License for more details.
00014 **
00015 ** You should have received a copy of the GNU General Public License
00016 ** along with this program; if not, write to the Free Software
00017 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 */
00019 
00020 
00021 
00022 
00023 #ifndef __AWHANDLER_UPNP__
00024 #define __AWHANDLER_UPNP__
00025 
00026 #include "sysnet.h" 
00027 #include "trie.h" 
00028 #include "regexmatch.h"
00029 #include "monitor.h"
00030 #include "wire.h"
00031 
00124 /****
00125      moved below comment out of above because the "wire"
00126      interface does not allow you to set these currently
00127 ****/
00128 /*
00129 Options:
00130 <ul>
00131 <li> AW_UPNPHANDLER_GROUP : Watch a group of devices defined by
00132 matching the regex. Only send 'up' events when the first
00133 match appears and only send 'down' events when the last match disappears.
00134 <li> AW_UPNPHANDLER_COUNTONCHANGE : Only send 'count' events when the
00135 count of the devicees matching the regex changes. By default, the
00136 'count' events are generated every cycle. This option is useful is you
00137 are interested in notification if the number of devices gets
00138 below or above a certain number (used with the rfilterhandler).
00139 </ul>
00140 */
00141 
00142 /*
00143 Changes:
00144 
00145 03/12/04 Russell Leighton
00146 Port to handler
00147 */
00148 
00149 
00150 /* ip and port defined in standard */
00151 #define UPNP_MCAST_GROUP "239.255.255.250"
00152 #define UPNP_MCAST_PORT 1900
00153 
00154 
00158 typedef enum {
00159 
00160   AW_UPNPHANDLER_GROUP = 0x1,         /* only send 'up'/'down' events based on whole group */
00161   AW_UPNPHANDLER_COUNTONCHANGE = 0x2, /* only send 'count' events when count changes */
00162 
00163 } aw_upnphandler_flag_t;
00164 
00168 #define AW_UPNPHANDLER_DEFAULT_FLAGS (AW_UPNPHANDLER_COUNTONCHANGE)
00169 
00173 typedef struct {
00174 
00175   aw_event_sig_t
00176    up,               /* regex matched and device is noticed up */
00177    down,             /* regex matched and device is noticed down */
00178    count;            /* count of regex matched devices */
00179 
00180 } aw_upnphandler_events_t;
00181 
00182 
00186 typedef enum {
00187 
00188   AW_UPNPHANDLER_IDLE,               /* not doing anything */
00189   AW_UPNPHANDLER_WAIT_READ,          /* waiting on a read() */
00190 
00191 } aw_upnphandler_state_cd_t;
00192 
00196 typedef struct aw_upnphandler_devlist_s {
00197 
00198   /* list */
00199   struct aw_upnphandler_devlist_s
00200    *next;
00201 
00202   /* calculated from CACHE-CONTROL: */
00203   aw_timestamp_t
00204    expires;
00205 
00206   /* 
00207      this is LOCATION \t SERVER \t USN, unless substituted by regex, where
00208 
00209      LOCATION , is assumed to be a unique URL that points 
00210      to the device (via IP) and can be used to get detailed info.
00211 
00212      SERVER, is the concatenation of OS name, OS version, UPnP/1.0, 
00213      product name, and product version. Specified by UPnP vendor. 
00214 
00215      USN, Unique service name
00216   */
00217   byte_t
00218    *name;
00219 
00220   /* unique key to identify this device */
00221   byte_t
00222    *key;
00223 
00224 } aw_upnphandler_devlist_t;
00225 
00226 
00230 typedef struct {
00231 
00232   aw_upnphandler_state_cd_t
00233    state_cd;
00234 
00235   int32_t
00236     outmcfd, /* to send M-SEARCH messages */
00237     inmcfd;  /* to listen for alive and byebye */
00238 
00239   /* space to read packets, max size of a UDP packet */
00240   byte_t
00241    rx_message[0xffff]; 
00242 
00243   /* set to 1 in start(), back to 0 in handler */
00244   u_int32_t
00245    first_handler;
00246 
00247   /* sorted list of devs that were up last poll interval */
00248   aw_upnphandler_devlist_t
00249    *up,                            /* the last _complete_ set of up records */
00250    *new_up;                        /* for accumulating up records during m-search */
00251 
00252   /* used for fast look up of what's in 'up' and 'new_up' */
00253   aw_trie_t
00254    *up_index,
00255    *new_up_index;
00256 
00257   aw_cache_allocator_t
00258    devlist_factory;
00259 
00260 } aw_upnphandler_state_t;
00261 
00265 typedef struct {
00266 
00267   aw_upnphandler_flag_t
00268    flags;
00269 
00270   aw_upnphandler_events_t
00271     events;
00272 
00273   /* regex string */
00274   const byte_t
00275    *regex;
00276 
00277   /* holds substitution string, may be NULL */
00278   const byte_t
00279    *subst;
00280 
00281 } aw_upnphandler_regex_t;
00282 
00286 typedef struct {
00287 
00288   aw_upnphandler_flag_t
00289    flags;
00290 
00291   aw_upnphandler_events_t
00292     events;
00293 
00294   /* holds compiled regular expression  */
00295   regex_t
00296    pattern;
00297 
00298   /* holds substitution string, may be NULL */
00299   byte_t
00300    *subst;
00301 
00302   aw_upnphandler_devlist_t
00303    *watch;           /* list of devs that are in 'up' that matched regex */
00304 
00305   u_int32_t
00306    watchcount;       /* length of watch list */
00307 
00308 } aw_upnphandler_watcher_t;
00309 
00310 
00314 typedef struct {
00315 
00316   aw_handler_t 
00317     handler_header;
00318 
00319   u_int32_t
00320    nwatchers;
00321 
00322   aw_upnphandler_watcher_t
00323    *watchers;
00324 
00325   aw_upnphandler_state_t
00326    state;
00327 
00328   /* address of interface to send multicast packets */
00329   struct in_addr
00330    mcast_if_addr;
00331 
00332   /* non-zero if enabling IP_MULTICAST_LOOP */
00333   u_int32_t
00334    mcast_loopback;
00335 
00336   /* multicast address to send M-SEARCH packets */
00337   struct sockaddr_in
00338    mcast_grp_addr;
00339 
00340   /* schedule for M-SEARCH queries */
00341   aw_alarm_sched_t
00342    msearch_sched;
00343 
00344   /* max time allowed to succeed per try */
00345   aw_timestamp_t
00346    timeout;
00347 
00348 } aw_upnphandler_t ;
00349 
00360 aw_upnphandler_t *aw_create_upnphandler(u_int32_t nregexs,
00361                                         const aw_upnphandler_regex_t *regexs, 
00362                                         const byte_t *mcinterface,
00363                                         u_int32_t mcloopback,
00364                                         const aw_alarm_sched_t *sched,
00365                                         aw_logger_t *logger
00366                                         );
00367 
00372 void aw_free_upnphandler(aw_upnphandler_t *p);
00373 
00377 aw_handler_t *aw_wire_upnphandler(aw_wire_mkhandler_args_t *args);
00378 
00379 #endif
00380 
00381 
Aware 0.11.1 Copyright (C) 1998-2005 Russell Leighton (russ@elegant-software.com)