Code style fixes

This commit is contained in:
adamdunkels 2008-02-24 21:13:36 +00:00
parent 307fc41646
commit 3106840c56

View file

@ -1,18 +1,18 @@
/* /*
* Copyright (c) 2001, Adam Dunkels. * Copyright (c) 2001, Adam Dunkels.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
* 1. Redistributions of source code must retain the above copyright * 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote * 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior * products derived from this software without specific prior
* written permission. * written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
@ -24,11 +24,11 @@
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* This file is part of the uIP TCP/IP stack. * This file is part of the uIP TCP/IP stack.
* *
* $Id: tunslip.c,v 1.14 2007/05/21 15:22:56 bg- Exp $ * $Id: tunslip.c,v 1.15 2008/02/24 21:13:36 adamdunkels Exp $
* *
*/ */
@ -139,7 +139,7 @@ struct dhcp_light_msg {
#define DHCPDECLINE 4 #define DHCPDECLINE 4
#define DHCPACK 5 #define DHCPACK 5
#define DHCPNAK 6 #define DHCPNAK 6
#define DHCPRELEASE 7 #define DHCPRELEASE 7
#define BOOTP_BROADCAST 0x8000 #define BOOTP_BROADCAST 0x8000
@ -416,11 +416,13 @@ int
is_sensible_string(const unsigned char *s, int len) is_sensible_string(const unsigned char *s, int len)
{ {
int i; int i;
for(i = 1; i < len; i++) for(i = 1; i < len; i++) {
if (s[i] == 0 || s[i] == '\r' || s[i] == '\n' || s[i] == '\t') if(s[i] == 0 || s[i] == '\r' || s[i] == '\n' || s[i] == '\t') {
continue; continue;
else if (s[i] < ' ' || '~' < s[i]) } else if(s[i] < ' ' || '~' < s[i]) {
return 0; return 0;
}
}
return 1; return 1;
} }
@ -475,13 +477,15 @@ serial_to_tun(FILE *inslip, int outfd)
#ifdef linux #ifdef linux
after_fread: after_fread:
#endif #endif
if(ret == -1) err(1, "serial_to_tun: read"); if(ret == -1) {
err(1, "serial_to_tun: read");
}
if(ret == 0) { if(ret == 0) {
clearerr(inslip); clearerr(inslip);
return; return;
fprintf(stderr, "serial_to_tun: EOF\n"); fprintf(stderr, "serial_to_tun: EOF\n");
exit(1); exit(1);
} }
/* fprintf(stderr, ".");*/ /* fprintf(stderr, ".");*/
switch(c) { switch(c) {
case SLIP_END: case SLIP_END:
@ -496,8 +500,9 @@ serial_to_tun(FILE *inslip, int outfd)
static struct in_addr ipa; static struct in_addr ipa;
inbufptr = 0; inbufptr = 0;
if(memcmp(&ipa, &uip.inbuf[4], sizeof(ipa)) == 0) if(memcmp(&ipa, &uip.inbuf[4], sizeof(ipa)) == 0) {
break; break;
}
/* New address. */ /* New address. */
if(ipa.s_addr != 0) { if(ipa.s_addr != 0) {
@ -525,28 +530,31 @@ serial_to_tun(FILE *inslip, int outfd)
/* /*
* If sensible ASCII string, print it as debug info! * If sensible ASCII string, print it as debug info!
*/ */
if (uip.inbuf[0] == DEBUG_LINE_MARKER) if(uip.inbuf[0] == DEBUG_LINE_MARKER) {
fwrite(uip.inbuf + 1, inbufptr - 1, 1, stderr); fwrite(uip.inbuf + 1, inbufptr - 1, 1, stderr);
else if (is_sensible_string(uip.inbuf, inbufptr)) } else if(is_sensible_string(uip.inbuf, inbufptr)) {
fwrite(uip.inbuf, inbufptr, 1, stderr); fwrite(uip.inbuf, inbufptr, 1, stderr);
else } else {
fprintf(stderr, fprintf(stderr,
"serial_to_tun: drop packet len=%d ecode=%d\n", "serial_to_tun: drop packet len=%d ecode=%d\n",
inbufptr, ecode); inbufptr, ecode);
}
inbufptr = 0; inbufptr = 0;
break; break;
} }
PROGRESS("s"); PROGRESS("s");
if (dhsock != -1) { if(dhsock != -1) {
struct ip *ip = (void *)uip.inbuf; struct ip *ip = (void *)uip.inbuf;
if (ip->ip_p == 17 && ip->ip_dst == 0xffffffff /* UDP and broadcast */ if(ip->ip_p == 17 && ip->ip_dst == 0xffffffff /* UDP and broadcast */
&& ip->uh_sport == ntohs(BOOTPC) && ip->uh_dport == ntohs(BOOTPS)){ && ip->uh_sport == ntohs(BOOTPC) && ip->uh_dport == ntohs(BOOTPS)) {
relay_dhcp_to_server(ip, inbufptr); relay_dhcp_to_server(ip, inbufptr);
inbufptr = 0; inbufptr = 0;
} }
} }
if(write(outfd, uip.inbuf, inbufptr) != inbufptr) err(1, "serial_to_tun: write"); if(write(outfd, uip.inbuf, inbufptr) != inbufptr) {
err(1, "serial_to_tun: write");
}
inbufptr = 0; inbufptr = 0;
} }
break; break;
@ -566,7 +574,7 @@ serial_to_tun(FILE *inslip, int outfd)
case SLIP_ESC_ESC: case SLIP_ESC_ESC:
c = SLIP_ESC; c = SLIP_ESC;
break; break;
} }
/* FALLTHROUGH */ /* FALLTHROUGH */
default: default:
uip.inbuf[inbufptr++] = c; uip.inbuf[inbufptr++] = c;
@ -604,14 +612,15 @@ slip_flushbuf(int fd)
n = write(fd, slip_buf + slip_begin, (slip_end - slip_begin)); n = write(fd, slip_buf + slip_begin, (slip_end - slip_begin));
if (n == -1 && errno != EAGAIN) if(n == -1 && errno != EAGAIN) {
err(1, "slip_flushbuf write failed"); err(1, "slip_flushbuf write failed");
else if (n == -1) } else if(n == -1) {
PROGRESS("Q"); /* Outqueueis full! */ PROGRESS("Q"); /* Outqueueis full! */
else { } else {
slip_begin += n; slip_begin += n;
if (slip_begin == slip_end) if(slip_begin == slip_end) {
slip_begin = slip_end = 0; slip_begin = slip_end = 0;
}
} }
} }
@ -631,13 +640,14 @@ write_to_serial(int outfd, void *inbuf, int len)
return; return;
} }
if (iphdr->ip_id == 0 && iphdr->ip_off & IP_DF) { if(iphdr->ip_id == 0 && iphdr->ip_off & IP_DF) {
uint16_t nid = htons(ip_id++); uint16_t nid = htons(ip_id++);
iphdr->ip_id = nid; iphdr->ip_id = nid;
nid = ~nid; /* negate */ nid = ~nid; /* negate */
iphdr->ip_sum += nid; /* add */ iphdr->ip_sum += nid; /* add */
if (iphdr->ip_sum < nid) /* 1-complement overflow? */ if(iphdr->ip_sum < nid) { /* 1-complement overflow? */
iphdr->ip_sum++; iphdr->ip_sum++;
}
ecode = check_ip(inbuf, len); ecode = check_ip(inbuf, len);
if(ecode < 0) { if(ecode < 0) {
fprintf(stderr, "tun_to_serial: drop packet %d\n", ecode); fprintf(stderr, "tun_to_serial: drop packet %d\n", ecode);
@ -759,12 +769,12 @@ tun_alloc(char *dev)
memset(&ifr, 0, sizeof(ifr)); memset(&ifr, 0, sizeof(ifr));
/* Flags: IFF_TUN - TUN device (no Ethernet headers) /* Flags: IFF_TUN - TUN device (no Ethernet headers)
* IFF_TAP - TAP device * IFF_TAP - TAP device
* *
* IFF_NO_PI - Do not provide packet information * IFF_NO_PI - Do not provide packet information
*/ */
ifr.ifr_flags = IFF_TUN | IFF_NO_PI; ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
if(*dev != 0) if(*dev != 0)
strncpy(ifr.ifr_name, dev, IFNAMSIZ); strncpy(ifr.ifr_name, dev, IFNAMSIZ);
@ -836,14 +846,16 @@ ifconf(const char *tundev, const char *ipaddr, const char *netmask)
#ifdef linux #ifdef linux
ssystem("ifconfig %s inet `hostname` up", tundev); ssystem("ifconfig %s inet `hostname` up", tundev);
if (strcmp(ipaddr, "0.0.0.0") != 0) if(strcmp(ipaddr, "0.0.0.0") != 0) {
ssystem("route add -net %s netmask %s dev %s", ssystem("route add -net %s netmask %s dev %s",
inet_ntoa(netname), netmask, tundev); inet_ntoa(netname), netmask, tundev);
}
#else #else
ssystem("ifconfig %s inet `hostname` %s up", tundev, ipaddr); ssystem("ifconfig %s inet `hostname` %s up", tundev, ipaddr);
if (strcmp(ipaddr, "0.0.0.0") != 0) if(strcmp(ipaddr, "0.0.0.0") != 0) {
ssystem("route add -net %s -netmask %s -interface %s", ssystem("route add -net %s -netmask %s -interface %s",
inet_ntoa(netname), netmask, tundev); inet_ntoa(netname), netmask, tundev);
}
ssystem("sysctl -w net.inet.ip.forwarding=1"); ssystem("sysctl -w net.inet.ip.forwarding=1");
#endif /* !linux */ #endif /* !linux */
@ -867,7 +879,7 @@ main(int argc, char **argv)
setvbuf(stdout, NULL, _IOLBF, 0); /* Line buffered output. */ setvbuf(stdout, NULL, _IOLBF, 0); /* Line buffered output. */
while ((c = getopt(argc, argv, "B:D:hs:t:")) != -1) { while((c = getopt(argc, argv, "B:D:hs:t:")) != -1) {
switch (c) { switch (c) {
case 'B': case 'B':
baudrate = atoi(optarg); baudrate = atoi(optarg);
@ -878,17 +890,19 @@ main(int argc, char **argv)
break; break;
case 's': case 's':
if (strncmp("/dev/", optarg, 5) == 0) if(strncmp("/dev/", optarg, 5) == 0) {
siodev = optarg + 5; siodev = optarg + 5;
else } else {
siodev = optarg; siodev = optarg;
}
break; break;
case 't': case 't':
if (strncmp("/dev/", optarg, 5) == 0) if(strncmp("/dev/", optarg, 5) == 0) {
strcpy(tundev, optarg + 5); strcpy(tundev, optarg + 5);
else } else {
strcpy(tundev, optarg); strcpy(tundev, optarg);
}
break; break;
case '?': case '?':
@ -901,14 +915,15 @@ main(int argc, char **argv)
argc -= (optind - 1); argc -= (optind - 1);
argv += (optind - 1); argv += (optind - 1);
if (argc != 3 && argc != 4) if(argc != 3 && argc != 4) {
err(1, "usage: tunslip [-s siodev] [-t tundev] [-D dhcp-server] ipaddress netmask [dhcp-server]"); err(1, "usage: tunslip [-s siodev] [-t tundev] [-D dhcp-server] ipaddress netmask [dhcp-server]");
}
ipaddr = argv[1]; ipaddr = argv[1];
netmask = argv[2]; netmask = argv[2];
circuit_addr = inet_addr(ipaddr); circuit_addr = inet_addr(ipaddr);
netaddr = inet_addr(ipaddr) & inet_addr(netmask); netaddr = inet_addr(ipaddr) & inet_addr(netmask);
switch (baudrate) { switch(baudrate) {
case -2: case -2:
break; /* Use default. */ break; /* Use default. */
case 9600: case 9600:
@ -935,22 +950,23 @@ main(int argc, char **argv)
* Set up DHCP relay agent socket and find the address of this relay * Set up DHCP relay agent socket and find the address of this relay
* agent. * agent.
*/ */
if (argc == 4) { if(argc == 4) {
dhcp_server = argv[3]; dhcp_server = argv[3];
} }
if (dhcp_server != NULL) { if(dhcp_server != NULL) {
struct sockaddr_in myaddr; struct sockaddr_in myaddr;
socklen_t len; socklen_t len;
in_addr_t a; in_addr_t a;
if (strchr(dhcp_server, ':') != NULL) { if(strchr(dhcp_server, ':') != NULL) {
dhport = atoi(strchr(dhcp_server, ':') + 1); dhport = atoi(strchr(dhcp_server, ':') + 1);
myport = dhport + 1; myport = dhport + 1;
*strchr(dhcp_server, ':') = '\0'; *strchr(dhcp_server, ':') = '\0';
} }
a = inet_addr(dhcp_server); a = inet_addr(dhcp_server);
if (a == -1) if(a == -1) {
err(1, "illegal dhcp-server address"); err(1, "illegal dhcp-server address");
}
#ifndef linux #ifndef linux
dhaddr.sin_len = sizeof(dhaddr); dhaddr.sin_len = sizeof(dhaddr);
#endif #endif
@ -959,8 +975,9 @@ main(int argc, char **argv)
dhaddr.sin_addr.s_addr = a; dhaddr.sin_addr.s_addr = a;
dhsock = socket(AF_INET, SOCK_DGRAM, 0); dhsock = socket(AF_INET, SOCK_DGRAM, 0);
if (dhsock < 0) if(dhsock < 0) {
err (1, "socket"); err (1, "socket");
}
memset(&myaddr, 0x0, sizeof(myaddr)); memset(&myaddr, 0x0, sizeof(myaddr));
#ifndef linux #ifndef linux
myaddr.sin_len = sizeof(myaddr); myaddr.sin_len = sizeof(myaddr);
@ -968,15 +985,18 @@ main(int argc, char **argv)
myaddr.sin_family = AF_INET; myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = INADDR_ANY; myaddr.sin_addr.s_addr = INADDR_ANY;
myaddr.sin_port = htons(myport); myaddr.sin_port = htons(myport);
if (bind(dhsock, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) if(bind(dhsock, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
err(1, "bind dhcp-relay"); err(1, "bind dhcp-relay");
}
if (connect(dhsock, (struct sockaddr *)&dhaddr, sizeof(dhaddr)) < 0) if(connect(dhsock, (struct sockaddr *)&dhaddr, sizeof(dhaddr)) < 0) {
err(1, "connect to dhcp-server"); err(1, "connect to dhcp-server");
}
len = sizeof(myaddr); len = sizeof(myaddr);
if (getsockname(dhsock, (struct sockaddr *)&myaddr, &len) < 0) if(getsockname(dhsock, (struct sockaddr *)&myaddr, &len) < 0) {
err(1, "getsockname dhsock"); err(1, "getsockname dhsock");
}
giaddr = myaddr.sin_addr.s_addr; giaddr = myaddr.sin_addr.s_addr;
@ -985,19 +1005,23 @@ main(int argc, char **argv)
*/ */
close(dhsock); close(dhsock);
dhsock = socket(AF_INET, SOCK_DGRAM, 0); dhsock = socket(AF_INET, SOCK_DGRAM, 0);
if (dhsock < 0) if(dhsock < 0) {
err (1, "socket"); err (1, "socket");
}
myaddr.sin_family = AF_INET; myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = INADDR_ANY; myaddr.sin_addr.s_addr = INADDR_ANY;
myaddr.sin_port = htons(myport); myaddr.sin_port = htons(myport);
if (bind(dhsock, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) if(bind(dhsock, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
err(1, "bind dhcp-relay"); err(1, "bind dhcp-relay");
}
fprintf(stderr, "DHCP server at %s:%d\n", dhcp_server, dhport); fprintf(stderr, "DHCP server at %s:%d\n", dhcp_server, dhport);
} }
if (siodev != NULL) { if(siodev != NULL) {
slipfd = devopen(siodev, O_RDWR | O_NONBLOCK); slipfd = devopen(siodev, O_RDWR | O_NONBLOCK);
if (slipfd == -1) err(1, "can't open siodev ``/dev/%s''", siodev); if(slipfd == -1) {
err(1, "can't open siodev ``/dev/%s''", siodev);
}
} else { } else {
static const char *siodevs[] = { static const char *siodevs[] = {
"ttyUSB0", "cuaU0", "ucom0" /* linux, fbsd6, fbsd5 */ "ttyUSB0", "cuaU0", "ucom0" /* linux, fbsd6, fbsd5 */
@ -1009,7 +1033,9 @@ main(int argc, char **argv)
if (slipfd != -1) if (slipfd != -1)
break; break;
} }
if (slipfd == -1) err(1, "can't open siodev"); if(slipfd == -1) {
err(1, "can't open siodev");
}
} }
fprintf(stderr, "slip started on ``/dev/%s''\n", siodev); fprintf(stderr, "slip started on ``/dev/%s''\n", siodev);
stty_telos(slipfd); stty_telos(slipfd);
@ -1043,8 +1069,9 @@ main(int argc, char **argv)
got_sigalarm = 0; got_sigalarm = 0;
} }
if (!slip_empty()) /* Anything to flush? */ if(!slip_empty()) { /* Anything to flush? */
FD_SET(slipfd, &wset); FD_SET(slipfd, &wset);
}
FD_SET(slipfd, &rset); /* Read from slip ASAP! */ FD_SET(slipfd, &rset); /* Read from slip ASAP! */
if(slipfd > maxfd) maxfd = slipfd; if(slipfd > maxfd) maxfd = slipfd;
@ -1060,12 +1087,13 @@ main(int argc, char **argv)
} }
ret = select(maxfd + 1, &rset, &wset, NULL, NULL); ret = select(maxfd + 1, &rset, &wset, NULL, NULL);
if(ret == -1 && errno != EINTR) if(ret == -1 && errno != EINTR) {
err(1, "select"); err(1, "select");
else if(ret > 0) { } else if(ret > 0) {
if(FD_ISSET(slipfd, &rset)) if(FD_ISSET(slipfd, &rset)) {
serial_to_tun(inslip, tunfd); serial_to_tun(inslip, tunfd);
}
if(FD_ISSET(slipfd, &wset)) { if(FD_ISSET(slipfd, &wset)) {
slip_flushbuf(slipfd); slip_flushbuf(slipfd);
sigalarm_reset(); sigalarm_reset();