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
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>
37 #include "net-support.h"
38 #include "pathnames.h"
42 #include "net-features.h"
44 #if HAVE_FW_MASQUERADE
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
);
74 printf("%10lu %5hd %5hd ", ms
->initseq
,
75 ms
->delta
, ms
->pdelta
);
77 printf("%10lu %5hd - ", ms
->initseq
,
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
)
95 for (nread
= 0; nread
< nmslist
; nread
++) {
98 if ((n
= fscanf(f
, " %s %lX:%hX %lX:%hX %hX %lX %hd %hd %lu",
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)
106 if ((n
= fscanf(f
, " %s %lX:%hX %lX:%hX %hX %lX %hd %lu",
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)
114 if ((has_pdelta
&& (n
!= 10)) || (!has_pdelta
&& (n
!= 9))) {
115 EINTERN("masq_info.c", "ip_masquerade format error");
118 ms
->src
.sin_family
= AF_INET
;
119 ms
->dst
.sin_family
= AF_INET
;
121 if (strcmp("TCP", buf
) == 0)
123 else if (strcmp("UDP", buf
) == 0)
125 else if (strcmp("ICMP", buf
) == 0)
127 else if (strcmp("GRE", buf
) == 0)
129 else if (strcmp("ESP", buf
) == 0)
132 EINTERN("masq_info.c", "ip_masquerade unknown type");
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
);
147 int ip_masq_info(int numeric
, int ext
)
153 int ntotal
= 0, nread
;
155 if (!(f
= fopen(_PATH_PROCNET_IP_MASQ
, "r"))) {
156 if (errno
!= ENOENT
) {
157 perror(_PATH_PROCNET_IP_MASQ
);
160 ESYSNOT("netstat", "ip_masquerade");
163 if ((ap
= get_aftype("inet")) == NULL
) {
164 ENOSUPP("masq_info", "AF INET");
168 fgets(buf
, sizeof(buf
), f
);
169 has_pdelta
= strstr(buf
, "PDelta") ? 1 : 0;
171 mslist
= (struct masq
*) malloc(16 * sizeof(struct masq
));
173 EINTERN("masq_info", "malloc() failed");
177 while ((nread
= read_masqinfo(f
, &(mslist
[ntotal
]), 16)) == 16) {
179 mslist
= (struct masq
*) realloc(mslist
,
180 (ntotal
+ 16) * sizeof(struct masq
));
182 EINTERN("masq_info", "realloc() failed");
197 printf(_("IP masquerading entries\n"));
200 printf(_("prot expire source destination ports\n"));
203 printf(_("prot expire initseq delta prevd source destination ports\n"));
206 for (i
= 0; i
< ntotal
; i
++)
207 print_masq(&(mslist
[i
]), numeric
, ext
);