Bump version to 1.53
[oss-qm-packages.git] / lib / masq_info.c
blob41ad691943292168033e7741603d8974d435a06e
1 /*
2 * lib/masq_info.c This file contains a the functio masq_info
3 * to print a table of current masquerade connections.
5 * NET-LIB A collection of functions used from the base set of the
6 * NET-3 Networking Distribution for the LINUX operating
7 * system. (net-tools, net-drivers)
9 * Version: $Id: masq_info.c,v 1.6 1999/06/12 23:04:19 philip Exp $
11 * Author: Bernd 'eckes' Eckenfels <net-tools@lina.inka.de>
12 * Copyright 1999 Bernd Eckenfels, Germany
14 * Modifications:
16 *960217 {0.01} Bernd Eckenfels: creatin from the code of
17 * Jos Vos' ipfwadm 2.0beta1
18 *950218 {0.02} Bernd Eckenfels: <linux/if.h> added
20 *980405 {0.03} Arnaldo Carvalho: i18n CATGETS -> gettext
22 * This program is free software; you can redistribute it
23 * and/or modify it under the terms of the GNU General
24 * Public License as published by the Free Software
25 * Foundation; either version 2 of the License, or (at
26 * your option) any later version.
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <errno.h>
33 #include <stdio.h>
34 #include <malloc.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include "net-support.h"
38 #include "pathnames.h"
39 #include "version.h"
40 #include "config.h"
41 #include "intl.h"
42 #include "net-features.h"
44 #if HAVE_FW_MASQUERADE
46 struct masq {
47 unsigned long expires; /* Expiration timer */
48 char *proto; /* Which protocol are we talking? */
49 struct sockaddr_in src, dst; /* Source and destination IP addresses */
50 unsigned short sport, dport; /* Source and destination ports */
51 unsigned short mport; /* Masqueraded port */
52 unsigned long initseq; /* Add delta from this seq. on */
53 short delta; /* Delta in sequence numbers */
54 short pdelta; /* Delta in sequence numbers before last */
57 static struct aftype *ap; /* current address family */
58 static int has_pdelta;
60 static void print_masq(struct masq *ms, int numeric, int ext)
62 unsigned long minutes, seconds, sec100s;
64 printf("%-4s", ms->proto);
66 sec100s = ms->expires % 100L;
67 seconds = (ms->expires / 100L) % 60;
68 minutes = ms->expires / 6000L;
70 printf("%3ld:%02ld.%02ld ", minutes, seconds, sec100s);
72 if (ext > 1) {
73 if (has_pdelta)
74 printf("%10lu %5hd %5hd ", ms->initseq,
75 ms->delta, ms->pdelta);
76 else
77 printf("%10lu %5hd - ", ms->initseq,
78 ms->delta);
80 printf("%-20s ", ap->sprint((struct sockaddr *) &(ms->src), numeric));
81 printf("%-20s ", ap->sprint((struct sockaddr *) &(ms->dst), numeric));
83 printf("%s -> ", get_sname(ms->sport, ms->proto, numeric));
84 printf("%s", get_sname(ms->dport, ms->proto, numeric));
85 printf(" (%s)\n", get_sname(ms->mport, ms->proto, numeric));
89 static int read_masqinfo(FILE * f, struct masq *mslist, int nmslist)
91 int n, nread = 0;
92 struct masq *ms;
93 char buf[256];
95 for (nread = 0; nread < nmslist; nread++) {
96 ms = &mslist[nread];
97 if (has_pdelta) {
98 if ((n = fscanf(f, " %s %lX:%hX %lX:%hX %hX %lX %hd %hd %lu",
99 buf,
100 (unsigned long *) &ms->src.sin_addr.s_addr, &ms->sport,
101 (unsigned long *) &ms->dst.sin_addr.s_addr, &ms->dport,
102 &ms->mport, &ms->initseq, &ms->delta,
103 &ms->pdelta, &ms->expires)) == -1)
104 return nread;
105 } else {
106 if ((n = fscanf(f, " %s %lX:%hX %lX:%hX %hX %lX %hd %lu",
107 buf,
108 (unsigned long *) &ms->src.sin_addr.s_addr, &ms->sport,
109 (unsigned long *) &ms->dst.sin_addr.s_addr, &ms->dport,
110 &ms->mport, &ms->initseq, &ms->delta,
111 &ms->expires)) == -1)
112 return nread;
114 if ((has_pdelta && (n != 10)) || (!has_pdelta && (n != 9))) {
115 EINTERN("masq_info.c", "ip_masquerade format error");
116 return (-1);
118 ms->src.sin_family = AF_INET;
119 ms->dst.sin_family = AF_INET;
121 if (strcmp("TCP", buf) == 0)
122 ms->proto = "tcp";
123 else if (strcmp("UDP", buf) == 0)
124 ms->proto = "udp";
125 else if (strcmp("ICMP", buf) == 0)
126 ms->proto = "icmp";
127 else if (strcmp("GRE", buf) == 0)
128 ms->proto = "gre";
129 else if (strcmp("ESP", buf) == 0)
130 ms->proto = "esp";
131 else {
132 EINTERN("masq_info.c", "ip_masquerade unknown type");
133 return (-1);
136 /* we always keep these addresses in network byte order */
137 ms->src.sin_addr.s_addr = htonl(ms->src.sin_addr.s_addr);
138 ms->dst.sin_addr.s_addr = htonl(ms->dst.sin_addr.s_addr);
139 ms->sport = htons(ms->sport);
140 ms->dport = htons(ms->dport);
141 ms->mport = htons(ms->mport);
143 return nread;
147 int ip_masq_info(int numeric, int ext)
149 FILE *f;
150 int i;
151 char buf[256];
152 struct masq *mslist;
153 int ntotal = 0, nread;
155 if (!(f = fopen(_PATH_PROCNET_IP_MASQ, "r"))) {
156 if (errno != ENOENT) {
157 perror(_PATH_PROCNET_IP_MASQ);
158 return (-1);
160 ESYSNOT("netstat", "ip_masquerade");
161 return (1);
163 if ((ap = get_aftype("inet")) == NULL) {
164 ENOSUPP("masq_info", "AF INET");
165 fclose(f);
166 return (-1);
168 fgets(buf, sizeof(buf), f);
169 has_pdelta = strstr(buf, "PDelta") ? 1 : 0;
171 mslist = (struct masq *) malloc(16 * sizeof(struct masq));
172 if (!mslist) {
173 EINTERN("masq_info", "malloc() failed");
174 fclose(f);
175 return (-1);
177 while ((nread = read_masqinfo(f, &(mslist[ntotal]), 16)) == 16) {
178 ntotal += nread;
179 mslist = (struct masq *) realloc(mslist,
180 (ntotal + 16) * sizeof(struct masq));
181 if (!mslist) {
182 EINTERN("masq_info", "realloc() failed");
183 fclose(f);
184 return (-1);
187 fclose(f);
189 if (nread < 0) {
190 if (mslist)
191 free(mslist);
192 return (-1);
194 ntotal += nread;
196 if (ntotal > 0) {
197 printf(_("IP masquerading entries\n"));
198 switch (ext) {
199 case 1:
200 printf(_("prot expire source destination ports\n"));
201 break;
202 default:
203 printf(_("prot expire initseq delta prevd source destination ports\n"));
204 break;
206 for (i = 0; i < ntotal; i++)
207 print_masq(&(mslist[i]), numeric, ext);
208 if (mslist)
209 free(mslist);
212 return 0;
214 #endif