- Add threading support. Listeners now have their own threads
- Cleanup Makefile
This commit is contained in:
parent
8a79888d5e
commit
4c689a160f
2
Makefile
2
Makefile
@ -16,5 +16,5 @@ test: logfile.c logfile_test.c
|
|||||||
$(CC) $(CFLAGS) $(INC) $(LIB) logfile.c logfile_test.c -o logfile_test
|
$(CC) $(CFLAGS) $(INC) $(LIB) logfile.c logfile_test.c -o logfile_test
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f *.BAK *.log *.o *.a a.out core temp.* $(LIBPIDFILE) $(PROGS)
|
rm -f *.BAK *.log *.o *.a a.out core temp.* $(PROGS)
|
||||||
rm -fr *.dSYM
|
rm -fr *.dSYM
|
||||||
|
112
fsipd.c
112
fsipd.c
@ -46,6 +46,7 @@
|
|||||||
#include <sysexits.h>
|
#include <sysexits.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
|
#include <pthread.h>
|
||||||
#include <pidutil.h>
|
#include <pidutil.h>
|
||||||
|
|
||||||
#include "logfile.h"
|
#include "logfile.h"
|
||||||
@ -55,10 +56,28 @@
|
|||||||
|
|
||||||
/* Globals */
|
/* Globals */
|
||||||
struct pidfh *pfh;
|
struct pidfh *pfh;
|
||||||
struct sockaddr_in sa;
|
struct sockaddr_in u_sa;
|
||||||
struct protoent *proto_tcp, *proto_udp;
|
struct protoent *proto_tcp, *proto_udp;
|
||||||
log_t *lfh;
|
log_t *lfh;
|
||||||
|
|
||||||
|
/* Interface */
|
||||||
|
|
||||||
|
void *tcp_handler(void *args);
|
||||||
|
void *udp_handler(void *args);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* remove training newline character from string
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
chomp(char *s)
|
||||||
|
{
|
||||||
|
char *p;
|
||||||
|
|
||||||
|
while (NULL != s && NULL != (p = strrchr(s, '\n'))) {
|
||||||
|
*p = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Prepare for a clean shutdown
|
* Prepare for a clean shutdown
|
||||||
*/
|
*/
|
||||||
@ -91,12 +110,13 @@ signal_handler(int sig)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
process_request(char *str)
|
process_request(const struct sockaddr_in *sa, char *str)
|
||||||
{
|
{
|
||||||
/* TODO: change format to CSV */
|
/* TODO: change format to CSV */
|
||||||
|
|
||||||
|
chomp(str);
|
||||||
log_tsprintf(lfh, "sip: %s, sport: %d, payload: \"%s\"",
|
log_tsprintf(lfh, "sip: %s, sport: %d, payload: \"%s\"",
|
||||||
inet_ntoa(sa.sin_addr), ntohs(sa.sin_port), str);
|
inet_ntoa(sa->sin_addr), ntohs(sa->sin_port), str);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -109,15 +129,11 @@ daemon_start()
|
|||||||
sigset_t sig_set;
|
sigset_t sig_set;
|
||||||
pid_t otherpid;
|
pid_t otherpid;
|
||||||
int curPID;
|
int curPID;
|
||||||
register int s, c;
|
pthread_t tcp_thread, udp_thread;
|
||||||
unsigned int b;
|
|
||||||
FILE *client;
|
|
||||||
char inputstr[8192];
|
|
||||||
|
|
||||||
/* Check if we can acquire the pid file */
|
/* Check if we can acquire the pid file */
|
||||||
pfh = pidfile_open(NULL, 0600, &otherpid);
|
pfh = pidfile_open(NULL, 0600, &otherpid);
|
||||||
|
|
||||||
|
|
||||||
if (pfh == NULL) {
|
if (pfh == NULL) {
|
||||||
if (errno == EEXIST) {
|
if (errno == EEXIST) {
|
||||||
errx(EXIT_FAILURE, "Daemon already running, pid: %jd.", (intmax_t)otherpid);
|
errx(EXIT_FAILURE, "Daemon already running, pid: %jd.", (intmax_t)otherpid);
|
||||||
@ -128,23 +144,6 @@ daemon_start()
|
|||||||
if ((lfh = log_open(NULL, 0644)) == NULL) {
|
if ((lfh = log_open(NULL, 0644)) == NULL) {
|
||||||
err(EXIT_FAILURE, "Cannot open log file");
|
err(EXIT_FAILURE, "Cannot open log file");
|
||||||
}
|
}
|
||||||
/* setup socket */
|
|
||||||
if ((proto_tcp = getprotobyname("tcp")) == NULL)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
bzero(&sa, sizeof(sa));
|
|
||||||
sa.sin_port = htons(PORT);
|
|
||||||
sa.sin_family = AF_INET;
|
|
||||||
sa.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
||||||
|
|
||||||
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
|
|
||||||
perror("socket");
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
if (bind(s, (struct sockaddr *)&sa, sizeof sa) < 0) {
|
|
||||||
perror("bind");
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
/* start daemonizing */
|
/* start daemonizing */
|
||||||
curPID = fork();
|
curPID = fork();
|
||||||
|
|
||||||
@ -193,29 +192,70 @@ daemon_start()
|
|||||||
/* persist pid */
|
/* persist pid */
|
||||||
pidfile_write(pfh);
|
pidfile_write(pfh);
|
||||||
|
|
||||||
/* TODO: Network Logic Here */
|
/* Create TCP and UDP listener threads */
|
||||||
|
pthread_create(&tcp_thread, NULL, tcp_handler, NULL);
|
||||||
|
pthread_create(&udp_thread, NULL, udp_handler, NULL);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Wait until first thread terminates, which normally shouldn't ever
|
||||||
|
* happen
|
||||||
|
*/
|
||||||
|
|
||||||
|
pthread_join(tcp_thread, NULL);
|
||||||
|
|
||||||
|
return (EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *
|
||||||
|
tcp_handler(void *args)
|
||||||
|
{
|
||||||
|
unsigned int b;
|
||||||
|
int c, t_sockfd;
|
||||||
|
struct sockaddr_in t_sa;
|
||||||
|
FILE *client;
|
||||||
|
char inputstr[8192];
|
||||||
|
|
||||||
|
/* setup TCP socket */
|
||||||
|
if ((proto_tcp = getprotobyname("tcp")) == NULL)
|
||||||
|
pthread_exit(NULL);
|
||||||
|
|
||||||
|
bzero(&t_sa, sizeof(t_sa));
|
||||||
|
t_sa.sin_port = htons(PORT);
|
||||||
|
t_sa.sin_family = AF_INET;
|
||||||
|
t_sa.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||||
|
|
||||||
|
if ((t_sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
|
||||||
|
perror("socket");
|
||||||
|
pthread_exit(NULL);
|
||||||
|
}
|
||||||
|
if (bind(t_sockfd, (struct sockaddr *)&t_sa, sizeof t_sa) < 0) {
|
||||||
|
perror("bind");
|
||||||
|
pthread_exit(NULL);
|
||||||
|
}
|
||||||
|
listen(t_sockfd, BACKLOG);
|
||||||
|
|
||||||
listen(s, BACKLOG);
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
b = sizeof(t_sa);
|
||||||
b = sizeof(sa);
|
if ((c = accept(t_sockfd, (struct sockaddr *)&t_sa, &b)) < 0) {
|
||||||
if ((c = accept(s, (struct sockaddr *)&sa, &b)) < 0) {
|
|
||||||
perror("accept");
|
perror("accept");
|
||||||
return (EXIT_FAILURE);
|
pthread_exit(NULL);
|
||||||
}
|
}
|
||||||
if ((client = fdopen(c, "r")) == NULL) {
|
if ((client = fdopen(c, "r")) == NULL) {
|
||||||
perror("fdopen");
|
perror("fdopen");
|
||||||
return (EXIT_FAILURE);
|
pthread_exit(NULL);
|
||||||
}
|
}
|
||||||
fgets(inputstr, sizeof(inputstr), client);
|
fgets(inputstr, sizeof(inputstr), client);
|
||||||
|
process_request(&t_sa, inputstr);
|
||||||
process_request(inputstr);
|
|
||||||
|
|
||||||
fclose(client);
|
fclose(client);
|
||||||
}
|
}
|
||||||
|
return (args);
|
||||||
|
}
|
||||||
|
|
||||||
return (EXIT_SUCCESS);
|
void *
|
||||||
|
udp_handler(void *args)
|
||||||
|
{
|
||||||
|
return (args); /* mute the compiler warning */
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
Loading…
x
Reference in New Issue
Block a user