Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / ipf / tools / ipsyncm.c
blob6bae3abb41c07b3223564a602a93e2e589a84293
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2001-2006 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
7 */
8 #if !defined(lint)
9 static const char sccsid[] = "@(#)ip_fil.c 2.41 6/5/96 (C) 1993-2000 Darren Reed";
10 static const char rcsid[] = "@(#)Id: ipsyncm.c,v 1.4.2.6 2009/03/29 01:17:53 darrenr Exp";
11 #endif
12 #include <sys/types.h>
13 #include <sys/time.h>
14 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <net/if.h>
19 #include <arpa/inet.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <syslog.h>
27 #include <signal.h>
29 #include "netinet/ip_compat.h"
30 #include "netinet/ip_fil.h"
31 #include "netinet/ip_nat.h"
32 #include "netinet/ip_state.h"
33 #include "netinet/ip_sync.h"
36 int main __P((int, char *[]));
37 void usage __P((const char *));
39 int terminate = 0;
41 void usage(const char *progname) {
42 fprintf(stderr, "Usage: %s <destination IP> <destination port>\n", progname);
45 #if 0
46 static void handleterm(int sig)
48 terminate = sig;
50 #endif
53 /* should be large enough to hold header + any datatype */
54 #define BUFFERLEN 1400
56 int main(argc, argv)
57 int argc;
58 char *argv[];
60 struct sockaddr_in sin;
61 char buff[BUFFERLEN];
62 synclogent_t *sl;
63 syncupdent_t *su;
64 int nfd = -1, lfd = -1, n1, n2, n3, len;
65 int inbuf;
66 u_32_t magic;
67 synchdr_t *sh;
68 char *progname;
70 progname = strrchr(argv[0], '/');
71 if (progname) {
72 progname++;
73 } else {
74 progname = argv[0];
78 if (argc < 2) {
79 usage(progname);
80 exit(1);
83 #if 0
84 signal(SIGHUP, handleterm);
85 signal(SIGINT, handleterm);
86 signal(SIGTERM, handleterm);
87 #endif
89 openlog(progname, LOG_PID, LOG_SECURITY);
91 bzero((char *)&sin, sizeof(sin));
92 sin.sin_family = AF_INET;
93 sin.sin_addr.s_addr = inet_addr(argv[1]);
94 if (argc > 2)
95 sin.sin_port = htons(atoi(argv[2]));
96 else
97 sin.sin_port = htons(43434);
99 while (1) {
101 if (lfd != -1)
102 close(lfd);
103 if (nfd != -1)
104 close(nfd);
106 lfd = open(IPSYNC_NAME, O_RDONLY);
107 if (lfd == -1) {
108 syslog(LOG_ERR, "Opening %s :%m", IPSYNC_NAME);
109 goto tryagain;
112 nfd = socket(AF_INET, SOCK_DGRAM, 0);
113 if (nfd == -1) {
114 syslog(LOG_ERR, "Socket :%m");
115 goto tryagain;
118 if (connect(nfd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
119 syslog(LOG_ERR, "Connect: %m");
120 goto tryagain;
123 syslog(LOG_INFO, "Sending data to %s",
124 inet_ntoa(sin.sin_addr));
126 inbuf = 0;
127 while (1) {
129 n1 = read(lfd, buff+inbuf, BUFFERLEN-inbuf);
131 printf("header : %d bytes read (header = %d bytes)\n",
132 n1, (int) sizeof(*sh));
134 if (n1 < 0) {
135 syslog(LOG_ERR, "Read error (header): %m");
136 goto tryagain;
139 if (n1 == 0) {
140 /* XXX can this happen??? */
141 syslog(LOG_ERR,
142 "Read error (header) : No data");
143 sleep(1);
144 continue;
147 inbuf += n1;
149 moreinbuf:
150 if (inbuf < sizeof(*sh)) {
151 continue; /* need more data */
154 sh = (synchdr_t *)buff;
155 len = ntohl(sh->sm_len);
156 magic = ntohl(sh->sm_magic);
158 if (magic != SYNHDRMAGIC) {
159 syslog(LOG_ERR,
160 "Invalid header magic %x", magic);
161 goto tryagain;
164 #define IPSYNC_DEBUG
165 #ifdef IPSYNC_DEBUG
166 printf("v:%d p:%d len:%d magic:%x", sh->sm_v,
167 sh->sm_p, len, magic);
169 if (sh->sm_cmd == SMC_CREATE)
170 printf(" cmd:CREATE");
171 else if (sh->sm_cmd == SMC_UPDATE)
172 printf(" cmd:UPDATE");
173 else
174 printf(" cmd:Unknown(%d)", sh->sm_cmd);
176 if (sh->sm_table == SMC_NAT)
177 printf(" table:NAT");
178 else if (sh->sm_table == SMC_STATE)
179 printf(" table:STATE");
180 else
181 printf(" table:Unknown(%d)", sh->sm_table);
183 printf(" num:%d\n", (u_32_t)ntohl(sh->sm_num));
184 #endif
186 if (inbuf < sizeof(*sh) + len) {
187 continue; /* need more data */
188 goto tryagain;
191 #ifdef IPSYNC_DEBUG
192 if (sh->sm_cmd == SMC_CREATE) {
193 sl = (synclogent_t *)buff;
195 } else if (sh->sm_cmd == SMC_UPDATE) {
196 su = (syncupdent_t *)buff;
197 if (sh->sm_p == IPPROTO_TCP) {
198 printf(" TCP Update: age %lu state %d/%d\n",
199 su->sup_tcp.stu_age,
200 su->sup_tcp.stu_state[0],
201 su->sup_tcp.stu_state[1]);
203 } else {
204 printf("Unknown command\n");
206 #endif
208 n2 = sizeof(*sh) + len;
209 n3 = write(nfd, buff, n2);
210 if (n3 <= 0) {
211 syslog(LOG_ERR, "Write error: %m");
212 goto tryagain;
216 if (n3 != n2) {
217 syslog(LOG_ERR, "Incomplete write (%d/%d)",
218 n3, n2);
219 goto tryagain;
222 /* signal received? */
223 if (terminate)
224 break;
226 /* move buffer to the front,we might need to make
227 * this more efficient, by using a rolling pointer
228 * over the buffer and only copying it, when
229 * we are reaching the end
231 inbuf -= n2;
232 if (inbuf) {
233 bcopy(buff+n2, buff, inbuf);
234 printf("More data in buffer\n");
235 goto moreinbuf;
239 if (terminate)
240 break;
241 tryagain:
242 sleep(1);
246 /* terminate */
247 if (lfd != -1)
248 close(lfd);
249 if (nfd != -1)
250 close(nfd);
252 syslog(LOG_ERR, "signal %d received, exiting...", terminate);
254 exit(1);