2015-09-16 11:29:27 +04:30
|
|
|
/*-
|
2016-04-06 12:42:13 +04:30
|
|
|
* Copyright (c) 2016, Babak Farrokhi
|
2015-09-16 11:29:27 +04:30
|
|
|
* 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-11-07 13:08:04 +03:30
|
|
|
#define SYSLOG_NAMES
|
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-31 12:53:53 +03:30
|
|
|
#ifndef IPV6_BINDV6ONLY /* Linux does not have IPV6_BINDV6ONLY */
|
2015-10-27 10:42:05 +03:30
|
|
|
#define IPV6_BINDV6ONLY IPV6_V6ONLY
|
2015-10-31 12:53:53 +03:30
|
|
|
#endif /* IPV6_BINDV6ONLY */
|
2015-10-26 18:15:27 +03:30
|
|
|
|
2015-10-25 15:16:55 +03:30
|
|
|
/*
|
|
|
|
* Globals
|
|
|
|
*/
|
2015-10-26 18:15:27 +03:30
|
|
|
log_t *lfh;
|
2015-09-16 11:29:27 +04:30
|
|
|
struct pidfh *pfh;
|
2015-11-06 23:28:33 +03:30
|
|
|
bool use_syslog = false;
|
2015-11-07 13:49:47 +03:30
|
|
|
char *logfilename = NULL;
|
2015-11-07 13:08:04 +03:30
|
|
|
int syslog_pri = -1;
|
2015-10-26 18:15:27 +03:30
|
|
|
|
2015-10-25 15:16:55 +03:30
|
|
|
struct sockaddr_in t_sa, u_sa;
|
|
|
|
int t_sockfd, u_sockfd;
|
2015-09-16 11:29:27 +04:30
|
|
|
|
2015-10-26 18:15:27 +03:30
|
|
|
#ifdef PF_INET6
|
|
|
|
struct sockaddr_in6 t6_sa, u6_sa;
|
|
|
|
int t6_sockfd, u6_sockfd;
|
|
|
|
|
|
|
|
#endif /* PF_INET6 */
|
2015-10-25 14:10:45 +03:30
|
|
|
|
|
|
|
/*
|
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
|
2015-10-27 10:42:05 +03:30
|
|
|
chomp(char *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-11-07 12:23:42 +03:30
|
|
|
if (!use_syslog)
|
|
|
|
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-11-07 12:23:42 +03:30
|
|
|
if (!use_syslog)
|
|
|
|
log_reopen(&lfh); /* necessary for log file
|
|
|
|
* rotation */
|
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-27 10:42:05 +03:30
|
|
|
process_request(int af, struct sockaddr *src, int proto, char *str)
|
2015-09-16 16:41:39 +04:30
|
|
|
{
|
2015-10-26 18:15:27 +03:30
|
|
|
char *p_names[] = {"TCP", "UDP", "RAW", "UNKNOWN"};
|
|
|
|
char *pname;
|
|
|
|
u_int port;
|
|
|
|
char addr_str[46];
|
|
|
|
struct sockaddr_in *s_in;
|
2015-10-25 15:58:23 +03:30
|
|
|
|
2015-10-26 18:15:27 +03:30
|
|
|
#ifdef PF_INET6
|
|
|
|
struct sockaddr_in6 *s_in6;
|
|
|
|
|
|
|
|
#endif /* PF_INET6 */
|
|
|
|
|
|
|
|
switch (proto) {
|
2015-10-25 16:07:29 +03:30
|
|
|
case SOCK_STREAM:
|
2015-10-26 18:15:27 +03:30
|
|
|
pname = p_names[0];
|
2015-10-25 16:07:29 +03:30
|
|
|
break;
|
|
|
|
case SOCK_DGRAM:
|
2015-10-26 18:15:27 +03:30
|
|
|
pname = p_names[1];
|
2015-10-25 16:07:29 +03:30
|
|
|
break;
|
|
|
|
case SOCK_RAW:
|
2015-10-26 18:15:27 +03:30
|
|
|
pname = p_names[2];
|
2015-10-25 16:12:59 +03:30
|
|
|
break;
|
2015-10-25 16:07:29 +03:30
|
|
|
default:
|
2015-10-26 18:15:27 +03:30
|
|
|
pname = p_names[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-26 18:15:27 +03:30
|
|
|
|
|
|
|
#ifdef PF_INET6
|
|
|
|
switch (af) {
|
|
|
|
case AF_INET6:
|
|
|
|
s_in6 = (struct sockaddr_in6 *)src;
|
|
|
|
inet_ntop(af, &s_in6->sin6_addr, addr_str, sizeof(addr_str));
|
|
|
|
port = ntohs(s_in6->sin6_port);
|
2015-11-07 12:23:42 +03:30
|
|
|
if (use_syslog) {
|
|
|
|
syslog(syslog_pri, "From: %s:%d (%s6) - Message: \"%s\"", addr_str, port, pname, str);
|
|
|
|
} else {
|
|
|
|
log_printf(lfh, "%ld,%s6,%s,%d,\"%s\"", time(NULL), pname, addr_str, port, str);
|
|
|
|
}
|
2015-10-26 18:15:27 +03:30
|
|
|
break;
|
|
|
|
case AF_INET:
|
|
|
|
s_in = (struct sockaddr_in *)src;
|
2015-11-07 12:23:42 +03:30
|
|
|
if (use_syslog) {
|
|
|
|
syslog(syslog_pri, "From: %s:%d (%s4) - Message: \"%s\"", inet_ntoa(s_in->sin_addr), ntohs(s_in->sin_port), pname, str);
|
|
|
|
} else {
|
|
|
|
log_printf(lfh, "%ld,%s4,%s,%d,\"%s\"", time(NULL), pname, inet_ntoa(s_in->sin_addr), ntohs(s_in->sin_port), str);
|
|
|
|
}
|
2015-10-26 18:15:27 +03:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
#else
|
2015-10-31 12:53:53 +03:30
|
|
|
s_in = (struct sockaddr_in *)src;
|
2015-11-07 12:23:42 +03:30
|
|
|
if (use_syslog) {
|
|
|
|
syslog(syslog_pri, "From: %s:%d (%s4) - Message: \"%s\"", inet_ntoa(s_in->sin_addr), ntohs(s_in->sin_port), pname, str);
|
|
|
|
} else {
|
|
|
|
log_printf(lfh, "%ld,%s4,%s,%d,\"%s\"", time(NULL), pname, inet_ntoa(s_in->sin_addr), ntohs(s_in->sin_port), str);
|
|
|
|
}
|
2015-10-26 18:15:27 +03:30
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* setup TCP listener socket
|
|
|
|
*/
|
2015-10-26 21:29:33 +03:30
|
|
|
int
|
2015-10-26 18:15:27 +03:30
|
|
|
init_tcp()
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef PF_INET6
|
|
|
|
/* Setup TCP6 Listener */
|
|
|
|
bzero(&t6_sa, sizeof(t6_sa));
|
|
|
|
t6_sa.sin6_port = htons(PORT);
|
|
|
|
t6_sa.sin6_family = AF_INET6;
|
|
|
|
t6_sa.sin6_addr = in6addr_any;
|
|
|
|
t6_sa.sin6_scope_id = 0;
|
|
|
|
if ((t6_sockfd = socket(PF_INET6, SOCK_STREAM, 0)) < 0) {
|
|
|
|
perror("tcp6 socket()");
|
|
|
|
return (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
int on = 1;
|
2015-10-31 12:53:53 +03:30
|
|
|
|
2015-10-26 18:15:27 +03:30
|
|
|
setsockopt(t6_sockfd, IPPROTO_IPV6, IPV6_BINDV6ONLY, (char *)&on, sizeof(on));
|
|
|
|
|
|
|
|
if (bind(t6_sockfd, (struct sockaddr *)&t6_sa, sizeof(t6_sa)) < 0) {
|
|
|
|
perror("tcp6 bind()");
|
|
|
|
return (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
#endif /* PF6_INET */
|
|
|
|
|
|
|
|
/* Setup TCP4 Listener */
|
|
|
|
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("tcp4 socket()");
|
|
|
|
return (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (bind(t_sockfd, (struct sockaddr *)&t_sa, sizeof(t_sa)) < 0) {
|
|
|
|
perror("tcp4 bind()");
|
|
|
|
return (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
return (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* setup UDP listener socket
|
|
|
|
*/
|
2015-10-26 21:29:33 +03:30
|
|
|
int
|
2015-10-26 18:15:27 +03:30
|
|
|
init_udp()
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef PF_INET6
|
|
|
|
|
|
|
|
/* Setup UDP6 Listener */
|
|
|
|
bzero(&u6_sa, sizeof(u6_sa));
|
|
|
|
u6_sa.sin6_port = htons(PORT);
|
|
|
|
u6_sa.sin6_family = AF_INET6;
|
|
|
|
u6_sa.sin6_addr = in6addr_any;
|
|
|
|
u6_sa.sin6_scope_id = 0;
|
|
|
|
if ((u6_sockfd = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) {
|
|
|
|
perror("udp6 socket()");
|
|
|
|
return (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
int on = 1;
|
2015-10-31 12:53:53 +03:30
|
|
|
|
2015-10-26 18:15:27 +03:30
|
|
|
setsockopt(u6_sockfd, IPPROTO_IPV6, IPV6_BINDV6ONLY, (char *)&on, sizeof(on));
|
|
|
|
|
|
|
|
if (bind(u6_sockfd, (struct sockaddr *)&u6_sa, sizeof(u6_sa)) < 0) {
|
|
|
|
perror("udp6 bind()");
|
|
|
|
return (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
#endif /* PF_INET6 */
|
|
|
|
|
|
|
|
/* Setup UDP4 Listener */
|
|
|
|
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("udp4 socket()");
|
|
|
|
return (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (bind(u_sockfd, (struct sockaddr *)&u_sa, sizeof(u_sa)) < 0) {
|
|
|
|
perror("udp4 bind()");
|
|
|
|
return (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
return (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
tcp4_handler(void *args)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
struct sockaddr_in t_other;
|
|
|
|
FILE *client;
|
|
|
|
char str[8192];
|
|
|
|
socklen_t sa_len;
|
|
|
|
|
|
|
|
listen(t_sockfd, BACKLOG);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
sa_len = sizeof(t_sa);
|
|
|
|
if ((c = accept(t_sockfd, (struct sockaddr *)&t_other, &sa_len)) < 0) {
|
|
|
|
perror("tcp accept()");
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
if ((client = fdopen(c, "r")) == NULL) {
|
|
|
|
perror("tcp fdopen()");
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
bzero(str, sizeof(str));/* just in case */
|
|
|
|
fgets(str, sizeof(str), client);
|
|
|
|
process_request(t_other.sin_family, (struct sockaddr *)&t_other, SOCK_STREAM, str);
|
|
|
|
fclose(client);
|
|
|
|
}
|
|
|
|
return (args); /* suppress compiler warning */
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
udp4_handler(void *args)
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
process_request(u_other.sin_family, (struct sockaddr *)&u_other, SOCK_DGRAM, str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (args); /* suppress compiler warning */
|
2015-09-16 16:41:39 +04:30
|
|
|
}
|
|
|
|
|
2015-10-26 18:15:27 +03:30
|
|
|
#ifdef PF_INET6
|
|
|
|
|
|
|
|
void *
|
|
|
|
tcp6_handler(void *args)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
struct sockaddr_in6 t_other;
|
|
|
|
FILE *client;
|
|
|
|
char str[8192];
|
|
|
|
socklen_t sa_len;
|
|
|
|
|
|
|
|
listen(t6_sockfd, BACKLOG);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
sa_len = sizeof(t6_sa);
|
|
|
|
if ((c = accept(t6_sockfd, (struct sockaddr *)&t_other, &sa_len)) < 0) {
|
|
|
|
perror("tcp6 accept()");
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
if ((client = fdopen(c, "r")) == NULL) {
|
|
|
|
perror("tcp6 fdopen()");
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
bzero(str, sizeof(str));/* just in case */
|
|
|
|
fgets(str, sizeof(str), client);
|
|
|
|
process_request(t_other.sin6_family, (struct sockaddr *)&t_other, SOCK_STREAM, str);
|
|
|
|
fclose(client);
|
|
|
|
}
|
|
|
|
return (args); /* suppress compiler warning */
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
udp6_handler(void *args)
|
|
|
|
{
|
|
|
|
char str[8192];
|
|
|
|
struct sockaddr_in6 u_other;
|
|
|
|
socklen_t sa_len;
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
sa_len = sizeof(u_other);
|
|
|
|
while (1) {
|
|
|
|
if ((len = recvfrom(u6_sockfd, str, sizeof(str), 0, (struct sockaddr *)&u_other, &sa_len)) > 0) {
|
|
|
|
process_request(u_other.sin6_family, (struct sockaddr *)&u_other, SOCK_DGRAM, str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (args); /* suppress compiler warning */
|
|
|
|
}
|
|
|
|
|
2015-10-31 12:53:53 +03:30
|
|
|
#endif /* PF_INET6 */
|
2015-10-26 18:15:27 +03:30
|
|
|
|
2015-11-07 12:23:42 +03:30
|
|
|
void
|
|
|
|
init_logger()
|
|
|
|
{
|
|
|
|
if (use_syslog) {
|
|
|
|
/* initialize facility and level parameters */
|
2015-11-07 13:08:04 +03:30
|
|
|
if (syslog_pri == -1) /* not specidied by user, use default */
|
|
|
|
syslog_pri = LOG_USER | LOG_NOTICE | LOG_PID;
|
2015-11-07 12:23:42 +03:30
|
|
|
} else {
|
|
|
|
/* open a log file in current directory */
|
2015-11-07 13:49:47 +03:30
|
|
|
if (logfilename == NULL)
|
|
|
|
logfilename = strdup("fsipd.log");
|
|
|
|
if ((lfh = log_open(logfilename, 0644)) == NULL)
|
|
|
|
err(EXIT_FAILURE, "Cannot open log file \"%s\"", logfilename);
|
2015-11-07 12:23:42 +03: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-26 18:15:27 +03:30
|
|
|
pthread_t tcp4_thread, udp4_thread;
|
|
|
|
pthread_t tcp6_thread, udp6_thread;
|
2015-09-16 16:41:39 +04:30
|
|
|
|
|
|
|
/* Check if we can acquire the pid file */
|
2016-08-16 11:24:31 +04:30
|
|
|
pfh = pidfile_open(NULL, 0644, &otherpid);
|
2015-09-16 16:41:39 +04:30
|
|
|
|
|
|
|
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-11-07 12:23:42 +03:30
|
|
|
init_logger();
|
|
|
|
|
2015-10-26 18:15:27 +03:30
|
|
|
/* Initialize TCP46 and UDP46 sockets */
|
|
|
|
if (init_tcp() == EXIT_FAILURE)
|
2015-10-25 15:16:55 +03:30
|
|
|
return (EXIT_FAILURE);
|
2015-10-26 18:15:27 +03:30
|
|
|
if (init_udp() == EXIT_FAILURE)
|
2015-10-25 15:16:55 +03:30
|
|
|
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 */
|
2015-10-26 18:15:27 +03:30
|
|
|
sigaddset(&sig_set, SIGTSTP); /* ignore tty stop signals */
|
|
|
|
sigaddset(&sig_set, SIGTTOU); /* ignore tty background writes */
|
|
|
|
sigaddset(&sig_set, SIGTTIN); /* ignore tty background reads */
|
2015-09-16 16:41:39 +04:30
|
|
|
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();
|
|
|
|
|
|
|
|
/* persist pid */
|
|
|
|
pidfile_write(pfh);
|
|
|
|
|
2015-10-25 14:10:45 +03:30
|
|
|
/* Create TCP and UDP listener threads */
|
2015-10-26 18:15:27 +03:30
|
|
|
pthread_create(&tcp4_thread, NULL, tcp4_handler, NULL);
|
|
|
|
pthread_create(&udp4_thread, NULL, udp4_handler, NULL);
|
|
|
|
#ifdef PF_INET6
|
|
|
|
pthread_create(&tcp6_thread, NULL, tcp6_handler, NULL);
|
|
|
|
pthread_create(&udp6_thread, NULL, udp6_handler, NULL);
|
|
|
|
#endif
|
2015-09-16 16:41:39 +04:30
|
|
|
|
2015-10-25 14:10:45 +03:30
|
|
|
/*
|
2015-11-06 23:28:33 +03:30
|
|
|
* Wait for threads to terminate, which normally shouldn't ever
|
|
|
|
* happen
|
2015-10-25 14:10:45 +03:30
|
|
|
*/
|
2015-10-26 18:15:27 +03:30
|
|
|
pthread_join(tcp4_thread, NULL);
|
|
|
|
pthread_join(udp4_thread, NULL);
|
|
|
|
#ifdef PF_INET6
|
|
|
|
pthread_join(tcp6_thread, NULL);
|
|
|
|
pthread_join(udp6_thread, NULL);
|
|
|
|
#endif
|
2015-09-16 11:29:27 +04:30
|
|
|
|
2015-10-25 14:10:45 +03:30
|
|
|
return (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2015-11-06 23:28:33 +03:30
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
2015-11-07 13:49:47 +03:30
|
|
|
printf("usage: fsipd [-h] [-l logfile] [-s] [-p priority] \n");
|
2015-11-06 23:28:33 +03:30
|
|
|
printf("\t-h: this message\n");
|
2015-11-07 13:08:04 +03:30
|
|
|
printf("\t-s: use syslog instead of local log file\n");
|
|
|
|
printf("\t-p: syslog priotiry (default: user.notice)\n");
|
2015-11-07 13:49:47 +03:30
|
|
|
printf("\t-l: specify output log filename (default: fsipd.log)\n");
|
2015-11-07 13:08:04 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
decode(char *name, const CODE * codetab)
|
|
|
|
{
|
|
|
|
const CODE *c;
|
|
|
|
|
|
|
|
if (isdigit(*name))
|
|
|
|
return (atoi(name));
|
|
|
|
|
|
|
|
for (c = codetab; c->c_name; c++)
|
|
|
|
if (!strcasecmp(name, c->c_name))
|
|
|
|
return (c->c_val);
|
|
|
|
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
decodepri(char *s)
|
|
|
|
{
|
|
|
|
char *save;
|
|
|
|
int fac, lev;
|
|
|
|
|
|
|
|
for (save = s; *s && *s != '.'; ++s);
|
|
|
|
if (*s) {
|
|
|
|
*s = '\0';
|
|
|
|
fac = decode(save, facilitynames);
|
|
|
|
if (fac < 0)
|
|
|
|
errx(1, "unknown facility name: %s", save);
|
|
|
|
*s++ = '.';
|
|
|
|
} else {
|
|
|
|
fac = 0;
|
|
|
|
s = save;
|
|
|
|
}
|
|
|
|
lev = decode(s, prioritynames);
|
|
|
|
if (lev < 0)
|
|
|
|
errx(1, "unknown priority name: %s", save);
|
|
|
|
return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
|
2015-11-06 23:28:33 +03:30
|
|
|
}
|
|
|
|
|
2015-09-16 16:41:39 +04:30
|
|
|
int
|
2015-11-06 23:28:33 +03:30
|
|
|
main(int argc, char *argv[])
|
2015-09-16 11:29:27 +04:30
|
|
|
{
|
2015-11-06 23:28:33 +03:30
|
|
|
int opt;
|
|
|
|
|
2015-11-07 13:49:47 +03:30
|
|
|
while ((opt = getopt(argc, argv, "hl:sp:")) != -1) {
|
2015-11-06 23:28:33 +03:30
|
|
|
switch (opt) {
|
|
|
|
case 's':
|
|
|
|
use_syslog = true;
|
|
|
|
break;
|
2015-11-07 13:08:04 +03:30
|
|
|
case 'p':
|
|
|
|
if (use_syslog) {
|
|
|
|
syslog_pri = decodepri(optarg) | LOG_PID;
|
|
|
|
} else {
|
|
|
|
errx(EX_USAGE, "you need to specify \"-s\".");
|
|
|
|
}
|
|
|
|
break;
|
2015-11-07 13:49:47 +03:30
|
|
|
case 'l':
|
|
|
|
logfilename = strdup(optarg);
|
|
|
|
break;
|
2015-11-06 23:28:33 +03:30
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("invalid option: %c\n", opt);
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 16:41:39 +04:30
|
|
|
return (daemon_start());
|
2015-09-16 11:29:27 +04:30
|
|
|
}
|