Network notification added; TODO: logging of calls
This commit is contained in:
parent
ccc6a3c849
commit
c1064ddc10
@ -1,6 +1,6 @@
|
||||
# cnd Makefile
|
||||
# (C)2005, Dan Ponte
|
||||
# $Amigan: phoned/phoned/Makefile,v 1.5 2005/06/01 20:48:28 dcp1990 Exp $
|
||||
# $Amigan: phoned/phoned/Makefile,v 1.6 2005/06/02 02:40:52 dcp1990 Exp $
|
||||
include ../global.mk
|
||||
# basic stuff. we append for a reason.
|
||||
CPPFLAGS=-I../include -DDEBUG
|
||||
@ -8,8 +8,8 @@ CFLAGS+=-g -Wall -W -ansi ${CPPFLAGS}
|
||||
LDFLAGS=-lutil
|
||||
# keep these up to date.
|
||||
MAINBIN=phoned
|
||||
SRCS=main.c init.c log.c cfg.c socket.c y.tab.c lex.yy.c signals.c cid.c modem.c
|
||||
OBJS=main.o init.o log.o cfg.o socket.o y.tab.o lex.yy.o signals.o cid.o modem.o
|
||||
SRCS=main.c init.c log.c cfg.c socket.c y.tab.c lex.yy.c signals.c cid.c modem.c notify.c
|
||||
OBJS=main.o init.o log.o cfg.o socket.o y.tab.o lex.yy.o signals.o cid.o modem.o notify.o
|
||||
OHDRS=y.tab.h
|
||||
CLEANFILES=y.tab.c y.tab.h lex.yy.c
|
||||
LEX=lex
|
||||
|
22
phoned/cfg.c
22
phoned/cfg.c
@ -2,12 +2,17 @@
|
||||
* FUCK....lex overwrote this :-(
|
||||
* (C)2005, Dan Ponte...again.
|
||||
*/
|
||||
/* $Amigan: phoned/phoned/cfg.c,v 1.2 2005/06/01 00:43:07 dcp1990 Exp $ */
|
||||
/* $Amigan: phoned/phoned/cfg.c,v 1.3 2005/06/02 02:40:52 dcp1990 Exp $ */
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#define HAVE_INET_INCS
|
||||
#include <phoned.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
extern addrsll_t *top;
|
||||
struct conf cf;
|
||||
void read_config(void)
|
||||
{
|
||||
@ -20,3 +25,18 @@ void read_config(void)
|
||||
parse(&con);
|
||||
fclose(con);
|
||||
}
|
||||
void addtoaddrs(const char* par)
|
||||
{
|
||||
addrsll_t *l;
|
||||
l = getlast(top);
|
||||
#ifdef DEBUG
|
||||
if(l->next != 0x0) {
|
||||
lprintf(fatal, "your last routine is broken, moron! fix it!\n");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
l->next = allocaddr();
|
||||
l = l->next;
|
||||
l->addr = inet_addr(par);
|
||||
}
|
||||
|
||||
|
@ -71,6 +71,7 @@ ipadr:
|
||||
IPADDR
|
||||
{
|
||||
lprintf(debug, "Encountered ipaddress %s\n", $1);
|
||||
addtoaddrs($1);
|
||||
}
|
||||
;
|
||||
modemdev:
|
||||
|
@ -17,6 +17,8 @@ extern struct conf cf;
|
||||
void shutd(void)
|
||||
{
|
||||
lprintf(fatal, "phoned shutting down...\n");
|
||||
close_modem(cf.modemdev);
|
||||
flush_lists();
|
||||
fclose(logf);
|
||||
unlink(SOCKETFILE);
|
||||
}
|
||||
|
@ -48,24 +48,49 @@
|
||||
/* globals */
|
||||
FILE* modem;
|
||||
int modemfd;
|
||||
int cou = 0;
|
||||
char buffer[512];
|
||||
short doing_cid = 0;
|
||||
void stmod(const char* str)
|
||||
{
|
||||
fputs(str, modem);
|
||||
fflush(modem);
|
||||
}
|
||||
int close_modem(char* dev)
|
||||
{
|
||||
if(strlen(dev) < 7) {
|
||||
lprintf(error , "dev %s too short\n", dev);
|
||||
return -4;
|
||||
}
|
||||
if(strncmp(dev, "/dev/", strlen("/dev/")) != 0) {
|
||||
lprintf(error, "dev %s must begin with /dev/\n", dev);
|
||||
return -5;
|
||||
}
|
||||
uu_unlock((dev+(sizeof("/dev/")-1)));
|
||||
fclose(modem);
|
||||
return 1;
|
||||
}
|
||||
int init_modem(char* dev)
|
||||
{
|
||||
int lres = 0;
|
||||
if(strlen(dev) < 7) {
|
||||
lprintf(error , "dev %s too short\n", dev);
|
||||
return -4;
|
||||
}
|
||||
if(strncmp(dev, "/dev/", strlen("/dev/")) != 0) {
|
||||
lprintf(error, "dev %s must begin with /dev/\n", dev);
|
||||
return -5;
|
||||
}
|
||||
lres = uu_lock((dev+(sizeof("/dev/")-1)));
|
||||
if(lres != UU_LOCK_OK) {
|
||||
lprintf(error, "%s\n", uu_lockerr(lres));
|
||||
return -1;
|
||||
}
|
||||
modemfd = open(dev, O_RDWR);
|
||||
if(modemfd == -1) {
|
||||
lprintf(error, "Error opening modem %s: %s\n", dev, strerror(errno));
|
||||
return -2;
|
||||
}
|
||||
lres = uu_lock((dev+(sizeof("/dev/")-1)));
|
||||
if(lres != 0) {
|
||||
lprintf(error, "%s\n", uu_lockerr(lres));
|
||||
return -1;
|
||||
}
|
||||
modem = fdopen(modemfd, "w+");
|
||||
if(!modem) {
|
||||
lprintf(error, "Error fdopening modemfd %d: %s\n", modemfd, strerror(errno));
|
||||
@ -74,3 +99,50 @@ int init_modem(char* dev)
|
||||
stmod(INITSTRING);
|
||||
return 1;
|
||||
}
|
||||
int modem_evalrc(char* result)
|
||||
{
|
||||
int rescode, i;
|
||||
for(i = 0; i <= strlen(result); i++) {
|
||||
if(result[i] == '\r' || result[i] == '\n') result[i] = '\0';
|
||||
}
|
||||
rescode = atoi(result);
|
||||
switch(rescode) {
|
||||
case 0:
|
||||
/* OK */
|
||||
return 0;
|
||||
break;
|
||||
case 2:
|
||||
break;
|
||||
case 4:
|
||||
return -1;
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
void modem_hread(char* cbuf)
|
||||
{
|
||||
if(cou < sizeof(buffer) - 2)
|
||||
buffer[cou] = cbuf[0];
|
||||
if(buffer[0] == '8' && buffer[1] == '0')
|
||||
doing_cid = 1;
|
||||
if(cbuf[0] == '\n') {
|
||||
if(doing_cid) {
|
||||
cid_t *rc;
|
||||
rc = parse_cid(buffer);
|
||||
cid_notify(rc);
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
doing_cid = 0;
|
||||
cou = 0;
|
||||
} else {
|
||||
modem_evalrc(buffer);
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
cbuf[0] = 0;
|
||||
cou = 0;
|
||||
}
|
||||
} else {
|
||||
cou++;
|
||||
}
|
||||
}
|
||||
|
109
phoned/notify.c
Normal file
109
phoned/notify.c
Normal file
@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright (c) 2005, Dan Ponte
|
||||
*
|
||||
* notify.c - notification of clients (CID)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
/* $Amigan: phoned/phoned/notify.c,v 1.1 2005/06/02 02:40:53 dcp1990 Exp $ */
|
||||
#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>
|
||||
#include <errno.h>
|
||||
#define HAVE_INET_INCS
|
||||
#include <phoned.h>
|
||||
addrsll_t head;
|
||||
addrsll_t *top = &head;
|
||||
addrsll_t* getlast(addrsll_t* hd)
|
||||
{
|
||||
addrsll_t *cur;
|
||||
for(cur = hd; cur->next != 0x0; cur = cur->next) ;
|
||||
return cur;
|
||||
}
|
||||
void freeaddrl(addrsll_t* hd)
|
||||
{
|
||||
addrsll_t *cur, *nx;
|
||||
nx = cur = hd;
|
||||
for(cur = hd; nx != 0x0; cur = nx) {
|
||||
nx = cur->next;
|
||||
if(cur != &head) free(cur);
|
||||
}
|
||||
}
|
||||
addrsll_t* allocaddr(void)
|
||||
{
|
||||
addrsll_t* nw;
|
||||
nw = malloc(sizeof(addrsll_t));
|
||||
memset(nw, 0x0, sizeof(addrsll_t));
|
||||
return nw;
|
||||
}
|
||||
void flush_lists(void)
|
||||
{
|
||||
freeaddrl(top);
|
||||
}
|
||||
int cid_notify(cid_t* c)
|
||||
{
|
||||
short len = 0;
|
||||
char* msg;
|
||||
int s;
|
||||
struct sockaddr_in sin;
|
||||
short on = 0x1;
|
||||
addrsll_t *cur;
|
||||
len = strlen(c->number) + strlen(c->name) + 8 + 5 + 4;
|
||||
msg = malloc(len * sizeof(char));
|
||||
snprintf(msg, len, "%02d%02d:%02d%02d:0:%s:%s", c->month, c->day, c->hour, c->minute,
|
||||
c->name, c->number);
|
||||
s = socket(PF_INET, SOCK_DGRAM, 0);
|
||||
bzero(&sin, sizeof(sin));
|
||||
sin.sin_family = AF_INET;
|
||||
sin.sin_port = htons(3890);
|
||||
if(setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char*)&on, sizeof(on)) < 0) {
|
||||
lprintf(error, "setsockopt: %s\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
for(cur = top; cur->next != 0x0; cur = cur->next) {
|
||||
sin.sin_addr.s_addr = cur->addr;
|
||||
if(connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
|
||||
lprintf(error, "connect: %s\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
write(s, msg, strlen(msg) + 1);
|
||||
}
|
||||
close(s);
|
||||
return 1;
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
main {
|
||||
modemdev "/dev/ttyd0";
|
||||
modemdev "/dev/cuaa2";
|
||||
};
|
||||
notify {
|
||||
10.10.10.13;
|
||||
|
@ -64,6 +64,8 @@ void network(void) /* name is misleading because we also do modem IO here */
|
||||
struct timeval tv;
|
||||
int si = 1;
|
||||
int sin_size, ilen;
|
||||
char cbuf[1];
|
||||
cbuf[0] = '\0'; cbuf[1] = '\0';
|
||||
struct sockaddr_un it;
|
||||
if((s = socket(AF_LOCAL, SOCK_STREAM, 0)) == -1) {
|
||||
perror("socket");
|
||||
@ -84,6 +86,7 @@ void network(void) /* name is misleading because we also do modem IO here */
|
||||
for(;;) {
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(s, &fds);
|
||||
FD_SET(modemfd, &fds);
|
||||
switch(select(s + 1, &fds, NULL, NULL, NULL)) {
|
||||
case -1:
|
||||
perror("select");
|
||||
@ -106,6 +109,12 @@ void network(void) /* name is misleading because we also do modem IO here */
|
||||
handclient(sn);
|
||||
close(sn);
|
||||
}
|
||||
if(FD_ISSET(modemfd, &fds) != 0)
|
||||
{
|
||||
read(modemfd, cbuf, 1);
|
||||
modem_hread(cbuf);
|
||||
*cbuf = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user