Initial revision

This commit is contained in:
dcp1990 2005-06-01 00:37:22 +00:00
commit f6b4fb7583
23 changed files with 29289 additions and 0 deletions

30
COPYRIGHT Normal file
View File

@ -0,0 +1,30 @@
This is the phoned distribution.
Copyright (C) 2005, Dan Ponte. All rights reserved.
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.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software written by Dan Ponte and contributors.
4. Neither the name of the Dan Ponte nor the names of his contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY 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 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.

17
Makefile Normal file
View File

@ -0,0 +1,17 @@
# main makefile...
# Oh My Fucking God, this is such a big, unportable mess. Oh well.
# BSD Make > *
APPS=cnd phoned
APPSUF=${APPS:S/$/_app/}
APPSCLEAN=${APPS:S/$/_cl/}
all: ${APPSUF}
${APPSUF}:
@cd ${@:S/_app$//} && make
clean: ${APPSCLEAN} ourclean
${APPSCLEAN}:
@cd ${@:S/_cl$//} && make clean
ourclean:
# rm -f .conf
#.conf:
# @perl -e 'require 5.0001; require Modem::fgetty' \
# || echo "Needs Modem::Vgetty and perl 5.0001 at least."

28
README Normal file
View File

@ -0,0 +1,28 @@
+=======================================+
| phoned |
| (C)2005, Dan Ponte |
+=======================================+
This is the phoned distribution, version 0.1.
phoned is distributed under the license outlined in COPYRIGHT. Please read
it before use!
This is now a hybrid of the older cidserv distribution. I might go to CVS
some day.
To build, simply type 'make'. phoned was written for BSD make (just "make"
on regular BSD systems, "pmake" on many linux machines.) For further
installation instructions, please see doc/install_guide.txt.
You will find the following directories in this source archive:
cnd/ - Caller ID program
doc/ - misc. documentation
examples/ - example scripts
include/ - global include files
lib/ - API libraries
phonectl/ - command line client to phoned
phoned/ - main phone daemon written in C
xfone/ - Tcl/Tk interface to phoned
dcp1990@neptune.atopia.net - http://www.theamigan.net/phoned/index.html
vim:tw=76

12
cnd/Makefile Normal file
View File

@ -0,0 +1,12 @@
# cnd Makefile
# (C)2005, Dan Ponte
include ../global.mk
# basic stuff. we append for a reason.
CPPFLAGS=-I../include
CFLAGS+=-g -Wall -W -ansi ${CPPFLAGS}
LDFLAGS=
# keep these up to date.
MAINBIN=cnd
SRCS=cnd.c
OBJS=cnd.o
include ../main.mk

115
cnd/cnd.c Normal file
View File

@ -0,0 +1,115 @@
/*
* Caller ID Server mgetty support
* (C)2004, Dan Ponte
* Licensed under the BSD license, with advertising clause.
* Excerpted from the original cidserv distribution. Modified to work
* with phoned.
*/
#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 <phoned.h>
#define VERSION "0.1"
#define ADDRS "/usr/local/etc/cidserv.conf"
int nhosts = 0;
char hosts[10][18];
static const char rcsid[] = "$Amigan: phoned/cnd/cnd.c,v 1.1 2005/06/01 00:37:22 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 __unused;
char* phone;
char* name;
int dist_r __unused;
char* called __unused;
{
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);
}

51
doc/install_guide.txt Normal file
View File

@ -0,0 +1,51 @@
**************************************************************************
** phoned installation guide -- it's good **
** (C)2005, Dan Ponte **
**************************************************************************
1.0 Introduction
phoned is [supposed to be] a powerful phone answering system, much like
VOCP. However, it is more-client-server based.
Its power stems from the use of vgetty, part of the mgetty+sendfax
distribution. It is available at:
http://alpha.greenie.net/mgetty/
mgetty+sendfax is also available under FreeBSD ports as comms/mgetty+sendfax/.
I will not cover the installation and configuration of mgetty/vgetty here.
There are many good docs on the process.
The phoned distribution consists of the following:
*phoned - the main daemon responsible for logging stuff and talking to
clients
*phonecall - a Perl script called by vgetty when it answers the phone,
responsible for talking to phoned and telling vgetty what to do (play sound
files, interpret menu options, etc)
*xfone - a Tcl/Tk application for checking messages and administering
mailboxes
*cnd - called by vgetty when the phone rings to tell phoned to log the call,
alert network users and either reject or accept the call based on the
caller. (based on a tiny bit of code from my old cidserv application, it is
compatible with its clients.)
2.0 Requirements
In order to use the base phoned and phonecall, you must have at least the
following:
*A Unix system capable of running perl, vgetty, and using sockets
(tested on FreeBSD) (the phoned suite will NOT run on windows. Get a
better OS. It might run on OSX.)
*Perl (tested on 5.8.6)
*The Modem::Vgetty Perl module, available from CPAN
*A properly installed and configured vgetty
*A voice modem supported by vgetty. Tested with the Diamond SupraExpress 56i
Sp (Rockwell chipset/driver)
*Telephone service
*A telephone, mic, or other recording device to record the greeting messages
*A friend who has a telephone to annoy so he can test the system ;-)
Optional:
*Caller ID service from your phone company in order to use cnd
*The Festival Speech Synthesis package, available under FreeBSD ports as
audio/festival/, in order to use the speech capabilities (highly
recommended.)
In order to run xfone, you need:
*Tcl 8.4
*Tk 8.4

