From 7e1ad0ad1d142104402da18fd9af4a1ac24f937d Mon Sep 17 00:00:00 2001 From: dcp1990 Date: Sun, 13 Mar 2005 06:32:21 +0000 Subject: [PATCH] Added --- src/Makefile | 17 +++++--- src/cidserv.c | 96 +++++++++++++++++++++++++++++++++-------- src/cnd_mgetty.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 199 insertions(+), 24 deletions(-) create mode 100644 src/cnd_mgetty.c diff --git a/src/Makefile b/src/Makefile index 8435bce..a691450 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,16 +1,19 @@ -CFLAGS=-g -Wall +CFLAGS=-g -Wall #-DSPEAKER # -DDEBUG -all: cidserv bcast hex +all: cidserv bcast hex cnd_mgetty cidserv: cidserv.c - gcc ${CFLAGS} -lutil -o cidserv cidserv.c + cc ${CFLAGS} -lutil -o cidserv cidserv.c bcast: bcast.c - gcc ${CFLAGS} -o bcast bcast.c + cc ${CFLAGS} -o bcast bcast.c hex: hex.c - gcc ${CFLAGS} -o hex hex.c + cc ${CFLAGS} -o hex hex.c +cnd_mgetty: cnd_mgetty.c + cc ${CFLAGS} -o cnd_mgetty cnd_mgetty.c clean: - rm -f *.o *.core hex bcast cidserv -strip: cidserv bcast hex + rm -f *.o *.core hex bcast cidserv cnd_mgetty +strip: cidserv bcast hex cnd_mgetty strip cidserv strip bcast strip hex + cnd_mgetty diff --git a/src/cidserv.c b/src/cidserv.c index 1d0042e..215eeb9 100644 --- a/src/cidserv.c +++ b/src/cidserv.c @@ -32,7 +32,7 @@ int ring = 0, nhosts = 0; char hosts[10][18]; FILE* logfh; char* devi; -static const char rcsid[] = "$Amigan: cidserv/src/cidserv.c,v 1.3 2004/12/23 23:46:03 dcp1990 Exp $"; +static const char rcsid[] = "$Amigan: cidserv/src/cidserv.c,v 1.4 2005/03/13 06:32:21 dcp1990 Exp $"; int modemfd, sfd; struct tm *ct; time_t now; @@ -41,6 +41,7 @@ int evalrc(char* result); void send_dgram(char* address, char* datar); int parse_cid(char* cidstring); void load_addrs(char* fl); +short unsigned int longformat = 0; char* logtime(void) { static char tstring[12]; @@ -65,6 +66,7 @@ void brring(void) } } #endif +/* old static void trap_hup(int nused) { fprintf(stderr, @@ -99,11 +101,52 @@ static void trap_term(int nused) close(sfd); fclose(logfh); exit(0); +}*/ +static void trap_sig(int sig) +{ + switch(sig) { + case SIGHUP: + fprintf(logfh, + "Caught signal %d\n", sig); + fflush(logfh); + parse_cid("80190108313232313135303508014F020A3430313437343737343063\n"); + signal(SIGHUP, SIG_IGN); + break; + case SIGUSR1: + fprintf(logfh, + "Caught signal %d\n", sig); + parse_cid("802701083039303532303130070F574952454C4553532043414C4C2020020A30303332303532363239AF\n"); + signal(SIGUSR1, SIG_IGN); + break; + case SIGUSR2: + fprintf(logfh, + "Caught signal %d\n", sig); + parse_cid("802701083132323130383234070F5354414E444953482048454154494E020A343031333937333337325C\n"); + signal(SIGUSR2, SIG_IGN); + break; + case SIGTERM: + case SIGINT: +#ifdef DEBUG + fprintf(stderr, + "Caught signal %d, cleaning up...\n", sig); +#endif + fprintf(logfh, "%s Caught signal %d, cleaning up...\n", + logtime(), sig); + fflush(logfh); + uu_unlock((devi+(sizeof("/dev/")-1))); + close(modemfd); + close(sfd); + fclose(logfh); + exit(0); + break; + default: + fprintf(logfh, "Caught signal %d\n", sig); + } } void usage(char* pname) { - fprintf(stderr, "Usage: %s %s[-D] -d device -l logfile -a addresses\n" - "-D to run as daemon (detach)\n%s", pname, + fprintf(stderr, "Usage: %s %s[-Df] -d device -l logfile -a addresses\n" + "-D to run as daemon (detach)\n-f for long log format\n%s", pname, wantspkr ? "[-s] " : "", wantspkr ? "-s uses speaker(4) (FreeBSD) when the" " phone rings.\n" : ""); @@ -127,9 +170,9 @@ int main(int argc, char* argv[]) char netbuf[256]; FD_ZERO(&fds_read); #ifdef SPEAKER - while ((ch = getopt(argc, argv, "sDd:l:a:")) != -1) { + while ((ch = getopt(argc, argv, "sDd:l:a:f")) != -1) { #else - while ((ch = getopt(argc, argv, "Dd:l:a:")) != -1) { + while ((ch = getopt(argc, argv, "Dd:l:a:f")) != -1) { #endif switch(ch) { case 'd': @@ -152,6 +195,9 @@ int main(int argc, char* argv[]) case 'D': runasd = 1; break; + case 'f': + longformat = 1; + break; default: usage(argv[0]); exit(-1); @@ -194,11 +240,11 @@ int main(int argc, char* argv[]) fflush(logfh); modemfd = open(dev, O_RDWR); lres = uu_lock((dev+(sizeof("/dev/")-1))); - signal(SIGTERM, trap_term); - signal(SIGHUP, trap_hup); - signal(SIGUSR1, trap_usr1); - signal(SIGUSR2, trap_usr2); - signal(SIGINT, trap_term); + signal(SIGTERM, trap_sig); + signal(SIGHUP, trap_sig); + signal(SIGUSR1, trap_sig); + signal(SIGUSR2, trap_sig); + signal(SIGINT, trap_sig); if(lres != 0) { fprintf(stderr, "%s\n", uu_lockerr(lres)); exit(-1); @@ -272,7 +318,7 @@ int main(int argc, char* argv[]) { switch(getc(stdin)) { case 'q': - trap_term(SIGTERM); + trap_sig(SIGTERM); break; case 's': parse_cid("802701083039303532303130070F574952454C4553532043414C4C2020020A30303332303532363239AF\n"); @@ -385,6 +431,7 @@ int parse_cid(char* cidstring) char *p, *datep; int len = 0, i; char finalbuf[1024], msg[512]; + char printbuf[2048]; unsigned char cch; char cbyte, cbyte2; char bytebuf[10]; @@ -400,6 +447,7 @@ int parse_cid(char* cidstring) memset(bytebuf, 0, sizeof bytebuf); memset(date, 0, sizeof date); memset(finalbuf, 0, sizeof(finalbuf)); + memset(printbuf, 0, sizeof(printbuf) * sizeof(char)); if(cidstring[strlen(cidstring)] == '\n') cidstring[strlen(cidstring)] = 0; sz = strlen(cidstring); @@ -444,10 +492,16 @@ int parse_cid(char* cidstring) cidtime[4] = 0; p += 8; len -= 8; - fprintf(logfh, "Date: %s (local %02d/%02d)\nTime: %s (local %02d:%02d)\n", - date, ctm->tm_mon+1, ctm->tm_mday, cidtime, - ctm->tm_hour, ctm->tm_min); - fflush(logfh); + if(longformat) { + fprintf(logfh, "Date: %s (local %02d/%02d)\nTime: %s (local %02d:%02d)\n", + date, ctm->tm_mon+1, ctm->tm_mday, cidtime, + ctm->tm_hour, ctm->tm_min); + fflush(logfh); + } else { + snprintf(printbuf, sizeof(printbuf), "%02d:%02d:%02d: L%02d/%02d %s:%s:", + ctm->tm_hour, ctm->tm_min, ctm->tm_sec, ctm->tm_mon + 1, + ctm->tm_mday, date, cidtime); + } while(len && !isprint(*p)) { p++; len--; @@ -486,8 +540,16 @@ int parse_cid(char* cidstring) strcpy(phone, "PRIVATE"); if(phone[0] == 'O') strcpy(phone, "UNAVAILABLE"); - fprintf(logfh, "Name: %s\nPhone Number: %s\n*********\n", name, phone); - fflush(logfh); + if(longformat) { + fprintf(logfh, "Name: %s\nPhone Number: %s\n*********\n", name, phone); + fflush(logfh); + } else { + snprintf(printbuf + strlen(printbuf), sizeof(printbuf) - strlen(printbuf) - 1, "%s:%s\n", name, phone); + } + if(!longformat) { + fputs(printbuf, logfh); + fflush(logfh); + } sprintf(msg, "%s:%s:0:%s:%s", date, cidtime, name, phone); #ifdef DEBUG printf("msg will be %s\n", msg); diff --git a/src/cnd_mgetty.c b/src/cnd_mgetty.c new file mode 100644 index 0000000..f75ec03 --- /dev/null +++ b/src/cnd_mgetty.c @@ -0,0 +1,110 @@ +/* + * Caller ID Server mgetty support + * (C)2004, Dan Ponte + * Licensed under the BSD license, with advertising clause. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define VERSION "0.1" +#define ADDRS "/usr/local/etc/cidserv.conf" /* XXX: change this */ +int nhosts = 0; +char hosts[10][18]; +static const char rcsid[] = "$Amigan: cidserv/src/cnd_mgetty.c,v 1.1 2005/03/13 06:32:21 dcp1990 Exp $"; +void send_dgram(char* address, char* datar); +void load_addrs(const char* fl); +int parse_cid(char* tty, char* phone, char* name, int dist_r, char* called); +int main(int argc, char* argv[]) +{ + if(argc < 6) { + fprintf(stderr, "Needs 5 args:\n" + " \n"); + exit(-2); + } + load_addrs(ADDRS); + parse_cid(argv[1], argv[2], argv[3], atoi(argv[4]), argv[5]); + return 0; +} +int parse_cid(tty, phone, name, dist_r, called) + char* tty; + char* phone; + char* name; + int dist_r; + char* called; +{ + int i = 0; + char msg[512]; + char fdate[25]; + time_t rnow; + struct tm *ctm; + memset(msg, 0, sizeof msg); + memset(fdate, 0, sizeof fdate); + rnow = time(NULL); + ctm = localtime(&rnow); + strftime(fdate, sizeof(fdate) * sizeof(char), + "%m%d:%H%M", ctm); + sprintf(msg, "%s:0:%s:%s", fdate, name, phone); + for(i = 0; i <= nhosts; i++) { + send_dgram(hosts[i], msg); + } + return 0; +} +void send_dgram(char* address, char* datar) +{ + char msg[212]; + int s; + int on = 1; + struct sockaddr_in sin; + if(strlen(address) < 3) return; +#ifdef DEBUG + printf("send_dgram(%s) %p\n", address, address); +#endif + strcpy(msg, datar); + s = socket(PF_INET, SOCK_DGRAM, 0); + bzero(&sin, sizeof sin); + sin.sin_family = AF_INET; + sin.sin_addr.s_addr = inet_addr(address); + sin.sin_port = htons(3890); + if(setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char*) &on, sizeof(on)) < 0) { + perror("setsockopt"); + exit(-1); + } + if (connect(s, (struct sockaddr *)&sin, sizeof sin) < 0) { + perror("connect"); + close(s); + return; + } + write(s, msg, strlen(msg) + 1); + close(s); + return; +} +void load_addrs(const char* fl) +{ + FILE* tfl; + char fbuf[128]; + if(!(tfl = fopen(fl, "r"))) { + perror("fopen"); + exit(-1); + } + while(!feof(tfl)) { + fgets(fbuf, 126, tfl); + if(fbuf[strlen(fbuf)] == '\n') fbuf[strlen(fbuf)] = 0; + if(strlen(fbuf) > 4 && fbuf[0] != '#') strcpy(hosts[nhosts++], fbuf); + memset(fbuf, 0, sizeof fbuf); + } + fclose(tfl); +}