2 * Copyright (c) 1998, 1999 Martin Husemann. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 * 4. Altered versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software and/or documentation.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 *---------------------------------------------------------------------------
33 * i4b daemon - network monitor server module
34 * ------------------------------------------
38 * last edit-date: [Sat May 13 13:08:15 2006]
40 *---------------------------------------------------------------------------*/
44 #ifndef I4B_EXTERNAL_MONITOR
47 * dummy version of routines needed by config file parser
48 * (config files should be valid with and without external montioring
49 * support compiled into the daemon)
52 void monitor_clear_rights()
55 int monitor_start_rights(const char *clientspec)
58 void monitor_add_rights(int rights_mask)
61 void monitor_fixup_rights()
67 #include <sys/socket.h>
69 #ifndef I4B_NOTCPIP_MONITOR
70 #include <netinet/in.h>
71 #include <arpa/inet.h>
76 static TAILQ_HEAD(rights_q, monitor_rights) rights = TAILQ_HEAD_INITIALIZER(rights);
78 static struct monitor_rights * local_rights = NULL; /* entry for local socket */
80 /* for each active monitor connection we have one of this: */
82 struct monitor_connection {
83 TAILQ_ENTRY(monitor_connection) connections;
84 int sock; /* socket for this connection */
85 int rights; /* active rights for this connection */
86 int events; /* bitmask of events client is interested in */
87 char source[FILENAME_MAX];
90 static TAILQ_HEAD(connections_tq, monitor_connection) connections = TAILQ_HEAD_INITIALIZER(connections);
92 /* local prototypes */
93 static int cmp_rights(const struct monitor_rights *pa, const struct monitor_rights *pb);
94 static int monitor_command(struct monitor_connection *con, int fd, int rights);
95 static void cmd_dump_rights(int fd, int rights, u_int8_t *cmd, const char * source);
96 static void cmd_dump_mcons(int fd, int rights, u_int8_t *cmd, const char * source);
97 static void cmd_reread_cfg(int fd, int rights, u_int8_t *cmd, const char * source);
98 static void cmd_hangup(int fd, int rights, u_int8_t *cmd, const char * source);
99 static void monitor_broadcast(int mask, u_int8_t *pkt, size_t bytes);
100 static int anybody(int mask);
101 static void hangup_channel(int controller, int channel, const char *source);
102 static ssize_t sock_read(int fd, void *buf, size_t nbytes);
103 static ssize_t sock_write(int fd, void *buf, size_t nbytes);
106 * Due to the way we structure config files, the rights for an external
107 * monitor might be stated in multiple steps. First a call to
108 * monitor_start_rights opens an entry. Further (optional) calls to
109 * montior_add_rights assemble additional rights for this "current"
110 * entry. When closing the sys-file section of the config file, the
111 * "current" entry becomes invalid.
113 static struct monitor_rights * cur_add_entry = NULL;
115 /*---------------------------------------------------------------------------
116 * Initialize the monitor server module. This affects only active
117 * connections, the access rights are not modified here!
118 *---------------------------------------------------------------------------*/
122 struct monitor_connection * con;
124 while ((con = TAILQ_FIRST(&connections)) != NULL)
126 TAILQ_REMOVE(&connections, con, connections);
131 /*---------------------------------------------------------------------------
133 *---------------------------------------------------------------------------*/
137 struct monitor_connection *c;
139 /* Close all open connections. */
140 while((c = TAILQ_FIRST(&connections)) != NULL) {
142 TAILQ_REMOVE(&connections, c, connections);
147 /*---------------------------------------------------------------------------
148 * Initialize access rights. No active connections are affected!
149 *---------------------------------------------------------------------------*/
151 monitor_clear_rights(void)
153 struct monitor_rights *r;
154 while ((r = TAILQ_FIRST(&rights)) != NULL) {
155 TAILQ_REMOVE(&rights, r, list);
158 cur_add_entry = NULL;
162 /*---------------------------------------------------------------------------
163 * Add an entry to the access lists. The clientspec either is
164 * the name of the local socket or a host- or networkname or
165 * numeric ip/host-bit-len spec.
166 *---------------------------------------------------------------------------*/
168 monitor_start_rights(const char *clientspec)
170 struct monitor_rights r;
172 /* initialize the new rights entry */
174 memset(&r, 0, sizeof r);
176 /* check clientspec */
178 if (*clientspec == '/')
180 struct sockaddr_un sa;
182 /* this is a local socket spec, check if we already have one */
184 if (local_rights != NULL)
187 /* does it fit in a local socket address? */
189 if (strlen(clientspec) > sizeof sa.sun_path)
190 return I4BMAR_LENGTH;
193 strcpy(r.name, clientspec);
195 #ifndef I4B_NOTCPIP_MONITOR
200 /* remote entry, parse host/net and cidr */
202 struct monitor_rights * rp;
203 char hostname[FILENAME_MAX];
206 p = strchr(clientspec, '/');
210 struct hostent *host;
213 /* must be a host spec */
216 host = gethostbyname(clientspec);
221 memcpy(&hn, host->h_addr_list[0], sizeof hn);
222 r.net = (u_int32_t)ntohl(hn);
226 /* must be net/cidr spec */
231 int num = strtol(p+1, NULL, 10);
233 if (num < 0 || num > 32)
240 if (l >= sizeof hostname)
241 return I4BMAR_LENGTH;
243 strncpy(hostname, clientspec, l);
247 net = getnetbyname(hostname);
250 r.net = (u_int32_t)inet_network(hostname);
252 r.net = (u_int32_t)net->n_net;
262 /* check for duplicate entry */
264 for (rp = TAILQ_FIRST(&rights); rp != NULL; rp = TAILQ_NEXT(rp, list))
266 if (rp->mask == r.mask &&
268 rp->local == r.local)
278 /* entry ok, add it to the collection */
280 cur_add_entry = malloc(sizeof(r));
281 memcpy(cur_add_entry, &r, sizeof(r));
282 TAILQ_INSERT_TAIL(&rights, cur_add_entry, list);
285 local_rights = cur_add_entry;
287 DBGL(DL_RCCF, (llog(LL_DBG, "system: monitor = %s", clientspec)));
292 /*---------------------------------------------------------------------------
293 * Add rights to the currently constructed entry - if any.
294 *---------------------------------------------------------------------------*/
296 monitor_add_rights(int rights_mask)
298 if(cur_add_entry == NULL)
299 return; /* noone under construction */
301 cur_add_entry->rights |= rights_mask;
303 DBGL(DL_RCCF, (llog(LL_DBG, "system: monitor-access = 0x%x", rights_mask)));
306 /*---------------------------------------------------------------------------
307 * All rights have been added now. Sort the to get most specific
308 * host/net masks first, so we can travel the list and use the first
309 * match for actual rights.
310 *---------------------------------------------------------------------------*/
312 monitor_fixup_rights(void)
314 struct monitor_rights * cur, * test, * next;
316 /* no more rights may be added to the current entry */
318 cur_add_entry = NULL;
320 /* sort the rights */
321 for (next = NULL, cur = TAILQ_FIRST(&rights); cur != NULL; cur = next)
323 next = TAILQ_NEXT(cur, list);
324 for (test = TAILQ_FIRST(&rights); test != NULL && test != cur; test = TAILQ_NEXT(test, list))
326 if (cmp_rights(cur, test) > 0) {
327 /* move cur up the list and insert before test */
328 TAILQ_REMOVE(&rights, cur, list);
329 if (test == TAILQ_FIRST(&rights))
330 TAILQ_INSERT_HEAD(&rights, cur, list);
332 TAILQ_INSERT_BEFORE(test, cur, list);
339 /*---------------------------------------------------------------------------
340 * comparator for rights
341 *---------------------------------------------------------------------------*/
343 cmp_rights(const struct monitor_rights *pa, const struct monitor_rights *pb)
347 /* local sorts first */
352 /* which is the less specific netmask? */
356 if ((pb->mask & mask) == 0)
359 /* are the entries disjunct? */
361 if ((pa->net & mask) != (pb->net & mask))
363 /* simply compare net part of address */
364 return ((pa->net & mask) < (pb->net & mask)) ? -1 : 1;
367 /* One entry is part of the others net. We already now "mask" is
368 * the netmask of the less specific (i.e. greater) one */
370 return (pa->mask == mask) ? 1 : -1;
373 #ifndef I4B_NOTCPIP_MONITOR
374 /*---------------------------------------------------------------------------
375 * Check if access rights for a remote socket are specified and
376 * create this socket. Return -1 otherwise.
377 *---------------------------------------------------------------------------*/
379 monitor_create_remote_socket(int portno)
381 struct sockaddr_in sa;
385 remotesockfd = socket(AF_INET, SOCK_STREAM, 0);
387 if(remotesockfd == -1)
389 llog(LL_MER, "could not create remote monitor socket: %s", strerror(errno));
395 if(setsockopt(remotesockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof val))
397 llog(LL_MER, "could not setsockopt: %s", strerror(errno));
401 memset(&sa, 0, sizeof sa);
402 sa.sin_len = sizeof sa;
403 sa.sin_family = AF_INET;
404 sa.sin_port = htons(portno);
405 sa.sin_addr.s_addr = htonl(INADDR_ANY);
407 if(bind(remotesockfd, (struct sockaddr *)&sa, sizeof sa) == -1)
409 llog(LL_MER, "could not bind remote monitor socket to port %d: %s", portno, strerror(errno));
413 if(listen(remotesockfd, 0))
415 llog(LL_MER, "could not listen on monitor socket: %s", strerror(errno));
419 return(remotesockfd);
423 /*---------------------------------------------------------------------------
424 * Check if access rights for a local socket are specified and
425 * create this socket. Return -1 otherwise.
426 *---------------------------------------------------------------------------*/
428 monitor_create_local_socket(void)
431 struct sockaddr_un sa;
433 /* check for a local entry */
435 if (local_rights == NULL)
438 /* create and setup socket */
440 s = socket(AF_LOCAL, SOCK_STREAM, 0);
444 llog(LL_MER, "could not create local monitor socket, errno = %d", errno);
448 unlink(local_rights->name);
450 memset(&sa, 0, sizeof sa);
451 sa.sun_len = sizeof sa;
452 sa.sun_family = AF_LOCAL;
453 strcpy(sa.sun_path, local_rights->name);
455 if (bind(s, (struct sockaddr *)&sa, SUN_LEN(&sa)))
457 llog(LL_MER, "could not bind local monitor socket [%s], errno = %d", local_rights->name, errno);
461 chmod(local_rights->name, 0500);
465 llog(LL_MER, "could not listen on local monitor socket, errno = %d", errno);
472 /*---------------------------------------------------------------------------
473 * Prepare a fd_set for a select call. Add all our local
474 * filedescriptors to the set, increment max_fd if appropriate.
475 *---------------------------------------------------------------------------*/
477 monitor_prepselect(fd_set *selset, int *max_fd)
479 struct monitor_connection * con;
481 for (con = TAILQ_FIRST(&connections); con != NULL; con = TAILQ_NEXT(con, connections))
492 /*---------------------------------------------------------------------------
493 * Check if the result from a select call indicates something
495 *---------------------------------------------------------------------------*/
497 monitor_handle_input(fd_set *selset)
499 struct monitor_connection * con, * next;
501 for (next = NULL, con = TAILQ_FIRST(&connections); con != NULL; con = next)
504 next = TAILQ_NEXT(con, connections);
506 if (FD_ISSET(fd, selset))
508 /* handle command from this client */
510 if (monitor_command(con, fd, con->rights) != 0)
512 /* broken or closed connection */
514 char source[FILENAME_MAX];
516 strcpy(source, con->source);
517 TAILQ_REMOVE(&connections, con, connections);
519 llog(LL_DMN, "monitor closed from %s", source );
524 /* all connections gone? */
526 if (TAILQ_FIRST(&connections) == NULL)
530 /*---------------------------------------------------------------------------
531 * Try new incoming connection on the given socket.
532 * Setup client descriptor and send initial data.
533 *---------------------------------------------------------------------------*/
535 monitor_handle_connect(int sockfd, int is_local)
537 struct monitor_connection *con;
538 struct monitor_rights *rp;
540 #ifndef I4B_NOTCPIP_MONITOR
541 struct sockaddr_in ia;
545 struct sockaddr_un ua;
546 u_int8_t idata[I4B_MON_IDATA_SIZE];
547 int fd = -1, s, i, r_mask, t_events;
548 char source[FILENAME_MAX];
550 /* accept the connection */
555 fd = accept(sockfd, (struct sockaddr *)&ua, &s);
556 strcpy(source, "local");
558 #ifndef I4B_NOTCPIP_MONITOR
565 fd = accept(sockfd, (struct sockaddr *)&ia, &s);
567 hp = gethostbyaddr((char *)&ia.sin_addr, 4, AF_INET);
570 snprintf(source, sizeof source, "%s (%s)", inet_ntoa(ia.sin_addr), inet_ntoa(ia.sin_addr));
572 snprintf(source, sizeof source, "%s (%s)", hp->h_name, inet_ntoa(ia.sin_addr));
574 memcpy(&ha, &ia.sin_addr.s_addr, sizeof ha);
580 /* check the access rights of this connection */
584 for (rp = TAILQ_FIRST(&rights); rp != NULL; rp = TAILQ_NEXT(rp, list))
594 #ifndef I4B_NOTCPIP_MONITOR
598 if((ha & rp->mask) == rp->net)
609 /* no rights - go away */
610 llog(LL_MER, "monitor access denied from %s", source);
617 con = malloc(sizeof(struct monitor_connection));
618 memset(con, 0, sizeof *con);
619 TAILQ_INSERT_TAIL(&connections, con, connections);
621 con->rights = r_mask;
622 strcpy(con->source, source);
624 llog(LL_DMN, "monitor opened from %s rights 0x%x", source, r_mask);
626 /* send initial data */
627 I4B_PREP_CMD(idata, I4B_MON_IDATA_CODE);
628 I4B_PUT_2B(idata, I4B_MON_IDATA_VERSMAJOR, MPROT_VERSION);
629 I4B_PUT_2B(idata, I4B_MON_IDATA_VERSMINOR, MPROT_REL);
630 I4B_PUT_2B(idata, I4B_MON_IDATA_NUMCTRL, ncontroller);
631 I4B_PUT_2B(idata, I4B_MON_IDATA_NUMENTR, nentries);
632 I4B_PUT_4B(idata, I4B_MON_IDATA_CLACCESS, r_mask);
634 if((sock_write(fd, idata, sizeof idata)) == -1)
636 llog(LL_MER, "monitor_handle_connect: sock_write 1 error - %s", strerror(errno));
639 for (i = 0; i < ncontroller; i++)
641 u_int8_t ictrl[I4B_MON_ICTRL_SIZE];
643 I4B_PREP_CMD(ictrl, I4B_MON_ICTRL_CODE);
644 I4B_PUT_STR(ictrl, I4B_MON_ICTRL_NAME, name_of_controller(isdn_ctrl_tab[i].ctrl_type, isdn_ctrl_tab[i].card_type));
645 I4B_PUT_2B(ictrl, I4B_MON_ICTRL_BUSID, 0);
646 I4B_PUT_4B(ictrl, I4B_MON_ICTRL_FLAGS, 0);
647 I4B_PUT_4B(ictrl, I4B_MON_ICTRL_NCHAN, 2);
649 if((sock_write(fd, ictrl, sizeof ictrl)) == -1)
651 llog(LL_MER, "monitor_handle_connect: sock_write 2 error - %s", strerror(errno));
656 /* send device names from entries */
658 for(i=0; i < nentries; i++) /* walk thru all entries */
660 u_int8_t ictrl[I4B_MON_IDEV_SIZE];
663 p = &cfg_entry_tab[i]; /* get ptr to enry */
665 snprintf(nbuf, sizeof(nbuf), "%s%d ", bdrivername(p->usrdevicename), p->usrdeviceunit);
667 I4B_PREP_CMD(ictrl, I4B_MON_IDEV_CODE);
668 /*XXX*/ I4B_PUT_2B(ictrl, I4B_MON_IDEV_STATE, 1);
669 I4B_PUT_STR(ictrl, I4B_MON_IDEV_NAME, nbuf);
671 if((sock_write(fd, ictrl, sizeof ictrl)) == -1)
673 llog(LL_MER, "monitor_handle_connect: sock_write 3 error - %s", strerror(errno));
677 /*XXX*/ t_events = con->events;
678 /*XXX*/ con->events = -1;
680 /* current state of controller(s) */
682 for(i=0; i < ncontroller; i++)
684 monitor_evnt_tei(i, isdn_ctrl_tab[i].tei);
685 monitor_evnt_l12stat(i, LAYER_ONE, isdn_ctrl_tab[i].l1stat);
686 monitor_evnt_l12stat(i, LAYER_TWO, isdn_ctrl_tab[i].l2stat);
689 /* current state of entries */
691 for(i=0; i < nentries; i++)
693 cfg_entry_t *cep = &cfg_entry_tab[i];
695 if(cep->state == ST_CONNECTED)
697 monitor_evnt_connect(cep);
698 monitor_evnt_acct(cep);
699 monitor_evnt_charge(cep, cep->charge, 1);
703 /*XXX*/ con->events = t_events;
707 /*---------------------------------------------------------------------------
708 * dump all monitor rights
709 *---------------------------------------------------------------------------*/
711 cmd_dump_rights(int fd, int r_mask, u_int8_t *cmd, const char *source)
713 struct monitor_rights * r;
715 u_int8_t drini[I4B_MON_DRINI_SIZE];
716 u_int8_t dr[I4B_MON_DR_SIZE];
718 for (num_rights = 0, r = TAILQ_FIRST(&rights); r != NULL; r = TAILQ_NEXT(r, list))
721 I4B_PREP_EVNT(drini, I4B_MON_DRINI_CODE);
722 I4B_PUT_2B(drini, I4B_MON_DRINI_COUNT, num_rights);
724 if((sock_write(fd, drini, sizeof drini)) == -1)
726 llog(LL_MER, "cmd_dump_rights: sock_write 1 error - %s", strerror(errno));
729 for (r = TAILQ_FIRST(&rights); r != NULL; r = TAILQ_NEXT(r, list))
731 I4B_PREP_EVNT(dr, I4B_MON_DR_CODE);
732 I4B_PUT_4B(dr, I4B_MON_DR_RIGHTS, r->rights);
733 I4B_PUT_4B(dr, I4B_MON_DR_NET, r->net);
734 I4B_PUT_4B(dr, I4B_MON_DR_MASK, r->mask);
735 I4B_PUT_1B(dr, I4B_MON_DR_LOCAL, r->local);
736 if((sock_write(fd, dr, sizeof dr)) == -1)
738 llog(LL_MER, "cmd_dump_rights: sock_write 2 error - %s", strerror(errno));
743 /*---------------------------------------------------------------------------
745 *---------------------------------------------------------------------------*/
747 cmd_reread_cfg(int fd, int rights, u_int8_t *cmd, const char * source)
752 /*---------------------------------------------------------------------------
753 * drop one connection
754 *---------------------------------------------------------------------------*/
756 cmd_hangup(int fd, int rights, u_int8_t *cmd, const char * source)
758 int channel = I4B_GET_4B(cmd, I4B_MON_HANGUP_CHANNEL);
759 int ctrl = I4B_GET_4B(cmd, I4B_MON_HANGUP_CTRL);
761 hangup_channel(ctrl, channel, source);
764 /*---------------------------------------------------------------------------
765 * dump all active monitor connections
766 *---------------------------------------------------------------------------*/
768 cmd_dump_mcons(int fd, int rights, u_int8_t *cmd, const char * source)
771 struct monitor_connection *con;
772 u_int8_t dcini[I4B_MON_DCINI_SIZE];
774 for (num_connections = 0, con = TAILQ_FIRST(&connections); con != NULL; con = TAILQ_NEXT(con, connections))
777 I4B_PREP_EVNT(dcini, I4B_MON_DCINI_CODE);
778 I4B_PUT_2B(dcini, I4B_MON_DCINI_COUNT, num_connections);
780 if((sock_write(fd, dcini, sizeof dcini)) == -1)
782 llog(LL_MER, "cmd_dump_mcons: sock_write 1 error - %s", strerror(errno));
785 for (con = TAILQ_FIRST(&connections); con != NULL; con = TAILQ_NEXT(con, connections))
787 #ifndef I4B_NOTCPIP_MONITOR
789 struct sockaddr_in name;
791 u_int8_t dc[I4B_MON_DC_SIZE];
793 I4B_PREP_EVNT(dc, I4B_MON_DC_CODE);
794 I4B_PUT_4B(dc, I4B_MON_DC_RIGHTS, con->rights);
796 #ifndef I4B_NOTCPIP_MONITOR
797 namelen = sizeof name;
799 if (getpeername(con->sock, (struct sockaddr*)&name, &namelen) == 0)
800 memcpy(dc+I4B_MON_DC_WHO, &name.sin_addr, sizeof name.sin_addr);
802 if((sock_write(fd, dc, sizeof dc)) == -1)
804 llog(LL_MER, "cmd_dump_mcons: sock_write 2 error - %s", strerror(errno));
809 /*---------------------------------------------------------------------------
810 * Handle a command from the given socket. The client
811 * has rights as specified in the rights parameter.
812 * Return non-zero if connection is closed.
813 *---------------------------------------------------------------------------*/
815 monitor_command(struct monitor_connection * con, int fd, int rights)
817 char cmd[I4B_MAX_MON_CLIENT_CMD];
820 /* command dispatch table */
821 typedef void (*cmd_func_t)(int fd, int rights, u_int8_t *cmd, const char *source);
824 cmd_func_t call; /* function to execute */
825 u_int rights; /* necessary rights */
829 /* 1 */ { cmd_dump_rights, I4B_CA_COMMAND_FULL },
830 /* 2 */ { cmd_dump_mcons, I4B_CA_COMMAND_FULL },
831 /* 3 */ { cmd_reread_cfg, I4B_CA_COMMAND_FULL },
832 /* 4 */ { cmd_hangup, I4B_CA_COMMAND_FULL },
834 #define NUMCMD (sizeof cmd_tab / sizeof cmd_tab[0])
839 /* Network transfer may deliver two or more packets concatenated.
840 * Peek at the header and read only one event at a time... */
842 ioctl(fd, FIONREAD, &u);
844 if (u < I4B_MON_CMD_HDR)
848 /* llog(LL_MER, "monitor read 0 bytes"); */
849 /* socket closed by peer */
853 return 0; /* not enough data there yet */
856 bytes = recv(fd, cmd, I4B_MON_CMD_HDR, MSG_PEEK);
858 if (bytes < I4B_MON_CMD_HDR)
860 llog(LL_MER, "monitor read only %d bytes", bytes);
861 return 0; /* errh? something must be wrong... */
864 bytes = I4B_GET_2B(cmd, I4B_MON_CMD_LEN);
866 if (bytes >= sizeof cmd)
869 llog(LL_MER, "monitor: garbage on connection");
873 /* now we know the size, it fits, so lets read it! */
875 if(sock_read(fd, cmd, bytes) <= 0)
877 llog(LL_MER, "monitor: sock_read <= 0");
883 code = I4B_GET_2B(cmd, I4B_MON_CMD);
885 /* special case: may modify our connection descriptor, is
886 * beyound all rights checks */
888 if (code == I4B_MON_CCMD_SETMASK)
892 u_int major = I4B_GET_2B(cmd, I4B_MON_ICLIENT_VERMAJOR);
893 u_int minor = I4B_GET_2B(cmd, I4B_MON_ICLIENT_VERMINOR);
896 int events = I4B_GET_4B(cmd, I4B_MON_ICLIENT_EVENTS);
897 con->events = events & rights;
901 if (code < 0 || code >= NUMCMD)
903 llog(LL_MER, "illegal command from client, code = %d\n",
908 if (cmd_tab[code].call == NULL)
911 if ((cmd_tab[code].rights & rights) == cmd_tab[code].rights)
912 cmd_tab[code].call(fd, rights, cmd, con->source);
917 /*---------------------------------------------------------------------------
918 * Check if somebody would receive an event with this mask.
919 * We are lazy and try to avoid assembling unneccesary packets.
920 * Return 0 if no one interested, nonzero otherwise.
921 *---------------------------------------------------------------------------*/
925 struct monitor_connection * con;
927 for (con = TAILQ_FIRST(&connections); con != NULL; con = TAILQ_NEXT(con, connections))
929 if ((con->events & mask) == mask)
935 /*---------------------------------------------------------------------------
936 * exec hangup command
937 *---------------------------------------------------------------------------*/
939 hangup_channel(int controller, int channel, const char *source)
941 cfg_entry_t * cep = NULL;
944 if(controller < ncontroller)
946 if(isdn_ctrl_tab[controller].state != CTRL_UP)
948 for (i = 0; i < isdn_ctrl_tab[controller].nbch; i++)
950 if(isdn_ctrl_tab[controller].stateb[i] != CHAN_IDLE)
952 cep = get_cep_by_cc(controller, i);
953 if (cep != NULL && cep->isdnchannelused == channel &&
954 cep->isdncontrollerused == controller)
963 llog(LL_CHD, "%05d %s manual disconnect (remote from %s)", cep->cdid, cep->name, source);
968 /*---------------------------------------------------------------------------
969 * Send an event to every connection interested in this kind of
971 *---------------------------------------------------------------------------*/
973 monitor_broadcast(int mask, u_int8_t *pkt, size_t bytes)
975 struct monitor_connection *con;
977 for (con = TAILQ_FIRST(&connections); con != NULL; con = TAILQ_NEXT(con, connections))
979 if ((con->events & mask) == mask)
983 if((sock_write(fd, pkt, bytes)) == -1)
985 llog(LL_MER, "monitor_broadcast: sock_write error - %s", strerror(errno));
991 /*---------------------------------------------------------------------------
992 * Post a logfile event
993 *---------------------------------------------------------------------------*/
995 monitor_evnt_log(int prio, const char * what, const char * msg)
997 u_int8_t evnt[I4B_MON_LOGEVNT_SIZE];
1000 if (!anybody(I4B_CA_EVNT_I4B))
1005 I4B_PREP_EVNT(evnt, I4B_MON_LOGEVNT_CODE);
1006 I4B_PUT_4B(evnt, I4B_MON_LOGEVNT_TSTAMP, (long)now);
1007 I4B_PUT_4B(evnt, I4B_MON_LOGEVNT_PRIO, prio);
1008 I4B_PUT_STR(evnt, I4B_MON_LOGEVNT_WHAT, what);
1009 I4B_PUT_STR(evnt, I4B_MON_LOGEVNT_MSG, msg);
1011 monitor_broadcast(I4B_CA_EVNT_I4B, evnt, sizeof evnt);
1014 /*---------------------------------------------------------------------------
1015 * Post a charging event on the connection described
1016 * by the given config entry.
1017 *---------------------------------------------------------------------------*/
1019 monitor_evnt_charge(cfg_entry_t *cep, int units, int estimate)
1023 u_int8_t evnt[I4B_MON_CHRG_SIZE];
1025 mask = (cep->direction == DIR_IN) ? I4B_CA_EVNT_CALLIN : I4B_CA_EVNT_CALLOUT;
1032 I4B_PREP_EVNT(evnt, I4B_MON_CHRG_CODE);
1033 I4B_PUT_4B(evnt, I4B_MON_CHRG_TSTAMP, (long)now);
1034 I4B_PUT_4B(evnt, I4B_MON_CHRG_CTRL, cep->isdncontrollerused);
1035 I4B_PUT_4B(evnt, I4B_MON_CHRG_CHANNEL, cep->isdnchannelused);
1036 I4B_PUT_4B(evnt, I4B_MON_CHRG_UNITS, units);
1037 I4B_PUT_4B(evnt, I4B_MON_CHRG_ESTIMATED, estimate ? 1 : 0);
1039 monitor_broadcast(mask, evnt, sizeof evnt);
1042 /*---------------------------------------------------------------------------
1043 * Post a connection event
1044 *---------------------------------------------------------------------------*/
1046 monitor_evnt_connect(cfg_entry_t *cep)
1048 u_int8_t evnt[I4B_MON_CONNECT_SIZE];
1049 char devname[I4B_MAX_MON_STRING];
1053 mask = (cep->direction == DIR_IN) ? I4B_CA_EVNT_CALLIN : I4B_CA_EVNT_CALLOUT;
1060 snprintf(devname, sizeof devname, "%s%d", bdrivername(cep->usrdevicename), cep->usrdeviceunit);
1062 I4B_PREP_EVNT(evnt, I4B_MON_CONNECT_CODE);
1063 I4B_PUT_4B(evnt, I4B_MON_CONNECT_TSTAMP, (long)now);
1064 I4B_PUT_4B(evnt, I4B_MON_CONNECT_DIR, cep->direction == DIR_OUT ? 1 : 0);
1065 I4B_PUT_4B(evnt, I4B_MON_CONNECT_CTRL, cep->isdncontrollerused);
1066 I4B_PUT_4B(evnt, I4B_MON_CONNECT_CHANNEL, cep->isdnchannelused);
1067 I4B_PUT_STR(evnt, I4B_MON_CONNECT_CFGNAME, cep->name);
1068 I4B_PUT_STR(evnt, I4B_MON_CONNECT_DEVNAME, devname);
1070 if(cep->direction == DIR_OUT)
1072 I4B_PUT_STR(evnt, I4B_MON_CONNECT_REMPHONE, cep->remote_phone_dialout.number);
1073 I4B_PUT_STR(evnt, I4B_MON_CONNECT_LOCPHONE, cep->local_phone_dialout.number);
1077 I4B_PUT_STR(evnt, I4B_MON_CONNECT_REMPHONE, cep->real_phone_incoming.number);
1078 I4B_PUT_STR(evnt, I4B_MON_CONNECT_LOCPHONE, cep->local_phone_incoming.number);
1080 monitor_broadcast(mask, evnt, sizeof evnt);
1083 /*---------------------------------------------------------------------------
1084 * Post a disconnect event
1085 *---------------------------------------------------------------------------*/
1087 monitor_evnt_disconnect(cfg_entry_t *cep)
1089 u_int8_t evnt[I4B_MON_DISCONNECT_SIZE];
1093 mask = (cep->direction == DIR_IN) ? I4B_CA_EVNT_CALLIN : I4B_CA_EVNT_CALLOUT;
1100 I4B_PREP_EVNT(evnt, I4B_MON_DISCONNECT_CODE);
1101 I4B_PUT_4B(evnt, I4B_MON_DISCONNECT_TSTAMP, (long)now);
1102 I4B_PUT_4B(evnt, I4B_MON_DISCONNECT_CTRL, cep->isdncontrollerused);
1103 I4B_PUT_4B(evnt, I4B_MON_DISCONNECT_CHANNEL, cep->isdnchannelused);
1105 monitor_broadcast(mask, evnt, sizeof evnt);
1108 /*---------------------------------------------------------------------------
1109 * Post an up/down event
1110 *---------------------------------------------------------------------------*/
1112 monitor_evnt_updown(cfg_entry_t *cep, int up)
1114 u_int8_t evnt[I4B_MON_UPDOWN_SIZE];
1118 mask = (cep->direction == DIR_IN) ? I4B_CA_EVNT_CALLIN : I4B_CA_EVNT_CALLOUT;
1125 I4B_PREP_EVNT(evnt, I4B_MON_UPDOWN_CODE);
1126 I4B_PUT_4B(evnt, I4B_MON_UPDOWN_TSTAMP, (long)now);
1127 I4B_PUT_4B(evnt, I4B_MON_UPDOWN_CTRL, cep->isdncontrollerused);
1128 I4B_PUT_4B(evnt, I4B_MON_UPDOWN_CHANNEL, cep->isdnchannelused);
1129 I4B_PUT_4B(evnt, I4B_MON_UPDOWN_ISUP, up);
1131 monitor_broadcast(mask, evnt, sizeof evnt);
1134 /*---------------------------------------------------------------------------
1135 * Post a Layer1/2 status change event
1136 *---------------------------------------------------------------------------*/
1138 monitor_evnt_l12stat(int controller, int layer, int state)
1140 u_int8_t evnt[I4B_MON_L12STAT_SIZE];
1143 if(!anybody(I4B_CA_EVNT_I4B))
1148 I4B_PREP_EVNT(evnt, I4B_MON_L12STAT_CODE);
1149 I4B_PUT_4B(evnt, I4B_MON_L12STAT_TSTAMP, (long)now);
1150 I4B_PUT_4B(evnt, I4B_MON_L12STAT_CTRL, controller);
1151 I4B_PUT_4B(evnt, I4B_MON_L12STAT_LAYER, layer);
1152 I4B_PUT_4B(evnt, I4B_MON_L12STAT_STATE, state);
1154 monitor_broadcast(I4B_CA_EVNT_I4B, evnt, sizeof evnt);
1157 /*---------------------------------------------------------------------------
1158 * Post a TEI change event
1159 *---------------------------------------------------------------------------*/
1161 monitor_evnt_tei(int controller, int tei)
1163 u_int8_t evnt[I4B_MON_TEI_SIZE];
1166 if(!anybody(I4B_CA_EVNT_I4B))
1171 I4B_PREP_EVNT(evnt, I4B_MON_TEI_CODE);
1172 I4B_PUT_4B(evnt, I4B_MON_TEI_TSTAMP, (long)now);
1173 I4B_PUT_4B(evnt, I4B_MON_TEI_CTRL, controller);
1174 I4B_PUT_4B(evnt, I4B_MON_TEI_TEI, tei);
1176 monitor_broadcast(I4B_CA_EVNT_I4B, evnt, sizeof evnt);
1179 /*---------------------------------------------------------------------------
1180 * Post an accounting event
1181 *---------------------------------------------------------------------------*/
1183 monitor_evnt_acct(cfg_entry_t *cep)
1185 u_int8_t evnt[I4B_MON_ACCT_SIZE];
1188 if(!anybody(I4B_CA_EVNT_I4B))
1193 I4B_PREP_EVNT(evnt, I4B_MON_ACCT_CODE);
1194 I4B_PUT_4B(evnt, I4B_MON_ACCT_TSTAMP, (long)now);
1196 I4B_PUT_4B(evnt, I4B_MON_ACCT_CTRL, cep->isdncontrollerused);
1197 I4B_PUT_4B(evnt, I4B_MON_ACCT_CHAN, cep->isdnchannelused);
1198 I4B_PUT_4B(evnt, I4B_MON_ACCT_OBYTES, cep->outbytes);
1199 I4B_PUT_4B(evnt, I4B_MON_ACCT_OBPS, cep->outbps);
1200 I4B_PUT_4B(evnt, I4B_MON_ACCT_IBYTES, cep->inbytes);
1201 I4B_PUT_4B(evnt, I4B_MON_ACCT_IBPS, cep->inbps);
1203 monitor_broadcast(I4B_CA_EVNT_I4B, evnt, sizeof evnt);
1206 /*---------------------------------------------------------------------------
1207 * read from a socket
1208 *---------------------------------------------------------------------------*/
1210 sock_read(int fd, void *buf, size_t nbytes)
1221 if((nread = read(fd, ptr, nleft)) < 0)
1240 return(nbytes - nleft);
1243 /*---------------------------------------------------------------------------
1245 *---------------------------------------------------------------------------*/
1247 sock_write(int fd, void *buf, size_t nbytes)
1258 if((nwritten = write(fd, ptr, nleft)) <= 0)
1276 struct monitor_rights * monitor_next_rights(const struct monitor_rights *r)
1279 return TAILQ_FIRST(&rights);
1281 return TAILQ_NEXT(r, list);
1284 #endif /* I4B_EXTERNAL_MONITOR */