3
global.mk Normal file
View File

@ -0,0 +1,3 @@
# global stuff
CC=cc
PREFIX=/usr/local

44
include/phoned.h Normal file
View File

@ -0,0 +1,44 @@
/*
* Global stuff for apps in the phoned distribution.
* (C)2005, Dan Ponte
* BSDL w/ advert.
*/
#define VERSION "0.1"
#define LOGFILE "/var/log/phoned.log"
#define SOCKETFILE "/tmp/phoned.sock"
#define CONFIGFILE "phoned.conf"
#define _unused __attribute__((__unused__))
struct conf {
char* cfile;
char* logfile;
int loglevels;
};
#define LL_DEBUG 0x1
#define LL_GARBAGE 0x2
#define LL_INFO 0x4
#define LL_WARN 0x10
#define LL_ERROR 0x20
#define LL_CRITICAL 0x40
#define LL_FATAL 0x80
#define LL_ALL LL_DEBUG | LL_GARBAGE | LL_INFO | LL_WARN | LL_ERROR \
| LL_CRITICAL | LL_FATAL /* 247 */
enum ltype {
debug,
garbage,
info,
warn,
error,
critical,
fatal
};
/* function prottypes */
void initialize(void);
void open_log(void);
void read_config(void);
void shutd(void);
void network(void);
int lprintf(enum ltype logtype, const char* fmt, ...);
void handsig(int sig);
void install_handlers(void);
int parse(FILE** fp);

12
main.mk Normal file
View File

@ -0,0 +1,12 @@
all: .depend ${MAINBIN}
# I know, I know, but it's good.
.depend: ${SRCS} ${OHDRS}
mkdep ${CPPFLAGS} -MM -p ${SRCS}
${MAINBIN}: ${OBJS}
${CC} ${CFLAGS} ${LDFLAGS} -o ${MAINBIN} ${OBJS}
# for this app
%.o: %.c
${CC} ${CFLAGS} -c ${.SOURCE}
# end ours
clean:
rm -f *.o ${MAINBIN} .depend *~ *.core ${CLEANFILES}

12
phonectl/Makefile Normal file
View File

@ -0,0 +1,12 @@
# cnd Makefile
# (C)2005, Dan Ponte
include ../global.mk
# basic stuff. we append for a reason.
CPPFLAGS=-I../include
CFLAGS+=-g -Wall -W -ansi ${CPPFLAGS}
LDFLAGS=
# keep these up to date.
MAINBIN=phonectl
SRCS=phonectl.c
OBJS=phonectl.o
include ../main.mk

