| 1 | /* INDI Server for protocol version 1.7. | 
|---|
| 2 | * Copyright (C) 2007 Elwood C. Downey ecdowney@clearskyinstitute.com | 
|---|
| 3 |  | 
|---|
| 4 | This library is free software; you can redistribute it and/or | 
|---|
| 5 | modify it under the terms of the GNU Lesser General Public | 
|---|
| 6 | License as published by the Free Software Foundation; either | 
|---|
| 7 | version 2.1 of the License, or (at your option) any later version. | 
|---|
| 8 |  | 
|---|
| 9 | This library is distributed in the hope that it will be useful, | 
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 12 | Lesser General Public License for more details. | 
|---|
| 13 |  | 
|---|
| 14 | You should have received a copy of the GNU Lesser General Public | 
|---|
| 15 | License along with this library; if not, write to the Free Software | 
|---|
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA | 
|---|
| 17 |  | 
|---|
| 18 | * argv lists names of Driver programs to run or sockets to connect for Devices. | 
|---|
| 19 | * Drivers are restarted if they exit or connection closes. | 
|---|
| 20 | * Each local Driver's stdin/out are assumed to provide INDI traffic and are | 
|---|
| 21 | *   connected here via pipes. Local Drivers' stderr are connected to our | 
|---|
| 22 | *   stderr with date stamp and driver name prepended. | 
|---|
| 23 | * We only support Drivers that advertise support for one Device. The problem | 
|---|
| 24 | *   with multiple Devices in one Driver is without a way to know what they | 
|---|
| 25 | *   _all_ are there is no way to avoid sending all messages to all Drivers. | 
|---|
| 26 | * Outbound messages are limited to Devices and Properties seen inbound. | 
|---|
| 27 | *   Messages to Devices on sockets always include Device so the chained | 
|---|
| 28 | *   indiserver will only pass back info from that Device. | 
|---|
| 29 | * All newXXX() received from one Client are echoed to all other Clients who | 
|---|
| 30 | *   have shown an interest in the same Device and property. | 
|---|
| 31 | * | 
|---|
| 32 | * Implementation notes: | 
|---|
| 33 | * | 
|---|
| 34 | * We fork each driver and open a server socket listening for INDI clients. | 
|---|
| 35 | * Then forever we listen for new clients and pass traffic between clients and | 
|---|
| 36 | * drivers, subject to optimizations based on sniffing messages for matching | 
|---|
| 37 | * Devices and Properties. Since one message might be destined to more than | 
|---|
| 38 | * one client or device, they are queued and only removed after the last | 
|---|
| 39 | * consumer is finished. XMLEle are converted to linear strings before being | 
|---|
| 40 | * sent to optimize write system calls and avoid blocking to slow clients. | 
|---|
| 41 | * Clients that get more than maxqsiz bytes behind are shut down. | 
|---|
| 42 | */ | 
|---|
| 43 |  | 
|---|
| 44 | #include <stdio.h> | 
|---|
| 45 | #include <stdlib.h> | 
|---|
| 46 | #include <stdarg.h> | 
|---|
| 47 | #include <signal.h> | 
|---|
| 48 | #include <string.h> | 
|---|
| 49 | #include <errno.h> | 
|---|
| 50 | #include <unistd.h> | 
|---|
| 51 | #include <time.h> | 
|---|
| 52 | #include <fcntl.h> | 
|---|
| 53 | #include <sys/time.h> | 
|---|
| 54 | #include <sys/types.h> | 
|---|
| 55 | #include <sys/stat.h> | 
|---|
| 56 | #include <sys/socket.h> | 
|---|
| 57 | #include <netinet/in.h> | 
|---|
| 58 | #include <netdb.h> | 
|---|
| 59 | #include <arpa/inet.h> | 
|---|
| 60 |  | 
|---|
| 61 | #include "lilxml.h" | 
|---|
| 62 | #include "indiapi.h" | 
|---|
| 63 | #include "fq.h" | 
|---|
| 64 |  | 
|---|
| 65 | #define INDIPORT        7624            /* default TCP/IP port to listen */ | 
|---|
| 66 | #define REMOTEDVR       (-1234)         /* invalid PID to flag remote drivers */ | 
|---|
| 67 | #define MAXRBUF         4096            /* max read buffering here */ | 
|---|
| 68 | #define MAXWSIZ         4096            /* max bytes/write */ | 
|---|
| 69 | #define DEFMAXQSIZ      10              /* default max q behind, MB */ | 
|---|
| 70 |  | 
|---|
| 71 | /* associate a usage count with queuded client or device message */ | 
|---|
| 72 | typedef struct { | 
|---|
| 73 | int count;                          /* number of consumers left */ | 
|---|
| 74 | unsigned int cl;                    /* content length */ | 
|---|
| 75 | char *cp;                           /* content: buf or malloced */ | 
|---|
| 76 | char buf[MAXWSIZ];          /* local buf for most messages */ | 
|---|
| 77 | } Msg; | 
|---|
| 78 |  | 
|---|
| 79 | /* BLOB handling, NEVER is the default */ | 
|---|
| 80 | typedef enum {B_NEVER=0, B_ALSO, B_ONLY} BLOBHandling; | 
|---|
| 81 |  | 
|---|
| 82 | /* device + property name */ | 
|---|
| 83 | typedef struct { | 
|---|
| 84 | char dev[MAXINDIDEVICE]; | 
|---|
| 85 | char name[MAXINDINAME]; | 
|---|
| 86 | } Property; | 
|---|
| 87 |  | 
|---|
| 88 | /* record of each snooped property */ | 
|---|
| 89 | typedef struct { | 
|---|
| 90 | Property prop; | 
|---|
| 91 | BLOBHandling blob;                  /* when to snoop BLOBs */ | 
|---|
| 92 | } Snoopee; | 
|---|
| 93 |  | 
|---|
| 94 | /* info for each connected client */ | 
|---|
| 95 | typedef struct { | 
|---|
| 96 | int active;                         /* 1 when this record is in use */ | 
|---|
| 97 | Property *props;                    /* malloced array of props we want */ | 
|---|
| 98 | int nprops;                         /* n entries in props[] */ | 
|---|
| 99 | int allprops;                       /* saw getProperties w/o device */ | 
|---|
| 100 | BLOBHandling blob;                  /* when to send setBLOBs */ | 
|---|
| 101 | int s;                              /* socket for this client */ | 
|---|
| 102 | LilXML *lp;                         /* XML parsing context */ | 
|---|
| 103 | FQ *msgq;                           /* Msg queue */ | 
|---|
| 104 | unsigned int nsent;                         /* bytes of current Msg sent so far */ | 
|---|
| 105 | } ClInfo; | 
|---|
| 106 | static ClInfo *clinfo;                  /*  malloced pool of clients */ | 
|---|
| 107 | static int nclinfo;                     /* n total (not active) */ | 
|---|
| 108 |  | 
|---|
| 109 | /* info for each connected driver */ | 
|---|
| 110 | typedef struct { | 
|---|
| 111 | const char *name;                           /* persistent malloced name */ | 
|---|
| 112 | char dev[MAXINDIDEVICE];            /* device served by this driver */ | 
|---|
| 113 | Snoopee *sprops;                    /* malloced array of props we snoop */ | 
|---|
| 114 | int nsprops;                        /* n entries in sprops[] */ | 
|---|
| 115 | int pid;                            /* process id or REMOTEDVR if remote */ | 
|---|
| 116 | int rfd;                            /* read pipe fd */ | 
|---|
| 117 | int wfd;                            /* write pipe fd */ | 
|---|
| 118 | int efd;                            /* stderr from driver, if local */ | 
|---|
| 119 | int restarts;                       /* times process has been restarted */ | 
|---|
| 120 | LilXML *lp;                         /* XML parsing context */ | 
|---|
| 121 | FQ *msgq;                           /* Msg queue */ | 
|---|
| 122 | unsigned int nsent;                 /* bytes of current Msg sent so far */ | 
|---|
| 123 | } DvrInfo; | 
|---|
| 124 | static DvrInfo *dvrinfo;                /* malloced array of drivers */ | 
|---|
| 125 | static int ndvrinfo;                    /* n total */ | 
|---|
| 126 |  | 
|---|
| 127 | static char *me;                        /* our name */ | 
|---|
| 128 | static int port = INDIPORT;             /* public INDI port */ | 
|---|
| 129 | static int verbose;                     /* chattiness */ | 
|---|
| 130 | static int lsocket;                     /* listen socket */ | 
|---|
| 131 | static char *ldir;                      /* where to log driver messages */ | 
|---|
| 132 | static int maxqsiz = (DEFMAXQSIZ*1024*1024); /* kill if these bytes behind */ | 
|---|
| 133 |  | 
|---|
| 134 | static void logStartup(int ac, char *av[]); | 
|---|
| 135 | static void usage (void); | 
|---|
| 136 | static void noZombies (void); | 
|---|
| 137 | static void noSIGPIPE (void); | 
|---|
| 138 | static void indiRun (void); | 
|---|
| 139 | static void indiListen (void); | 
|---|
| 140 | static void newClient (void); | 
|---|
| 141 | static int newClSocket (void); | 
|---|
| 142 | static void shutdownClient (ClInfo *cp); | 
|---|
| 143 | static int readFromClient (ClInfo *cp); | 
|---|
| 144 | static void startDvr (DvrInfo *dp); | 
|---|
| 145 | static void startLocalDvr (DvrInfo *dp); | 
|---|
| 146 | static void startRemoteDvr (DvrInfo *dp); | 
|---|
| 147 | static int openINDIServer (char host[], int indi_port); | 
|---|
| 148 | static void restartDvr (DvrInfo *dp); | 
|---|
| 149 | static void q2RDrivers (const char *dev, Msg *mp, XMLEle *root); | 
|---|
| 150 | static void q2SDrivers (int isblob, const char *dev, const char *name, Msg *mp, | 
|---|
| 151 | XMLEle *root); | 
|---|
| 152 | static int q2Clients (ClInfo *notme, int isblob, const char *dev, const char *name, | 
|---|
| 153 | Msg *mp, XMLEle *root); | 
|---|
| 154 | static void addSDevice (DvrInfo *dp, const char *dev, const char *name); | 
|---|
| 155 | static Snoopee *findSDevice (DvrInfo *dp, const char *dev, const char *name); | 
|---|
| 156 | static void addClDevice (ClInfo *cp, const char *dev, const char *name); | 
|---|
| 157 | static int findClDevice (ClInfo *cp, const char *dev, const char *name); | 
|---|
| 158 | static int readFromDriver (DvrInfo *dp); | 
|---|
| 159 | static int stderrFromDriver (DvrInfo *dp); | 
|---|
| 160 | static int msgQSize (FQ *q); | 
|---|
| 161 | static void setMsgXMLEle (Msg *mp, XMLEle *root); | 
|---|
| 162 | static void setMsgStr (Msg *mp, char *str); | 
|---|
| 163 | static void freeMsg (Msg *mp); | 
|---|
| 164 | static Msg *newMsg (void); | 
|---|
| 165 | static int sendClientMsg (ClInfo *cp); | 
|---|
| 166 | static int sendDriverMsg (DvrInfo *cp); | 
|---|
| 167 | static void crackBLOB (char *enableBLOB, BLOBHandling *bp); | 
|---|
| 168 | static void traceMsg (XMLEle *root); | 
|---|
| 169 | static char *indi_tstamp (char *s); | 
|---|
| 170 | static void logDMsg (XMLEle *root, const char *dev); | 
|---|
| 171 | static void Bye(void); | 
|---|
| 172 |  | 
|---|
| 173 | int | 
|---|
| 174 | main (int ac, char *av[]) | 
|---|
| 175 | { | 
|---|
| 176 | /* log startup */ | 
|---|
| 177 | logStartup(ac, av); | 
|---|
| 178 |  | 
|---|
| 179 | /* save our name */ | 
|---|
| 180 | me = av[0]; | 
|---|
| 181 |  | 
|---|
| 182 | /* crack args */ | 
|---|
| 183 | while ((--ac > 0) && ((*++av)[0] == '-')) { | 
|---|
| 184 | char *s; | 
|---|
| 185 | for (s = av[0]+1; *s != '\0'; s++) | 
|---|
| 186 | switch (*s) { | 
|---|
| 187 | case 'l': | 
|---|
| 188 | if (ac < 2) { | 
|---|
| 189 | fprintf (stderr, "-l requires log directory\n"); | 
|---|
| 190 | usage(); | 
|---|
| 191 | } | 
|---|
| 192 | ldir = *++av; | 
|---|
| 193 | ac--; | 
|---|
| 194 | break; | 
|---|
| 195 | case 'm': | 
|---|
| 196 | if (ac < 2) { | 
|---|
| 197 | fprintf (stderr, "-m requires max MB behind\n"); | 
|---|
| 198 | usage(); | 
|---|
| 199 | } | 
|---|
| 200 | maxqsiz = 1024*1024*atoi(*++av); | 
|---|
| 201 | ac--; | 
|---|
| 202 | break; | 
|---|
| 203 | case 'p': | 
|---|
| 204 | if (ac < 2) { | 
|---|
| 205 | fprintf (stderr, "-p requires port value\n"); | 
|---|
| 206 | usage(); | 
|---|
| 207 | } | 
|---|
| 208 | port = atoi(*++av); | 
|---|
| 209 | ac--; | 
|---|
| 210 | break; | 
|---|
| 211 | case 'v': | 
|---|
| 212 | verbose++; | 
|---|
| 213 | break; | 
|---|
| 214 | default: | 
|---|
| 215 | usage(); | 
|---|
| 216 | } | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 | /* at this point there are ac args in av[] to name our drivers */ | 
|---|
| 220 | if (ac == 0) | 
|---|
| 221 | usage(); | 
|---|
| 222 |  | 
|---|
| 223 | /* take care of some unixisms */ | 
|---|
| 224 | noZombies(); | 
|---|
| 225 | noSIGPIPE(); | 
|---|
| 226 |  | 
|---|
| 227 | /* realloc seed for client pool */ | 
|---|
| 228 | clinfo = (ClInfo *) malloc (1); | 
|---|
| 229 | nclinfo = 0; | 
|---|
| 230 |  | 
|---|
| 231 | /* create driver info array all at once since size never changes */ | 
|---|
| 232 | ndvrinfo = ac; | 
|---|
| 233 | dvrinfo = (DvrInfo *) calloc (ndvrinfo, sizeof(DvrInfo)); | 
|---|
| 234 |  | 
|---|
| 235 | /* start each driver */ | 
|---|
| 236 | while (ac-- > 0) { | 
|---|
| 237 | dvrinfo[ac].name = *av++; | 
|---|
| 238 | startDvr (&dvrinfo[ac]); | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 | /* announce we are online */ | 
|---|
| 242 | indiListen(); | 
|---|
| 243 |  | 
|---|
| 244 | /* handle new clients and all io */ | 
|---|
| 245 | while (1) | 
|---|
| 246 | indiRun(); | 
|---|
| 247 |  | 
|---|
| 248 | /* whoa! */ | 
|---|
| 249 | fprintf (stderr, "unexpected return from main\n"); | 
|---|
| 250 | return (1); | 
|---|
| 251 | } | 
|---|
| 252 |  | 
|---|
| 253 | /* record we have started and our args */ | 
|---|
| 254 | static void | 
|---|
| 255 | logStartup(int ac, char *av[]) | 
|---|
| 256 | { | 
|---|
| 257 | int i; | 
|---|
| 258 |  | 
|---|
| 259 | fprintf (stderr, "%s: startup: ", indi_tstamp(NULL)); | 
|---|
| 260 | for (i = 0; i < ac; i++) | 
|---|
| 261 | fprintf (stderr, "%s ", av[i]); | 
|---|
| 262 | fprintf (stderr, "\n"); | 
|---|
| 263 | } | 
|---|
| 264 |  | 
|---|
| 265 | /* print usage message and exit (2) */ | 
|---|
| 266 | static void | 
|---|
| 267 | usage(void) | 
|---|
| 268 | { | 
|---|
| 269 | fprintf (stderr, "Usage: %s [options] driver [driver ...]\n", me); | 
|---|
| 270 | fprintf (stderr, "Purpose: server for local and remote INDI drivers\n"); | 
|---|
| 271 | fprintf (stderr, "Code %s. Protocol %g.\n", "$Revision: 726523 $", INDIV); | 
|---|
| 272 | fprintf (stderr, "Options:\n"); | 
|---|
| 273 | fprintf (stderr, " -l d  : log driver messages to <d>/YYYY-MM-DD.islog\n"); | 
|---|
| 274 | fprintf (stderr, " -m m  : kill client if gets more than this many MB behind, default %d\n", DEFMAXQSIZ); | 
|---|
| 275 | fprintf (stderr, " -p p  : alternate IP port, default %d\n", INDIPORT); | 
|---|
| 276 | fprintf (stderr, " -v    : show key events, no traffic\n"); | 
|---|
| 277 | fprintf (stderr, " -vv   : -v + key message content\n"); | 
|---|
| 278 | fprintf (stderr, " -vvv  : -vv + complete xml\n"); | 
|---|
| 279 | fprintf (stderr, "driver : executable or device@host[:port]\n"); | 
|---|
| 280 |  | 
|---|
| 281 | exit (2); | 
|---|
| 282 | } | 
|---|
| 283 |  | 
|---|
| 284 | /* arrange for no zombies if drivers die */ | 
|---|
| 285 | static void | 
|---|
| 286 | noZombies() | 
|---|
| 287 | { | 
|---|
| 288 | struct sigaction sa; | 
|---|
| 289 | sa.sa_handler = SIG_IGN; | 
|---|
| 290 | sigemptyset(&sa.sa_mask); | 
|---|
| 291 | #ifdef SA_NOCLDWAIT | 
|---|
| 292 | sa.sa_flags = SA_NOCLDWAIT; | 
|---|
| 293 | #else | 
|---|
| 294 | sa.sa_flags = 0; | 
|---|
| 295 | #endif | 
|---|
| 296 | (void)sigaction(SIGCHLD, &sa, NULL); | 
|---|
| 297 | } | 
|---|
| 298 |  | 
|---|
| 299 | /* turn off SIGPIPE on bad write so we can handle it inline */ | 
|---|
| 300 | static void | 
|---|
| 301 | noSIGPIPE() | 
|---|
| 302 | { | 
|---|
| 303 | struct sigaction sa; | 
|---|
| 304 | sa.sa_handler = SIG_IGN; | 
|---|
| 305 | sigemptyset(&sa.sa_mask); | 
|---|
| 306 | (void)sigaction(SIGPIPE, &sa, NULL); | 
|---|
| 307 | } | 
|---|
| 308 |  | 
|---|
| 309 | /* start the given INDI driver process or connection. | 
|---|
| 310 | * exit if trouble. | 
|---|
| 311 | */ | 
|---|
| 312 | static void | 
|---|
| 313 | startDvr (DvrInfo *dp) | 
|---|
| 314 | { | 
|---|
| 315 | if (strchr (dp->name, '@')) | 
|---|
| 316 | startRemoteDvr (dp); | 
|---|
| 317 | else | 
|---|
| 318 | startLocalDvr (dp); | 
|---|
| 319 | } | 
|---|
| 320 |  | 
|---|
| 321 | /* start the given local INDI driver process. | 
|---|
| 322 | * exit if trouble. | 
|---|
| 323 | */ | 
|---|
| 324 | static void | 
|---|
| 325 | startLocalDvr (DvrInfo *dp) | 
|---|
| 326 | { | 
|---|
| 327 | Msg *mp; | 
|---|
| 328 | char buf[1024]; | 
|---|
| 329 | int rp[2], wp[2], ep[2]; | 
|---|
| 330 | int pid; | 
|---|
| 331 |  | 
|---|
| 332 | /* build three pipes: r, w and error*/ | 
|---|
| 333 | if (pipe (rp) < 0) { | 
|---|
| 334 | fprintf (stderr, "%s: read pipe: %s\n", indi_tstamp(NULL), | 
|---|
| 335 | strerror(errno)); | 
|---|
| 336 | Bye(); | 
|---|
| 337 | } | 
|---|
| 338 | if (pipe (wp) < 0) { | 
|---|
| 339 | fprintf (stderr, "%s: write pipe: %s\n", indi_tstamp(NULL), | 
|---|
| 340 | strerror(errno)); | 
|---|
| 341 | Bye(); | 
|---|
| 342 | } | 
|---|
| 343 | if (pipe (ep) < 0) { | 
|---|
| 344 | fprintf (stderr, "%s: stderr pipe: %s\n", indi_tstamp(NULL), | 
|---|
| 345 | strerror(errno)); | 
|---|
| 346 | Bye(); | 
|---|
| 347 | } | 
|---|
| 348 |  | 
|---|
| 349 | /* fork&exec new process */ | 
|---|
| 350 | pid = fork(); | 
|---|
| 351 | if (pid < 0) { | 
|---|
| 352 | fprintf (stderr, "%s: fork: %s\n", indi_tstamp(NULL), strerror(errno)); | 
|---|
| 353 | Bye(); | 
|---|
| 354 | } | 
|---|
| 355 | if (pid == 0) { | 
|---|
| 356 | /* child: exec name */ | 
|---|
| 357 | int fd; | 
|---|
| 358 |  | 
|---|
| 359 | /* rig up pipes */ | 
|---|
| 360 | dup2 (wp[0], 0);    /* driver stdin reads from wp[0] */ | 
|---|
| 361 | dup2 (rp[1], 1);    /* driver stdout writes to rp[1] */ | 
|---|
| 362 | dup2 (ep[1], 2);    /* driver stderr writes to e[]1] */ | 
|---|
| 363 | for (fd = 3; fd < 100; fd++) | 
|---|
| 364 | (void) close (fd); | 
|---|
| 365 |  | 
|---|
| 366 | /* go -- should never return */ | 
|---|
| 367 | execlp (dp->name, dp->name, NULL); | 
|---|
| 368 | fprintf (stderr, "%s: Driver %s: execlp: %s\n", indi_tstamp(NULL), | 
|---|
| 369 | dp->name, strerror(errno)); | 
|---|
| 370 | _exit (1);  /* parent will notice EOF shortly */ | 
|---|
| 371 | } | 
|---|
| 372 |  | 
|---|
| 373 | /* don't need child's side of pipes */ | 
|---|
| 374 | close (wp[0]); | 
|---|
| 375 | close (rp[1]); | 
|---|
| 376 | close (ep[1]); | 
|---|
| 377 |  | 
|---|
| 378 | /* record pid, io channels, init lp and snoop list */ | 
|---|
| 379 | dp->pid = pid; | 
|---|
| 380 | dp->rfd = rp[0]; | 
|---|
| 381 | dp->wfd = wp[1]; | 
|---|
| 382 | dp->efd = ep[0]; | 
|---|
| 383 | dp->lp = newLilXML(); | 
|---|
| 384 | dp->msgq = newFQ(1); | 
|---|
| 385 | dp->sprops = (Snoopee*) malloc (1);     /* seed for realloc */ | 
|---|
| 386 | dp->nsprops = 0; | 
|---|
| 387 | dp->nsent = 0; | 
|---|
| 388 |  | 
|---|
| 389 | /* first message primes driver to report its properties -- dev known | 
|---|
| 390 | * if restarting | 
|---|
| 391 | */ | 
|---|
| 392 | mp = newMsg(); | 
|---|
| 393 | pushFQ (dp->msgq, mp); | 
|---|
| 394 | if (dp->dev[0]) | 
|---|
| 395 | sprintf (buf, "<getProperties device='%s' version='%g'/>\n", | 
|---|
| 396 | dp->dev, INDIV); | 
|---|
| 397 | else | 
|---|
| 398 | sprintf (buf, "<getProperties version='%g'/>\n", INDIV); | 
|---|
| 399 | setMsgStr (mp, buf); | 
|---|
| 400 | mp->count++; | 
|---|
| 401 |  | 
|---|
| 402 | if (verbose > 0) | 
|---|
| 403 | fprintf (stderr, "%s: Driver %s: pid=%d rfd=%d wfd=%d efd=%d\n", | 
|---|
| 404 | indi_tstamp(NULL), dp->name, dp->pid, dp->rfd, dp->wfd, dp->efd); | 
|---|
| 405 | } | 
|---|
| 406 |  | 
|---|
| 407 | /* start the given remote INDI driver connection. | 
|---|
| 408 | * exit if trouble. | 
|---|
| 409 | */ | 
|---|
| 410 | static void | 
|---|
| 411 | startRemoteDvr (DvrInfo *dp) | 
|---|
| 412 | { | 
|---|
| 413 | Msg *mp; | 
|---|
| 414 | char dev[1024]; | 
|---|
| 415 | char host[1024]; | 
|---|
| 416 | char buf[1024]; | 
|---|
| 417 | int indi_port, sockfd; | 
|---|
| 418 |  | 
|---|
| 419 | /* extract host and port */ | 
|---|
| 420 | indi_port = INDIPORT; | 
|---|
| 421 | if (sscanf (dp->name, "%[^@]@%[^:]:%d", dev, host, &indi_port) < 2) { | 
|---|
| 422 | fprintf (stderr, "Bad remote device syntax: %s\n", dp->name); | 
|---|
| 423 | Bye(); | 
|---|
| 424 | } | 
|---|
| 425 |  | 
|---|
| 426 | /* connect */ | 
|---|
| 427 | sockfd = openINDIServer (host, indi_port); | 
|---|
| 428 |  | 
|---|
| 429 | /* record flag pid, io channels, init lp and snoop list */ | 
|---|
| 430 | dp->pid = REMOTEDVR; | 
|---|
| 431 | dp->rfd = sockfd; | 
|---|
| 432 | dp->wfd = sockfd; | 
|---|
| 433 | dp->lp = newLilXML(); | 
|---|
| 434 | dp->msgq = newFQ(1); | 
|---|
| 435 | dp->sprops = (Snoopee*) malloc (1);     /* seed for realloc */ | 
|---|
| 436 | dp->nsprops = 0; | 
|---|
| 437 | dp->nsent = 0; | 
|---|
| 438 |  | 
|---|
| 439 | /* N.B. storing name now is key to limiting outbound traffic to this | 
|---|
| 440 | * dev. | 
|---|
| 441 | */ | 
|---|
| 442 | strncpy (dp->dev, dev, MAXINDIDEVICE-1); | 
|---|
| 443 | dp->dev[MAXINDIDEVICE-1] = '\0'; | 
|---|
| 444 |  | 
|---|
| 445 | /* Sending getProperties with device lets remote server limit its | 
|---|
| 446 | * outbound (and our inbound) traffic on this socket to this device. | 
|---|
| 447 | */ | 
|---|
| 448 | mp = newMsg(); | 
|---|
| 449 | pushFQ (dp->msgq, mp); | 
|---|
| 450 | sprintf (buf, "<getProperties device='%s' version='%g'/>\n", | 
|---|
| 451 | dp->dev, INDIV); | 
|---|
| 452 | setMsgStr (mp, buf); | 
|---|
| 453 | mp->count++; | 
|---|
| 454 |  | 
|---|
| 455 | if (verbose > 0) | 
|---|
| 456 | fprintf (stderr, "%s: Driver %s: socket=%d\n", indi_tstamp(NULL), | 
|---|
| 457 | dp->name, sockfd); | 
|---|
| 458 | } | 
|---|
| 459 |  | 
|---|
| 460 | /* open a connection to the given host and port or die. | 
|---|
| 461 | * return socket fd. | 
|---|
| 462 | */ | 
|---|
| 463 | static int | 
|---|
| 464 | openINDIServer (char host[], int indi_port) | 
|---|
| 465 | { | 
|---|
| 466 | struct sockaddr_in serv_addr; | 
|---|
| 467 | struct hostent *hp; | 
|---|
| 468 | int sockfd; | 
|---|
| 469 |  | 
|---|
| 470 | /* lookup host address */ | 
|---|
| 471 | hp = gethostbyname (host); | 
|---|
| 472 | if (!hp) { | 
|---|
| 473 | fprintf (stderr, "gethostbyname(%s): %s\n", host, strerror(errno)); | 
|---|
| 474 | Bye(); | 
|---|
| 475 | } | 
|---|
| 476 |  | 
|---|
| 477 | /* create a socket to the INDI server */ | 
|---|
| 478 | (void) memset ((char *)&serv_addr, 0, sizeof(serv_addr)); | 
|---|
| 479 | serv_addr.sin_family = AF_INET; | 
|---|
| 480 | serv_addr.sin_addr.s_addr = | 
|---|
| 481 | ((struct in_addr *)(hp->h_addr_list[0]))->s_addr; | 
|---|
| 482 | serv_addr.sin_port = htons(indi_port); | 
|---|
| 483 | if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0) { | 
|---|
| 484 | fprintf (stderr, "socket(%s,%d): %s\n", host, indi_port,strerror(errno)); | 
|---|
| 485 | Bye(); | 
|---|
| 486 | } | 
|---|
| 487 |  | 
|---|
| 488 | /* connect */ | 
|---|
| 489 | if (connect (sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0){ | 
|---|
| 490 | fprintf (stderr, "connect(%s,%d): %s\n", host,indi_port,strerror(errno)); | 
|---|
| 491 | Bye(); | 
|---|
| 492 | } | 
|---|
| 493 |  | 
|---|
| 494 | /* ok */ | 
|---|
| 495 | return (sockfd); | 
|---|
| 496 | } | 
|---|
| 497 |  | 
|---|
| 498 | /* create the public INDI Driver endpoint lsocket on port. | 
|---|
| 499 | * return server socket else exit. | 
|---|
| 500 | */ | 
|---|
| 501 | static void | 
|---|
| 502 | indiListen () | 
|---|
| 503 | { | 
|---|
| 504 | struct sockaddr_in serv_socket; | 
|---|
| 505 | int sfd; | 
|---|
| 506 | int reuse = 1; | 
|---|
| 507 |  | 
|---|
| 508 | /* make socket endpoint */ | 
|---|
| 509 | if ((sfd = socket (AF_INET, SOCK_STREAM, 0)) < 0) { | 
|---|
| 510 | fprintf (stderr, "%s: socket: %s\n", indi_tstamp(NULL), strerror(errno)); | 
|---|
| 511 | Bye(); | 
|---|
| 512 | } | 
|---|
| 513 |  | 
|---|
| 514 | /* bind to given port for any IP address */ | 
|---|
| 515 | memset (&serv_socket, 0, sizeof(serv_socket)); | 
|---|
| 516 | serv_socket.sin_family = AF_INET; | 
|---|
| 517 | #ifdef SSH_TUNNEL | 
|---|
| 518 | serv_socket.sin_addr.s_addr = htonl (INADDR_LOOPBACK); | 
|---|
| 519 | #else | 
|---|
| 520 | serv_socket.sin_addr.s_addr = htonl (INADDR_ANY); | 
|---|
| 521 | #endif | 
|---|
| 522 | serv_socket.sin_port = htons ((unsigned short)port); | 
|---|
| 523 | if (setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0){ | 
|---|
| 524 | fprintf (stderr, "%s: setsockopt: %s\n", indi_tstamp(NULL), | 
|---|
| 525 | strerror(errno)); | 
|---|
| 526 | Bye(); | 
|---|
| 527 | } | 
|---|
| 528 | if (bind(sfd,(struct sockaddr*)&serv_socket,sizeof(serv_socket)) < 0){ | 
|---|
| 529 | fprintf (stderr, "%s: bind: %s\n", indi_tstamp(NULL), strerror(errno)); | 
|---|
| 530 | Bye(); | 
|---|
| 531 | } | 
|---|
| 532 |  | 
|---|
| 533 | /* willing to accept connections with a backlog of 5 pending */ | 
|---|
| 534 | if (listen (sfd, 5) < 0) { | 
|---|
| 535 | fprintf (stderr, "%s: listen: %s\n", indi_tstamp(NULL), strerror(errno)); | 
|---|
| 536 | Bye(); | 
|---|
| 537 | } | 
|---|
| 538 |  | 
|---|
| 539 | /* ok */ | 
|---|
| 540 | lsocket = sfd; | 
|---|
| 541 | if (verbose > 0) | 
|---|
| 542 | fprintf (stderr, "%s: listening to port %d on fd %d\n", | 
|---|
| 543 | indi_tstamp(NULL), port, sfd); | 
|---|
| 544 | } | 
|---|
| 545 |  | 
|---|
| 546 | /* service traffic from clients and drivers */ | 
|---|
| 547 | static void | 
|---|
| 548 | indiRun(void) | 
|---|
| 549 | { | 
|---|
| 550 | fd_set rs, ws; | 
|---|
| 551 | int maxfd; | 
|---|
| 552 | int i, s; | 
|---|
| 553 |  | 
|---|
| 554 | /* init with no writers or readers */ | 
|---|
| 555 | FD_ZERO(&ws); | 
|---|
| 556 | FD_ZERO(&rs); | 
|---|
| 557 |  | 
|---|
| 558 | /* always listen for new clients */ | 
|---|
| 559 | FD_SET(lsocket, &rs); | 
|---|
| 560 | maxfd = lsocket; | 
|---|
| 561 |  | 
|---|
| 562 | /* add all client readers and client writers with work to send */ | 
|---|
| 563 | for (i = 0; i < nclinfo; i++) { | 
|---|
| 564 | ClInfo *cp = &clinfo[i]; | 
|---|
| 565 | if (cp->active) { | 
|---|
| 566 | FD_SET(cp->s, &rs); | 
|---|
| 567 | if (nFQ(cp->msgq) > 0) | 
|---|
| 568 | FD_SET(cp->s, &ws); | 
|---|
| 569 | if (cp->s > maxfd) | 
|---|
| 570 | maxfd = cp->s; | 
|---|
| 571 | } | 
|---|
| 572 | } | 
|---|
| 573 |  | 
|---|
| 574 | /* add all driver readers and driver writers with work to send */ | 
|---|
| 575 | for (i = 0; i < ndvrinfo; i++) { | 
|---|
| 576 | DvrInfo *dp = &dvrinfo[i]; | 
|---|
| 577 | FD_SET(dp->rfd, &rs); | 
|---|
| 578 | if (dp->rfd > maxfd) | 
|---|
| 579 | maxfd = dp->rfd; | 
|---|
| 580 | if (dp->pid != REMOTEDVR) { | 
|---|
| 581 | FD_SET(dp->efd, &rs); | 
|---|
| 582 | if (dp->efd > maxfd) | 
|---|
| 583 | maxfd = dp->efd; | 
|---|
| 584 | } | 
|---|
| 585 | if (nFQ(dp->msgq) > 0) { | 
|---|
| 586 | FD_SET(dp->wfd, &ws); | 
|---|
| 587 | if (dp->wfd > maxfd) | 
|---|
| 588 | maxfd = dp->wfd; | 
|---|
| 589 | } | 
|---|
| 590 | } | 
|---|
| 591 |  | 
|---|
| 592 | /* wait for action */ | 
|---|
| 593 | s = select (maxfd+1, &rs, &ws, NULL, NULL); | 
|---|
| 594 | if (s < 0) { | 
|---|
| 595 | fprintf (stderr, "%s: select(%d): %s\n", indi_tstamp(NULL), maxfd+1, | 
|---|
| 596 | strerror(errno)); | 
|---|
| 597 | Bye(); | 
|---|
| 598 | } | 
|---|
| 599 |  | 
|---|
| 600 | /* new client? */ | 
|---|
| 601 | if (s > 0 && FD_ISSET(lsocket, &rs)) { | 
|---|
| 602 | newClient(); | 
|---|
| 603 | s--; | 
|---|
| 604 | } | 
|---|
| 605 |  | 
|---|
| 606 | /* message to/from client? */ | 
|---|
| 607 | for (i = 0; s > 0 && i < nclinfo; i++) { | 
|---|
| 608 | ClInfo *cp = &clinfo[i]; | 
|---|
| 609 | if (cp->active) { | 
|---|
| 610 | if (FD_ISSET(cp->s, &rs)) { | 
|---|
| 611 | if (readFromClient(cp) < 0) | 
|---|
| 612 | return; /* fds effected */ | 
|---|
| 613 | s--; | 
|---|
| 614 | } | 
|---|
| 615 | if (s > 0 && FD_ISSET(cp->s, &ws)) { | 
|---|
| 616 | if (sendClientMsg(cp) < 0) | 
|---|
| 617 | return; /* fds effected */ | 
|---|
| 618 | s--; | 
|---|
| 619 | } | 
|---|
| 620 | } | 
|---|
| 621 | } | 
|---|
| 622 |  | 
|---|
| 623 | /* message to/from driver? */ | 
|---|
| 624 | for (i = 0; s > 0 && i < ndvrinfo; i++) { | 
|---|
| 625 | DvrInfo *dp = &dvrinfo[i]; | 
|---|
| 626 | if (dp->pid != REMOTEDVR && FD_ISSET(dp->efd, &rs)) { | 
|---|
| 627 | if (stderrFromDriver(dp) < 0) | 
|---|
| 628 | return;     /* fds effected */ | 
|---|
| 629 | s--; | 
|---|
| 630 | } | 
|---|
| 631 | if (s > 0 && FD_ISSET(dp->rfd, &rs)) { | 
|---|
| 632 | if (readFromDriver(dp) < 0) | 
|---|
| 633 | return;     /* fds effected */ | 
|---|
| 634 | s--; | 
|---|
| 635 | } | 
|---|
| 636 | if (s > 0 && FD_ISSET(dp->wfd, &ws) && nFQ(dp->msgq) > 0) { | 
|---|
| 637 | if (sendDriverMsg(dp) < 0) | 
|---|
| 638 | return;     /* fds effected */ | 
|---|
| 639 | s--; | 
|---|
| 640 | } | 
|---|
| 641 | } | 
|---|
| 642 | } | 
|---|
| 643 |  | 
|---|
| 644 | /* prepare for new client arriving on lsocket. | 
|---|
| 645 | * exit if trouble. | 
|---|
| 646 | */ | 
|---|
| 647 | static void | 
|---|
| 648 | newClient() | 
|---|
| 649 | { | 
|---|
| 650 | ClInfo *cp = NULL; | 
|---|
| 651 | int s, cli; | 
|---|
| 652 |  | 
|---|
| 653 | /* assign new socket */ | 
|---|
| 654 | s = newClSocket (); | 
|---|
| 655 |  | 
|---|
| 656 | /* try to reuse a clinfo slot, else add one */ | 
|---|
| 657 | for (cli = 0; cli < nclinfo; cli++) | 
|---|
| 658 | if (!(cp = &clinfo[cli])->active) | 
|---|
| 659 | break; | 
|---|
| 660 | if (cli == nclinfo) { | 
|---|
| 661 | /* grow clinfo */ | 
|---|
| 662 | clinfo = (ClInfo *) realloc (clinfo, (nclinfo+1)*sizeof(ClInfo)); | 
|---|
| 663 | if (!clinfo) { | 
|---|
| 664 | fprintf (stderr, "no memory for new client\n"); | 
|---|
| 665 | Bye(); | 
|---|
| 666 | } | 
|---|
| 667 | cp = &clinfo[nclinfo++]; | 
|---|
| 668 | } | 
|---|
| 669 |  | 
|---|
| 670 | /* rig up new clinfo entry */ | 
|---|
| 671 | memset (cp, 0, sizeof(*cp)); | 
|---|
| 672 | cp->active = 1; | 
|---|
| 673 | cp->s = s; | 
|---|
| 674 | cp->lp = newLilXML(); | 
|---|
| 675 | cp->msgq = newFQ(1); | 
|---|
| 676 | cp->props = malloc (1); | 
|---|
| 677 | cp->nsent = 0; | 
|---|
| 678 |  | 
|---|
| 679 | if (verbose > 0) { | 
|---|
| 680 | struct sockaddr_in addr; | 
|---|
| 681 | socklen_t len = sizeof(addr); | 
|---|
| 682 | getpeername(s, (struct sockaddr*)&addr, &len); | 
|---|
| 683 | fprintf(stderr,"%s: Client %d: new arrival from %s:%d - welcome!\n", | 
|---|
| 684 | indi_tstamp(NULL), cp->s, inet_ntoa(addr.sin_addr), | 
|---|
| 685 | ntohs(addr.sin_port)); | 
|---|
| 686 | } | 
|---|
| 687 | } | 
|---|
| 688 |  | 
|---|
| 689 | /* read more from the given client, send to each appropriate driver when see | 
|---|
| 690 | * xml closure. also send all newXXX() to all other interested clients. | 
|---|
| 691 | * return -1 if had to shut down anything, else 0. | 
|---|
| 692 | */ | 
|---|
| 693 | static int | 
|---|
| 694 | readFromClient (ClInfo *cp) | 
|---|
| 695 | { | 
|---|
| 696 | char buf[MAXRBUF]; | 
|---|
| 697 | int shutany = 0; | 
|---|
| 698 | int i, nr; | 
|---|
| 699 |  | 
|---|
| 700 | /* read client */ | 
|---|
| 701 | nr = read (cp->s, buf, sizeof(buf)); | 
|---|
| 702 | if (nr <= 0) { | 
|---|
| 703 | if (nr < 0) | 
|---|
| 704 | fprintf (stderr, "%s: Client %d: read: %s\n", indi_tstamp(NULL), | 
|---|
| 705 | cp->s, strerror(errno)); | 
|---|
| 706 | else if (verbose > 0) | 
|---|
| 707 | fprintf (stderr, "%s: Client %d: read EOF\n", indi_tstamp(NULL), | 
|---|
| 708 | cp->s); | 
|---|
| 709 | shutdownClient (cp); | 
|---|
| 710 | return (-1); | 
|---|
| 711 | } | 
|---|
| 712 |  | 
|---|
| 713 | /* process XML, sending when find closure */ | 
|---|
| 714 | for (i = 0; i < nr; i++) { | 
|---|
| 715 | char err[1024]; | 
|---|
| 716 | XMLEle *root = readXMLEle (cp->lp, buf[i], err); | 
|---|
| 717 | if (root) { | 
|---|
| 718 | char *roottag = tagXMLEle(root); | 
|---|
| 719 | const char *dev = findXMLAttValu (root, "device"); | 
|---|
| 720 | const char *name = findXMLAttValu (root, "name"); | 
|---|
| 721 | int isblob = !strcmp (tagXMLEle(root), "setBLOBVector"); | 
|---|
| 722 | Msg *mp; | 
|---|
| 723 |  | 
|---|
| 724 | if (verbose > 2) { | 
|---|
| 725 | fprintf (stderr, "%s: Client %d: read ",indi_tstamp(NULL),cp->s); | 
|---|
| 726 | traceMsg (root); | 
|---|
| 727 | } else if (verbose > 1) { | 
|---|
| 728 | fprintf (stderr, "%s: Client %d: read <%s device='%s' name='%s'>\n", | 
|---|
| 729 | indi_tstamp(NULL), cp->s, tagXMLEle(root), | 
|---|
| 730 | findXMLAttValu (root, "device"), | 
|---|
| 731 | findXMLAttValu (root, "name")); | 
|---|
| 732 | } | 
|---|
| 733 |  | 
|---|
| 734 | /* snag interested properties. | 
|---|
| 735 | * N.B. don't open to alldevs if seen specific dev already, else | 
|---|
| 736 | *   remote client connections start returning too much. | 
|---|
| 737 | */ | 
|---|
| 738 | if (dev[0]) | 
|---|
| 739 | addClDevice (cp, dev, name); | 
|---|
| 740 | else if (!strcmp (roottag, "getProperties") && !cp->nprops) | 
|---|
| 741 | cp->allprops = 1; | 
|---|
| 742 |  | 
|---|
| 743 | /* snag enableBLOB -- send to remote drivers too */ | 
|---|
| 744 | if (!strcmp (roottag, "enableBLOB")) | 
|---|
| 745 | crackBLOB (pcdataXMLEle(root), &cp->blob); | 
|---|
| 746 |  | 
|---|
| 747 | /* build a new message -- set content iff anyone cares */ | 
|---|
| 748 | mp = newMsg(); | 
|---|
| 749 |  | 
|---|
| 750 | /* send message to driver(s) responsible for dev */ | 
|---|
| 751 | q2RDrivers (dev, mp, root); | 
|---|
| 752 |  | 
|---|
| 753 | /* echo new* commands back to other clients */ | 
|---|
| 754 | if (!strncmp (roottag, "new", 3)) { | 
|---|
| 755 | if (q2Clients (cp, isblob, dev, name, mp, root) < 0) | 
|---|
| 756 | shutany++; | 
|---|
| 757 | } | 
|---|
| 758 |  | 
|---|
| 759 | /* set message content if anyone cares else forget it */ | 
|---|
| 760 | if (mp->count > 0) | 
|---|
| 761 | setMsgXMLEle (mp, root); | 
|---|
| 762 | else | 
|---|
| 763 | freeMsg (mp); | 
|---|
| 764 | delXMLEle (root); | 
|---|
| 765 |  | 
|---|
| 766 | } else if (err[0]) { | 
|---|
| 767 | char *ts = indi_tstamp(NULL); | 
|---|
| 768 | fprintf (stderr, "%s: Client %d: XML error: %s\n", ts, | 
|---|
| 769 | cp->s, err); | 
|---|
| 770 | fprintf (stderr, "%s: Client %d: XML read: %.*s\n", ts, | 
|---|
| 771 | cp->s, nr, buf); | 
|---|
| 772 | shutdownClient (cp); | 
|---|
| 773 | return (-1); | 
|---|
| 774 | } | 
|---|
| 775 | } | 
|---|
| 776 |  | 
|---|
| 777 | return (shutany ? -1 : 0); | 
|---|
| 778 | } | 
|---|
| 779 |  | 
|---|
| 780 | /* read more from the given driver, send to each interested client when see | 
|---|
| 781 | * xml closure. if driver dies, try restarting. | 
|---|
| 782 | * return 0 if ok else -1 if had to shut down anything. | 
|---|
| 783 | */ | 
|---|
| 784 | static int | 
|---|
| 785 | readFromDriver (DvrInfo *dp) | 
|---|
| 786 | { | 
|---|
| 787 | char buf[MAXRBUF]; | 
|---|
| 788 | int shutany = 0; | 
|---|
| 789 | int i, nr; | 
|---|
| 790 |  | 
|---|
| 791 | /* read driver */ | 
|---|
| 792 | nr = read (dp->rfd, buf, sizeof(buf)); | 
|---|
| 793 | if (nr <= 0) { | 
|---|
| 794 | if (nr < 0) | 
|---|
| 795 | fprintf (stderr, "%s: Driver %s: stdin %s\n", indi_tstamp(NULL), | 
|---|
| 796 | dp->name, strerror(errno)); | 
|---|
| 797 | else | 
|---|
| 798 | fprintf (stderr, "%s: Driver %s: stdin EOF\n", | 
|---|
| 799 | indi_tstamp(NULL), dp->name); | 
|---|
| 800 | restartDvr (dp); | 
|---|
| 801 | return (-1); | 
|---|
| 802 | } | 
|---|
| 803 |  | 
|---|
| 804 | /* process XML, sending when find closure */ | 
|---|
| 805 | for (i = 0; i < nr; i++) { | 
|---|
| 806 | char err[1024]; | 
|---|
| 807 | XMLEle *root = readXMLEle (dp->lp, buf[i], err); | 
|---|
| 808 | if (root) { | 
|---|
| 809 | char *roottag = tagXMLEle(root); | 
|---|
| 810 | const char *dev = findXMLAttValu (root, "device"); | 
|---|
| 811 | const char *name = findXMLAttValu (root, "name"); | 
|---|
| 812 | int isblob = !strcmp (tagXMLEle(root), "setBLOBVector"); | 
|---|
| 813 | Msg *mp; | 
|---|
| 814 |  | 
|---|
| 815 | if (verbose > 2) { | 
|---|
| 816 | fprintf(stderr, "%s: Driver %s: read ", indi_tstamp(0),dp->name); | 
|---|
| 817 | traceMsg (root); | 
|---|
| 818 | } else if (verbose > 1) { | 
|---|
| 819 | fprintf (stderr, "%s: Driver %s: read <%s device='%s' name='%s'>\n", | 
|---|
| 820 | indi_tstamp(NULL), dp->name, tagXMLEle(root), | 
|---|
| 821 | findXMLAttValu (root, "device"), | 
|---|
| 822 | findXMLAttValu (root, "name")); | 
|---|
| 823 | } | 
|---|
| 824 |  | 
|---|
| 825 | /* that's all if driver is just registering a snoop */ | 
|---|
| 826 | if (!strcmp (roottag, "getProperties")) { | 
|---|
| 827 | addSDevice (dp, dev, name); | 
|---|
| 828 | delXMLEle (root); | 
|---|
| 829 | continue; | 
|---|
| 830 | } | 
|---|
| 831 |  | 
|---|
| 832 | /* that's all if driver is just registering a BLOB mode */ | 
|---|
| 833 | if (!strcmp (roottag, "enableBLOB")) { | 
|---|
| 834 | Snoopee *sp = findSDevice (dp, dev, name); | 
|---|
| 835 | if (sp) | 
|---|
| 836 | crackBLOB (pcdataXMLEle (root), &sp->blob); | 
|---|
| 837 | delXMLEle (root); | 
|---|
| 838 | continue; | 
|---|
| 839 | } | 
|---|
| 840 |  | 
|---|
| 841 | /* snag device name if not known yet */ | 
|---|
| 842 | if (!dp->dev[0] && dev[0]) { | 
|---|
| 843 | strncpy (dp->dev, dev, MAXINDIDEVICE-1); | 
|---|
| 844 | dp->dev[MAXINDIDEVICE-1] = '\0'; | 
|---|
| 845 | } | 
|---|
| 846 |  | 
|---|
| 847 | /* log messages if any and wanted */ | 
|---|
| 848 | if (ldir) | 
|---|
| 849 | logDMsg (root, dev); | 
|---|
| 850 |  | 
|---|
| 851 | /* build a new message -- set content iff anyone cares */ | 
|---|
| 852 | mp = newMsg(); | 
|---|
| 853 |  | 
|---|
| 854 | /* send to interested clients */ | 
|---|
| 855 | if (q2Clients (NULL, isblob, dev, name, mp, root) < 0) | 
|---|
| 856 | shutany++; | 
|---|
| 857 |  | 
|---|
| 858 | /* send to snooping drivers */ | 
|---|
| 859 | q2SDrivers (isblob, dev, name, mp, root); | 
|---|
| 860 |  | 
|---|
| 861 | /* set message content if anyone cares else forget it */ | 
|---|
| 862 | if (mp->count > 0) | 
|---|
| 863 | setMsgXMLEle (mp, root); | 
|---|
| 864 | else | 
|---|
| 865 | freeMsg (mp); | 
|---|
| 866 | delXMLEle (root); | 
|---|
| 867 |  | 
|---|
| 868 | } else if (err[0]) { | 
|---|
| 869 | char *ts = indi_tstamp(NULL); | 
|---|
| 870 | fprintf (stderr, "%s: Driver %s: XML error: %s\n", ts, | 
|---|
| 871 | dp->name, err); | 
|---|
| 872 | fprintf (stderr, "%s: Driver %s: XML read: %.*s\n", ts, | 
|---|
| 873 | dp->name, nr, buf); | 
|---|
| 874 | restartDvr (dp); | 
|---|
| 875 | return (-1); | 
|---|
| 876 | } | 
|---|
| 877 | } | 
|---|
| 878 |  | 
|---|
| 879 | return (shutany ? -1 : 0); | 
|---|
| 880 | } | 
|---|
| 881 |  | 
|---|
| 882 | /* read more from the given driver stderr, add prefix and send to our stderr. | 
|---|
| 883 | * return 0 if ok else -1 if had to restart. | 
|---|
| 884 | */ | 
|---|
| 885 | static int | 
|---|
| 886 | stderrFromDriver (DvrInfo *dp) | 
|---|
| 887 | { | 
|---|
| 888 | static char exbuf[MAXRBUF]; | 
|---|
| 889 | static int nexbuf; | 
|---|
| 890 | int i, nr; | 
|---|
| 891 |  | 
|---|
| 892 | /* read more */ | 
|---|
| 893 | nr = read (dp->efd, exbuf+nexbuf, sizeof(exbuf)-nexbuf); | 
|---|
| 894 | if (nr <= 0) { | 
|---|
| 895 | if (nr < 0) | 
|---|
| 896 | fprintf (stderr, "%s: Driver %s: stderr %s\n", indi_tstamp(NULL), | 
|---|
| 897 | dp->name, strerror(errno)); | 
|---|
| 898 | else | 
|---|
| 899 | fprintf (stderr, "%s: Driver %s: stderr EOF\n", | 
|---|
| 900 | indi_tstamp(NULL), dp->name); | 
|---|
| 901 | restartDvr (dp); | 
|---|
| 902 | return (-1); | 
|---|
| 903 | } | 
|---|
| 904 | nexbuf += nr; | 
|---|
| 905 |  | 
|---|
| 906 | /* prefix each whole line to our stderr, save extra for next time */ | 
|---|
| 907 | for (i = 0; i < nexbuf; i++) { | 
|---|
| 908 | if (exbuf[i] == '\n') { | 
|---|
| 909 | fprintf (stderr, "%s: Driver %s: %.*s\n", indi_tstamp(NULL), | 
|---|
| 910 | dp->name, i, exbuf); | 
|---|
| 911 | i++;                              /* count including nl */ | 
|---|
| 912 | nexbuf -= i;                      /* remove from nexbuf */ | 
|---|
| 913 | memmove (exbuf, exbuf+i, nexbuf); /* slide remaining to front */ | 
|---|
| 914 | i = -1;                           /* restart for loop scan */ | 
|---|
| 915 | } | 
|---|
| 916 | } | 
|---|
| 917 |  | 
|---|
| 918 | return (0); | 
|---|
| 919 | } | 
|---|
| 920 |  | 
|---|
| 921 | /* close down the given client */ | 
|---|
| 922 | static void | 
|---|
| 923 | shutdownClient (ClInfo *cp) | 
|---|
| 924 | { | 
|---|
| 925 | Msg *mp; | 
|---|
| 926 |  | 
|---|
| 927 | /* close connection */ | 
|---|
| 928 | shutdown (cp->s, SHUT_RDWR); | 
|---|
| 929 | close (cp->s); | 
|---|
| 930 |  | 
|---|
| 931 | /* free memory */ | 
|---|
| 932 | delLilXML (cp->lp); | 
|---|
| 933 | free (cp->props); | 
|---|
| 934 |  | 
|---|
| 935 | /* decrement and possibly free any unsent messages for this client */ | 
|---|
| 936 | while ((mp = (Msg*) popFQ(cp->msgq)) != NULL) | 
|---|
| 937 | if (--mp->count == 0) | 
|---|
| 938 | freeMsg (mp); | 
|---|
| 939 | delFQ (cp->msgq); | 
|---|
| 940 |  | 
|---|
| 941 | /* ok now to recycle */ | 
|---|
| 942 | cp->active = 0; | 
|---|
| 943 |  | 
|---|
| 944 | if (verbose > 0) | 
|---|
| 945 | fprintf (stderr, "%s: Client %d: shut down complete - bye!\n", | 
|---|
| 946 | indi_tstamp(NULL), cp->s); | 
|---|
| 947 | } | 
|---|
| 948 |  | 
|---|
| 949 | /* close down the given driver and restart */ | 
|---|
| 950 | static void | 
|---|
| 951 | restartDvr (DvrInfo *dp) | 
|---|
| 952 | { | 
|---|
| 953 | Msg *mp; | 
|---|
| 954 |  | 
|---|
| 955 | /* make sure it's dead, reclaim resources */ | 
|---|
| 956 | if (dp->pid == REMOTEDVR) { | 
|---|
| 957 | /* socket connection */ | 
|---|
| 958 | shutdown (dp->wfd, SHUT_RDWR); | 
|---|
| 959 | close (dp->wfd);    /* same as rfd */ | 
|---|
| 960 | } else { | 
|---|
| 961 | /* local pipe connection */ | 
|---|
| 962 | kill (dp->pid, SIGKILL);    /* we've insured there are no zombies */ | 
|---|
| 963 | close (dp->wfd); | 
|---|
| 964 | close (dp->rfd); | 
|---|
| 965 | close (dp->efd); | 
|---|
| 966 | } | 
|---|
| 967 |  | 
|---|
| 968 | /* free memory */ | 
|---|
| 969 | free (dp->sprops); | 
|---|
| 970 | delLilXML (dp->lp); | 
|---|
| 971 |  | 
|---|
| 972 | /* decrement and possibly free any unsent messages for this client */ | 
|---|
| 973 | while ((mp = (Msg*) popFQ(dp->msgq)) != NULL) | 
|---|
| 974 | if (--mp->count == 0) | 
|---|
| 975 | freeMsg (mp); | 
|---|
| 976 | delFQ (dp->msgq); | 
|---|
| 977 |  | 
|---|
| 978 | fprintf (stderr, "%s: Driver %s: restart #%d\n", indi_tstamp(NULL), | 
|---|
| 979 | dp->name, ++dp->restarts); | 
|---|
| 980 | startDvr (dp); | 
|---|
| 981 | } | 
|---|
| 982 |  | 
|---|
| 983 | /* put Msg mp on queue of each driver responsible for dev, or all drivers | 
|---|
| 984 | * if dev not specified. | 
|---|
| 985 | */ | 
|---|
| 986 | static void | 
|---|
| 987 | q2RDrivers (const char *dev, Msg *mp, XMLEle *root) | 
|---|
| 988 | { | 
|---|
| 989 | int sawremote = 0; | 
|---|
| 990 | DvrInfo *dp; | 
|---|
| 991 |  | 
|---|
| 992 | /* queue message to each interested driver. | 
|---|
| 993 | * N.B. don't send generic getProps to more than one remote driver, | 
|---|
| 994 | *   otherwise they all fan out and we get multiple responses back. | 
|---|
| 995 | */ | 
|---|
| 996 | for (dp = dvrinfo; dp < &dvrinfo[ndvrinfo]; dp++) { | 
|---|
| 997 | int isremote = (dp->pid == REMOTEDVR); | 
|---|
| 998 | if (dev[0] && dp->dev[0] && strcmp (dev, dp->dev)) | 
|---|
| 999 | continue;       /* driver known to not support this dev */ | 
|---|
| 1000 | if (!dev[0] && isremote && sawremote) | 
|---|
| 1001 | continue;       /* already sent generic to another remote */ | 
|---|
| 1002 | if (isremote) | 
|---|
| 1003 | sawremote = 1; | 
|---|
| 1004 |  | 
|---|
| 1005 | /* ok: queue message to this driver */ | 
|---|
| 1006 | mp->count++; | 
|---|
| 1007 | pushFQ (dp->msgq, mp); | 
|---|
| 1008 | if (verbose > 1) | 
|---|
| 1009 | fprintf (stderr, "%s: Driver %s: queuing responsible for <%s device='%s' name='%s'>\n", | 
|---|
| 1010 | indi_tstamp(NULL), dp->name, tagXMLEle(root), | 
|---|
| 1011 | findXMLAttValu (root, "device"), | 
|---|
| 1012 | findXMLAttValu (root, "name")); | 
|---|
| 1013 | } | 
|---|
| 1014 | } | 
|---|
| 1015 |  | 
|---|
| 1016 | /* put Msg mp on queue of each driver snooping dev/name. | 
|---|
| 1017 | * if BLOB always honor current mode. | 
|---|
| 1018 | */ | 
|---|
| 1019 | static void | 
|---|
| 1020 | q2SDrivers (int isblob, const char *dev, const char *name, Msg *mp, XMLEle *root) | 
|---|
| 1021 | { | 
|---|
| 1022 | DvrInfo *dp; | 
|---|
| 1023 |  | 
|---|
| 1024 | for (dp = dvrinfo; dp < &dvrinfo[ndvrinfo]; dp++) { | 
|---|
| 1025 | Snoopee *sp = findSDevice (dp, dev, name); | 
|---|
| 1026 |  | 
|---|
| 1027 | /* nothing for dp if not snooping for dev/name or wrong BLOB mode */ | 
|---|
| 1028 | if (!sp) | 
|---|
| 1029 | continue; | 
|---|
| 1030 | if ((isblob && sp->blob==B_NEVER) || (!isblob && sp->blob==B_ONLY)) | 
|---|
| 1031 | continue; | 
|---|
| 1032 |  | 
|---|
| 1033 | /* ok: queue message to this device */ | 
|---|
| 1034 | mp->count++; | 
|---|
| 1035 | pushFQ (dp->msgq, mp); | 
|---|
| 1036 | if (verbose > 1) { | 
|---|
| 1037 | fprintf (stderr, "%s: Driver %s: queuing snooped <%s device='%s' name='%s'>\n", | 
|---|
| 1038 | indi_tstamp(NULL), dp->name, tagXMLEle(root), | 
|---|
| 1039 | findXMLAttValu (root, "device"), | 
|---|
| 1040 | findXMLAttValu (root, "name")); | 
|---|
| 1041 | } | 
|---|
| 1042 | } | 
|---|
| 1043 | } | 
|---|
| 1044 |  | 
|---|
| 1045 | /* add dev/name to dp's snooping list. | 
|---|
| 1046 | * init with blob mode set to B_NEVER. | 
|---|
| 1047 | */ | 
|---|
| 1048 | static void | 
|---|
| 1049 | addSDevice (DvrInfo *dp, const char *dev, const char *name) | 
|---|
| 1050 | { | 
|---|
| 1051 | Snoopee *sp; | 
|---|
| 1052 | char *ip; | 
|---|
| 1053 |  | 
|---|
| 1054 | /* no dups */ | 
|---|
| 1055 | sp = findSDevice (dp, dev, name); | 
|---|
| 1056 | if (sp) | 
|---|
| 1057 | return; | 
|---|
| 1058 |  | 
|---|
| 1059 | /* add dev to sdevs list */ | 
|---|
| 1060 | dp->sprops = (Snoopee*) realloc (dp->sprops, | 
|---|
| 1061 | (dp->nsprops+1)*sizeof(Snoopee)); | 
|---|
| 1062 | sp = &dp->sprops[dp->nsprops++]; | 
|---|
| 1063 |  | 
|---|
| 1064 | ip = sp->prop.dev; | 
|---|
| 1065 | strncpy (ip, dev, MAXINDIDEVICE-1); | 
|---|
| 1066 | ip[MAXINDIDEVICE-1] = '\0'; | 
|---|
| 1067 |  | 
|---|
| 1068 | ip = sp->prop.name; | 
|---|
| 1069 | strncpy (ip, name, MAXINDINAME-1); | 
|---|
| 1070 | ip[MAXINDINAME-1] = '\0'; | 
|---|
| 1071 |  | 
|---|
| 1072 | sp->blob = B_NEVER; | 
|---|
| 1073 |  | 
|---|
| 1074 | if (verbose) | 
|---|
| 1075 | fprintf (stderr, "%s: Driver %s: snooping on %s.%s\n", indi_tstamp(NULL), | 
|---|
| 1076 | dp->name, dev, name); | 
|---|
| 1077 | } | 
|---|
| 1078 |  | 
|---|
| 1079 | /* return Snoopee if dp is snooping dev/name, else NULL. | 
|---|
| 1080 | */ | 
|---|
| 1081 | static Snoopee * | 
|---|
| 1082 | findSDevice (DvrInfo *dp, const char *dev, const char *name) | 
|---|
| 1083 | { | 
|---|
| 1084 | int i; | 
|---|
| 1085 |  | 
|---|
| 1086 | for (i = 0; i < dp->nsprops; i++) { | 
|---|
| 1087 | Snoopee *sp = &dp->sprops[i]; | 
|---|
| 1088 | Property *pp = &sp->prop; | 
|---|
| 1089 | if (!strcmp (pp->dev, dev) && | 
|---|
| 1090 | (!pp->name[0] || !strcmp(pp->name, name))) | 
|---|
| 1091 | return (sp); | 
|---|
| 1092 | } | 
|---|
| 1093 |  | 
|---|
| 1094 | return (NULL); | 
|---|
| 1095 | } | 
|---|
| 1096 |  | 
|---|
| 1097 | /* put Msg mp on queue of each client interested in dev/name, except notme. | 
|---|
| 1098 | * if BLOB always honor current mode. | 
|---|
| 1099 | * return -1 if had to shut down any clients, else 0. | 
|---|
| 1100 | */ | 
|---|
| 1101 | static int | 
|---|
| 1102 | q2Clients (ClInfo *notme, int isblob, const char *dev, const char *name, Msg *mp, XMLEle *root) | 
|---|
| 1103 | { | 
|---|
| 1104 | int shutany = 0; | 
|---|
| 1105 | ClInfo *cp; | 
|---|
| 1106 | int ql; | 
|---|
| 1107 |  | 
|---|
| 1108 | /* queue message to each interested client */ | 
|---|
| 1109 | for (cp = clinfo; cp < &clinfo[nclinfo]; cp++) { | 
|---|
| 1110 | /* cp in use? notme? want this dev/name? blob? */ | 
|---|
| 1111 | if (!cp->active || cp == notme) | 
|---|
| 1112 | continue; | 
|---|
| 1113 | if (findClDevice (cp, dev, name) < 0) | 
|---|
| 1114 | continue; | 
|---|
| 1115 | if ((isblob && cp->blob==B_NEVER) || (!isblob && cp->blob==B_ONLY)) | 
|---|
| 1116 | continue; | 
|---|
| 1117 |  | 
|---|
| 1118 | /* shut down this client if its q is already too large */ | 
|---|
| 1119 | ql = msgQSize(cp->msgq); | 
|---|
| 1120 | if (ql > maxqsiz) { | 
|---|
| 1121 | if (verbose) | 
|---|
| 1122 | fprintf (stderr, "%s: Client %d: %d bytes behind, shutting down\n", | 
|---|
| 1123 | indi_tstamp(NULL), cp->s, ql); | 
|---|
| 1124 | shutdownClient (cp); | 
|---|
| 1125 | shutany++; | 
|---|
| 1126 | continue; | 
|---|
| 1127 | } | 
|---|
| 1128 |  | 
|---|
| 1129 | /* ok: queue message to this client */ | 
|---|
| 1130 | mp->count++; | 
|---|
| 1131 | pushFQ (cp->msgq, mp); | 
|---|
| 1132 | if (verbose > 1) | 
|---|
| 1133 | fprintf (stderr, "%s: Client %d: queuing <%s device='%s' name='%s'>\n", | 
|---|
| 1134 | indi_tstamp(NULL), cp->s, tagXMLEle(root), | 
|---|
| 1135 | findXMLAttValu (root, "device"), | 
|---|
| 1136 | findXMLAttValu (root, "name")); | 
|---|
| 1137 | } | 
|---|
| 1138 |  | 
|---|
| 1139 | return (shutany ? -1 : 0); | 
|---|
| 1140 | } | 
|---|
| 1141 |  | 
|---|
| 1142 | /* return size of all Msqs on the given q */ | 
|---|
| 1143 | static int | 
|---|
| 1144 | msgQSize (FQ *q) | 
|---|
| 1145 | { | 
|---|
| 1146 | int i, l = 0; | 
|---|
| 1147 |  | 
|---|
| 1148 | for (i = 0; i < nFQ(q); i++) { | 
|---|
| 1149 | Msg *mp = (Msg *) peekiFQ(q,i); | 
|---|
| 1150 | l += mp->cl; | 
|---|
| 1151 | } | 
|---|
| 1152 |  | 
|---|
| 1153 | return (l); | 
|---|
| 1154 | } | 
|---|
| 1155 |  | 
|---|
| 1156 | /* print root as content in Msg mp. | 
|---|
| 1157 | */ | 
|---|
| 1158 | static void | 
|---|
| 1159 | setMsgXMLEle (Msg *mp, XMLEle *root) | 
|---|
| 1160 | { | 
|---|
| 1161 | /* want cl to only count content, but need room for final \0 */ | 
|---|
| 1162 | mp->cl = sprlXMLEle (root, 0); | 
|---|
| 1163 | if (mp->cl < sizeof(mp->buf)) | 
|---|
| 1164 | mp->cp = mp->buf; | 
|---|
| 1165 | else | 
|---|
| 1166 | mp->cp = malloc (mp->cl+1); | 
|---|
| 1167 | sprXMLEle (mp->cp, root, 0); | 
|---|
| 1168 | } | 
|---|
| 1169 |  | 
|---|
| 1170 | /* save str as content in Msg mp. | 
|---|
| 1171 | */ | 
|---|
| 1172 | static void | 
|---|
| 1173 | setMsgStr (Msg *mp, char *str) | 
|---|
| 1174 | { | 
|---|
| 1175 | /* want cl to only count content, but need room for final \0 */ | 
|---|
| 1176 | mp->cl = strlen (str); | 
|---|
| 1177 | if (mp->cl < sizeof(mp->buf)) | 
|---|
| 1178 | mp->cp = mp->buf; | 
|---|
| 1179 | else | 
|---|
| 1180 | mp->cp = malloc (mp->cl+1); | 
|---|
| 1181 | strcpy (mp->cp, str); | 
|---|
| 1182 | } | 
|---|
| 1183 |  | 
|---|
| 1184 | /* return pointer to one new nulled Msg | 
|---|
| 1185 | */ | 
|---|
| 1186 | static Msg * | 
|---|
| 1187 | newMsg (void) | 
|---|
| 1188 | { | 
|---|
| 1189 | return ((Msg *) calloc (1, sizeof(Msg))); | 
|---|
| 1190 | } | 
|---|
| 1191 |  | 
|---|
| 1192 | /* free Msg mp and everything it contains */ | 
|---|
| 1193 | static void | 
|---|
| 1194 | freeMsg (Msg *mp) | 
|---|
| 1195 | { | 
|---|
| 1196 | if (mp->cp && mp->cp != mp->buf) | 
|---|
| 1197 | free (mp->cp); | 
|---|
| 1198 | free (mp); | 
|---|
| 1199 | } | 
|---|
| 1200 |  | 
|---|
| 1201 | /* write the next chunk of the current message in the queue to the given | 
|---|
| 1202 | * client. pop message from queue when complete and free the message if we are | 
|---|
| 1203 | * the last one to use it. shut down this client if trouble. | 
|---|
| 1204 | * N.B. we assume we will never be called with cp->msgq empty. | 
|---|
| 1205 | * return 0 if ok else -1 if had to shut down. | 
|---|
| 1206 | */ | 
|---|
| 1207 | static int | 
|---|
| 1208 | sendClientMsg (ClInfo *cp) | 
|---|
| 1209 | { | 
|---|
| 1210 | int nsend, nw; | 
|---|
| 1211 | Msg *mp; | 
|---|
| 1212 |  | 
|---|
| 1213 | /* get current message */ | 
|---|
| 1214 | mp = (Msg *) peekFQ (cp->msgq); | 
|---|
| 1215 |  | 
|---|
| 1216 | /* send next chunk, never more than MAXWSIZ to reduce blocking */ | 
|---|
| 1217 | nsend = mp->cl - cp->nsent; | 
|---|
| 1218 | if (nsend > MAXWSIZ) | 
|---|
| 1219 | nsend = MAXWSIZ; | 
|---|
| 1220 | nw = write (cp->s, &mp->cp[cp->nsent], nsend); | 
|---|
| 1221 |  | 
|---|
| 1222 | /* shut down if trouble */ | 
|---|
| 1223 | if (nw <= 0) { | 
|---|
| 1224 | if (nw == 0) | 
|---|
| 1225 | fprintf (stderr, "%s: Client %d: write returned 0\n", | 
|---|
| 1226 | indi_tstamp(NULL), cp->s); | 
|---|
| 1227 | else | 
|---|
| 1228 | fprintf (stderr, "%s: Client %d: write: %s\n", indi_tstamp(NULL), | 
|---|
| 1229 | cp->s, strerror(errno)); | 
|---|
| 1230 | shutdownClient (cp); | 
|---|
| 1231 | return (-1); | 
|---|
| 1232 | } | 
|---|
| 1233 |  | 
|---|
| 1234 | /* trace */ | 
|---|
| 1235 | if (verbose > 2) { | 
|---|
| 1236 | fprintf(stderr, "%s: Client %d: sending msg copy %d nq %d:\n%.*s\n", | 
|---|
| 1237 | indi_tstamp(NULL), cp->s, mp->count, nFQ(cp->msgq), | 
|---|
| 1238 | nw, &mp->cp[cp->nsent]); | 
|---|
| 1239 | } else if (verbose > 1) { | 
|---|
| 1240 | fprintf(stderr, "%s: Client %d: sending %.50s\n", indi_tstamp(NULL), | 
|---|
| 1241 | cp->s, &mp->cp[cp->nsent]); | 
|---|
| 1242 | } | 
|---|
| 1243 |  | 
|---|
| 1244 | /* update amount sent. when complete: free message if we are the last | 
|---|
| 1245 | * to use it and pop from our queue. | 
|---|
| 1246 | */ | 
|---|
| 1247 | cp->nsent += nw; | 
|---|
| 1248 | if (cp->nsent == mp->cl) { | 
|---|
| 1249 | if (--mp->count == 0) | 
|---|
| 1250 | freeMsg (mp); | 
|---|
| 1251 | popFQ (cp->msgq); | 
|---|
| 1252 | cp->nsent = 0; | 
|---|
| 1253 | } | 
|---|
| 1254 |  | 
|---|
| 1255 | return (0); | 
|---|
| 1256 | } | 
|---|
| 1257 |  | 
|---|
| 1258 | /* write the next chunk of the current message in the queue to the given | 
|---|
| 1259 | * driver. pop message from queue when complete and free the message if we are | 
|---|
| 1260 | * the last one to use it. restart this driver if touble. | 
|---|
| 1261 | * N.B. we assume we will never be called with dp->msgq empty. | 
|---|
| 1262 | * return 0 if ok else -1 if had to shut down. | 
|---|
| 1263 | */ | 
|---|
| 1264 | static int | 
|---|
| 1265 | sendDriverMsg (DvrInfo *dp) | 
|---|
| 1266 | { | 
|---|
| 1267 | int nsend, nw; | 
|---|
| 1268 | Msg *mp; | 
|---|
| 1269 |  | 
|---|
| 1270 | /* get current message */ | 
|---|
| 1271 | mp = (Msg *) peekFQ (dp->msgq); | 
|---|
| 1272 |  | 
|---|
| 1273 | /* send next chunk, never more than MAXWSIZ to reduce blocking */ | 
|---|
| 1274 | nsend = mp->cl - dp->nsent; | 
|---|
| 1275 | if (nsend > MAXWSIZ) | 
|---|
| 1276 | nsend = MAXWSIZ; | 
|---|
| 1277 | nw = write (dp->wfd, &mp->cp[dp->nsent], nsend); | 
|---|
| 1278 |  | 
|---|
| 1279 | /* restart if trouble */ | 
|---|
| 1280 | if (nw <= 0) { | 
|---|
| 1281 | if (nw == 0) | 
|---|
| 1282 | fprintf (stderr, "%s: Driver %s: write returned 0\n", | 
|---|
| 1283 | indi_tstamp(NULL), dp->name); | 
|---|
| 1284 | else | 
|---|
| 1285 | fprintf (stderr, "%s: Driver %s: write: %s\n", indi_tstamp(NULL), | 
|---|
| 1286 | dp->name, strerror(errno)); | 
|---|
| 1287 | restartDvr (dp); | 
|---|
| 1288 | return (-1); | 
|---|
| 1289 | } | 
|---|
| 1290 |  | 
|---|
| 1291 | /* trace */ | 
|---|
| 1292 | if (verbose > 2) { | 
|---|
| 1293 | fprintf(stderr, "%s: Driver %s: sending msg copy %d nq %d:\n%.*s\n", | 
|---|
| 1294 | indi_tstamp(NULL), dp->name, mp->count, nFQ(dp->msgq), | 
|---|
| 1295 | nw, &mp->cp[dp->nsent]); | 
|---|
| 1296 | } else if (verbose > 1) { | 
|---|
| 1297 | fprintf(stderr, "%s: Driver %s: sending %.50s\n", indi_tstamp(NULL), | 
|---|
| 1298 | dp->name, &mp->cp[dp->nsent]); | 
|---|
| 1299 | } | 
|---|
| 1300 |  | 
|---|
| 1301 | /* update amount sent. when complete: free message if we are the last | 
|---|
| 1302 | * to use it and pop from our queue. | 
|---|
| 1303 | */ | 
|---|
| 1304 | dp->nsent += nw; | 
|---|
| 1305 | if (dp->nsent == mp->cl) { | 
|---|
| 1306 | if (--mp->count == 0) | 
|---|
| 1307 | freeMsg (mp); | 
|---|
| 1308 | popFQ (dp->msgq); | 
|---|
| 1309 | dp->nsent = 0; | 
|---|
| 1310 | } | 
|---|
| 1311 |  | 
|---|
| 1312 | return (0); | 
|---|
| 1313 | } | 
|---|
| 1314 |  | 
|---|
| 1315 | /* return 0 if cp may be interested in dev/name else -1 | 
|---|
| 1316 | */ | 
|---|
| 1317 | static int | 
|---|
| 1318 | findClDevice (ClInfo *cp, const char *dev, const char *name) | 
|---|
| 1319 | { | 
|---|
| 1320 | int i; | 
|---|
| 1321 |  | 
|---|
| 1322 | if (cp->allprops || !dev[0]) | 
|---|
| 1323 | return (0); | 
|---|
| 1324 | for (i = 0; i < cp->nprops; i++) { | 
|---|
| 1325 | Property *pp = &cp->props[i]; | 
|---|
| 1326 | if (!strcmp (pp->dev, dev) && | 
|---|
| 1327 | (!pp->name[0] || !strcmp(pp->name, name))) | 
|---|
| 1328 | return (0); | 
|---|
| 1329 | } | 
|---|
| 1330 | return (-1); | 
|---|
| 1331 | } | 
|---|
| 1332 |  | 
|---|
| 1333 | /* add the given device and property to the devs[] list of client if new. | 
|---|
| 1334 | */ | 
|---|
| 1335 | static void | 
|---|
| 1336 | addClDevice (ClInfo *cp, const char *dev, const char *name) | 
|---|
| 1337 | { | 
|---|
| 1338 | Property *pp; | 
|---|
| 1339 | char *ip; | 
|---|
| 1340 |  | 
|---|
| 1341 | /* no dups */ | 
|---|
| 1342 | if (!findClDevice (cp, dev, name)) | 
|---|
| 1343 | return; | 
|---|
| 1344 |  | 
|---|
| 1345 | /* add */ | 
|---|
| 1346 | cp->props = (Property *) realloc (cp->props, | 
|---|
| 1347 | (cp->nprops+1)*sizeof(Property)); | 
|---|
| 1348 | pp = &cp->props[cp->nprops++]; | 
|---|
| 1349 |  | 
|---|
| 1350 | ip = pp->dev; | 
|---|
| 1351 | strncpy (ip, dev, MAXINDIDEVICE-1); | 
|---|
| 1352 | ip[MAXINDIDEVICE-1] = '\0'; | 
|---|
| 1353 |  | 
|---|
| 1354 | ip = pp->name; | 
|---|
| 1355 | strncpy (ip, name, MAXINDINAME-1); | 
|---|
| 1356 | ip[MAXINDINAME-1] = '\0'; | 
|---|
| 1357 | } | 
|---|
| 1358 |  | 
|---|
| 1359 |  | 
|---|
| 1360 | /* block to accept a new client arriving on lsocket. | 
|---|
| 1361 | * return private nonblocking socket or exit. | 
|---|
| 1362 | */ | 
|---|
| 1363 | static int | 
|---|
| 1364 | newClSocket () | 
|---|
| 1365 | { | 
|---|
| 1366 | struct sockaddr_in cli_socket; | 
|---|
| 1367 | socklen_t cli_len; | 
|---|
| 1368 | int cli_fd; | 
|---|
| 1369 |  | 
|---|
| 1370 | /* get a private connection to new client */ | 
|---|
| 1371 | cli_len = sizeof(cli_socket); | 
|---|
| 1372 | cli_fd = accept (lsocket, (struct sockaddr *)&cli_socket, &cli_len); | 
|---|
| 1373 | if(cli_fd < 0) { | 
|---|
| 1374 | fprintf (stderr, "accept: %s\n", strerror(errno)); | 
|---|
| 1375 | Bye(); | 
|---|
| 1376 | } | 
|---|
| 1377 |  | 
|---|
| 1378 | /* ok */ | 
|---|
| 1379 | return (cli_fd); | 
|---|
| 1380 | } | 
|---|
| 1381 |  | 
|---|
| 1382 | /* convert the string value of enableBLOB to our B_ state value. | 
|---|
| 1383 | * no change if unrecognized | 
|---|
| 1384 | */ | 
|---|
| 1385 | static void | 
|---|
| 1386 | crackBLOB (char *enableBLOB, BLOBHandling *bp) | 
|---|
| 1387 | { | 
|---|
| 1388 | if (!strcmp (enableBLOB, "Also")) | 
|---|
| 1389 | *bp = B_ALSO; | 
|---|
| 1390 | else if (!strcmp (enableBLOB, "Only")) | 
|---|
| 1391 | *bp = B_ONLY; | 
|---|
| 1392 | else if (!strcmp (enableBLOB, "Never")) | 
|---|
| 1393 | *bp = B_NEVER; | 
|---|
| 1394 | } | 
|---|
| 1395 |  | 
|---|
| 1396 | /* print key attributes and values of the given xml to stderr. | 
|---|
| 1397 | */ | 
|---|
| 1398 | static void | 
|---|
| 1399 | traceMsg (XMLEle *root) | 
|---|
| 1400 | { | 
|---|
| 1401 | static const char *prtags[] = { | 
|---|
| 1402 | "defNumber", "oneNumber", | 
|---|
| 1403 | "defText",   "oneText", | 
|---|
| 1404 | "defSwitch", "oneSwitch", | 
|---|
| 1405 | "defLight",  "oneLight", | 
|---|
| 1406 | }; | 
|---|
| 1407 | XMLEle *e; | 
|---|
| 1408 | const char *msg, *perm, *pcd; | 
|---|
| 1409 | unsigned int i; | 
|---|
| 1410 |  | 
|---|
| 1411 | /* print tag header */ | 
|---|
| 1412 | fprintf (stderr, "%s %s %s %s", tagXMLEle(root), | 
|---|
| 1413 | findXMLAttValu(root,"device"), | 
|---|
| 1414 | findXMLAttValu(root,"name"), | 
|---|
| 1415 | findXMLAttValu(root,"state")); | 
|---|
| 1416 | pcd = pcdataXMLEle (root); | 
|---|
| 1417 | if (pcd[0]) | 
|---|
| 1418 | fprintf (stderr, " %s", pcd); | 
|---|
| 1419 | perm = findXMLAttValu(root,"perm"); | 
|---|
| 1420 | if (perm[0]) | 
|---|
| 1421 | fprintf (stderr, " %s", perm); | 
|---|
| 1422 | msg = findXMLAttValu(root,"message"); | 
|---|
| 1423 | if (msg[0]) | 
|---|
| 1424 | fprintf (stderr, " '%s'", msg); | 
|---|
| 1425 |  | 
|---|
| 1426 | /* print each array value */ | 
|---|
| 1427 | for (e = nextXMLEle(root,1); e; e = nextXMLEle(root,0)) | 
|---|
| 1428 | for (i = 0; i < sizeof(prtags)/sizeof(prtags[0]); i++) | 
|---|
| 1429 | if (strcmp (prtags[i], tagXMLEle(e)) == 0) | 
|---|
| 1430 | fprintf (stderr, "\n %10s='%s'", findXMLAttValu(e,"name"), | 
|---|
| 1431 | pcdataXMLEle (e)); | 
|---|
| 1432 |  | 
|---|
| 1433 | fprintf (stderr, "\n"); | 
|---|
| 1434 | } | 
|---|
| 1435 |  | 
|---|
| 1436 | /* fill s with current UT string. | 
|---|
| 1437 | * if no s, use a static buffer | 
|---|
| 1438 | * return s or buffer. | 
|---|
| 1439 | * N.B. if use our buffer, be sure to use before calling again | 
|---|
| 1440 | */ | 
|---|
| 1441 | static char * | 
|---|
| 1442 | indi_tstamp (char *s) | 
|---|
| 1443 | { | 
|---|
| 1444 | static char sbuf[64]; | 
|---|
| 1445 | struct tm *tp; | 
|---|
| 1446 | time_t t; | 
|---|
| 1447 |  | 
|---|
| 1448 | time (&t); | 
|---|
| 1449 | tp = gmtime (&t); | 
|---|
| 1450 | if (!s) | 
|---|
| 1451 | s = sbuf; | 
|---|
| 1452 | strftime (s, sizeof(sbuf), "%Y-%m-%dT%H:%M:%S", tp); | 
|---|
| 1453 | return (s); | 
|---|
| 1454 | } | 
|---|
| 1455 |  | 
|---|
| 1456 | /* log message in root known to be from device dev to ldir, if any. | 
|---|
| 1457 | */ | 
|---|
| 1458 | static void | 
|---|
| 1459 | logDMsg (XMLEle *root, const char *dev) | 
|---|
| 1460 | { | 
|---|
| 1461 | char stamp[64]; | 
|---|
| 1462 | char logfn[1024]; | 
|---|
| 1463 | const char *ts, *ms; | 
|---|
| 1464 | FILE *fp; | 
|---|
| 1465 |  | 
|---|
| 1466 | /* get message, if any */ | 
|---|
| 1467 | ms = findXMLAttValu (root, "message"); | 
|---|
| 1468 | if (!ms[0]) | 
|---|
| 1469 | return; | 
|---|
| 1470 |  | 
|---|
| 1471 | /* get timestamp now if not provided */ | 
|---|
| 1472 | ts = findXMLAttValu (root, "timestamp"); | 
|---|
| 1473 | if (!ts[0]) | 
|---|
| 1474 | { | 
|---|
| 1475 | indi_tstamp (stamp); | 
|---|
| 1476 | ts = stamp; | 
|---|
| 1477 | } | 
|---|
| 1478 |  | 
|---|
| 1479 | /* append to log file, name is date portion of time stamp */ | 
|---|
| 1480 | sprintf (logfn, "%s/%.10s.islog", ldir, ts); | 
|---|
| 1481 | fp = fopen (logfn, "a"); | 
|---|
| 1482 | if (!fp) | 
|---|
| 1483 | return;     /* oh well */ | 
|---|
| 1484 | fprintf (fp, "%s: %s: %s\n", ts, dev, ms); | 
|---|
| 1485 | fclose (fp); | 
|---|
| 1486 | } | 
|---|
| 1487 |  | 
|---|
| 1488 | /* log when then exit */ | 
|---|
| 1489 | static void | 
|---|
| 1490 | Bye() | 
|---|
| 1491 | { | 
|---|
| 1492 | fprintf (stderr, "%s: good bye\n", indi_tstamp(NULL)); | 
|---|
| 1493 | exit(1); | 
|---|
| 1494 | } | 
|---|
| 1495 |  | 
|---|