2 * Copyright (C) 1993-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
7 static const char sccsid[] = "@(#)ip_fil.c 2.41 6/5/96 (C) 1993-2000 Darren Reed";
8 static const char rcsid[] = "@(#)$Id: ipsyncm.c,v 1.4.2.4 2006/03/27 02:09:46 darrenr Exp $";
10 #include <sys/types.h>
12 #include <sys/socket.h>
14 #include <netinet/in.h>
17 #include <arpa/inet.h>
27 #include "netinet/ip_compat.h"
28 #include "netinet/ip_fil.h"
29 #include "netinet/ip_nat.h"
30 #include "netinet/ip_state.h"
31 #include "netinet/ip_sync.h"
34 int main __P((int, char *[]));
35 void usage __P((const char *));
39 void usage(const char *progname) {
40 fprintf(stderr, "Usage: %s <destination IP> <destination port>\n", progname);
44 static void handleterm(int sig)
51 /* should be large enough to hold header + any datatype */
52 #define BUFFERLEN 1400
58 struct sockaddr_in sin;
62 int nfd = -1, lfd = -1, n1, n2, n3, len;
68 progname = strrchr(argv[0], '/');
82 signal(SIGHUP, handleterm);
83 signal(SIGINT, handleterm);
84 signal(SIGTERM, handleterm);
87 openlog(progname, LOG_PID, LOG_SECURITY);
89 bzero((char *)&sin, sizeof(sin));
90 sin.sin_family = AF_INET;
91 sin.sin_addr.s_addr = inet_addr(argv[1]);
93 sin.sin_port = htons(atoi(argv[2]));
95 sin.sin_port = htons(43434);
104 lfd = open(IPSYNC_NAME, O_RDONLY);
106 syslog(LOG_ERR, "Opening %s :%m", IPSYNC_NAME);
110 nfd = socket(AF_INET, SOCK_DGRAM, 0);
112 syslog(LOG_ERR, "Socket :%m");
116 if (connect(nfd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
117 syslog(LOG_ERR, "Connect: %m");
121 syslog(LOG_INFO, "Sending data to %s",
122 inet_ntoa(sin.sin_addr));
127 n1 = read(lfd, buff+inbuf, BUFFERLEN-inbuf);
129 printf("header : %d bytes read (header = %d bytes)\n",
133 syslog(LOG_ERR, "Read error (header): %m");
138 /* XXX can this happen??? */
140 "Read error (header) : No data");
148 if (inbuf < sizeof(*sh)) {
149 continue; /* need more data */
152 sh = (synchdr_t *)buff;
153 len = ntohl(sh->sm_len);
154 magic = ntohl(sh->sm_magic);
156 if (magic != SYNHDRMAGIC) {
158 "Invalid header magic %x", magic);
164 printf("v:%d p:%d len:%d magic:%x", sh->sm_v,
165 sh->sm_p, len, magic);
167 if (sh->sm_cmd == SMC_CREATE)
168 printf(" cmd:CREATE");
169 else if (sh->sm_cmd == SMC_UPDATE)
170 printf(" cmd:UPDATE");
172 printf(" cmd:Unknown(%d)", sh->sm_cmd);
174 if (sh->sm_table == SMC_NAT)
175 printf(" table:NAT");
176 else if (sh->sm_table == SMC_STATE)
177 printf(" table:STATE");
179 printf(" table:Unknown(%d)", sh->sm_table);
181 printf(" num:%d\n", (u_32_t)ntohl(sh->sm_num));
184 if (inbuf < sizeof(*sh) + len) {
185 continue; /* need more data */
190 if (sh->sm_cmd == SMC_CREATE) {
191 sl = (synclogent_t *)buff;
193 } else if (sh->sm_cmd == SMC_UPDATE) {
194 su = (syncupdent_t *)buff;
195 if (sh->sm_p == IPPROTO_TCP) {
196 printf(" TCP Update: age %lu state %d/%d\n",
198 su->sup_tcp.stu_state[0],
199 su->sup_tcp.stu_state[1]);
202 printf("Unknown command\n");
206 n2 = sizeof(*sh) + len;
207 n3 = write(nfd, buff, n2);
209 syslog(LOG_ERR, "Write error: %m");
215 syslog(LOG_ERR, "Incomplete write (%d/%d)",
220 /* signal received? */
224 /* move buffer to the front,we might need to make
225 * this more efficient, by using a rolling pointer
226 * over the buffer and only copying it, when
227 * we are reaching the end
231 bcopy(buff+n2, buff, inbuf);
232 printf("More data in buffer\n");
250 syslog(LOG_ERR, "signal %d received, exiting...", terminate);