File: inetnum2countryalloc.c

package info (click to toggle)
ntop 3%3A5.0.1%2Bdfsg1-2.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 15,720 kB
  • ctags: 11,480
  • sloc: ansic: 79,804; sh: 21,658; python: 1,948; awk: 1,504; perl: 971; makefile: 745; php: 123; xml: 71; sql: 13; sed: 11
file content (110 lines) | stat: -rw-r--r-- 3,187 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 *  Copyright (C) 2003 Luca Deri <deri@ntop.org>
 *                     Andreas Pfaller <apfaller@yahoo.com.au>
 *
 *                        http://www.ntop.org/
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include "p2clib.h"

#define VERSION "2.0"

#define FLAG_HAVE_INETNUM 1
#define FLAG_HAVE_COUNTRY 2

/* ************************************************************************* */

void convert2Table(void)
{
  char buff[256];
  int flags=0;
  char ip1s[32], ip2s[32], country[4];
  uint32_t ip1, ip2;
  
  while (!feof(stdin)) {
    if (fgets(buff, sizeof(buff), stdin)==NULL)
      continue;

    if ((2==sscanf(buff, "inetnum:%*[ ] %31[0-9.] - %31[0-9.]", ip1s, ip2s)) ||
        (2==sscanf(buff, "*in: %31[0-9.] - %31[0-9.]", ip1s, ip2s))) {
      ip1=xaton(ip1s);
      ip2=xaton(ip2s);
      if (ip2>ip1)
        flags=FLAG_HAVE_INETNUM;
    }
    else if (1==sscanf(buff, "country: %3s", country))
      flags|=FLAG_HAVE_COUNTRY;
    else if (1==sscanf(buff, "*cy: %3s", country))
      flags|=FLAG_HAVE_COUNTRY;
    else if (strstr(buff, "not allocated to APNIC") ||
             strstr(buff, "Not allocated by APNIC") ||
             strstr(buff, "Early registration addresses"))
      flags=0;
    else if (buff[0]=='\n')
      flags=0;

    if (flags==(FLAG_HAVE_INETNUM | FLAG_HAVE_COUNTRY)) {
      printf("apf|%s|ipv4|%s|%u|x|x\n", country, ip1s, ip2-ip1+1);
      flags=0;
    }
  }
}

/* ************************************************************************* */

void usage(void)
{
  fprintf(stderr, "inetnum2countryalloc %s\n", VERSION);
  fprintf(stderr, "Usage: inetnum2countryalloc\n");
  fprintf(stderr, "Convert whois address file (e.g ripe.db.inetnum.gz) to standard\n");
  fprintf(stderr, "format which can be processed by prefixtablegen\n\n");
  fprintf(stderr, "Example:\n");
  fprintf(stderr, "  zcat ripe.db.inetnum.gz | ./inetnum2countryalloc >ripe.inetnum.data\n");
  exit(EXIT_FAILURE);
}

/* ************************************************************************* */

void parseOptions(int argc, char *argv[])
{
  int c;

  while ((c=getopt(argc, argv, "?h"))!=-1) {
    switch (c) {
      case '?':
      case 'h':
      default:
        usage();
        break;
    }
  }
}
/* ************************************************************************* */

int main(int argc, char *argv[]) 
{
  parseOptions(argc, argv);
  
  convert2Table();
  exit (EXIT_SUCCESS);
}