50
phonectl/phonectl.c Normal file
View File

@ -0,0 +1,50 @@
/*
* Copyright (c) 2005, Dan Ponte
*
* phonectl.c - command line client
*
* 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.
*/
/* system includes */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <optarg.h>
#include <netinet/in.h>
#include <errno.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/un.h>
/* us */
#include <phonectl.h>
int main(argc, argv)
int argc;
char* argv[];
{

21
phoned/Makefile Normal file
View File

@ -0,0 +1,21 @@
# cnd Makefile
# (C)2005, Dan Ponte
include ../global.mk
# basic stuff. we append for a reason.
CPPFLAGS=-I../include -DDEBUG
CFLAGS+=-g -Wall -W -ansi ${CPPFLAGS}
LDFLAGS=
# 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
OBJS=main.o init.o log.o cfg.o socket.o y.tab.o lex.yy.o signals.o
OHDRS=y.tab.h
CLEANFILES=y.tab.c y.tab.h lex.yy.c
include ../main.mk
y.tab.h: y.tab.c
y.tab.c: config.y
yacc -d config.y
lex.yy.c: config.l y.tab.c y.tab.h
lex config.l

21
phoned/cfg.c Normal file
View File

@ -0,0 +1,21 @@
/*
* FUCK....lex overwrote this :-(
* (C)2005, Dan Ponte...again.
*/
#include <stdio.h>
#include <string.h>
#include <phoned.h>
#include <unistd.h>
#include <stdlib.h>
struct conf cf;
void read_config(void)
{
FILE* con;
con = fopen(cf.cfile, "r");
if(!con) {
perror("error opening config file");
exit(-1);
}
parse(&con);
fclose(con);
}

27
phoned/config.l Normal file
View File

@ -0,0 +1,27 @@
/* configuration lexer */
%{
#include <stdio.h>
#include "y.tab.h"
#include <phoned.h>
extern int chrcnt, lincnt;
%}
%%
. ++chrcnt; REJECT;
notify return NOTIFY;
\{ return OBRACE;
\} return CBRACE;
[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} yylval.string = strdup(yytext); return IPADDR;
; return SCOLON;
\n chrcnt = 0; ++lincnt;/* DONOTHING */
[\t ] /* DONOTHING */
. /* DO nothing */
%%
int parse(fp)
FILE** fp;
{
extern int yyparse __P((void));
yyin = *fp;
if(yyparse())
return -1;
return 0;
}

55
phoned/config.y Normal file
View File

@ -0,0 +1,55 @@
%union
{
int number;
char* string;
}
%{
#include <stdio.h>
#include <string.h>
#include <phoned.h>
int chrcnt = 0;
int lincnt = 0;
int yylex(void);
void yyerror(str)
char* str;
{
lprintf(fatal, "parser: error: %s at line %d chr %d\n", str, lincnt,
chrcnt);
exit(-1);
}
int yywrap(void)
{
return 1;
}
%}
%token NOTIFY OBRACE CBRACE SCOLON
%token <string> IPADDR
%%
commands:
|
command commands SCOLON
;
command:
notify
;
notify:
NOTIFY iplist
{
lprintf(info, "parser: end notify\n");
}
;
iplist:
OBRACE ipaddrs CBRACE
;
ipaddrs:
|
ipaddrs ipadr SCOLON
;
ipadr:
IPADDR
{
lprintf(debug, "Encountered ipaddress %s\n", $1);
}
;
%%

28375
phoned/cscope.out Normal file

File diff suppressed because it is too large Load Diff

40
phoned/init.c Normal file
View File

@ -0,0 +1,40 @@
/*
* phoned initialization
* (C)2005, Dan Ponte
* BSD license
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <phoned.h>
extern FILE* logf;
extern short difflog;
extern struct conf cf;
void shutd(void)
{
lprintf(fatal, "phoned shutting down...\n");
fclose(logf);
unlink(SOCKETFILE);
}
void open_logs(void)
{
logf = fopen(difflog ? cf.logfile : LOGFILE, "a");
if(!logf) {
perror("logf open");
exit(-1);
}
lprintf(info, "phoned v" VERSION " starting..\n");
}
void initialize(void)
{
open_logs();
install_handlers();
read_config();
}

138
phoned/log.c Normal file
View File

@ -0,0 +1,138 @@
/*
* log.c - logfile stuff
* (C)2005, Dan Ponte.
* BSDL with advert.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <phoned.h>
#include <time.h>
FILE* logf;
extern struct conf cf;
int check_loglevel(lt, ll)
enum ltype lt;
int ll;
{
switch(lt) {
case debug:
if(!(ll & LL_DEBUG))
return -1;
case garbage:
if(!(ll & LL_GARBAGE))
return -1;
case info:
if(!(ll & LL_INFO))
return -1;
case warn:
if(!(ll & LL_WARN))
return -1;
case error:
if(!(ll & LL_ERROR))
return -1;
case critical:
if(!(ll & LL_CRITICAL))
return -1;
/* we cannot ignore FATAL. Because it's fatal. */
default:
return 1;
}
return 1;
}
int lprintf(enum ltype logtype, const char* fmt, ...)
{
va_list ap;
const char* fmtp;
char* ofmt;
int cnt = 0;
int j, i, l;
unsigned uns;
double dou;
char cha;
char* str;
void* voi;
int maxsize;
time_t now;
char tmt[128];
now = time(NULL);
l = cf.loglevels;
if(!check_loglevel(logtype, l))
return -1;
strftime(tmt, 128, "%d%b %H:%M:%S: ", localtime(&now));
fmtp = fmt;
maxsize = strlen(fmt) + 1;
ofmt = malloc(sizeof(char) * (strlen(fmt) + 2));
fputs(tmt, logf);
va_start(ap, fmt);
while(*fmtp) {
for(i = 0; fmtp[i] && fmtp[i] != '%' && i < maxsize; i++)
ofmt[i] = fmtp[i];
if(i) {
ofmt[i] = '\0';
cnt += fprintf(logf, ofmt);
fmtp += i;
} else {
for(i = 0; !isalpha(fmtp[i]) && i < maxsize; i++) {
ofmt[i] = fmtp[i];
if(i && fmtp[i] == '%') break;
}
ofmt[i] = fmtp[i];
ofmt[i + 1] = '\0';
fmtp += i + 1;
switch(ofmt[i]) {
case 'd':
j = va_arg(ap, int);
cnt += fprintf(logf, ofmt, j);
break;
case 'o':
case 'x':
case 'X':
case 'u':
uns = va_arg(ap, unsigned);
cnt += fprintf(logf, ofmt, uns);
break;
case 'c':
cha = (char) va_arg(ap, int);
cnt += fprintf(logf, ofmt, cha);
break;
case 's':
str = va_arg(ap, char*);
cnt += fprintf(logf, ofmt, str);
break;
case 'f':
case 'e':
case 'E':
case 'G':
case 'g':
dou = va_arg(ap, double);
cnt += fprintf(logf, ofmt, dou);
break;
case 'p':
voi = va_arg(ap, void*);
cnt += fprintf(logf, ofmt, voi);
break;
case 'n':
cnt += fprintf(logf, "%d", cnt);
break;
case '%':
fputc('%', logf);
cnt++;
break;
default:
fprintf(stderr, "Invalid format in log!\n");
break;
}
}
}
fflush(logf);
va_end(ap);
free(ofmt); /* MUST do this */
return cnt;
}

