1 /* $NetBSD: main.c,v 1.10 2008/07/15 17:51:38 perry Exp $ */
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Martin Husemann <martin@NetBSD.org>.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
43 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <sys/ioctl.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
53 int getopt(int nargc
, char * const nargv
[], const char *ostr
);
54 #define close(f) closesocket(f)
55 #define sleep(s) Sleep(s*1000)
56 #define vsnprintf _vsnprintf
64 #include "monprivate.h"
68 #define AF_LOCAL AF_UNIX
78 * Local function prototypes
80 static int connect_local(char *sockpath
);
81 static int connect_remote(char *host
, int portno
);
82 static void usage(void);
83 static void mloop(void);
84 static void handle_input(void);
85 static void print_menu(void);
86 static void print_logevent(time_t tstamp
, int prio
, char * what
, char * msg
);
87 static void print_charge(time_t tstamp
, int controller
, int channel
, int units
, int estimated
);
88 static void print_connect(time_t tstamp
, int dir
, int controller
, int channel
, char * cfgname
, char * devname
, char * remphone
, char * locphone
);
89 static void print_disconnect(time_t tstamp
, int controller
, int channel
);
90 static void print_updown(time_t tstamp
, int contoller
, int channel
, int isup
);
91 static void handle_event(u_int8_t
*msg
, int len
);
93 static void dump_event(u_int8_t
*msg
, int len
, int readflag
);
96 static ssize_t
sock_read(int fd
, void *buf
, size_t nbytes
);
97 static ssize_t
sock_write(int fd
, void *buf
, size_t nbytes
);
99 static void mprintf(const char *fmt
, ...)
100 __attribute__((__format__(__printf__
, 1, 2)));
105 static int debug
= 0;
106 #define DBG_DUMPALL 0x01
107 #define DBG_PSEND 0x02
109 static int monsock
= -1;
110 static int state
= ST_INIT
;
111 static int sub_state
= 0;
112 static int sub_state_count
= 0;
114 static int isdn_major
= 0;
115 static int isdn_minor
= 0;
116 static u_int32_t rights
= 0;
118 static char *logfilename
= NULL
;
119 static FILE *lfp
= NULL
;
121 /*---------------------------------------------------------------------------
122 * Display usage and exit
123 *---------------------------------------------------------------------------*/
127 fprintf(stderr
, "\n");
128 fprintf(stderr
, "isdnmonitor - version %02d.%02d.%d, (protocol %02d.%02d)\n", VERSION
, REL
, STEP
, MPROT_VERSION
, MPROT_REL
);
130 fprintf(stderr
, " usage: isdnmonitor [-c] [-d val] [-f name] [-h host] [-p port]\n");
132 fprintf(stderr
, " usage: isdnmonitor [-c] [-d val] [-f name] [-h host] [-l path] [-p port]\n");
134 fprintf(stderr
, " -c switch to curses fullscreen output\n");
135 fprintf(stderr
, " -d <val> debug flags (see source ...)\n");
136 fprintf(stderr
, " -dn no debug output on fullscreen display\n");
137 fprintf(stderr
, " -f <name> filename to log output to\n");
138 fprintf(stderr
, " -h <host> hostname/address to connect to\n");
140 fprintf(stderr
, " -l <path> pathname to local domain socket to connect to\n");
142 fprintf(stderr
, " -p <port> portnumber to use to connect to remote host\n");
146 /*---------------------------------------------------------------------------
147 * Parse command line, startup monitor client
148 *---------------------------------------------------------------------------*/
149 int main(int argc
, char **argv
)
155 WSAStartup(MAKEWORD(2, 0), &wsCaps
);
158 portno
= DEF_MONPORT
;
162 while((i
= getopt(argc
, argv
, "cd:f:h:p:l:")) != -1)
164 while((i
= getopt(argc
, argv
, "cd:f:h:p:")) != -1)
179 if ((sscanf(optarg
, "%i", &debug
)) != 1)
184 logfilename
= optarg
;
195 if ((sscanf(optarg
, "%i", &portno
)) != 1)
205 if (hostname
&& sockpath
)
207 fprintf(stderr
, "Error: can not use local socket path on remote machine\n"
208 "conflicting options -h and -l!\n");
214 monsock
= connect_local(sockpath
);
222 monsock
= connect_remote(hostname
, portno
);
231 fprintf(stderr
, "Could not connect to i4b isdn daemon.\n");
235 if (logfilename
!= NULL
)
237 if ((lfp
= fopen(logfilename
, "w")) == NULL
)
239 fprintf(stderr
, "could not open logfile [%s], %s\n", logfilename
, strerror(errno
));
245 signal(SIGPIPE
, SIG_IGN
);
255 /*---------------------------------------------------------------------------
256 * Connect via tcp/ip.
257 * Return socket if successful, -1 on error.
258 ---------------------------------------------------------------------------*/
260 connect_remote(char *host
, int portnum
)
262 struct sockaddr_in sa
;
266 h
= gethostbyname(host
);
270 fprintf(stderr
, "could not resolve hostname '%s'\n", host
);
274 remotesockfd
= socket(AF_INET
, SOCK_STREAM
, 0);
276 if (remotesockfd
== -1)
278 fprintf(stderr
, "could not create remote monitor socket: %s\n", strerror(errno
));
282 memset(&sa
, 0, sizeof(sa
));
285 sa
.sin_len
= sizeof(sa
);
287 sa
.sin_family
= AF_INET
;
288 sa
.sin_port
= htons(portnum
);
290 memcpy(&sa
.sin_addr
.s_addr
, h
->h_addr_list
[0], sizeof(sa
.sin_addr
.s_addr
));
292 if (connect(remotesockfd
, (struct sockaddr
*)&sa
, sizeof(sa
)) == -1)
294 fprintf(stderr
, "could not connect remote monitor: %s\n", strerror(errno
));
302 /*---------------------------------------------------------------------------
304 * Return socket on success, -1 on failure.
305 *---------------------------------------------------------------------------*/
307 connect_local(char *clsockpath
)
310 struct sockaddr_un sa
;
312 /* check path length */
313 if (strlen(clsockpath
) >= sizeof(sa
.sun_path
))
315 fprintf(stderr
, "pathname to long for local socket: %s\n",
320 /* create and setup socket */
321 s
= socket(AF_LOCAL
, SOCK_STREAM
, 0);
325 fprintf(stderr
, "could not create local monitor socket:%s\n", strerror(errno
));
329 memset(&sa
, 0, sizeof(sa
));
331 sa
.sun_len
= sizeof(sa
);
332 sa
.sun_family
= AF_LOCAL
;
333 strlcpy(sa
.sun_path
, clsockpath
, sizeof(sa
.sun_path
));
335 if (connect(s
, (struct sockaddr
*)&sa
, sizeof(sa
)))
337 fprintf(stderr
, "could not connect local monitor socket [%s]: %s\n", clsockpath
, strerror(errno
));
344 /*---------------------------------------------------------------------------*
345 * data from keyboard available, read and process it
346 *---------------------------------------------------------------------------*/
355 case 0x0c: /* control L */
367 /*---------------------------------------------------------------------------
369 *---------------------------------------------------------------------------*/
373 struct pollfd set
[2];
375 set
[0].fd
= STDIN_FILENO
;
376 set
[0].events
= POLLIN
;
378 set
[1].events
= POLLIN
;
381 poll(set
, 2, INFTIM
);
383 if (set
[0].revents
& POLLIN
)
386 if (fullscreen
&& curses_ready
)
396 if (set
[1].revents
& POLLIN
)
401 /* Network transfer may deliver two or more packets concatenated.
402 * Peek at the header and read only one event at a time... */
404 bytes
= recv(monsock
, buf
, I4B_MON_EVNT_HDR
, MSG_PEEK
);
418 mprintf("remote isdnd has closed our connection\n");
423 fprintf(stderr
, "recv error: %s\n", strerror(errno
));
428 if (bytes
< I4B_MON_EVNT_HDR
)
429 continue; /* errh? something must be wrong... */
431 bytes
= I4B_GET_2B(buf
, I4B_MON_EVNT_LEN
);
433 if (bytes
>= (int)sizeof(buf
))
435 fprintf(stderr
, "mloop: socket recv buffer overflow %d!\n", bytes
);
439 /* now we know the size, it fits, so lets read it! */
441 ret
= sock_read(monsock
, buf
, bytes
);
450 mprintf("remote isdnd has closed our connection\n");
455 mprintf("error reading from isdnd: %s", strerror(errno
));
459 if (debug
& DBG_DUMPALL
)
460 dump_event(buf
, ret
, 1);
462 handle_event(buf
, ret
);
469 * Dump a complete event packet.
471 static void dump_event(u_int8_t
*msg
, int len
, int doread
)
476 mprintf("read from socket:");
478 mprintf("write to socket:");
480 for (i
= 0; i
< len
; i
++)
483 mprintf("\n%02d: ", i
);
484 mprintf("0x%02x %c ", msg
[i
], isprint(msg
[i
]) ? msg
[i
] : '.');
491 print_logevent(time_t tstamp
, int prio
, char * what
, char * msg
)
494 strftime(buf
, sizeof(buf
), I4B_TIME_FORMAT
, localtime(&tstamp
));
495 mprintf("log: %s prio %d what=%s msg=%s\n", buf
, prio
, what
, msg
);
500 if ((!debug_noscreen
) || (debug_noscreen
&& (((strcmp(what
, "DBG"))) != 0)))
503 * FreeBSD-current integrated ncurses. Since then it is no longer possible
504 * to write to the last column in the logfilewindow without causing an
505 * automatic newline to occur resulting in a blank line in that window.
508 #include <osreldate.h>
510 #if defined(__FreeBSD_version) && __FreeBSD_version >= 400009
511 #warning "FreeBSD ncurses is buggy: write to last column = auto newline!"
512 wprintw(lower_w
, "%s %s %-.*s\n", buf
, what
,
513 COLS
-((strlen(buf
))+(strlen(what
))+3), msg
);
515 wprintw(lower_w
, "%s %s %-.*s\n", buf
, what
,
516 (int)(COLS
-((strlen(buf
))+(strlen(what
))+2)), msg
);
525 print_charge(time_t tstamp
, int controller
, int channel
, int units
, int estimated
)
528 strftime(buf
, sizeof(buf
), I4B_TIME_FORMAT
, localtime(&tstamp
));
529 mprintf("%s: controller %d, channel %d, charge = %d%s\n",
530 buf
, controller
, channel
, units
, estimated
? " (estimated)" : "");
535 display_ccharge(CHPOS(controller
, channel
), units
);
537 display_charge(CHPOS(controller
, channel
), units
);
543 * Print a connect event.
544 * A real monitor would allocate state info for "channel" on this
547 static void print_connect(
548 time_t tstamp
, /* server time of event */
549 int outgoing
, /* 0 = incoming, 1 = outgoing */
550 int controller
, /* controller number */
551 int channel
, /* channel no, used to identify this connection until disconnect */
552 char * cfgname
, /* name of config entry/connection */
553 char * devnam
, /* device used (e.g. isp0) */
554 char * remphone
, /* phone no of remote side */
555 char * locphone
) /* local phone no */
560 remstate
[controller
].ch1state
= 1;
562 remstate
[controller
].ch2state
= 1;
564 strftime(buf
, sizeof(buf
), I4B_TIME_FORMAT
, localtime(&tstamp
));
567 mprintf("%s: calling out to '%s' [from msn: '%s']",
568 buf
, remphone
, locphone
);
570 mprintf("%s: incoming call from '%s' [to msn: '%s']",
571 buf
, remphone
, locphone
);
572 mprintf(", controller %d, channel %d, config '%s' on device '%s'\n",
573 controller
, channel
, cfgname
, devnam
);
577 display_connect(CHPOS(controller
, channel
), outgoing
, cfgname
, remphone
, devnam
);
582 * Print a disconnect event.
583 * A real monitor could free the "per connection" state
584 * for this channel now
587 print_disconnect(time_t tstamp
, int controller
, int channel
)
592 remstate
[controller
].ch1state
= 0;
594 remstate
[controller
].ch2state
= 0;
596 strftime(buf
, sizeof(buf
), I4B_TIME_FORMAT
, localtime(&tstamp
));
598 mprintf("%s: controller %d, channel %d disconnected\n",
599 buf
, controller
, channel
);
603 display_disconnect(CHPOS(controller
, channel
));
608 * Print an up- or down event
611 print_updown(time_t tstamp
, int controller
, int channel
, int isup
)
614 strftime(buf
, sizeof(buf
), I4B_TIME_FORMAT
, localtime(&tstamp
));
615 mprintf("%s: channel %d is %s\n",
616 buf
, channel
, isup
? "up" : "down");
620 * Print l1 / l2 status
623 print_l12stat(time_t tstamp
, int controller
, int layer
, int status
)
626 strftime(buf
, sizeof(buf
), I4B_TIME_FORMAT
, localtime(&tstamp
));
628 mprintf("%s: layer %d change on controller %d: %s\n",
629 buf
, layer
, controller
, status
? "up" : "down");
632 display_l12stat(controller
, layer
, status
);
640 print_tei(time_t tstamp
, int controller
, int tei
)
643 strftime(buf
, sizeof(buf
), I4B_TIME_FORMAT
, localtime(&tstamp
));
645 mprintf("%s: controller %d, TEI is %d\n",
646 buf
, controller
, tei
);
650 display_tei(controller
, tei
);
655 * Print accounting information
658 print_acct(time_t tstamp
, int controller
, int channel
, int obytes
, int obps
,
659 int ibytes
, int ibps
)
662 strftime(buf
, sizeof(buf
), I4B_TIME_FORMAT
, localtime(&tstamp
));
664 mprintf("%s: controller %d, channel %d: %d obytes, %d obps, %d ibytes, %d ibps\n",
665 buf
, controller
, channel
, obytes
, obps
, ibytes
, ibps
);
668 display_acct(CHPOS(controller
, channel
), obytes
, obps
, ibytes
, ibps
);
673 print_initialization(void)
678 if (curses_ready
== 0)
689 * Dispatch one message received from the daemon.
692 handle_event(u_int8_t
*msg
, int len
)
694 u_int8_t cmd
[I4B_MON_ICLIENT_SIZE
];
699 static int first
= 1;
703 case ST_INIT
: /* initial data */
705 isdn_major
= I4B_GET_2B(msg
, I4B_MON_IDATA_VERSMAJOR
);
706 isdn_minor
= I4B_GET_2B(msg
, I4B_MON_IDATA_VERSMINOR
);
707 nctrl
= I4B_GET_2B(msg
, I4B_MON_IDATA_NUMCTRL
);
708 nentries
= I4B_GET_2B(msg
, I4B_MON_IDATA_NUMENTR
);
709 rights
= I4B_GET_4B(msg
, I4B_MON_IDATA_CLACCESS
);
711 mprintf("remote protocol version is %02d.%02d\n", isdn_major
, isdn_minor
);
713 if (isdn_major
!= MPROT_VERSION
|| isdn_minor
!= MPROT_REL
)
715 fprintf(stderr
, "ERROR, remote protocol version mismatch:\n");
716 fprintf(stderr
, "\tremote major version is %02d, local major version is %02d\n", isdn_major
, MPROT_VERSION
);
717 fprintf(stderr
, "\tremote minor version is %02d, local minor version is %02d\n", isdn_minor
, MPROT_REL
);
721 mprintf("our rights = 0x%x\n", rights
);
730 else if (nentries
> 0)
738 print_initialization();
741 /* set maximum event mask */
742 I4B_PREP_CMD(cmd
, I4B_MON_CCMD_SETMASK
);
743 I4B_PUT_2B(cmd
, I4B_MON_ICLIENT_VERMAJOR
, MPROT_VERSION
);
744 I4B_PUT_2B(cmd
, I4B_MON_ICLIENT_VERMINOR
, MPROT_REL
);
745 I4B_PUT_4B(cmd
, I4B_MON_ICLIENT_EVENTS
, ~0U);
748 if (debug
& DBG_DUMPALL
)
749 dump_event(cmd
, sizeof(cmd
), 0);
752 if ((sock_write(monsock
, cmd
, sizeof(cmd
))) == -1)
754 fprintf(stderr
, "sock_write failed: %s\n", strerror(errno
));
759 case ST_ICTRL
: /* initial controller list */
763 mprintf("%d controller(s) found:\n", nctrl
);
765 mprintf("\tcontroller %d: %s\n", sub_state
++, msg
+I4B_MON_ICTRL_NAME
);
767 if (sub_state
>= nctrl
)
773 state
= ST_IDEV
; /* end of list reached */
779 print_initialization();
784 case ST_IDEV
: /* initial entry devicename list */
788 mprintf("%d entries found:\n", nentries
);
791 mprintf("\tentry %d: device %s\n", sub_state
++, msg
+I4B_MON_IDEV_NAME
);
793 strlcat(devbuf
, msg
+I4B_MON_IDEV_NAME
, sizeof(devbuf
));
794 /* strlcat(devbuf, " ", sizeof(devbuf)); */
796 if (sub_state
>= nentries
)
799 state
= ST_ANYEV
; /* end of list reached */
802 print_initialization();
806 case ST_ANYEV
: /* any event */
807 switch (I4B_GET_2B(msg
, I4B_MON_EVNT
))
809 case I4B_MON_DRINI_CODE
:
810 state
= ST_RIGHT
; /* list of rights entries will follow */
812 sub_state_count
= I4B_GET_2B(msg
, I4B_MON_DRINI_COUNT
);
813 mprintf("monitor rights:\n");
816 case I4B_MON_DCINI_CODE
:
819 sub_state_count
= I4B_GET_2B(msg
, I4B_MON_DCINI_COUNT
);
820 mprintf("monitor connections:\n");
823 case I4B_MON_LOGEVNT_CODE
:
824 print_logevent(I4B_GET_4B(msg
, I4B_MON_LOGEVNT_TSTAMP
),
825 I4B_GET_4B(msg
, I4B_MON_LOGEVNT_PRIO
),
826 msg
+I4B_MON_LOGEVNT_WHAT
,
827 msg
+I4B_MON_LOGEVNT_MSG
);
830 case I4B_MON_CHRG_CODE
:
831 print_charge(I4B_GET_4B(msg
, I4B_MON_CHRG_TSTAMP
),
832 I4B_GET_4B(msg
, I4B_MON_CHRG_CTRL
),
833 I4B_GET_4B(msg
, I4B_MON_CHRG_CHANNEL
),
834 I4B_GET_4B(msg
, I4B_MON_CHRG_UNITS
),
835 I4B_GET_4B(msg
, I4B_MON_CHRG_ESTIMATED
));
838 case I4B_MON_CONNECT_CODE
:
840 I4B_GET_4B(msg
, I4B_MON_CONNECT_TSTAMP
),
841 I4B_GET_4B(msg
, I4B_MON_CONNECT_DIR
),
842 I4B_GET_4B(msg
, I4B_MON_CONNECT_CTRL
),
843 I4B_GET_4B(msg
, I4B_MON_CONNECT_CHANNEL
),
844 msg
+I4B_MON_CONNECT_CFGNAME
,
845 msg
+I4B_MON_CONNECT_DEVNAME
,
846 msg
+I4B_MON_CONNECT_REMPHONE
,
847 msg
+I4B_MON_CONNECT_LOCPHONE
);
850 case I4B_MON_DISCONNECT_CODE
:
852 I4B_GET_4B(msg
, I4B_MON_DISCONNECT_TSTAMP
),
853 I4B_GET_4B(msg
, I4B_MON_DISCONNECT_CTRL
),
854 I4B_GET_4B(msg
, I4B_MON_DISCONNECT_CHANNEL
));
857 case I4B_MON_UPDOWN_CODE
:
859 I4B_GET_4B(msg
, I4B_MON_UPDOWN_TSTAMP
),
860 I4B_GET_4B(msg
, I4B_MON_UPDOWN_CTRL
),
861 I4B_GET_4B(msg
, I4B_MON_UPDOWN_CHANNEL
),
862 I4B_GET_4B(msg
, I4B_MON_UPDOWN_ISUP
));
864 case I4B_MON_L12STAT_CODE
:
866 I4B_GET_4B(msg
, I4B_MON_L12STAT_TSTAMP
),
867 I4B_GET_4B(msg
, I4B_MON_L12STAT_CTRL
),
868 I4B_GET_4B(msg
, I4B_MON_L12STAT_LAYER
),
869 I4B_GET_4B(msg
, I4B_MON_L12STAT_STATE
));
871 case I4B_MON_TEI_CODE
:
873 I4B_GET_4B(msg
, I4B_MON_TEI_TSTAMP
),
874 I4B_GET_4B(msg
, I4B_MON_TEI_CTRL
),
875 I4B_GET_4B(msg
, I4B_MON_TEI_TEI
));
877 case I4B_MON_ACCT_CODE
:
879 I4B_GET_4B(msg
, I4B_MON_ACCT_TSTAMP
),
880 I4B_GET_4B(msg
, I4B_MON_ACCT_CTRL
),
881 I4B_GET_4B(msg
, I4B_MON_ACCT_CHAN
),
882 I4B_GET_4B(msg
, I4B_MON_ACCT_OBYTES
),
883 I4B_GET_4B(msg
, I4B_MON_ACCT_OBPS
),
884 I4B_GET_4B(msg
, I4B_MON_ACCT_IBYTES
),
885 I4B_GET_4B(msg
, I4B_MON_ACCT_IBPS
));
888 mprintf("unknown event code: %d\n", I4B_GET_2B(msg
, I4B_MON_EVNT
));
892 case ST_RIGHT
: /* one record in a list of monitor rights */
893 rights
= I4B_GET_4B(msg
, I4B_MON_DR_RIGHTS
);
894 net
= I4B_GET_4B(msg
, I4B_MON_DR_NET
);
895 mask
= I4B_GET_4B(msg
, I4B_MON_DR_MASK
);
896 local
= I4B_GET_1B(msg
, I4B_MON_DR_LOCAL
);
900 mprintf("\tlocal: rights = %x\n", rights
);
904 mprintf("\tfrom: %d.%d.%d.%d, mask %d.%d.%d.%d, rights = %x\n",
905 (net
>> 24) & 0x00ff, (net
>> 16) & 0x00ff, (net
>> 8) & 0x00ff, net
& 0x00ff,
906 (mask
>> 24) & 0x00ff, (mask
>> 16) & 0x00ff, (mask
>> 8) & 0x00ff, mask
& 0x00ff,
912 if (sub_state
>= sub_state_count
)
915 print_initialization();
920 who
= I4B_GET_4B(msg
, I4B_MON_DC_WHO
);
921 rights
= I4B_GET_4B(msg
, I4B_MON_DC_RIGHTS
);
923 mprintf("\tfrom: %d.%d.%d.%d, rights = %x\n",
924 (who
>> 24) & 0x00ff, (who
>> 16) & 0x00ff, (who
>> 8) & 0x00ff, who
& 0x00ff,
929 if (sub_state
>= sub_state_count
)
932 print_initialization();
937 mprintf("unknown event from remote: local state = %d, evnt = %x, len = %d\n",
938 state
, I4B_GET_2B(msg
, I4B_MON_EVNT
), len
);
943 * Process input from user
949 int channel
, controller
;
951 fgets(buf
, sizeof(buf
), stdin
);
957 u_int8_t cmd
[I4B_MON_DUMPRIGHTS_SIZE
];
958 I4B_PREP_CMD(cmd
, I4B_MON_DUMPRIGHTS_CODE
);
960 if (debug
& DBG_DUMPALL
)
961 dump_event(cmd
, I4B_MON_DUMPRIGHTS_SIZE
, 0);
964 if ((sock_write(monsock
, cmd
, I4B_MON_DUMPRIGHTS_SIZE
)) == -1)
966 fprintf(stderr
, "sock_write failed: %s\n", strerror(errno
));
974 u_int8_t cmd
[I4B_MON_DUMPMCONS_SIZE
];
975 I4B_PREP_CMD(cmd
, I4B_MON_DUMPMCONS_CODE
);
977 if (debug
& DBG_DUMPALL
)
978 dump_event(cmd
, I4B_MON_DUMPMCONS_CODE
, 0);
981 if ((sock_write(monsock
, cmd
, I4B_MON_DUMPMCONS_SIZE
)) == -1)
983 fprintf(stderr
, "sock_write failed: %s\n", strerror(errno
));
991 u_int8_t cmd
[I4B_MON_CFGREREAD_SIZE
];
992 I4B_PREP_CMD(cmd
, I4B_MON_CFGREREAD_CODE
);
994 if (debug
& DBG_DUMPALL
)
995 dump_event(cmd
, I4B_MON_CFGREREAD_CODE
, 0);
998 if ((sock_write(monsock
, cmd
, I4B_MON_CFGREREAD_SIZE
)) == -1)
1000 fprintf(stderr
, "sock_write failed: %s\n", strerror(errno
));
1008 u_int8_t cmd
[I4B_MON_HANGUP_SIZE
];
1009 I4B_PREP_CMD(cmd
, I4B_MON_HANGUP_CODE
);
1011 printf("Which controller you wish to hangup? ");
1012 fgets(buf
, sizeof(buf
), stdin
);
1013 controller
= atoi(buf
);
1014 I4B_PUT_4B(cmd
, I4B_MON_HANGUP_CTRL
, controller
);
1016 printf("Which channel do you wish to hangup? ");
1017 fgets(buf
, sizeof(buf
), stdin
);
1018 channel
= atoi(buf
);
1019 I4B_PUT_4B(cmd
, I4B_MON_HANGUP_CHANNEL
, channel
);
1022 if (debug
& DBG_DUMPALL
)
1023 dump_event(cmd
, I4B_MON_HANGUP_CHANNEL
, 0);
1026 if ((sock_write(monsock
, cmd
, I4B_MON_HANGUP_SIZE
)) == -1)
1028 fprintf(stderr
, "sock_write failed: %s\n", strerror(errno
));
1048 u_int8_t cmd
[I4B_MON_CFGREREAD_SIZE
];
1049 I4B_PREP_CMD(cmd
, I4B_MON_CFGREREAD_CODE
);
1051 if (debug
& DBG_DUMPALL
)
1052 dump_event(cmd
, I4B_MON_CFGREREAD_CODE
, 0);
1054 if ((sock_write(monsock
, cmd
, I4B_MON_CFGREREAD_SIZE
)) == -1)
1056 fprintf(stderr
, "sock_write failed: %s\n", strerror(errno
));
1062 hangup(int ctrl
, int chan
)
1064 u_int8_t cmd
[I4B_MON_HANGUP_SIZE
];
1066 I4B_PREP_CMD(cmd
, I4B_MON_HANGUP_CODE
);
1067 I4B_PUT_4B(cmd
, I4B_MON_HANGUP_CTRL
, ctrl
);
1068 I4B_PUT_4B(cmd
, I4B_MON_HANGUP_CHANNEL
, chan
);
1071 if (debug
& DBG_DUMPALL
)
1072 dump_event(cmd
, I4B_MON_HANGUP_CHANNEL
, 0);
1075 if ((sock_write(monsock
, cmd
, I4B_MON_HANGUP_SIZE
)) == -1)
1077 fprintf(stderr
, "sock_write failed: %s\n", strerror(errno
));
1090 printf("Menu: <1> display rights, <2> display monitor connections,\n");
1091 printf(" <3> reread config file, <4> hangup \n");
1092 printf(" <9> quit isdnmonitor\n");
1098 sock_read(int fd
, void *buf
, size_t nbytes
)
1109 if ((nread
= read(fd
, ptr
, nleft
)) < 0)
1120 else if (nread
== 0)
1128 return(nbytes
- nleft
);
1132 sock_write(int fd
, void *buf
, size_t nbytes
)
1143 if ((nwritten
= write(fd
, ptr
, nleft
)) <= 0)
1162 mprintf(const char *fmt
, ...)
1164 #define PRBUFLEN 1024
1165 char buffer
[PRBUFLEN
];
1169 vsnprintf(buffer
, PRBUFLEN
-1, fmt
, ap
);
1172 if (!fullscreen
|| (fullscreen
&& (!curses_ready
)))
1173 printf("%s", buffer
);
1175 if (logfilename
!= NULL
)
1177 fprintf(lfp
, "%s", buffer
);