2015-09-16 11:29:27 +04:30
|
|
|
/*-
|
|
|
|
* Copyright (c) 2015, Babak Farrokhi
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/socket.h>
|
2015-09-16 16:41:39 +04:30
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
2015-10-11 14:56:33 +03:30
|
|
|
#include <arpa/inet.h>
|
2015-10-25 16:41:32 +03:30
|
|
|
#include <ctype.h>
|
2015-09-16 11:29:27 +04:30
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sysexits.h>
|
|
|
|
#include <unistd.h>
|
2015-09-16 16:41:39 +04:30
|
|
|
#include <syslog.h>
|
2015-10-25 14:10:45 +03:30
|
|
|
#include <pthread.h>
|
2015-10-21 11:38:14 +03:30
|
|
|
#include <pidutil.h>
|
2015-09-16 11:29:27 +04:30
|
|
|
|
2015-10-21 17:13:44 +03:30
|
|
|
#include "logfile.h"
|
|
|
|
|
2015-09-16 16:41:39 +04:30
|
|
|
#define PORT 5060
|
|
|
|
#define BACKLOG 1024
|
|
|
|
|
2015-10-25 15:16:55 +03:30
|
|
|
/*
|
|
|
|
* Globals
|
|
|
|
*/
|
2015-09-16 11:29:27 +04:30
|
|
|
struct pidfh *pfh;
|
2015-09-16 16:41:39 +04:30
|
|
|
struct protoent *proto_tcp, *proto_udp;
|
2015-10-25 15:16:55 +03:30
|
|
|
struct sockaddr_in t_sa, u_sa;
|
|
|
|
int t_sockfd, u_sockfd;
|
2015-10-21 18:16:13 +03:30
|
|
|
log_t *lfh;
|
2015-09-16 11:29:27 +04:30
|
|
|
|
2015-10-25 15:16:55 +03:30
|
|
|
/*
|
|
|
|
* Interface
|
|
|
|
*/
|
2015-10-25 14:10:45 +03:30
|
|
|
void *tcp_handler(void *args);
|
|
|
|
void *udp_handler(void *args);
|
|
|
|
|
|
|
|
/*
|
2015-10-25 16:39:42 +03:30
|
|
|
* trim string from whitespace characters
|
2015-10-25 14:10:45 +03:30
|
|
|
*/
|
2015-10-25 16:39:42 +03:30
|
|
|
size_t
|
|
|
|
chomp(char *restrict s)
|
2015-10-25 14:10:45 +03:30
|
|
|
{
|
2015-10-25 16:39:42 +03:30
|
|
|
int i;
|
2015-10-25 14:10:45 +03:30
|
|
|
|
2015-10-25 16:39:42 +03:30
|
|
|
/* trim leading spaces */
|
|
|
|
while (isspace(*s))
|
|
|
|
s++;
|
|
|
|
|
|
|
|
/* All spaces? */
|
|
|
|
if (*s == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* trim trailing spaces */
|
|
|
|
i = strlen(s);
|
|
|
|
|
|
|
|
while ((i > 0) && (isspace(s[i - 1])))
|
|
|
|
i--;
|
|
|
|
|
|
|
|
s[i] = '\0';
|
|
|
|
|
|
|
|
return i;
|
2015-10-25 14:10:45 +03:30
|
|
|
}
|
|
|
|
|
2015-09-16 11:29:27 +04:30
|
|
|
/*
|
|
|
|
* Prepare for a clean shutdown
|
|
|
|
*/
|
2015-09-16 16:41:39 +04:30
|
|
|
void
|
2015-09-16 11:29:27 +04:30
|
|
|
daemon_shutdown()
|
|
|
|
{
|
|
|
|
pidfile_remove(pfh);
|
2015-10-21 18:16:13 +03:30
|
|
|
log_close(lfh);
|
2015-09-16 11:29:27 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Act upon receiving signals
|
|
|
|
*/
|
2015-09-16 16:41:39 +04:30
|
|
|
void
|
2015-09-16 11:29:27 +04:30
|
|
|
signal_handler(int sig)
|
|
|
|
{
|
|
|
|
switch (sig) {
|
|
|
|
|
|
|
|
case SIGHUP:
|
2015-10-21 18:16:13 +03:30
|
|
|
log_reopen(&lfh);
|
2015-09-16 16:41:39 +04:30
|
|
|
break;
|
2015-09-16 11:29:27 +04:30
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
|
|
|
daemon_shutdown();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 16:41:39 +04:30
|
|
|
void
|
2015-10-25 15:58:23 +03:30
|
|
|
process_request(const struct sockaddr_in *sa, int type, char *str)
|
2015-09-16 16:41:39 +04:30
|
|
|
{
|
2015-10-25 15:58:23 +03:30
|
|
|
char *s_types[] = {"TCP", "UDP", "RAW", "Unknown"};
|
|
|
|
char *ptype;
|
|
|
|
|
|
|
|
switch (type) {
|
2015-10-25 16:07:29 +03:30
|
|
|
case SOCK_STREAM:
|
|
|
|
ptype = s_types[0];
|
|
|
|
break;
|
|
|
|
case SOCK_DGRAM:
|
|
|
|
ptype = s_types[1];
|
|
|
|
break;
|
|
|
|
case SOCK_RAW:
|
|
|
|
ptype = s_types[2];
|
2015-10-25 16:12:59 +03:30
|
|
|
break;
|
2015-10-25 16:07:29 +03:30
|
|
|
default:
|
|
|
|
ptype = s_types[3];;
|
2015-10-25 15:58:23 +03:30
|
|
|
}
|
2015-10-20 17:03:29 +03:30
|
|
|
|
2015-10-25 14:10:45 +03:30
|
|
|
chomp(str);
|
2015-10-25 15:58:23 +03:30
|
|
|
log_printf(lfh, "%ld,%s,%s,%d,\"%s\"",
|
|
|
|
time(NULL), ptype, inet_ntoa(sa->sin_addr), ntohs(sa->sin_port), str);
|
2015-09-16 16:41:39 +04:30
|
|
|
}
|
|
|
|
|
2015-09-16 11:29:27 +04:30
|
|
|
/*
|
|
|
|
* Daemonize and persist pid
|
|
|
|
*/
|
2015-09-16 16:41:39 +04:30
|
|
|
int
|
2015-09-16 11:29:27 +04:30
|
|
|
daemon_start()
|
|
|
|
{
|
|
|
|
struct sigaction sig_action;
|
|
|
|
sigset_t sig_set;
|
|
|
|
pid_t otherpid;
|
2015-09-16 16:41:39 +04:30
|
|
|
int curPID;
|
2015-10-25 14:10:45 +03:30
|
|
|
pthread_t tcp_thread, udp_thread;
|
2015-09-16 16:41:39 +04:30
|
|
|
|
|
|
|
/* Check if we can acquire the pid file */
|
|
|
|
pfh = pidfile_open(NULL, 0600, &otherpid);
|
|
|
|
|
|
|
|
if (pfh == NULL) {
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
errx(EXIT_FAILURE, "Daemon already running, pid: %jd.", (intmax_t)otherpid);
|
|
|
|
}
|
2015-10-21 17:13:44 +03:30
|
|
|
err(EXIT_FAILURE, "Cannot open or create pidfile");
|
2015-09-16 16:41:39 +04:30
|
|
|
}
|
2015-10-21 18:16:13 +03:30
|
|
|
/* open a log file in current directory */
|
2015-10-25 15:58:23 +03:30
|
|
|
if ((lfh = log_open("fsipd.log", 0644)) == NULL) {
|
2015-10-21 18:16:13 +03:30
|
|
|
err(EXIT_FAILURE, "Cannot open log file");
|
|
|
|
}
|
2015-10-25 15:16:55 +03:30
|
|
|
/* setup TCP socket */
|
|
|
|
if ((proto_tcp = getprotobyname("tcp")) == NULL)
|
|
|
|
return (EXIT_FAILURE);
|
|
|
|
|
|
|
|
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("tcp socket()");
|
|
|
|
return (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (bind(t_sockfd, (struct sockaddr *)&t_sa, sizeof t_sa) < 0) {
|
|
|
|
perror("tcp bind()");
|
|
|
|
return (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
/* setup UDP socket */
|
|
|
|
if ((proto_udp = getprotobyname("udp")) == NULL)
|
|
|
|
return (EXIT_FAILURE);
|
|
|
|
|
|
|
|
bzero(&u_sa, sizeof(u_sa));
|
|
|
|
u_sa.sin_port = htons(PORT);
|
|
|
|
u_sa.sin_family = AF_INET;
|
|
|
|
u_sa.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
|
|
|
if ((u_sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
|
|
|
|
perror("udp socket()");
|
|
|
|
return (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (bind(u_sockfd, (struct sockaddr *)&u_sa, sizeof u_sa) < 0) {
|
|
|
|
perror("udp bind()");
|
|
|
|
return (EXIT_FAILURE);
|
|
|
|
}
|
2015-09-16 16:41:39 +04:30
|
|
|
/* start daemonizing */
|
|
|
|
curPID = fork();
|
|
|
|
|
|
|
|
switch (curPID) {
|
|
|
|
case 0: /* This process is the child */
|
|
|
|
break;
|
|
|
|
case -1: /* fork() failed, should exit */
|
|
|
|
perror("fork");
|
|
|
|
return (EXIT_FAILURE);
|
2015-10-25 15:16:55 +03:30
|
|
|
default: /* fork() successful, should exit
|
|
|
|
* (parent) */
|
2015-09-16 16:41:39 +04:30
|
|
|
return (EXIT_SUCCESS);
|
2015-09-16 11:29:27 +04:30
|
|
|
}
|
|
|
|
|
2015-09-16 16:41:39 +04:30
|
|
|
/* we are the child, complete the daemonization */
|
2015-10-25 15:16:55 +03:30
|
|
|
|
2015-09-16 16:41:39 +04:30
|
|
|
/* Close standard IO */
|
|
|
|
fclose(stdin);
|
|
|
|
fclose(stdout);
|
|
|
|
fclose(stderr);
|
|
|
|
|
|
|
|
/* Block unnecessary signals */
|
|
|
|
sigemptyset(&sig_set);
|
|
|
|
sigaddset(&sig_set, SIGCHLD); /* ignore child - i.e. we don't need
|
|
|
|
* to wait for it */
|
|
|
|
sigaddset(&sig_set, SIGTSTP); /* ignore Tty stop signals */
|
|
|
|
sigaddset(&sig_set, SIGTTOU); /* ignore Tty background writes */
|
|
|
|
sigaddset(&sig_set, SIGTTIN); /* ignore Tty background reads */
|
|
|
|
sigprocmask(SIG_BLOCK, &sig_set, NULL); /* Block the above specified
|
|
|
|
* signals */
|
|
|
|
|
|
|
|
/* Catch necessary signals */
|
|
|
|
sig_action.sa_handler = signal_handler;
|
|
|
|
sigemptyset(&sig_action.sa_mask);
|
|
|
|
sig_action.sa_flags = 0;
|
|
|
|
|
|
|
|
sigaction(SIGTERM, &sig_action, NULL);
|
|
|
|
sigaction(SIGHUP, &sig_action, NULL);
|
|
|
|
sigaction(SIGINT, &sig_action, NULL);
|
|
|
|
|
|
|
|
/* create new session and process group */
|
|
|
|
setsid();
|
|
|
|
|
2015-10-25 15:16:55 +03:30
|
|
|
#ifdef DEBUG
|
2015-10-21 18:16:13 +03:30
|
|
|
syslog(LOG_ALERT, "%s started with PID %d\n",
|
|
|
|
getprogname(), getpid());
|
2015-10-25 15:16:55 +03:30
|
|
|
#endif /* __DEBUG__ */
|
2015-10-21 18:16:13 +03:30
|
|
|
|
2015-09-16 16:41:39 +04:30
|
|
|
/* persist pid */
|
|
|
|
pidfile_write(pfh);
|
|
|
|
|
2015-10-25 14:10:45 +03:30
|
|
|
/* Create TCP and UDP listener threads */
|
|
|
|
pthread_create(&tcp_thread, NULL, tcp_handler, NULL);
|
|
|
|
pthread_create(&udp_thread, NULL, udp_handler, NULL);
|
2015-09-16 16:41:39 +04:30
|
|
|
|
2015-10-25 14:10:45 +03:30
|
|
|
/*
|
2015-10-25 15:16:55 +03:30
|
|
|
* Wait until first or second thread terminates, which
|
|
|
|
* normally shouldn't ever happen
|
2015-10-25 14:10:45 +03:30
|
|
|
*/
|
|
|
|
pthread_join(tcp_thread, NULL);
|
2015-10-25 15:16:55 +03:30
|
|
|
pthread_join(udp_thread, NULL);
|
2015-09-16 11:29:27 +04:30
|
|
|
|
2015-10-25 14:10:45 +03:30
|
|
|
return (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
tcp_handler(void *args)
|
|
|
|
{
|
2015-10-25 15:16:55 +03:30
|
|
|
int c;
|
|
|
|
struct sockaddr_in t_other;
|
2015-10-25 14:10:45 +03:30
|
|
|
FILE *client;
|
2015-10-25 15:16:55 +03:30
|
|
|
char str[8192];
|
|
|
|
socklen_t sa_len;
|
2015-10-25 14:10:45 +03:30
|
|
|
|
|
|
|
listen(t_sockfd, BACKLOG);
|
|
|
|
|
|
|
|
while (1) {
|
2015-10-25 15:16:55 +03:30
|
|
|
sa_len = sizeof(t_sa);
|
|
|
|
if ((c = accept(t_sockfd, (struct sockaddr *)&t_other, &sa_len)) < 0) {
|
2015-09-16 16:41:39 +04:30
|
|
|
perror("accept");
|
2015-10-25 14:10:45 +03:30
|
|
|
pthread_exit(NULL);
|
2015-09-16 16:41:39 +04:30
|
|
|
}
|
2015-09-16 16:51:12 +04:30
|
|
|
if ((client = fdopen(c, "r")) == NULL) {
|
2015-09-16 16:41:39 +04:30
|
|
|
perror("fdopen");
|
2015-10-25 14:10:45 +03:30
|
|
|
pthread_exit(NULL);
|
2015-09-16 16:41:39 +04:30
|
|
|
}
|
2015-10-25 15:16:55 +03:30
|
|
|
bzero(str, sizeof(str));/* just in case */
|
|
|
|
fgets(str, sizeof(str), client);
|
2015-10-25 15:58:23 +03:30
|
|
|
process_request(&t_other, SOCK_STREAM, str);
|
2015-09-16 16:41:39 +04:30
|
|
|
fclose(client);
|
2015-09-16 11:29:27 +04:30
|
|
|
}
|
2015-10-25 15:16:55 +03:30
|
|
|
return (args); /* mute the compiler warning */
|
2015-10-25 14:10:45 +03:30
|
|
|
}
|
2015-09-16 11:29:27 +04:30
|
|
|
|
2015-10-25 14:10:45 +03:30
|
|
|
void *
|
|
|
|
udp_handler(void *args)
|
|
|
|
{
|
2015-10-25 15:16:55 +03:30
|
|
|
char str[8192];
|
|
|
|
struct sockaddr_in u_other;
|
|
|
|
socklen_t sa_len;
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
sa_len = sizeof(u_other);
|
|
|
|
while (1) {
|
|
|
|
if ((len = recvfrom(u_sockfd, str, sizeof(str), 0, (struct sockaddr *)&u_other, &sa_len)) > 0) {
|
2015-10-25 15:58:23 +03:30
|
|
|
process_request(&u_other, SOCK_DGRAM, str);
|
2015-10-25 16:07:29 +03:30
|
|
|
}
|
2015-10-25 15:16:55 +03:30
|
|
|
}
|
|
|
|
|
2015-10-25 14:10:45 +03:30
|
|
|
return (args); /* mute the compiler warning */
|
2015-09-16 11:29:27 +04:30
|
|
|
}
|
|
|
|
|
2015-09-16 16:41:39 +04:30
|
|
|
int
|
2015-10-20 17:09:30 +03:30
|
|
|
main(void)
|
2015-09-16 11:29:27 +04:30
|
|
|
{
|
2015-09-16 16:41:39 +04:30
|
|
|
return (daemon_start());
|
2015-09-16 11:29:27 +04:30
|
|
|
}
|