~ubuntu-branches/ubuntu/natty/ntop/natty

« back to all changes in this revision

Viewing changes to utils/lookuptest.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-01-30 21:59:13 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050130215913-xc3ke963bw49b3k4
Tags: 2:3.0-5
* Updated README.Debian file so users will understand what to do at
  install, closes: #291794, #287802.
* Updated ntop init script to give better output.
* Also changed log directory from /var/lib/ntop to /var/log/ntop,
  closes: #252352.
* Quoted the interface list to allow whitespace, closes: #267248.
* Added a couple of logcheck ignores, closes: #269321, #269319.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2003 Luca Deri <deri@ntop.org>
 
3
 *                     Andreas Pfaller <apfaller@yahoo.com.au>
 
4
 *
 
5
 *                        http://www.ntop.org/
 
6
 *
 
7
 *  This program is free software; you can redistribute it and/or modify
 
8
 *  it under the terms of the GNU General Public License as published by
 
9
 *  the Free Software Foundation; either version 2 of the License, or
 
10
 *  (at your option) any later version.
 
11
 *
 
12
 *  This program is distributed in the hope that it will be useful,
 
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 *  GNU General Public License for more details.
 
16
 *
 
17
 *  You should have received a copy of the GNU General Public License
 
18
 *  along with this program; if not, write to the Free Software
 
19
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
20
 */
 
21
 
 
22
#define VERSION "2.0"
 
23
 
 
24
#include <stdlib.h>
 
25
#include <stdio.h>
 
26
#include <string.h>
 
27
#include <ctype.h>
 
28
#include <getopt.h>
 
29
#include "p2clib.h"
 
30
 
 
31
typedef struct IPNode 
 
32
{
 
33
  struct IPNode *b[2];
 
34
  char cc[8];
 
35
} IPNode;
 
36
 
 
37
IPNode *Head;
 
38
 
 
39
char *OptTableFilename="./p2c.opt.table";
 
40
 
 
41
/* ************************************************************************* */
 
42
 
 
43
void addNodeInternal(u_int32_t ip, int prefix, char *country)
 
44
{
 
45
  IPNode *p1=Head;
 
46
  IPNode *p2;
 
47
  int i, b;
 
48
  
 
49
  for (i=0; i<prefix; i++) {
 
50
    b=(ip>>(31-i)) & 0x1;
 
51
    if (!p1->b[b]) {
 
52
      if (!(p2=malloc(sizeof(IPNode))))
 
53
        exit(1);
 
54
      memset(p2, 0, sizeof(IPNode));
 
55
      p1->b[b]=p2;
 
56
    }
 
57
    else
 
58
      p2=p1->b[b];
 
59
    
 
60
    p1=p2;
 
61
  }
 
62
  if (p2->cc[0]==0) {
 
63
    strncpy(p2->cc, country, sizeof(p2->cc));
 
64
    p2->cc[sizeof(p2->cc)-1]=0;
 
65
  }
 
66
}
 
67
 
 
68
/* ************************************************************************* */
 
69
 
 
70
void initIPCountryTable(void)
 
71
{
 
72
  FILE *fd;
 
73
  
 
74
  if ((Head=malloc(sizeof(IPNode)))==NULL)
 
75
    exit(1);
 
76
  strcpy(Head->cc, "***");
 
77
  Head->b[0]=NULL;
 
78
  Head->b[1]=NULL;  
 
79
 
 
80
  fd = fopen(OptTableFilename, "r");
 
81
  if (fd==NULL) {
 
82
    perror(OptTableFilename);
 
83
    exit(EXIT_FAILURE);
 
84
  }
 
85
 
 
86
  while (!feof(fd)) {
 
87
    char buff[256];
 
88
    char *strtokState, *cc, *ip, *prefix;
 
89
      
 
90
    if (fgets(buff, sizeof(buff), fd)==NULL)
 
91
      continue;
 
92
    if ((cc=strtok_r(buff, ":", &strtokState))==NULL)
 
93
      continue;
 
94
    if ((ip=strtok_r(NULL, "/", &strtokState))==NULL)
 
95
      continue;
 
96
    if ((prefix=strtok_r(NULL, "\n", &strtokState))==NULL)
 
97
      continue;
 
98
    
 
99
    addNodeInternal(xaton(ip), atoi(prefix), cc);
 
100
  }
 
101
  fclose(fd);
 
102
}
 