50
phoned/main.c Normal file
View File

@ -0,0 +1,50 @@
/*
* main.c - phoned main
* (C)2005, Dan Ponte
* BSD license with advert. clause.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>
#include <phoned.h>
extern struct conf cf;
short difflog = 0;
void usage(argv)
const char* argv;
{
fprintf(stderr, "%s: usage: %s [-h] [-c config] [-l log]\n", argv, argv);
}
int main(argc, argv)
int argc;
char* argv[];
{
int c;
cf.cfile = CONFIGFILE;
#define OPTSTRING "hc:l:"
while((c = getopt(argc, argv, OPTSTRING)) != -1)
switch(c) {
case 'c':
cf.cfile = strdup(optarg);
break;
case 'h':
usage(strdup(*argv));
return 0;
case 'l':
cf.logfile = strdup(optarg);
difflog = 1;
break;
default:
usage(strdup(*argv));
return -2;
}
cf.loglevels = LL_ALL;
initialize();
network();
shutd();
return 0;
}

10
phoned/phd Normal file
View File

@ -0,0 +1,10 @@
24Mar 17:05:57: phoned v0.1 starting..
24Mar 17:05:57: Encountered ipaddress 10.10.10.13
24Mar 17:05:57: Encountered ipaddress 10.10.10.1
24Mar 17:05:57: parser: end notify
24Mar 17:06:05: phoned v0.1 starting..
24Mar 17:06:05: Encountered ipaddress 10.10.10.13
24Mar 17:06:05: Encountered ipaddress 10.10.10.1
24Mar 17:06:05: parser: end notify
24Mar 17:06:51: Received signal 2, cleaning up...
24Mar 17:06:51: phoned shutting down...

4
phoned/phoned.conf Normal file
View File

@ -0,0 +1,4 @@
notify {
10.10.10.13;
10.10.10.1;
};

63
phoned/signals.c Normal file
View File

@ -0,0 +1,63 @@
/*
* Copyright (c) 2005, Dan Ponte
*
* signals.c - signal handlers
*
* 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.
*/
/* system includes */
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <phoned.h>
void handsig(sig)
int sig;
{
signal(sig, handsig);
switch(sig) {
case SIGINT:
case SIGQUIT:
case SIGTERM:
lprintf(fatal, "Received signal %d, cleaning up...\n", sig);
shutd();
exit(0);
break;
case SIGHUP:
lprintf(info, "Received HUP, rereading configuration files...\n");
break;
default:
lprintf(warn, "Received signal %d!\n", sig);
}
}
void install_handlers(void)
{
signal(SIGINT, handsig);
signal(SIGQUIT, handsig);
signal(SIGHUP, handsig);
signal(SIGTERM, handsig);
}

