This commit is contained in:
dcp1990 2005-03-13 06:32:21 +00:00
parent 6290ac22a1
commit 7e1ad0ad1d
3 changed files with 199 additions and 24 deletions

View file

@ -1,16 +1,19 @@
CFLAGS=-g -Wall CFLAGS=-g -Wall
#-DSPEAKER #-DSPEAKER
# -DDEBUG # -DDEBUG
all: cidserv bcast hex all: cidserv bcast hex cnd_mgetty
cidserv: cidserv.c cidserv: cidserv.c
gcc ${CFLAGS} -lutil -o cidserv cidserv.c cc ${CFLAGS} -lutil -o cidserv cidserv.c
bcast: bcast.c bcast: bcast.c
gcc ${CFLAGS} -o bcast bcast.c cc ${CFLAGS} -o bcast bcast.c
hex: hex.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: clean:
rm -f *.o *.core hex bcast cidserv rm -f *.o *.core hex bcast cidserv cnd_mgetty
strip: cidserv bcast hex strip: cidserv bcast hex cnd_mgetty
strip cidserv strip cidserv
strip bcast strip bcast
strip hex strip hex
cnd_mgetty

View file

@ -32,7 +32,7 @@ int ring = 0, nhosts = 0;
char hosts[10][18]; char hosts[10][18];
FILE* logfh; FILE* logfh;
char* devi; 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; int modemfd, sfd;
struct tm *ct; struct tm *ct;
time_t now; time_t now;
@ -41,6 +41,7 @@ int evalrc(char* result);
void send_dgram(char* address, char* datar); void send_dgram(char* address, char* datar);
int parse_cid(char* cidstring); int parse_cid(char* cidstring);
void load_addrs(char* fl); void load_addrs(char* fl);
short unsigned int longformat = 0;
char* logtime(void) char* logtime(void)
{ {
static char tstring[12]; static char tstring[12];
@ -65,6 +66,7 @@ void brring(void)
} }
} }
#endif #endif
/* old
static void trap_hup(int nused) static void trap_hup(int nused)
{ {
fprintf(stderr, fprintf(stderr,
@ -99,11 +101,52 @@ static void trap_term(int nused)
close(sfd); close(sfd);
fclose(logfh); fclose(logfh);
exit(0); 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) void usage(char* pname)
{ {
fprintf(stderr, "Usage: %s %s[-D] -d device -l logfile -a addresses\n" fprintf(stderr, "Usage: %s %s[-Df] -d device -l logfile -a addresses\n"
"-D to run as daemon (detach)\n%s", pname, "-D to run as daemon (detach)\n-f for long log format\n%s", pname,
wantspkr ? "[-s] " : "", wantspkr ? "[-s] " : "",
wantspkr ? "-s uses speaker(4) (FreeBSD) when the" wantspkr ? "-s uses speaker(4) (FreeBSD) when the"
" phone rings.\n" : ""); " phone rings.\n" : "");
@ -127,9 +170,9 @@ int main(int argc, char* argv[])
char netbuf[256]; char netbuf[256];
FD_ZERO(&fds_read); FD_ZERO(&fds_read);
#ifdef SPEAKER #ifdef SPEAKER
while ((ch = getopt(argc, argv, "sDd:l:a:")) != -1) { while ((ch = getopt(argc, argv, "sDd:l:a:f")) != -1) {
#else #else
while ((ch = getopt(argc, argv, "Dd:l:a:")) != -1) { while ((ch = getopt(argc, argv, "Dd:l:a:f")) != -1) {
#endif #endif
switch(ch) { switch(ch) {
case 'd': case 'd':
@ -152,6 +195,9 @@ int main(int argc, char* argv[])
case 'D': case 'D':
runasd = 1; runasd = 1;
break; break;
case 'f':
longformat = 1;
break;
default: default:
usage(argv[0]); usage(argv[0]);
exit(-1); exit(-1);
@ -194,11 +240,11 @@ int main(int argc, char* argv[])
fflush(logfh); fflush(logfh);
modemfd = open(dev, O_RDWR); modemfd = open(dev, O_RDWR);
lres = uu_lock((dev+(sizeof("/dev/")-1))); lres = uu_lock((dev+(sizeof("/dev/")-1)));
signal(SIGTERM, trap_term); signal(SIGTERM, trap_sig);
signal(SIGHUP, trap_hup); signal(SIGHUP, trap_sig);
signal(SIGUSR1, trap_usr1); signal(SIGUSR1, trap_sig);
signal(SIGUSR2, trap_usr2); signal(SIGUSR2, trap_sig);
signal(SIGINT, trap_term); signal(SIGINT, trap_sig);
if(lres != 0) { if(lres != 0) {
fprintf(stderr, "%s\n", uu_lockerr(lres)); fprintf(stderr, "%s\n", uu_lockerr(lres));
exit(-1); exit(-1);
@ -272,7 +318,7 @@ int main(int argc, char* argv[])
{ {
switch(getc(stdin)) { switch(getc(stdin)) {
case 'q': case 'q':
trap_term(SIGTERM); trap_sig(SIGTERM);
break; break;
case 's': case 's':
parse_cid("802701083039303532303130070F574952454C4553532043414C4C2020020A30303332303532363239AF\n"); parse_cid("802701083039303532303130070F574952454C4553532043414C4C2020020A30303332303532363239AF\n");
@ -385,6 +431,7 @@ int parse_cid(char* cidstring)
char *p, *datep; char *p, *datep;
int len = 0, i; int len = 0, i;
char finalbuf[1024], msg[512]; char finalbuf[1024], msg[512];
char printbuf[2048];
unsigned char cch; unsigned char cch;
char cbyte, cbyte2; char cbyte, cbyte2;
char bytebuf[10]; char bytebuf[10];
@ -400,6 +447,7 @@ int parse_cid(char* cidstring)
memset(bytebuf, 0, sizeof bytebuf); memset(bytebuf, 0, sizeof bytebuf);
memset(date, 0, sizeof date); memset(date, 0, sizeof date);
memset(finalbuf, 0, sizeof(finalbuf)); memset(finalbuf, 0, sizeof(finalbuf));
memset(printbuf, 0, sizeof(printbuf) * sizeof(char));
if(cidstring[strlen(cidstring)] == '\n') if(cidstring[strlen(cidstring)] == '\n')
cidstring[strlen(cidstring)] = 0; cidstring[strlen(cidstring)] = 0;
sz = strlen(cidstring); sz = strlen(cidstring);
@ -444,10 +492,16 @@ int parse_cid(char* cidstring)
cidtime[4] = 0; cidtime[4] = 0;
p += 8; p += 8;
len -= 8; len -= 8;
fprintf(logfh, "Date: %s (local %02d/%02d)\nTime: %s (local %02d:%02d)\n", if(longformat) {
date, ctm->tm_mon+1, ctm->tm_mday, cidtime, fprintf(logfh, "Date: %s (local %02d/%02d)\nTime: %s (local %02d:%02d)\n",
ctm->tm_hour, ctm->tm_min); date, ctm->tm_mon+1, ctm->tm_mday, cidtime,
fflush(logfh); 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)) { while(len && !isprint(*p)) {
p++; p++;
len--; len--;
@ -486,8 +540,16 @@ int parse_cid(char* cidstring)
strcpy(phone, "PRIVATE"); strcpy(phone, "PRIVATE");
if(phone[0] == 'O') if(phone[0] == 'O')
strcpy(phone, "UNAVAILABLE"); strcpy(phone, "UNAVAILABLE");
fprintf(logfh, "Name: %s\nPhone Number: %s\n*********\n", name, phone); if(longformat) {
fflush(logfh); 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); sprintf(msg, "%s:%s:0:%s:%s", date, cidtime, name, phone);
#ifdef DEBUG #ifdef DEBUG
printf("msg will be %s\n", msg); printf("msg will be %s\n", msg);

110
src/cnd_mgetty.c Normal file
View file

@ -0,0 +1,110 @@
/*
* Caller ID Server mgetty support
* (C)2004, Dan Ponte
* Licensed under the BSD license, with advertising clause.
*/
#include <fcntl.h>
#include <ctype.h>
#include <unistd.h>
#include <signal.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <libutil.h>
#include <sys/types.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#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"
"<tty> <CallerID> <Name> <dist-ring-nr.> <Called Nr.>\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);
}