103
 
 
104
/* ************************************************************************* */
 
105
 
 
106
char *ip2CountryCode(u_int32_t ip)
 
107
{
 
108
  IPNode *p=Head;
 
109
  int i, b;
 
110
  char *cc="";
 
111
 
 
112
  i=0;
 
113
  while (p!=NULL) {
 
114
    if (p->cc[0]!=0)
 
115
    {
 
116
      cc=p->cc;
 
117
      printf("%s/%d ", cc, i);
 
118
    }
 
119
    b=(ip>>(31-i)) & 0x1;
 
120
    p=p->b[b];
 
121
    i++;
 
122
  }
 
123
  return cc;
 
124
}
 
125
 
 
126
/* ************************************************************************* */
 
127
 
 
128
void usage(void)
 
129
{
 
130
  fprintf(stderr, "lookuptest %s\n", VERSION);
 
131
  fprintf(stderr, "Usage: lookuptest [OPTION]... [IP]...\n");
 
132
  fprintf(stderr, "Lookup country codes for specified IPs.\n");
 
133
  fprintf(stderr, "If no IPs are specified on the commandline a list of IPs is read from stdin.\n\n");
 
134
  fprintf(stderr, "  -t fname  Read mapping table from fname\n\n");
 
135
  fprintf(stderr, "Example: ./lookuptest 19.203.239.24, returns:\n\n");
 
136
  fprintf(stderr, "19.203.239.24: ***/0 US/6 IL/29\n\n");
 
137
  fprintf(stderr, "  Which means the address, 19.203.239.24 is contained in the root block ***/0 (always).\n");
 
138
  fprintf(stderr, "  More specifically it is contained in a /6 listed as in the US and\n");
 
139
  fprintf(stderr, "  most specifically it is in a /29 which is listed as in IL (Israel).\n\n");
 
140
  exit(EXIT_FAILURE);
 
141
}
 
142
 
 
143
/* ************************************************************************* */
 
144
 
 
145
void parseOptions(int argc, char *argv[])
 
146
{
 
147
  int c;
 
148
 
 
149
  while ((c=getopt(argc, argv, "?ht:"))!=-1) {
 
150
    switch (c) {
 
151
      case 't':
 
152
        OptTableFilename=optarg;
 
153
        break;
 
154
      case '?':
 
155
      case 'h':
 
156
      default:
 
157
        usage();
 
158
        break;
 
159
    }
 
160
  }
 
161
}
 
162
 
 
163
/* ************************************************************************* */
 
164
 
 
165
int main(int argc, char *argv[])
 
166
{
 
167
  int i;
 
168
 
 
169
  parseOptions(argc, argv);
 
170
 
 
171
  initIPCountryTable();
 
172
 
 
173
  if (optind>=argc) {
 
174
    while (!feof(stdin)) {
 
175
      char buff[64];
 
176
      
 
177
      fgets(buff, sizeof(buff)-1, stdin);
 
178
      if (!feof(stdin)) {
 
179
        ip2CountryCode(xaton(buff));
 
180
        printf("\n");
 
181
      }
 
182
    }
 
183
  }
 
184
  else {
 
185
    for (i=optind; i<argc; i++) {
 
186
      printf("%s: ", argv[i]);
 
187
      ip2CountryCode(xaton(argv[i]));
 
188
      printf("\n");
 
189
    }
 
190
  }
 
191
  
 
192
  exit(EXIT_SUCCESS);
 
193
}
 
194