OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [userland/] [route/] [lib/] [masq_info.c] - Blame information for rev 1773

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 745 simons
/*
2
 * lib/masq_info.c    This file contains a the functio masq_info
3
 *                      to print a table of current masquerade connections.
4
 *
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)
8
 *
9
 * Version:     $Id: masq_info.c,v 1.1 2002-03-17 19:58:53 simons Exp $
10
 *
11
 * Author:      Bernd 'eckes' Eckenfels <net-tools@lina.inka.de>
12
 *              Copyright 1999 Bernd Eckenfels, Germany
13
 *
14
 * Modifications:
15
 *
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
19
 *
20
 *980405 {0.03} Arnaldo Carvalho:       i18n CATGETS -> gettext
21
 *
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.
27
 */
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"
43
 
44
#if HAVE_FW_MASQUERADE
45
 
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 */
55
};
56
 
57
static struct aftype *ap;       /* current address family       */
58
static int has_pdelta;
59
 
60
static void print_masq(struct masq *ms, int numeric, int ext)
61
{
62
    unsigned long minutes, seconds, sec100s;
63
 
64
    printf("%-4s", ms->proto);
65
 
66
    sec100s = ms->expires % 100L;
67
    seconds = (ms->expires / 100L) % 60;
68
    minutes = ms->expires / 6000L;
69
 
70
    printf("%3ld:%02ld.%02ld ", minutes, seconds, sec100s);
71
 
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);
79
    }
80
    printf("%-20s ", ap->sprint((struct sockaddr *) &(ms->src), numeric));
81
    printf("%-20s ", ap->sprint((struct sockaddr *) &(ms->dst), numeric));
82
 
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));
86
}
87
 
88
 
89
static int read_masqinfo(FILE * f, struct masq *mslist, int nmslist)
90
{
91
    int n, nread = 0;
92
    struct masq *ms;
93
    char buf[256];
94
 
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;
113
        }
114
        if ((has_pdelta && (n != 10)) || (!has_pdelta && (n != 9))) {
115
            EINTERN("masq_info.c", "ip_masquerade format error");
116
            return (-1);
117
        }
118
        ms->src.sin_family = AF_INET;
119
        ms->dst.sin_family = AF_INET;
120
 
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 {
128
            EINTERN("masq_info.c", "ip_masquerade unknown type");
129
            return (-1);
130
        }
131
 
132
        /* we always keep these addresses in network byte order */
133
        ms->src.sin_addr.s_addr = htonl(ms->src.sin_addr.s_addr);
134
        ms->dst.sin_addr.s_addr = htonl(ms->dst.sin_addr.s_addr);
135
        ms->sport = htons(ms->sport);
136
        ms->dport = htons(ms->dport);
137
        ms->mport = htons(ms->mport);
138
    }
139
    return nread;
140
}
141
 
142
 
143
int ip_masq_info(int numeric, int ext)
144
{
145
    FILE *f;
146
    int i;
147
    char buf[256];
148
    struct masq *mslist;
149
    int ntotal = 0, nread;
150
 
151
    if (!(f = fopen(_PATH_PROCNET_IP_MASQ, "r"))) {
152
        if (errno != ENOENT) {
153
            perror(_PATH_PROCNET_IP_MASQ);
154
            return (-1);
155
        }
156
        ESYSNOT("netstat", "ip_masquerade");
157
        return (1);
158
    }
159
    if ((ap = get_aftype("inet")) == NULL) {
160
        ENOSUPP("masq_info", "AF INET");
161
        fclose(f);
162
        return (-1);
163
    }
164
    fgets(buf, sizeof(buf), f);
165
    has_pdelta = strstr(buf, "PDelta") ? 1 : 0;
166
 
167
    mslist = (struct masq *) malloc(16 * sizeof(struct masq));
168
    if (!mslist) {
169
        EINTERN("masq_info", "malloc() failed");
170
        fclose(f);
171
        return (-1);
172
    }
173
    while ((nread = read_masqinfo(f, &(mslist[ntotal]), 16)) == 16) {
174
        ntotal += nread;
175
        mslist = (struct masq *) realloc(mslist,
176
                                    (ntotal + 16) * sizeof(struct masq));
177
        if (!mslist) {
178
            EINTERN("masq_info", "realloc() failed");
179
            fclose(f);
180
            return (-1);
181
        }
182
    }
183
    fclose(f);
184
 
185
    if (nread < 0) {
186
        if (mslist)
187
            free(mslist);
188
        return (-1);
189
    }
190
    ntotal += nread;
191
 
192
    if (ntotal > 0) {
193
        printf(_("IP masquerading entries\n"));
194
        switch (ext) {
195
        case 1:
196
            printf(_("prot   expire source               destination          ports\n"));
197
            break;
198
        default:
199
            printf(_("prot   expire    initseq delta prevd source               destination          ports\n"));
200
            break;
201
        }
202
        for (i = 0; i < ntotal; i++)
203
            print_masq(&(mslist[i]), numeric, ext);
204
        if (mslist)
205
            free(mslist);
206
 
207
    }
208
    return 0;
209
}
210
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.