Commit ecd4b1a4 authored by Krzysztof Kliś's avatar Krzysztof Kliś Committed by GitHub

Merge pull request #5 from ticpu/syslog

Add syslog support.
parents 84a36ddd 81286f92
CFLAGS=-O2 -std=c99 -s -Wall -DUSE_SPLICE
LDFLAGS=
all: all:
gcc -O2 -s -o proxy proxy.c gcc $(CFLAGS) -o proxy proxy.c $(LDFLAGS)
tomato: tomato:
mipsel-uclibc-gcc -O2 -s -o proxy proxy.c mipsel-uclibc-gcc $(CFLAGS) -o proxy proxy.c $(LDFLAGS)
openwrt: openwrt:
mipsel-linux-uclibc-gcc -O2 -s -o proxy proxy.c mipsel-linux-uclibc-gcc $(CFLAGS) -o proxy proxy.c $(LDFLAGS)
backfire: backfire:
mipsel-openwrt-linux-uclibc-gcc -O2 -s -o proxy proxy.c mipsel-openwrt-linux-uclibc-gcc $(CFLAGS) -o proxy proxy.c $(LDFLAGS)
clean: clean:
rm -rf proxy proxy.exe rm -f proxy proxy.exe
...@@ -37,10 +37,12 @@ ...@@ -37,10 +37,12 @@
#include <netdb.h> #include <netdb.h>
#include <resolv.h> #include <resolv.h>
#include <signal.h> #include <signal.h>
#include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <syslog.h>
#include <unistd.h> #include <unistd.h>
#include <wait.h> #include <wait.h>
#ifdef USE_SYSTEMD #ifdef USE_SYSTEMD
...@@ -74,11 +76,13 @@ void forward_data(int source_sock, int destination_sock); ...@@ -74,11 +76,13 @@ void forward_data(int source_sock, int destination_sock);
void forward_data_ext(int source_sock, int destination_sock, char *cmd); void forward_data_ext(int source_sock, int destination_sock, char *cmd);
int create_connection(); int create_connection();
int parse_options(int argc, char *argv[]); int parse_options(int argc, char *argv[]);
void plog(int priority, const char *format, ...);
int server_sock, client_sock, remote_sock, remote_port = 0; int server_sock, client_sock, remote_sock, remote_port = 0;
int connections_processed = 0; int connections_processed = 0;
char *remote_host, *cmd_in, *cmd_out; char *remote_host, *cmd_in, *cmd_out;
bool foreground = FALSE; bool foreground = FALSE;
bool use_syslog = FALSE;
/* Program start */ /* Program start */
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
...@@ -88,12 +92,15 @@ int main(int argc, char *argv[]) { ...@@ -88,12 +92,15 @@ int main(int argc, char *argv[]) {
local_port = parse_options(argc, argv); local_port = parse_options(argc, argv);
if (local_port < 0) { if (local_port < 0) {
printf("Syntax: %s -l local_port -h remote_host -p remote_port [-i \"input parser\"] [-o \"output parser\"] [-f (stay in foreground)]\n", argv[0]); printf("Syntax: %s -l local_port -h remote_host -p remote_port [-i \"input parser\"] [-o \"output parser\"] [-f (stay in foreground)] [-s (use syslog)]\n", argv[0]);
return local_port; return local_port;
} }
if (use_syslog)
openlog("proxy", LOG_PID, LOG_DAEMON);
if ((server_sock = create_socket(local_port)) < 0) { // start server if ((server_sock = create_socket(local_port)) < 0) { // start server
perror("Cannot run server"); plog(LOG_CRIT, "Cannot run server: %m");
return server_sock; return server_sock;
} }
...@@ -108,13 +115,16 @@ int main(int argc, char *argv[]) { ...@@ -108,13 +115,16 @@ int main(int argc, char *argv[]) {
server_loop(); server_loop();
break; break;
case -1: // error case -1: // error
perror("Cannot daemonize"); plog(LOG_CRIT, "Cannot daemonize: %m");
return pid; return pid;
default: // parent default: // parent
close(server_sock); close(server_sock);
} }
} }
if (use_syslog)
closelog();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
...@@ -122,7 +132,7 @@ int main(int argc, char *argv[]) { ...@@ -122,7 +132,7 @@ int main(int argc, char *argv[]) {
int parse_options(int argc, char *argv[]) { int parse_options(int argc, char *argv[]) {
int c, local_port = 0; int c, local_port = 0;
while ((c = getopt(argc, argv, "l:h:p:i:o:f")) != -1) { while ((c = getopt(argc, argv, "l:h:p:i:o:fs")) != -1) {
switch(c) { switch(c) {
case 'l': case 'l':
local_port = atoi(optarg); local_port = atoi(optarg);
...@@ -141,6 +151,10 @@ int parse_options(int argc, char *argv[]) { ...@@ -141,6 +151,10 @@ int parse_options(int argc, char *argv[]) {
break; break;
case 'f': case 'f':
foreground = TRUE; foreground = TRUE;
break;
case 's':
use_syslog = TRUE;
break;
} }
} }
...@@ -180,6 +194,24 @@ int create_socket(int port) { ...@@ -180,6 +194,24 @@ int create_socket(int port) {
return server_sock; return server_sock;
} }
/* Send log message to stderr or syslog */
void plog(int priority, const char *format, ...)
{
va_list ap;
va_start(ap, format);
if (use_syslog)
vsyslog(priority, format, ap);
else {
vfprintf(stderr, format, ap);
fprintf(stderr, "\n");
}
va_end(ap);
}
/* Update systemd status with connection count */
void update_connection_count() void update_connection_count()
{ {
#ifdef USE_SYSTEMD #ifdef USE_SYSTEMD
...@@ -226,7 +258,7 @@ void server_loop() { ...@@ -226,7 +258,7 @@ void server_loop() {
void handle_client(int client_sock, struct sockaddr_in client_addr) void handle_client(int client_sock, struct sockaddr_in client_addr)
{ {
if ((remote_sock = create_connection()) < 0) { if ((remote_sock = create_connection()) < 0) {
perror("Cannot connect to host"); plog(LOG_ERR, "Cannot connect to host: %m");
goto cleanup; goto cleanup;
} }
...@@ -261,14 +293,14 @@ void forward_data(int source_sock, int destination_sock) { ...@@ -261,14 +293,14 @@ void forward_data(int source_sock, int destination_sock) {
int buf_pipe[2]; int buf_pipe[2];
if (pipe(buf_pipe) == -1) { if (pipe(buf_pipe) == -1) {
perror("pipe"); plog(LOG_ERR, "pipe: %m");
exit(EXIT_FAILURE); exit(CREATE_PIPE_ERROR);
} }
while ((n = splice(source_sock, NULL, buf_pipe[WRITE], NULL, SSIZE_MAX, SPLICE_F_NONBLOCK|SPLICE_F_MOVE)) > 0) { while ((n = splice(source_sock, NULL, buf_pipe[WRITE], NULL, SSIZE_MAX, SPLICE_F_NONBLOCK|SPLICE_F_MOVE)) > 0) {
if (splice(buf_pipe[READ], NULL, destination_sock, NULL, SSIZE_MAX, SPLICE_F_MOVE) < 0) { if (splice(buf_pipe[READ], NULL, destination_sock, NULL, SSIZE_MAX, SPLICE_F_MOVE) < 0) {
perror("write"); plog(LOG_ERR, "write: %m");
exit(EXIT_FAILURE); exit(BROKEN_PIPE_ERROR);
} }
} }
#else #else
...@@ -279,8 +311,10 @@ void forward_data(int source_sock, int destination_sock) { ...@@ -279,8 +311,10 @@ void forward_data(int source_sock, int destination_sock) {
} }
#endif #endif
if (n < 0) if (n < 0) {
perror("read"); plog(LOG_ERR, "read: %m");
exit(BROKEN_PIPE_ERROR);
}
#ifdef USE_SPLICE #ifdef USE_SPLICE
close(buf_pipe[0]); close(buf_pipe[0]);
...@@ -300,7 +334,7 @@ void forward_data_ext(int source_sock, int destination_sock, char *cmd) { ...@@ -300,7 +334,7 @@ void forward_data_ext(int source_sock, int destination_sock, char *cmd) {
int n, i, pipe_in[2], pipe_out[2]; int n, i, pipe_in[2], pipe_out[2];
if (pipe(pipe_in) < 0 || pipe(pipe_out) < 0) { // create command input and output pipes if (pipe(pipe_in) < 0 || pipe(pipe_out) < 0) { // create command input and output pipes
perror("Cannot create pipe"); plog(LOG_CRIT, "Cannot create pipe: %m");
exit(CREATE_PIPE_ERROR); exit(CREATE_PIPE_ERROR);
} }
...@@ -317,7 +351,7 @@ void forward_data_ext(int source_sock, int destination_sock, char *cmd) { ...@@ -317,7 +351,7 @@ void forward_data_ext(int source_sock, int destination_sock, char *cmd) {
while ((n = recv(source_sock, buffer, BUF_SIZE, 0)) > 0) { // read data from input socket while ((n = recv(source_sock, buffer, BUF_SIZE, 0)) > 0) { // read data from input socket
if (write(pipe_in[WRITE], buffer, n) < 0) { // write data to input pipe of external command if (write(pipe_in[WRITE], buffer, n) < 0) { // write data to input pipe of external command
perror("Cannot write to pipe"); plog(LOG_ERR, "Cannot write to pipe: %m");
exit(BROKEN_PIPE_ERROR); exit(BROKEN_PIPE_ERROR);
} }
if ((i = read(pipe_out[READ], buffer, BUF_SIZE)) > 0) { // read command output if ((i = read(pipe_out[READ], buffer, BUF_SIZE)) > 0) { // read command output
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment