Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

- Changed a coding style to "bsd" (described in IMPLEMENTATION,

  chapter 8).
  • Loading branch information...
commit d2bf8f713f41fabc28245a55de40d1bc97a1c847 1 parent 87b08f2
fujisawa authored
View
77 kame/kame/natptlog/Makefile
@@ -1,48 +1,73 @@
-# Copyright (c) 1996 WIDE Project. All rights reserved.
+# $KAME: Makefile,v 1.4 2001/09/02 19:38:47 fujisawa Exp $
+
+# Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000 and 2001 WIDE Project.
+# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
-# modifications, are permitted provided that the above copyright notice
-# and this paragraph are duplicated in all such forms and that any
-# documentation, advertising materials, and other materials related to
-# such distribution and use acknowledge that the software was developed
-# by the WIDE Project, Japan. The name of the Project may not be used to
-# endorse or promote products derived from this software without
-# specific prior written permission. THIS SOFTWARE IS PROVIDED ``AS IS''
-# AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
-# LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-# A PARTICULAR PURPOSE.
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+# 3. Neither the name of the project nor the names of its contributors
+# may be used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
.if exists(${.CURDIR}/../Makefile.opsys)
.include "${.CURDIR}/../Makefile.opsys"
.endif
-BINDIR= ${PREFIX}/bin
+.PATH: ${.CURDIR}/../natptconfig
+BINDIR= ${PREFIX}/sbin
PROG= natptlog
-SRCS= natptlog.c showsubs.c
+LDADD+=-ly -ll
+SRCS= natptlog.c showsubs.c
-.if (${OPSYS} != "NetBSD")
-CFLAGS+= -Wall
+.if (${OPSYS} == "NetBSD")
+YFLAGS+=-d
.endif
-CFLAGS+= -g -I${.CURDIR}/../natptconfig -DNATPT
-
-LDADD+= -L${.CURDIR}/../libinet6 -L${.CURDIR}/../libinet6/obj \
- -L/usr/local/v6/lib -linet6
-DPADD+= ${.CURDIR}/../libinet6/libinet6.a \
- ${.CURDIR}/../libinet6/obj/libinet6.a \
- /usr/local/v6/lib/libinet6.a
-
+CC=gcc
.if (${OPSYS} != "NetBSD")
+CFLAGS+=-g -Wall
+.else
+CPPFLAGS+=-g
+.endif
+.if (${OPSYS} != "NetBSD")
+MAN1=
MAN8= natptlog.8
.else
MAN= natptlog.8
.endif
-etags:
- etags *.[ch] ../natptconfig/showsubs.c
+.if exists(/usr/local/v6/lib/libinet6.a)
+LDADD+= -L${.OBJDIR}/../libinet6 -L${.OBJDIR}/../libinet6/obj \
+ -L/usr/local/v6/lib -linet6
+DPADD+= ${.OBJDIR}/../libinet6/libinet6.a \
+ ${.OBJDIR}/../libinet6/obj/libinet6.a \
+ /usr/local/v6/lib/libinet6.a
+.endif
+tag:
+ etags *.[ch] /usr/include/netinet6/natpt_*.h
-.PATH: ${.CURDIR}/../natptconfig
+clean:
+ -rm -f $(PROG) *.o *.core
+ -rm -f natptlog.8.gz
.include <bsd.prog.mk>
View
48 kame/kame/natptlog/natptlog.8
@@ -1,8 +1,8 @@
-.\" $KAME: natptlog.8,v 1.5 2000/12/04 06:28:25 itojun Exp $
+.\" $KAME: natptlog.8,v 1.6 2001/09/02 19:38:47 fujisawa Exp $
.\"
-.\" Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
+.\" Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000 and 2001 WIDE Project.
.\" All rights reserved.
-.\"
+.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
@@ -14,7 +14,7 @@
.\" 3. Neither the name of the project nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
-.\"
+.\"
.\" THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -41,40 +41,24 @@
.Nd monitors for logged packets
.\"
.Sh SYNOPSIS
-.Nm natptlog
-.Op Fl b
-.Op Fl d Ar debuglevel
.\"
.Sh DESCRIPTION
-.Nm
-read the binary data from kernel by NAT-PT translator uses, and
-reprint it as human readable format. The output goes to syslog when
-invoked as daemon, or goes to standard error when invoked as non
-daemon mode.
-.Pp
-The options are as follows:
-.Bl -tag -width Fl
-.It Fl b
-Run as a daemon.
-.It Fl d Ar debuglevel
-Set debug level to
-.Ar debuglevel .
-.El
+.\"
+.Sh DIRECTIVES
+.\"
+.Sh RUNNING NAT-PT
+.\"
+.Sh EXAMPLE 1
+.Sh EXAMPLE 2
+.\"
+.Sh TABLE FORMAT
+.\"
+.Sh FILES
+.\"
.Sh SEE ALSO
-.Xr natptconfig 8 ,
-.Xr natptd 8
-.Rs
.\"
.Sh DIAGNOSTICS
-(to be written)
.\"
.Sh BUGS
-The
-.Nm
-command is now under development. It's user interface and overall
-functionality are subjects to future improvements and changes.
.\"
.Sh HISTORY
-.Nm
-are implemented by Shin-ichi Fujisawa
-.Li <fujisawa@kame.net> .
View
921 kame/kame/natptlog/natptlog.c
@@ -1,7 +1,9 @@
+/* $KAME: natptlog.c,v 1.14 2001/09/02 19:38:47 fujisawa Exp $ */
+
/*
- * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
+ * Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000 and 2001 WIDE Project.
* All rights reserved.
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
@@ -13,7 +15,7 @@
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
- *
+ *
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -25,27 +27,17 @@
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
- *
- * $Id: natptlog.c,v 1.13 2001/05/08 04:36:33 itojun Exp $
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
-#include <string.h>
-#include <errno.h>
-#include <signal.h>
-#include <unistd.h>
-
+#include <sys/param.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
+#include <sys/mbuf.h>
+#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syslog.h>
-#include <sys/param.h>
-#include <sys/mbuf.h>
-
#include <net/if.h>
#include <net/if_dl.h>
@@ -64,7 +56,13 @@
#include <netinet6/natpt_defs.h>
#include <netinet6/natpt_log.h>
-#include "showsubs.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <errno.h>
+#include <signal.h>
+#include <unistd.h>
/*
@@ -90,8 +88,6 @@
#define isOn(name) (u_opt.b.name == 1)
#define isOff(name) (u_opt.b.name == 0)
-#define isDebug(d) (u_debug & (d))
-
u_long u_debug;
sigset_t mask;
@@ -99,15 +95,15 @@ int signals[] = { SIGINT, SIGTERM, };
struct options
{
- struct
- {
- unsigned daemon:1; /* TRUE if daemon mode */
- unsigned logsyslog:1; /* TRUE if log to syslog */
- unsigned logstderr:1; /* TRUE if log to stderr */
- unsigned logopened:1; /* TRUE if openLog() done */
- } b;
- char *pidFilename;
- char *statsFilename;
+ struct
+ {
+ unsigned daemon:1; /* TRUE if daemon mode */
+ unsigned logsyslog:1; /* TRUE if log to syslog */
+ unsigned logstderr:1; /* TRUE if log to stderr */
+ unsigned logopened:1; /* TRUE if openLog() done */
+ } b;
+ char *pidFilename;
+ char *statsFilename;
} u_opt;
@@ -115,7 +111,7 @@ void parseArgument __P((int, char *[]));
void recvMesg __P((int));
void printLogMsg __P((struct lbuf *));
-void printLogMBuf __P((struct lbuf *));
+void printLogDump __P((struct lbuf *));
void printLogIP4 __P((struct lbuf *));
void printLogIP6 __P((struct lbuf *));
void printLogIN6addr __P((struct lbuf *));
@@ -145,8 +141,9 @@ void preinit __P((void));
void init_main __P((void));
-extern char *tcpstates[]; /* defined in <netinet/tcp_fsm.h> */
+void makeCSlotLine __P((char *, int, struct cSlot *));
+extern char *tcpstates[]; /* defined in <netinet/tcp_fsm.h> */
/*
@@ -156,428 +153,394 @@ extern char *tcpstates[]; /* defined in <netinet/tcp_fsm.h> */
int
main(int argc, char *argv[])
{
- int sockfd;
+ const char *fn = __FUNCTION__;
- preinit();
+ int sockfd;
- parseArgument(argc, argv);
+ preinit();
+ parseArgument(argc, argv);
+ init_main();
- init_main();
+ if ((sockfd = socket(PF_INET, SOCK_RAW, IPPROTO_AHIP)) < 0)
+ log(LOG_ERR, "%s(): socket open failure: %s", fn, strerror(errno)),
+ exit (errno);
- if ((sockfd = socket(PF_INET, SOCK_RAW, IPPROTO_AHIP)) < 0)
- log(LOG_ERR, "main(): socket open failure: %s", strerror(errno)),
- exit (errno);
+ recvMesg(sockfd);
- recvMesg(sockfd);
-
- exit (0);
+ exit (0);
}
void
parseArgument(int argc, char *argv[])
{
- int ch;
+ const char *fn = __FUNCTION__;
- extern char *optarg;
- extern int optind;
+ int ch;
- while ((ch = getopt(argc, argv, "bd:")) != -1)
- {
- switch (ch)
- {
- case 'b':
- u_opt.b.daemon = 1;
- u_opt.b.logsyslog = 1;
- u_opt.b.logstderr = 0;
- openLog();
- break;
-
- case 'd':
- u_debug = strtoul(optarg, (char **)NULL, 0);
- break;
-
- default:
- log(LOG_ERR, "parseArgument(): Illegal option name `-%c\'\n", ch);
- exit(1);
+ extern char *optarg;
+ extern int optind;
+
+ while ((ch = getopt(argc, argv, "bd:")) != -1) {
+ switch (ch) {
+ case 'b':
+ u_opt.b.daemon = 1;
+ u_opt.b.logsyslog = 1;
+ u_opt.b.logstderr = 0;
+ openLog();
+ break;
+
+ case 'd':
+ u_debug = strtoul(optarg, (char **)NULL, 0);
+ break;
+
+ default:
+ log(LOG_ERR, "%s(): Illegal option name `-%c\'\n", fn, ch);
+ exit(1);
+ }
}
- }
- argc -= optind;
- argv += optind;
+ argc -= optind;
+ argv += optind;
- return ;
+ return ;
}
void
recvMesg(int sockfd)
{
- int len;
- struct lbuf *lbuf;
- struct sockaddr_storage from;
- u_char Wow[PACKETSZ];
- fd_set sockvec;
- fd_set recvec;
-
- FD_ZERO(&sockvec);
- FD_SET(sockfd, &sockvec);
-
- while (TRUE)
- {
- FD_COPY(&sockvec, &recvec);
- switch (select(FD_SETSIZE, &recvec, NULL, NULL, NULL))
- {
- case -1:
- if (errno != EINTR)
- log(LOG_ERR, "recvMesg(): select failure: %s\n", strerror(errno)),
- quitting(errno);
- break;
-
- case 0:
- break;
-
- default:
- if (FD_ISSET(sockfd, &recvec))
- {
- int rv;
-
- if ((rv = ioctl(sockfd, FIONREAD, &len)) < 0)
- {
- log(LOG_ERR, "recvMesg(): ioctl failure: %s\n", strerror(errno));
- continue;
- }
-
- rv = recvfrom(sockfd, Wow, PACKETSZ, 0, (struct sockaddr *)&from, &len);
- if (rv <= 0)
- {
- log(LOG_ERR, "recvMesg(): recvfrom faulure: %s\n", strerror(errno));
- continue;
+ const char *fn = __FUNCTION__;
+
+ int len;
+ struct lbuf *lbuf;
+ struct sockaddr_storage from;
+ u_char Wow[PACKETSZ];
+ fd_set sockvec;
+ fd_set recvec;
+
+ FD_ZERO(&sockvec);
+ FD_SET(sockfd, &sockvec);
+
+ while (TRUE) {
+ FD_COPY(&sockvec, &recvec);
+ switch (select(FD_SETSIZE, &recvec, NULL, NULL, NULL)) {
+ case -1:
+ if (errno != EINTR)
+ log(LOG_ERR, "%s(): select failure: %s\n",
+ fn, strerror(errno)), quitting(errno);
+ break;
+
+ case 0:
+ break;
+
+ default:
+ if (FD_ISSET(sockfd, &recvec)) {
+ int rv;
+
+ if ((rv = ioctl(sockfd, FIONREAD, &len)) < 0) {
+ log(LOG_ERR, "%s(): ioctl failure: %s\n",
+ fn, strerror(errno));
+ continue;
+ }
+
+ rv = recvfrom(sockfd, Wow, PACKETSZ, 0,
+ (struct sockaddr *)&from, &len);
+ if (rv <= 0) {
+ log(LOG_ERR, "%s(): recvfrom faulure: %s\n",
+ fn, strerror(errno));
+ continue;
+ }
+
+ lbuf = (struct lbuf *)Wow;
+ switch (lbuf->l_hdr.lh_type) {
+ case LOG_MSG: printLogMsg(lbuf); break;
+ case LOG_DUMP: printLogDump(lbuf); break;
+ case LOG_IP4: printLogIP4(lbuf); break;
+ case LOG_IP6: printLogIP6(lbuf); break;
+ case LOG_IN6ADDR: printLogIN6addr(lbuf); break;
+ case LOG_CSLOT: printLogCSlot(lbuf); break;
+ case LOG_TCPFSM: printLogTCPFSM(lbuf); break;
+
+ default:
+ log(lbuf->l_hdr.lh_pri,
+ "%d uninstalled", lbuf->l_hdr.lh_type);
+ break;
+ }
+ }
+ break;
}
-
- lbuf = (struct lbuf *)Wow;
- switch (lbuf->l_hdr.lh_type)
- {
- case LOG_MSG: printLogMsg(lbuf); break;
- case LOG_MBUF: printLogMBuf(lbuf); break;
- case LOG_IP4: printLogIP4(lbuf); break;
- case LOG_IP6: printLogIP6(lbuf); break;
- case LOG_IN6ADDR: printLogIN6addr(lbuf); break;
- case LOG_CSLOT: printLogCSlot(lbuf); break;
- case LOG_TCPFSM: printLogTCPFSM(lbuf); break;
-
- default:
- log(lbuf->l_hdr.lh_pri, "%d uninstalled", lbuf->l_hdr.lh_type);
- break;
- }
- }
- break;
}
- }
}
void
printLogMsg(struct lbuf *lbuf)
{
- log(lbuf->l_hdr.lh_pri, "%s", lbuf->l_dat.__buf);
+ log(lbuf->l_hdr.lh_pri, "%s", lbuf->l_dat.__buf);
}
void
-printLogMBuf(struct lbuf *lbuf)
+printLogDump(struct lbuf *lbuf)
{
- int rv;
- int rbytes = lbuf->l_hdr.lh_size;
- char *dbytes = lbuf->l_dat.__buf;
-
- while (rbytes > 0)
- {
- rv = hexdump16(lbuf->l_hdr.lh_pri | 0x80000000, dbytes, rbytes);
- if (rv <= 0)
- {
- log(LOG_ERR, "printLogMBuf(): something wroing in hexdump16." );
- continue;
+ const char *fn = __FUNCTION__;
+
+ int rv;
+ int rbytes = lbuf->l_hdr.lh_size;
+ char *dbytes = lbuf->l_dat.__buf;
+
+ while (rbytes > 0) {
+ rv = hexdump16(lbuf->l_hdr.lh_pri | 0x80000000, dbytes, rbytes);
+ if (rv <= 0) {
+ log(LOG_ERR, "%s(): something wroing in hexdump16.", fn );
+ continue;
+ }
+
+ rbytes -= rv;
+ dbytes += rv;
}
-
- rbytes -= rv;
- dbytes += rv;
- }
}
void
printLogIP4(struct lbuf *lbuf)
{
- struct ip *ip4;
- char from[INET_ADDRSTRLEN];
- char to [INET_ADDRSTRLEN];
+ int tlen;
+ char *type;
+ struct ip *ip4;
+ struct icmp *icmp4;
+ struct tcphdr *tcp4;
+ struct udphdr *udp4;
+ char from[INET_ADDRSTRLEN];
+ char to [INET_ADDRSTRLEN];
+
+ ip4 = (struct ip *)lbuf->l_dat.__buf;
+ inet_ntop(AF_INET, (char *)&ip4->ip_src, from, INET_ADDRSTRLEN);
+ inet_ntop(AF_INET, (char *)&ip4->ip_dst, to, INET_ADDRSTRLEN);
+
+ switch (ip4->ip_p) {
+ case IPPROTO_ICMP:
+ icmp4 = (struct icmp *)(ip4 + 1);
+ switch (icmp4->icmp_type) {
+ case ICMP_ECHOREPLY: type = "echo reply"; break;
+ case ICMP_ECHO: type = "echo"; break;
+ default: type = "unknown"; break;
+ }
- ip4 = (struct ip *)lbuf->l_dat.__buf;
- inet_ntop(AF_INET, (char *)&ip4->ip_src, from, INET_ADDRSTRLEN);
- inet_ntop(AF_INET, (char *)&ip4->ip_dst, to, INET_ADDRSTRLEN);
+ log(lbuf->l_hdr.lh_pri, "from %s to %s icmp: %s",
+ from, to, type);
+ break;
- switch (ip4->ip_p)
- {
- case IPPROTO_ICMP:
- {
- char *type;
- struct icmp *icmp4;
-
- icmp4 = (struct icmp *)(ip4 + 1);
- switch (icmp4->icmp_type)
- {
- case ICMP_ECHOREPLY: type = "echo reply"; break;
- case ICMP_ECHO: type = "echo"; break;
- default: type = "unknown"; break;
- }
-
- log(lbuf->l_hdr.lh_pri, "from %s to %s icmp: %s",
- from, to, type);
- }
- break;
+ case IPPROTO_TCP:
+ tcp4 = (struct tcphdr *)(ip4 + 1);
+ tlen = ip4->ip_len - (tcp4->th_off << 2);
+ log(lbuf->l_hdr.lh_pri, "from %s.%d to %s.%d tcp %d",
+ from, ntohs(tcp4->th_sport), to, ntohs(tcp4->th_dport), tlen);
- case IPPROTO_TCP:
- {
- int tlen;
- struct tcphdr *tcp4;
+ break;
- tcp4 = (struct tcphdr *)(ip4 + 1);
- tlen = ip4->ip_len - (tcp4->th_off << 2);
- log(lbuf->l_hdr.lh_pri, "from %s.%d to %s.%d tcp %d",
- from, ntohs(tcp4->th_sport), to, ntohs(tcp4->th_dport), tlen);
- }
- break;
+ case IPPROTO_UDP:
+ udp4 = (struct udphdr *)(ip4 + 1);
+ log(lbuf->l_hdr.lh_pri, "from %s.%d to %s.%d udp %d",
+ from, ntohs(udp4->uh_sport), to, ntohs(udp4->uh_dport), udp4->uh_ulen);
- case IPPROTO_UDP:
- {
- struct udphdr *udp4;
-
- udp4 = (struct udphdr *)(ip4 + 1);
- log(lbuf->l_hdr.lh_pri, "from %s.%d to %s.%d udp %d",
- from, ntohs(udp4->uh_sport), to, ntohs(udp4->uh_dport), udp4->uh_ulen);
+ break;
+
+ default:
+ log(lbuf->l_hdr.lh_pri, "from %s to %s", from, to);
+ break;
}
- break;
-
- default:
- log(lbuf->l_hdr.lh_pri, "from %s to %s", from, to);
- break;
- }
}
void
printLogIP6(struct lbuf *lbuf)
{
- struct ip6_hdr *ip6;
- char from[INET6_ADDRSTRLEN];
- char to [INET6_ADDRSTRLEN];
-
- ip6 = (struct ip6_hdr *)lbuf->l_dat.__buf;
- inet_ntop(AF_INET6, (char *)&ip6->ip6_src, from, INET6_ADDRSTRLEN);
- inet_ntop(AF_INET6, (char *)&ip6->ip6_dst, to, INET6_ADDRSTRLEN);
- log(lbuf->l_hdr.lh_pri, "from %s to %s", from, to);
+ struct ip6_hdr *ip6;
+ char from[INET6_ADDRSTRLEN];
+ char to [INET6_ADDRSTRLEN];
+
+ ip6 = (struct ip6_hdr *)lbuf->l_dat.__buf;
+ inet_ntop(AF_INET6, (char *)&ip6->ip6_src, from, INET6_ADDRSTRLEN);
+ inet_ntop(AF_INET6, (char *)&ip6->ip6_dst, to, INET6_ADDRSTRLEN);
+ log(lbuf->l_hdr.lh_pri, "from %s to %s", from, to);
}
char *
displaySockaddr(struct sockaddr *from)
{
- switch (from->sa_family)
- {
- case AF_INET:
- return (displaySockaddrIn4((struct sockaddr_in *)from));
-
- case AF_INET6:
- return (displaySockaddrIn6((struct sockaddr_in6 *)from));
-
- case AF_LINK:
- return (displaySockaddrDl((struct sockaddr_dl *)from));
- }
-
- return ("unknown");
+ switch (from->sa_family) {
+ case AF_INET:
+ return (displaySockaddrIn4((struct sockaddr_in *)from));
+
+ case AF_INET6:
+ return (displaySockaddrIn6((struct sockaddr_in6 *)from));
+
+ case AF_LINK:
+ return (displaySockaddrDl((struct sockaddr_dl *)from));
+ }
+
+ return ("unknown");
}
void
printLogIN6addr(struct lbuf *lbuf)
{
- char in6addr[INET6_ADDRSTRLEN];
+ char in6addr[INET6_ADDRSTRLEN];
- inet_ntop(AF_INET6, (char *)lbuf->l_addr.in6addr, in6addr, INET6_ADDRSTRLEN);
- log(lbuf->l_hdr.lh_pri, "%s%s", lbuf->l_addr.__msg, in6addr);
+ inet_ntop(AF_INET6, (char *)lbuf->l_addr.in6addr, in6addr, INET6_ADDRSTRLEN);
+ log(lbuf->l_hdr.lh_pri, "%s%s", lbuf->l_addr.__msg, in6addr);
}
void
printLogCSlot(struct lbuf *lbuf)
{
- struct logmsg *lmsg = composeCSlotEntry((struct _cSlot *)lbuf->l_dat.__buf);
+ char wow[BUFSIZ];
- log(lbuf->l_hdr.lh_pri, "%s", &lmsg->lmsg_data[0]);
- free(lmsg);
+ makeCSlotLine(wow, sizeof(wow), (struct cSlot *)lbuf->l_dat.__buf);
+ log(lbuf->l_hdr.lh_pri, "%s", wow);
}
void
printLogTCPFSM(struct lbuf *lbuf)
{
- int *wow = (int *)lbuf->l_dat.__buf;
- char *session;
- char *inout;
- char *oldstate;
- char *flags;
- char *newstate;
-
- switch (wow[0])
- {
- case NATPT_INBOUND: session = "inbound"; break;
- case NATPT_OUTBOUND: session = "outbound"; break;
- default: session = "unspecified"; break;
- }
-
- switch (wow[1])
- {
- case NATPT_INBOUND: inout = "inbound"; break;
- case NATPT_OUTBOUND: inout = "outbound"; break;
- default: inout = "unspecified"; break;
- }
-
- oldstate = "unknown";
- if (wow[2] >= 0 && wow[2] < TCP_NSTATES)
- oldstate = tcpstates[wow[2]];
-
- flags = displayTcpflags(wow[3]);
-
- newstate = "unknown";
- if (wow[4] >= 0 && wow[4] < TCP_NSTATES)
- newstate = tcpstates[wow[4]];
-
- log(lbuf->l_hdr.lh_pri, "%9s%9s%13s%9s%13s",
- session,
- inout,
- oldstate,
- flags,
- newstate);
+ int *wow = (int *)lbuf->l_dat.__buf;
+ char *flags;
+ char *oldstate;
+ char *newstate;
+
+ oldstate = "unknown";
+ if (wow[2] >= 0 && wow[2] < TCP_NSTATES)
+ oldstate = tcpstates[wow[2]];
+
+ flags = displayTcpflags(wow[3]);
+
+ newstate = "unknown";
+ if (wow[4] >= 0 && wow[4] < TCP_NSTATES)
+ newstate = tcpstates[wow[4]];
+
+ log(lbuf->l_hdr.lh_pri, "%13s%9s%13s",
+ oldstate,
+ flags,
+ newstate);
}
char *
displaySockaddrIn4(struct sockaddr_in *from)
{
- static char in4txt[INET_ADDRSTRLEN];
+ static char in4txt[INET_ADDRSTRLEN];
- inet_ntop(AF_INET, (char *)&from->sin_addr, in4txt, INET_ADDRSTRLEN);
- return (in4txt);
+ inet_ntop(AF_INET, (char *)&from->sin_addr, in4txt, INET_ADDRSTRLEN);
+ return (in4txt);
}
char *
displaySockaddrIn6(struct sockaddr_in6 *from)
{
- static char in6txt[INET6_ADDRSTRLEN];
-
- inet_ntop(AF_INET6, (char *)&from->sin6_addr, in6txt, INET6_ADDRSTRLEN);
- return (in6txt);
+ static char in6txt[INET6_ADDRSTRLEN];
+
+ inet_ntop(AF_INET6, (char *)&from->sin6_addr, in6txt, INET6_ADDRSTRLEN);
+ return (in6txt);
}
char *
displaySockaddrDl(struct sockaddr_dl *from)
{
- char *cp;
- static char dltxt[sizeof("ff:ff:ff:ff:ff:ff:00")];
-
- cp = from->sdl_data;
- cp += from->sdl_nlen;
- sprintf(dltxt, "%02x:%02x:%02x:%02x:%02x:%02x",
- cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
- return (dltxt);
+ char *cp;
+ static char dltxt[sizeof("ff:ff:ff:ff:ff:ff:00")];
+
+ cp = from->sdl_data;
+ cp += from->sdl_nlen;
+ sprintf(dltxt, "%02x:%02x:%02x:%02x:%02x:%02x",
+ cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
+ return (dltxt);
}
char *
displayTcpflags(int flags)
{
- static char th_flags[8];
+ static char th_flags[8];
- strcpy(th_flags, "------");
- th_flags[6] = '\0';
- th_flags[7] = '\0';
+ strcpy(th_flags, "------");
+ th_flags[6] = '\0';
+ th_flags[7] = '\0';
- if (flags & TH_FIN) th_flags[5] = 'f';
- if (flags & TH_SYN) th_flags[4] = 's';
- if (flags & TH_RST) th_flags[3] = 'r';
- if (flags & TH_PUSH) th_flags[2] = 'p';
- if (flags & TH_ACK) th_flags[1] = 'a';
- if (flags & TH_URG) th_flags[0] = 'u';
+ if (flags & TH_FIN) th_flags[5] = 'f';
+ if (flags & TH_SYN) th_flags[4] = 's';
+ if (flags & TH_RST) th_flags[3] = 'r';
+ if (flags & TH_PUSH) th_flags[2] = 'p';
+ if (flags & TH_ACK) th_flags[1] = 'a';
+ if (flags & TH_URG) th_flags[0] = 'u';
- return (th_flags);
+ return (th_flags);
}
int
hexdump16(int priority, char *buffer, int len)
{
- int i, j;
- int offh, offc;
- int nbytes;
- u_char *dbyte;
- u_char Wow[128];
-
- dbyte = buffer;
- memset(Wow, ' ', sizeof(Wow));
-
- if (len < 16)
- {
- nbytes = len;
-
- for (i = 0, offh = 0, offc = 38; i <= 3; i++, offh += 9, offc += 5)
- {
- for (j = 0; j <= 3; j++, dbyte++)
- {
- sprintf(&Wow[offh+j*2], "%02x", *dbyte);
- sprintf(&Wow[offc+j], "%c",
- (*dbyte >= 0x20 && *dbyte <= 0x7e) ? *dbyte : '.');
- Wow[offh+j*2+2] = ' ';
- Wow[offc+j+1] = ' ';
-
- if (--nbytes <= 0)
- {
- Wow[57] = '\0';
- log(priority, "%s", Wow);
- return (len);
+ int i, j;
+ int offh, offc;
+ int nbytes;
+ u_char *dbyte;
+ u_char Wow[128];
+
+ dbyte = buffer;
+ memset(Wow, ' ', sizeof(Wow));
+
+ if (len < 16) {
+ nbytes = len;
+
+ for (i = 0, offh = 0, offc = 38; i <= 3; i++, offh += 9, offc += 5) {
+ for (j = 0; j <= 3; j++, dbyte++) {
+ sprintf(&Wow[offh+j*2], "%02x", *dbyte);
+ sprintf(&Wow[offc+j], "%c",
+ (*dbyte >= 0x20 && *dbyte <= 0x7e) ? *dbyte : '.');
+ Wow[offh+j*2+2] = ' ';
+ Wow[offc+j+1] = ' ';
+
+ if (--nbytes <= 0) {
+ Wow[57] = '\0';
+ log(priority, "%s", Wow);
+ return (len);
+ }
+ }
}
- }
+ return (0); /* may be something wrong */
}
- return (0); /* may be something wrong */
- }
- for (i = 0, offh = 0, offc = 38; i <= 3; i++, offh += 9, offc += 5)
- {
- for (j = 0; j <= 3; j++, dbyte++)
- {
- sprintf(&Wow[offh+j*2], "%02x", *dbyte);
- sprintf(&Wow[offc+j], "%c",
- (*dbyte >= 0x20 && *dbyte <= 0x7e) ? *dbyte : '.');
+ for (i = 0, offh = 0, offc = 38; i <= 3; i++, offh += 9, offc += 5) {
+ for (j = 0; j <= 3; j++, dbyte++) {
+ sprintf(&Wow[offh+j*2], "%02x", *dbyte);
+ sprintf(&Wow[offc+j], "%c",
+ (*dbyte >= 0x20 && *dbyte <= 0x7e) ? *dbyte : '.');
+ }
+ Wow[offh+j*2] = ' ';
+ Wow[offc+j] = ' ';
}
- Wow[offh+j*2] = ' ';
- Wow[offc+j] = ' ';
- }
- Wow[57] = '\0';
- log(priority, "%s", Wow);
- return (16);
+ Wow[57] = '\0';
+ log(priority, "%s", Wow);
+ return (16);
}
void
quitting(int status)
{
- if (u_opt.pidFilename != NULL)
- unlink(u_opt.pidFilename);
+ if (u_opt.pidFilename != NULL)
+ unlink(u_opt.pidFilename);
- exit(status);
+ exit(status);
}
@@ -588,71 +551,68 @@ quitting(int status)
void
openLog()
{
- if (isOff(logopened))
- {
- u_opt.b.logopened = 1;
+ if (isOff(logopened)) {
+ u_opt.b.logopened = 1;
- if (isOn(logsyslog))
- {
- int logopt = 0;
+ if (isOn(logsyslog)) {
+ int logopt = 0;
- logopt = LOG_PID | LOG_NOWAIT;
- openlog("natptlog", logopt, LOG_DAEMON);
- }
+ logopt = LOG_PID | LOG_NOWAIT;
+ openlog("natptlog", logopt, LOG_DAEMON);
+ }
- log(LOG_INFO, "starting natptlog");
- }
+ log(LOG_INFO, "starting natptlog");
+ }
}
void
log(int priority, char *fmt, ...)
{
- va_list ap;
- char Wow[BUFSIZ];
- time_t t;
-
- va_start(ap, fmt);
- vsprintf(Wow, fmt, ap);
+ va_list ap;
+ char Wow[BUFSIZ];
+ time_t t;
- if (isOn(logsyslog))
- syslog(priority, "%s", Wow);
+ va_start(ap, fmt);
+ vsprintf(Wow, fmt, ap);
- if (isOn(logstderr))
- {
- struct tm *tm;
- struct timeval atv;
- char *months[] =
- {
- "Jan", "Feb", "Mar", "Apr", "May", "Jun",
- "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
- };
-
- if (priority & 0x80000000)
- fprintf(stderr, " ");
- else
- {
- gettimeofday(&atv, NULL);
- t = (time_t)atv.tv_sec;
- tm = localtime(&t);
+ if (isOn(logsyslog))
+ syslog(priority, "%s", Wow);
- fprintf(stderr, "%s ", months[tm->tm_mon]);
- fprintf(stderr, "%02d ", tm->tm_mday);
- fprintf(stderr, "%02d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec);
+ if (isOn(logstderr)) {
+ struct tm *tm;
+ struct timeval atv;
+ char *months[] =
+ {
+ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
+ };
+
+ if (priority & 0x80000000)
+ fprintf(stderr, " ");
+ else {
+ gettimeofday(&atv, NULL);
+ t = (time_t)atv.tv_sec;
+ tm = localtime(&t);
+
+ fprintf(stderr, "%s ", months[tm->tm_mon]);
+ fprintf(stderr, "%02d ", tm->tm_mday);
+ fprintf(stderr, "%02d:%02d:%02d ",
+ tm->tm_hour, tm->tm_min, tm->tm_sec);
+ }
+ fprintf(stderr, "%s", Wow),
+ fprintf(stderr, "\n");
}
- fprintf(stderr, "%s", Wow),
- fprintf(stderr, "\n");
- }
- va_end(ap);
+ va_end(ap);
}
void
closeLog()
{
- if (isOn(daemon))
- closelog();
+ if (isOn(daemon))
+ closelog();
}
@@ -663,66 +623,63 @@ closeLog()
void
updatePidFile(char *filename)
{
- FILE *fp;
-
- if ((fp = writeOpen(filename, O_EXCL)) != NULL)
- {
- fprintf(fp, "%ld\n", (long)getpid());
- fclose(fp);
- }
- else
- {
- log(LOG_ERR, "updatePidFile(): couldn't create pid file '%s'", filename);
- }
+ const char *fn = __FUNCTION__;
+
+ FILE *fp;
+
+ if ((fp = writeOpen(filename, O_EXCL)) != NULL) {
+ fprintf(fp, "%ld\n", (long)getpid());
+ fclose(fp);
+ } else {
+ log(LOG_ERR, "%s(): couldn't create pid file '%s'", fn, filename);
+ }
}
FILE *
writeOpen(char *filename, int flag)
{
- int fd;
- int regular = 0;
- FILE *stream;
- struct stat sb;
-
- if (stat(filename, &sb) >= 0)
- regular = sb.st_mode & S_IFREG;
- else
- {
- regular = 1;
- if (errno != ENOENT)
- {
- log(LOG_ERR, "writeOpen(): stat of %s failed: %s\n",
- filename, strerror(errno));
- return (NULL);
+ const char *fn = __FUNCTION__;
+
+ int fd;
+ int regular = 0;
+ FILE *stream;
+ struct stat sb;
+
+ if (stat(filename, &sb) >= 0)
+ regular = sb.st_mode & S_IFREG;
+ else {
+ regular = 1;
+ if (errno != ENOENT) {
+ log(LOG_ERR, "%s(): stat of %s failed: %s\n",
+ fn, filename, strerror(errno));
+ return (NULL);
+ }
}
- }
-
- if (regular == 0)
- {
- log(LOG_ERR, "writeOpen(): %s isn't a regular file\n", filename);
- return (NULL);
- }
-
- if ((regular == 0x8000) /* exist regular file */
- && (flag == O_EXCL)) /* and error if already exists */
- {
- log(LOG_ERR, "writeOpen(): file exists\n");
- return (NULL);
- }
-
- unlink(filename);
- fd = open(filename,
- O_WRONLY|O_CREAT|O_EXCL,
- S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
- if (fd < 0)
- return (NULL);
-
- stream = fdopen(fd, "w");
- if (stream == NULL)
- close(fd);
-
- return (stream);
+
+ if (regular == 0) {
+ log(LOG_ERR, "fn(): %s isn't a regular file\n", fn, filename);
+ return (NULL);
+ }
+
+ if ((regular == 0x8000) /* exist regular file */
+ && (flag == O_EXCL)) { /* and error if already exists */
+ log(LOG_ERR, "%s(): file exists\n", fn);
+ return (NULL);
+ }
+
+ unlink(filename);
+ fd = open(filename,
+ O_WRONLY|O_CREAT|O_EXCL,
+ S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
+ if (fd < 0)
+ return (NULL);
+
+ stream = fdopen(fd, "w");
+ if (stream == NULL)
+ close(fd);
+
+ return (stream);
}
@@ -733,97 +690,93 @@ writeOpen(char *filename, int flag)
void
setFileName(char *filename, int type)
{
- switch (type)
- {
- case OPT_PIDFILE:
- u_opt.pidFilename = xmalloc(ROUNDUP(strlen(filename)+1));
- strcpy(u_opt.pidFilename, filename);
- log(LOG_INFO, " PidFile: %s", filename);
- break;
-
- case OPT_STATSFILE:
- u_opt.statsFilename = xmalloc(ROUNDUP(strlen(filename)+1));
- strcpy(u_opt.statsFilename, filename);
- log(LOG_INFO, " StatFile: %s", filename);
- break;
- }
+ switch (type) {
+ case OPT_PIDFILE:
+ u_opt.pidFilename = xmalloc(ROUNDUP(strlen(filename)+1));
+ strcpy(u_opt.pidFilename, filename);
+ log(LOG_INFO, " PidFile: %s", filename);
+ break;
+
+ case OPT_STATSFILE:
+ u_opt.statsFilename = xmalloc(ROUNDUP(strlen(filename)+1));
+ strcpy(u_opt.statsFilename, filename);
+ log(LOG_INFO, " StatFile: %s", filename);
+ break;
+ }
}
void
initSignal()
{
- sighandler(0);
+ sighandler(0);
}
void
sighandler(int sig)
{
- switch (sig)
- {
- case 0:
- {
- int iter;
+ const char *fn = __FUNCTION__;
- if (sigemptyset(&mask) < 0)
- log(LOG_ERR, "Unable to initialize a signal set.");
+ int iter;
- for (iter = 0; iter < sizeof(signals) / sizeof(signals[0]); iter++)
- {
- if (sigaddset(&mask, signals[iter]) < 0)
- log(LOG_ERR, "Unable to adds to the signal set");
- }
+ switch (sig) {
+ case 0:
+ if (sigemptyset(&mask) < 0)
+ log(LOG_ERR, "%s(): Unable to initialize a signal set.", fn);
- for (iter = 0; iter < sizeof(signals) / sizeof(signals[0]); iter++)
- {
- struct sigaction sa;
+ for (iter = 0; iter < sizeof(signals) / sizeof(signals[0]); iter++) {
+ if (sigaddset(&mask, signals[iter]) < 0)
+ log(LOG_ERR, "%s(): Unable to adds to the signal set", fn);
+ }
- bzero(&sa, sizeof(struct sigaction));
- sa.sa_mask = mask;
- sa.sa_handler = sighandler;
- if (sigaction(signals[iter], &sa, NULL) < 0)
- {
- log(LOG_NOTICE, "sighandler(): sigaction failed(%d): %s",
- signals[iter], strerror(errno));
+ for (iter = 0; iter < sizeof(signals) / sizeof(signals[0]); iter++) {
+ struct sigaction sa;
+
+ bzero(&sa, sizeof(struct sigaction));
+ sa.sa_mask = mask;
+ sa.sa_handler = sighandler;
+ if (sigaction(signals[iter], &sa, NULL) < 0) {
+ log(LOG_NOTICE, "%s(): sigaction failed(%d): %s",
+ fn, signals[iter], strerror(errno));
+ }
}
- }
- }
- break;
+ break;
- default:
- log(LOG_ERR, "sighandler(): caught signal, %d\n", sig);
- quitting (0);
- break;
- }
+ default:
+ log(LOG_ERR, "%s(): caught signal, %d\n", fn, sig);
+ quitting (0);
+ break;
+ }
}
void
preinit()
{
- u_opt.b.daemon = 0;
- u_opt.b.logsyslog = 0;
- u_opt.b.logstderr = 1;
- u_opt.b.logopened = 0;
+ u_opt.b.daemon = 0;
+ u_opt.b.logsyslog = 0;
+ u_opt.b.logstderr = 1;
+ u_opt.b.logopened = 0;
}
void
init_main()
{
- openLog();
+ const char *fn = __FUNCTION__;
+
+ openLog();
- if (isOn(daemon)
- && (daemon(0, 0) < 0))
- {
- log(LOG_ERR, "init_main(): failure on daemon()"),
- exit(errno);
- }
+ if (isOn(daemon)
+ && (daemon(0, 0) < 0)) {
+ log(LOG_ERR, "%s(): failure on daemon()", fn),
+ exit(errno);
+ }
- if (u_opt.pidFilename == NULL)
- setFileName(_PATH_PIDFILE, OPT_PIDFILE);
+ if (u_opt.pidFilename == NULL)
+ setFileName(_PATH_PIDFILE, OPT_PIDFILE);
- initSignal();
- updatePidFile(u_opt.pidFilename);
+ initSignal();
+ updatePidFile(u_opt.pidFilename);
}
Please sign in to comment.
Something went wrong with that request. Please try again.