111
phoned/socket.c Normal file
View File

@ -0,0 +1,111 @@
/*
* Copyright (c) 2005, Dan Ponte
*
* socket.c - sockets.
*
* 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.
*/
/* system includes */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/select.h>
/* network stuff */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <phoned.h>
void handclient(sk)
int sk;
{
char buffer[1024];
int rlen;
rlen = recv(sk, buffer, sizeof(buffer), 0);
lprintf(debug, "Client said %s.", buffer);
}
void network(void)
{
int s, us, sn;
fd_set fds;
struct timeval tv;
int si = 1;
int sin_size, ilen;
struct sockaddr_un it;
if((s = socket(AF_LOCAL, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(-1);
}
strcpy(it.sun_path, SOCKETFILE);
it.sun_family = AF_LOCAL;
if(bind(s, (struct sockaddr *)&it, 1 + strlen(it.sun_path) +
sizeof(it.sun_family)) == -1) {
perror("bind");
exit(-1);
}
if(listen(s, 5) == -1) {
perror("listen");
exit(-1);
}
sin_size = sizeof(struct sockaddr_in);
for(;;) {
FD_ZERO(&fds);
FD_SET(s, &fds);
switch(select(s + 1, &fds, NULL, NULL, NULL)) {
case -1:
perror("select");
exit(-1);
break;
case 0:
/* NOTREACHED */
break;
default:
{
if(FD_ISSET(s, &fds) != 0) {
ilen = sizeof(it);
if((sn = accept(s,
(struct sockaddr *)&it, &ilen))
== -1) {
perror("s accept");
exit(-3);
}
handclient(sn);
close(sn);
}
}
}
}
close(s);
unlink(SOCKETFILE);
}