--- ptunnel.c.orig Mon Jan 12 11:14:04 2009 +++ ptunnel.c Mon Jan 12 12:29:34 2009 @@ -88,11 +88,19 @@ static char * print_last_windows_error() { DWORD last_error = GetLastError(); memset(errorstr, 0, sizeof(errorstr)); FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, NULL, last_error, 0, errorstr, sizeof(errorstr), NULL); snprintf(errorstr, sizeof(errorstr), "%s (%d)", errorstr, last_error); return errorstr; } #define strerror(x) print_last_windows_error() #endif /* WIN32 */ + +#ifndef WIN32 +#include +#endif /* !WIN32 */ + // Lots of globals pthread_mutex_t chain_lock, // Lock protecting the chain of connections num_threads_lock; // Lock protecting the num_threads variable bool unprivileged = false, // True if user wants to run without root +#ifndef WIN32 + use_syslog = false, // True if user wants to log to syslog +#endif /* !WIN32 */ pcap = false; // True if user wants packet capturing FILE *log_file = 0; // Usually stdout, but can be altered by the user @@ -161,6 +169,10 @@ opt = kOpt_set_pcap_device; else if (strcmp(argv[i], "-f") == 0) opt = kOpt_set_log_file; +#ifndef WIN32 + else if (strcmp(argv[i], "-s") == 0) + use_syslog = !use_syslog; +#endif /* !WIN32 */ else { switch (opt) { case kOpt_set_proxy_addr: @@ -220,6 +232,8 @@ #ifndef WIN32 signal(SIGPIPE, SIG_IGN); + if (use_syslog) + openlog("ptunnel", LOG_PID, LOG_USER); #endif /* !WIN32 */ #ifdef WIN32 @@ -287,6 +301,9 @@ printf(" than running in privileged mode.\n"); printf(" -v: Verbosity level (-1 to 4, where -1 is no output, and 4 is all output)\n"); printf(" -c: Enable libpcap on the given device.\n"); +#ifndef WIN32 + printf(" -s: Output debug to syslog instead of standard out.\n"); +#endif /* !WIN32 */ printf(" -f: Specify a file to log to, rather than printing to standard out.\n\n"); printf("Starting the proxy (needs to run as root):\n"); printf(" [root #] %s\n", exec_name); @@ -1229,9 +1246,21 @@ "[vbs]: ", "[dbg]: ", "[xfr]: " }; - +#ifndef WIN32 + int syslog_levels[] = {LOG_ERR, LOG_NOTICE, LOG_NOTICE, LOG_INFO, LOG_DEBUG, LOG_DEBUG}; +#endif /* !WIN32 */ + if (level <= log_level) { va_start(args, fmt); +#ifndef WIN32 + if (use_syslog) { + char log[255]; + int header_len; + header_len = snprintf(log,sizeof(log),"%s",header[level]); + vsnprintf(log+header_len,sizeof(log)-header_len,fmt,args); + syslog(syslog_levels[level], "%s", log); + } else +#endif /* !WIN32 */ fprintf(log_file, "%s", header[level]), vfprintf(log_file, fmt, args); va_end(args); if (log_file != stdout)