37
37
#include <netdb.h>
38
38
#include <resolv.h>
39
39
#include <signal.h>
40
+ #include <stdarg.h>
40
41
#include <stdio.h>
41
42
#include <stdlib.h>
42
43
#include <string.h>
43
44
#include <sys/socket.h>
45
+ #include <syslog.h>
44
46
#include <unistd.h>
45
47
#include <wait.h>
46
48
#ifdef USE_SYSTEMD
@@ -74,11 +76,13 @@ void forward_data(int source_sock, int destination_sock);
74
76
void forward_data_ext (int source_sock , int destination_sock , char * cmd );
75
77
int create_connection ();
76
78
int parse_options (int argc , char * argv []);
79
+ void plog (int priority , const char * format , ...);
77
80
78
81
int server_sock , client_sock , remote_sock , remote_port = 0 ;
79
82
int connections_processed = 0 ;
80
83
char * remote_host , * cmd_in , * cmd_out ;
81
84
bool foreground = FALSE;
85
+ bool use_syslog = FALSE;
82
86
83
87
/* Program start */
84
88
int main (int argc , char * argv []) {
@@ -88,12 +92,15 @@ int main(int argc, char *argv[]) {
88
92
local_port = parse_options (argc , argv );
89
93
90
94
if (local_port < 0 ) {
91
- 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 ]);
95
+ 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 ]);
92
96
return local_port ;
93
97
}
94
98
99
+ if (use_syslog )
100
+ openlog ("proxy" , LOG_PID , LOG_DAEMON );
101
+
95
102
if ((server_sock = create_socket (local_port )) < 0 ) { // start server
96
- perror ( "Cannot run server" );
103
+ plog ( LOG_CRIT , "Cannot run server: %m " );
97
104
return server_sock ;
98
105
}
99
106
@@ -108,21 +115,24 @@ int main(int argc, char *argv[]) {
108
115
server_loop ();
109
116
break ;
110
117
case -1 : // error
111
- perror ( "Cannot daemonize" );
118
+ plog ( LOG_CRIT , "Cannot daemonize: %m " );
112
119
return pid ;
113
120
default : // parent
114
121
close (server_sock );
115
122
}
116
123
}
117
124
125
+ if (use_syslog )
126
+ closelog ();
127
+
118
128
return EXIT_SUCCESS ;
119
129
}
120
130
121
131
/* Parse command line options */
122
132
int parse_options (int argc , char * argv []) {
123
133
int c , local_port = 0 ;
124
134
125
- while ((c = getopt (argc , argv , "l:h:p:i:o:f " )) != -1 ) {
135
+ while ((c = getopt (argc , argv , "l:h:p:i:o:fs " )) != -1 ) {
126
136
switch (c ) {
127
137
case 'l' :
128
138
local_port = atoi (optarg );
@@ -141,6 +151,10 @@ int parse_options(int argc, char *argv[]) {
141
151
break ;
142
152
case 'f' :
143
153
foreground = TRUE;
154
+ break ;
155
+ case 's' :
156
+ use_syslog = TRUE;
157
+ break ;
144
158
}
145
159
}
146
160
@@ -180,6 +194,24 @@ int create_socket(int port) {
180
194
return server_sock ;
181
195
}
182
196
197
+ /* Send log message to stderr or syslog */
198
+ void plog (int priority , const char * format , ...)
199
+ {
200
+ va_list ap ;
201
+
202
+ va_start (ap , format );
203
+
204
+ if (use_syslog )
205
+ vsyslog (priority , format , ap );
206
+ else {
207
+ vfprintf (stderr , format , ap );
208
+ fprintf (stderr , "\n" );
209
+ }
210
+
211
+ va_end (ap );
212
+ }
213
+
214
+ /* Update systemd status with connection count */
183
215
void update_connection_count ()
184
216
{
185
217
#ifdef USE_SYSTEMD
@@ -226,7 +258,7 @@ void server_loop() {
226
258
void handle_client (int client_sock , struct sockaddr_in client_addr )
227
259
{
228
260
if ((remote_sock = create_connection ()) < 0 ) {
229
- perror ( "Cannot connect to host" );
261
+ plog ( LOG_ERR , "Cannot connect to host: %m " );
230
262
goto cleanup ;
231
263
}
232
264
@@ -261,14 +293,14 @@ void forward_data(int source_sock, int destination_sock) {
261
293
int buf_pipe [2 ];
262
294
263
295
if (pipe (buf_pipe ) == -1 ) {
264
- perror ( "pipe" );
265
- exit (EXIT_FAILURE );
296
+ plog ( LOG_ERR , "pipe: %m " );
297
+ exit (CREATE_PIPE_ERROR );
266
298
}
267
299
268
300
while ((n = splice (source_sock , NULL , buf_pipe [WRITE ], NULL , SSIZE_MAX , SPLICE_F_NONBLOCK |SPLICE_F_MOVE )) > 0 ) {
269
301
if (splice (buf_pipe [READ ], NULL , destination_sock , NULL , SSIZE_MAX , SPLICE_F_MOVE ) < 0 ) {
270
- perror ( "write" );
271
- exit (EXIT_FAILURE );
302
+ plog ( LOG_ERR , "write: %m " );
303
+ exit (BROKEN_PIPE_ERROR );
272
304
}
273
305
}
274
306
#else
@@ -279,8 +311,10 @@ void forward_data(int source_sock, int destination_sock) {
279
311
}
280
312
#endif
281
313
282
- if (n < 0 )
283
- perror ("read" );
314
+ if (n < 0 ) {
315
+ plog (LOG_ERR , "read: %m" );
316
+ exit (BROKEN_PIPE_ERROR );
317
+ }
284
318
285
319
#ifdef USE_SPLICE
286
320
close (buf_pipe [0 ]);
@@ -300,7 +334,7 @@ void forward_data_ext(int source_sock, int destination_sock, char *cmd) {
300
334
int n , i , pipe_in [2 ], pipe_out [2 ];
301
335
302
336
if (pipe (pipe_in ) < 0 || pipe (pipe_out ) < 0 ) { // create command input and output pipes
303
- perror ( "Cannot create pipe" );
337
+ plog ( LOG_CRIT , "Cannot create pipe: %m " );
304
338
exit (CREATE_PIPE_ERROR );
305
339
}
306
340
@@ -317,7 +351,7 @@ void forward_data_ext(int source_sock, int destination_sock, char *cmd) {
317
351
318
352
while ((n = recv (source_sock , buffer , BUF_SIZE , 0 )) > 0 ) { // read data from input socket
319
353
if (write (pipe_in [WRITE ], buffer , n ) < 0 ) { // write data to input pipe of external command
320
- perror ( "Cannot write to pipe" );
354
+ plog ( LOG_ERR , "Cannot write to pipe: %m " );
321
355
exit (BROKEN_PIPE_ERROR );
322
356
}
323
357
if ((i = read (pipe_out [READ ], buffer , BUF_SIZE )) > 0 ) { // read command output
0 commit comments