Mailing List Archive

[RFC PATCH] log to stderr when --no-detach
Hi,

I'm currently using the VPN connection for short periods
of time only, and found it is more convenient to add
No Detach
Debug 1
to my vpnc config file, so I can simply terminate the
connection with ^C. However, when doing so, vpnc logs
the following to syslog:

Sep 30 13:38:07 zzz vpnc[15687]: select: Interrupted system call
Sep 30 13:38:07 zzz vpnc[15687]: terminated by signal: 2

The patch below makes vpnc to print all messages to
stderr instead of syslog when running with No Detach.
Two potential issues are 1. vsyslog() requires
_BSD_SOURCE (i.e. it works with glibc but I'm not sure
e.g. about Mac OSX), and 2. __attribute__ probably only
works with gcc (but there are already other uses of
__attribute__ in vpnc). Both can easily be fixed
(by using vsnprintf() + syslog() / #ifdef __GCC__).

Let me know what you think.


Johannes



Index: tunip.c
===================================================================
--- tunip.c (revision 464)
+++ tunip.c (working copy)
@@ -169,15 +169,15 @@ static int encap_rawip_recv(struct sa_bl

r = recvfrom(s->esp_fd, buf, bufsize, 0, (struct sockaddr *)&from, &fromlen);
if (r == -1) {
- syslog(LOG_ERR, "recvfrom: %m");
+ logmsg(LOG_ERR, "recvfrom: %m");
return -1;
}
if (from.sin_addr.s_addr != s->dst.s_addr) {
- syslog(LOG_ALERT, "packet from unknown host %s", inet_ntoa(from.sin_addr));
+ logmsg(LOG_ALERT, "packet from unknown host %s", inet_ntoa(from.sin_addr));
return -1;
}
if (r < (p->ip_hl << 2) + s->ipsec.em->fixed_header_size) {
- syslog(LOG_ALERT, "packet too short. got %zd, expected %d", r, (p->ip_hl << 2) + s->ipsec.em->fixed_header_size);
+ logmsg(LOG_ALERT, "packet too short. got %zd, expected %d", r, (p->ip_hl << 2) + s->ipsec.em->fixed_header_size);
return -1;
}

@@ -203,7 +203,7 @@ static int encap_udp_recv(struct sa_bloc

r = recv(s->esp_fd, buf, bufsize, 0);
if (r == -1) {
- syslog(LOG_ERR, "recvfrom: %m");
+ logmsg(LOG_ERR, "recvfrom: %m");
return -1;
}
if (s->ipsec.natt_active_mode == NATT_ACTIVE_DRAFT_OLD && r > 8) {
@@ -216,7 +216,7 @@ static int encap_udp_recv(struct sa_bloc
return -1;
}
if (r < s->ipsec.em->fixed_header_size) {
- syslog(LOG_ALERT, "packet too short from %s. got %zd, expected %d",
+ logmsg(LOG_ALERT, "packet too short from %s. got %zd, expected %d",
inet_ntoa(s->dst), r, s->ipsec.em->fixed_header_size);
return -1;
}
@@ -273,7 +273,7 @@ static int tun_send_ip(struct sa_block *

sent = tun_write(s->tun_fd, start, len);
if (sent != len)
- syslog(LOG_ERR, "truncated in: %d -> %d\n", len, sent);
+ logmsg(LOG_ERR, "truncated in: %d -> %d\n", len, sent);
hex_dump("Tx pkt", start, len, NULL);
return 1;
}
@@ -436,11 +436,11 @@ static void encap_esp_send_peer(struct s
dstaddr.sin_port = 0;
sent = sendto(s->esp_fd, s->ipsec.tx.buf, s->ipsec.tx.buflen, 0, (struct sockaddr *)&dstaddr, sizeof(struct sockaddr_in));
if (sent == -1) {
- syslog(LOG_ERR, "esp sendto: %m");
+ logmsg(LOG_ERR, "esp sendto: %m");
return;
}
if (sent != s->ipsec.tx.buflen)
- syslog(LOG_ALERT, "esp truncated out (%lld out of %d)", (long long)sent, s->ipsec.tx.buflen);
+ logmsg(LOG_ALERT, "esp truncated out (%lld out of %d)", (long long)sent, s->ipsec.tx.buflen);
}

/*
@@ -475,11 +475,11 @@ static void encap_udp_send_peer(struct s

sent = send(s->esp_fd, s->ipsec.tx.buf, s->ipsec.tx.buflen, 0);
if (sent == -1) {
- syslog(LOG_ERR, "udp sendto: %m");
+ logmsg(LOG_ERR, "udp sendto: %m");
return;
}
if (sent != s->ipsec.tx.buflen)
- syslog(LOG_ALERT, "udp truncated out (%lld out of %d)",
+ logmsg(LOG_ALERT, "udp truncated out (%lld out of %d)",
(long long)sent, s->ipsec.tx.buflen);
}

@@ -499,7 +499,7 @@ static int encap_esp_recv_peer(struct sa
len = s->ipsec.rx.buflen - s->ipsec.rx.bufpayload - s->ipsec.em->fixed_header_size - s->ipsec.rx.var_header_size;

if (len < 0) {
- syslog(LOG_ALERT, "Packet too short");
+ logmsg(LOG_ALERT, "Packet too short");
return -1;
}

@@ -515,14 +515,14 @@ static int encap_esp_recv_peer(struct sa
0,
s->ipsec.rx.key_md,
s->ipsec.md_len) != 0) {
- syslog(LOG_ALERT, "HMAC mismatch in ESP mode");
+ logmsg(LOG_ALERT, "HMAC mismatch in ESP mode");
return -1;
}
}

blksz = s->ipsec.blk_len;
if ((len % blksz) != 0) {
- syslog(LOG_ALERT,
+ logmsg(LOG_ALERT,
"payload len %d not a multiple of algorithm block size %lu", len,
(unsigned long)blksz);
return -1;
@@ -551,11 +551,11 @@ static int encap_esp_recv_peer(struct sa
+ s->ipsec.em->fixed_header_size + s->ipsec.rx.var_header_size + len - 1];

if (padlen + 2 > len) {
- syslog(LOG_ALERT, "Inconsistent padlen");
+ logmsg(LOG_ALERT, "Inconsistent padlen");
return -1;
}
if (next_header != IPPROTO_IPIP) {
- syslog(LOG_ALERT, "Inconsistent next_header %d", next_header);
+ logmsg(LOG_ALERT, "Inconsistent next_header %d", next_header);
return -1;
}
DEBUG(3, printf("pad len: %d, next_header: %d\n", padlen, next_header));
@@ -568,7 +568,7 @@ static int encap_esp_recv_peer(struct sa
+ s->ipsec.em->fixed_header_size + s->ipsec.rx.var_header_size + len;
for (i = 1; i <= padlen; i++) {
if (*pad != i) {
- syslog(LOG_ALERT, "Bad padding");
+ logmsg(LOG_ALERT, "Bad padding");
return -1;
}
pad++;
@@ -693,7 +693,7 @@ static void process_tun(struct sa_block
}

if (pack == -1) {
- syslog(LOG_ERR, "read: %m");
+ logmsg(LOG_ERR, "read: %m");
return;
}

@@ -701,7 +701,7 @@ static void process_tun(struct sa_block
* 12: Offset of ip source address in ip header,
* 4: Length of IP address */
if (!memcmp(global_buffer_rx + MAX_HEADER + 12, &s->dst.s_addr, 4)) {
- syslog(LOG_ALERT, "routing loop to %s",
+ logmsg(LOG_ALERT, "routing loop to %s",
inet_ntoa(s->dst));
return;
}
@@ -732,7 +732,7 @@ static void process_socket(struct sa_blo
s->ipsec.rx.buflen - s->ipsec.rx.bufpayload - 4);
return;
} else if (eh->spi != s->ipsec.rx.spi) {
- syslog(LOG_NOTICE, "unknown spi %#08x from peer", ntohl(eh->spi));
+ logmsg(LOG_NOTICE, "unknown spi %#08x from peer", ntohl(eh->spi));
return;
}

@@ -741,7 +741,7 @@ static void process_socket(struct sa_blo
return;

if (encap_any_decap(s) == 0) {
- syslog(LOG_DEBUG, "received update probe from peer");
+ logmsg(LOG_DEBUG, "received update probe from peer");
} else {
/* Send the decapsulated packet to the tunnel interface */
s->ipsec.life.rx += s->ipsec.rx.buflen;
@@ -813,7 +813,7 @@ static void vpnc_main_loop(struct sa_blo

#if defined(__CYGWIN__)
if (pthread_create(&tid, NULL, tun_thread, s)) {
- syslog(LOG_ERR, "Cannot create tun thread!\n");
+ logmsg(LOG_ERR, "Cannot create tun thread!\n");
return;
}
#endif
@@ -862,7 +862,7 @@ static void vpnc_main_loop(struct sa_blo
}
/* send nat keepalive packet */
if (send(s->esp_fd, keepalive, keepalive_size, 0) == -1) {
- syslog(LOG_ERR, "keepalive sendto: %m");
+ logmsg(LOG_ERR, "keepalive sendto: %m");
}
}
if (s->ike.do_dpd) {
@@ -888,7 +888,7 @@ static void vpnc_main_loop(struct sa_blo
s->ipsec.life.kbytes));
} while ((presult == 0 || (presult == -1 && errno == EINTR)) && !do_kill);
if (presult == -1) {
- syslog(LOG_ERR, "select: %m");
+ logmsg(LOG_ERR, "select: %m");
continue;
}

@@ -948,13 +948,13 @@ static void vpnc_main_loop(struct sa_blo

switch (do_kill) {
case -2:
- syslog(LOG_NOTICE, "connection terminated by dead peer detection");
+ logmsg(LOG_NOTICE, "connection terminated by dead peer detection");
break;
case -1:
- syslog(LOG_NOTICE, "connection terminated by peer");
+ logmsg(LOG_NOTICE, "connection terminated by peer");
break;
default:
- syslog(LOG_NOTICE, "terminated by signal: %d", do_kill);
+ logmsg(LOG_NOTICE, "terminated by signal: %d", do_kill);
break;
}
}
@@ -973,7 +973,7 @@ static void write_pidfile(const char *pi

pf = fopen(pidfile, "w");
if (pf == NULL) {
- syslog(LOG_WARNING, "can't open pidfile %s for writing", pidfile);
+ logmsg(LOG_WARNING, "can't open pidfile %s for writing", pidfile);
return;
}

@@ -1055,10 +1055,10 @@ void vpnc_doit(struct sa_block *s)
printf("VPNC started in background (pid: %d)...\n", (int)pid);
exit(0);
}
+ openlog("vpnc", LOG_PID | LOG_PERROR, LOG_DAEMON);
} else {
printf("VPNC started in foreground...\n");
}
- openlog("vpnc", LOG_PID | LOG_PERROR, LOG_DAEMON);
write_pidfile(pidfile);

vpnc_main_loop(s);
Index: config.c
===================================================================
--- config.c (revision 464)
+++ config.c (working copy)
@@ -19,13 +19,16 @@
*/

#define _GNU_SOURCE
+#define _BSD_SOURCE /* vsyslog */

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
+#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
+#include <syslog.h>
#include <sys/utsname.h>

#include <gcrypt.h>
@@ -822,3 +825,16 @@ void do_config(int argc, char **argv)

return;
}
+
+void logmsg(int priority, const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ if (opt_nd) {
+ vfprintf(stderr, format, ap);
+ fprintf(stderr, "\n");
+ } else
+ vsyslog(priority, format, ap);
+ va_end(ap);
+}
Index: sysdep.c
===================================================================
--- sysdep.c (revision 464)
+++ sysdep.c (working copy)
@@ -110,37 +110,37 @@ int tun_open(char *dev, enum if_mode_enu
}

if ((ip_fd = open("/dev/ip", O_RDWR, 0)) < 0) {
- syslog(LOG_ERR, "Can't open /dev/ip");
+ logmsg(LOG_ERR, "Can't open /dev/ip");
return -1;
}

if ((tun_fd = open(((mode == IF_MODE_TUN) ? "/dev/tun" : "/dev/tap"), O_RDWR, 0)) < 0) {
- syslog(LOG_ERR, "Can't open /dev/tun");
+ logmsg(LOG_ERR, "Can't open /dev/tun");
return -1;
}

/* Assign a new PPA and get its unit number. */
if ((ppa = ioctl(tun_fd, TUNNEWPPA, ppa)) < 0) {
- syslog(LOG_ERR, "Can't assign new interface");
+ logmsg(LOG_ERR, "Can't assign new interface");
return -1;
}

if ((if_fd = open(((mode == IF_MODE_TUN) ? "/dev/tun" : "/dev/tap"), O_RDWR, 0)) < 0) {
- syslog(LOG_ERR, "Can't open /dev/tun (2)");
+ logmsg(LOG_ERR, "Can't open /dev/tun (2)");
return -1;
}
if (ioctl(if_fd, I_PUSH, "ip") < 0) {
- syslog(LOG_ERR, "Can't push IP module");
+ logmsg(LOG_ERR, "Can't push IP module");
return -1;
}

/* Assign ppa according to the unit number returned by tun device */
if (ioctl(if_fd, IF_UNITSEL, (char *)&ppa) < 0 && errno != EEXIST) {
- syslog(LOG_ERR, "Can't set PPA %d", ppa);
+ logmsg(LOG_ERR, "Can't set PPA %d", ppa);
return -1;
}
if ((muxid = ioctl(ip_fd, I_PLINK, if_fd)) < 0) {
- syslog(LOG_ERR, "Can't link TUN device to IP");
+ logmsg(LOG_ERR, "Can't link TUN device to IP");
return -1;
}
close(if_fd);
@@ -153,7 +153,7 @@ int tun_open(char *dev, enum if_mode_enu

if (ioctl(ip_fd, SIOCSIFMUXID, &ifr) < 0) {
ioctl(ip_fd, I_PUNLINK, muxid);
- syslog(LOG_ERR, "Can't set multiplexor id");
+ logmsg(LOG_ERR, "Can't set multiplexor id");
return -1;
}

@@ -493,12 +493,12 @@ int tun_close(int fd, char *dev)
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, dev);
if (ioctl(ip_fd, SIOCGIFFLAGS, &ifr) < 0) {
- syslog(LOG_ERR, "Can't get iface flags");
+ logmsg(LOG_ERR, "Can't get iface flags");
return 0;
}

if (ioctl(ip_fd, I_PUNLINK, muxid) < 0) {
- syslog(LOG_ERR, "Can't unlink interface");
+ logmsg(LOG_ERR, "Can't unlink interface");
return 0;
}

Index: config.h
===================================================================
--- config.h (revision 464)
+++ config.h (working copy)
@@ -131,4 +131,7 @@ extern uint16_t opt_udpencapport;
extern void hex_dump(const char *str, const void *data, ssize_t len, const struct debug_strings *decode);
extern void do_config(int argc, char **argv);

+extern void logmsg(int priority, const char *format, ...)
+ __attribute__ ((__format__ (__printf__, 2, 3)));
+
#endif
_______________________________________________
vpnc-devel mailing list
vpnc-devel@unix-ag.uni-kl.de
https://lists.unix-ag.uni-kl.de/mailman/listinfo/vpnc-devel
http://www.unix-ag.uni-kl.de/~massar/vpnc/
Re: [RFC PATCH] log to stderr when --no-detach [ In reply to ]
Hi Johannes,

I also use same options and agree on your comments.

Your patch is very similar with what I sent already three years ago
(time flies ...)
http://lists.unix-ag.uni-kl.de/pipermail/vpnc-devel/2008-August/002564.html
I don't remember why, but later on I did not posted it again.
I still have and use it in my personal build.

I'm currently quite busy and unable to review and dedicate time to this topic.
I hope in a couple of weeks to be able to review and apply the patch
to both trunk and Nortel branch.

Best Regards,
Antonio


On Fri, Sep 30, 2011 at 9:44 PM, Johannes Stezenbach <js@sig21.net> wrote:
> Hi,
>
> I'm currently using the VPN connection for short periods
> of time only, and found it is more convenient to add
>  No Detach
>  Debug 1
> to my vpnc config file, so I can simply terminate the
> connection with ^C.  However, when doing so, vpnc logs
> the following to syslog:
>
> Sep 30 13:38:07 zzz vpnc[15687]: select: Interrupted system call
> Sep 30 13:38:07 zzz vpnc[15687]: terminated by signal: 2
>
> The patch below makes vpnc to print all messages to
> stderr instead of syslog when running with No Detach.
> Two potential issues are 1. vsyslog() requires
> _BSD_SOURCE (i.e. it works with glibc but I'm not sure
> e.g. about Mac OSX), and 2. __attribute__ probably only
> works with gcc (but there are already other uses of
> __attribute__ in vpnc).  Both can easily be fixed
> (by using vsnprintf() + syslog() / #ifdef __GCC__).
>
> Let me know what you think.
>
>
> Johannes
_______________________________________________
vpnc-devel mailing list
vpnc-devel@unix-ag.uni-kl.de
https://lists.unix-ag.uni-kl.de/mailman/listinfo/vpnc-devel
http://www.unix-ag.uni-kl.de/~massar/vpnc/