## utils/history/surf.c

/* [<][>][^][v][top][bottom][index][help] */

## DEFINITIONS

This source file includes following functions.

1. process_arguments
2. get_operation_name
3. get_object_type_name
4. surf
5. main

       1 /***************************************
       2   $Revision: 1.1 $
       3
       4   Surf.  surf.c - whois DB archive surfing.
       5
       6   Status: NOT REVIEWED, NOT TESTED, NOT COMPLETE
       7
       8   Implementation by: Tiago Antao
       9
      10   ******************/ /******************
      11   Copyright (c) 2002               RIPE NCC
      12
      13   All Rights Reserved
      14
      15   Permission to use, copy, modify, and distribute this software and its
      16   documentation for any purpose and without fee is hereby granted,
      17   provided that the above copyright notice appear in all copies and that
      18   both that copyright notice and this permission notice appear in
      19   supporting documentation, and that the name of the author not be
      20   used in advertising or publicity pertaining to distribution of the
      21   software without specific, written prior permission.
      22
      23   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
      24   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
      25   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
      26   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
      27   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
      28   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
      29   ***************************************/
      30
      31
      32 #include <stdio.h>
      33 #include <stdlib.h>
      34 #include <time.h>
      35 #include <rip.h>
      36 #include "miniconf.h"
      37 #include "dbsupport.h"
      38 #include "aconf.h"
      39
      40 char* pkey;
      41 int   instance = -1;
      42
      43 void process_arguments (int argv, char** argc) {
         /* [<][>][^][v][top][bottom][index][help] */
      44   if (argv != 2 && argv != 3) {
      45     printf("Usage: %s <pkey> [instance]\n", argc[0]);
      46     exit(-1);
      47   }
      48
      49   pkey = argc[1];
      50   if (argv==3) {
      51     instance = atoi(argc[2]);
      52   }
      53 }
      54
      55 /*
      56   get_operation_type_name: Given an op type in number return the name string.
      57 */
      58 void get_operation_name(char *operation_name, long operation) {
         /* [<][>][^][v][top][bottom][index][help] */
      59   switch (operation) {
      60   case 1:
      61     strcpy(operation_name, "ADD");
      62     return;
      63   case 2:
      64     strcpy(operation_name, "DEL");
      65     return;
      66   case 3:
      67     strcpy(operation_name, "DAD");
      68     return;
      69   case 4:
      70     strcpy(operation_name, "UPD");
      71     return;
      72   default:
      73     sprintf(operation_name, "%d", operation);
      74   }
      75 }
      76
      77 /*
      78   get_object_type_name: Given an object type in number return the name string.
      79 */
      80 void get_object_type_name(char *object_type_name, long object_type) {
         /* [<][>][^][v][top][bottom][index][help] */
      81   switch (object_type) {
      82   case 0:
      83     strcpy(object_type_name, "AS-BLOCK");
      84     return;
      85   case 1:
      86     strcpy(object_type_name, "AS-SET");
      87     return;
      88   case 2:
      89     strcpy(object_type_name, "AUT-NUM");
      90     return;
      91   case 3:
      92     strcpy(object_type_name, "DOMAIN");
      93     return;
      94   case 4:
      95     strcpy(object_type_name, "INET-RTR");
      96     return;
      97   case 5:
      98     strcpy(object_type_name, "INET6NUM");
      99     return;
     100   case 6:
     101     strcpy(object_type_name, "INETNUM");
     102     return;
     103   case 7:
     104     strcpy(object_type_name, "KEY-CERT");
     105     return;
     106   case 8:
     107     strcpy(object_type_name, "LIMERICK");
     108     return;
     109   case 9:
     110     strcpy(object_type_name, "MNTNER");
     111     return;
     112   case 10:
     113     strcpy(object_type_name, "PERSON");
     114     return;
     115   case 11:
     116     strcpy(object_type_name, "ROLE");
     117     return;
     118   case 12:
     119     strcpy(object_type_name, "ROUTE");
     120     return;
     121   case 13:
     122     strcpy(object_type_name, "ROUTE-SET");
     123     return;
     124   case 14:
     125     strcpy(object_type_name, "FILTER-SET");
     126     return;
     127   case 15:
     128     strcpy(object_type_name, "PEER-SET");
     129     return;
     130   default:
     131     sprintf(object_type_name, "%d", object_type);
     132   }
     133 }
     134
     135 /*
     136   surf: Surfs a pkey (which might include more than 1 object).
     137
     138
     139 */
     140 void surf() {
         /* [<][>][^][v][top][bottom][index][help] */
     141   SQ_result_set_t* rs;
     142   SQ_row_t*        row;
     143   char             query[300];
     144   int              cont=1;
     145   long             operation;
     146   char             operation_name[20];
     147   long             timestamp;
     148   char             type_name[20];
     149   long             type;
     150   long             current_operation;
     151
     152   sprintf(query,
     153           "  SELECT operation, timestamp, object_type, object "
     154           "    FROM archive "
     155           "   WHERE pkey LIKE '%s' "
     156           "ORDER BY timestamp ASC, operation DESC",
     157           pkey);
     158
     159   SQ_execute_query(archive_conn, query, &rs);
     160
     161   current_operation = 0;
     162   while ((row = SQ_row_next(rs)) != NULL) {
     163     if (SQ_get_column_int(rs, row, 0, &operation) == -1) {
     164       operation = 1;
     165     }
     166     SQ_get_column_int(rs, row, 1, &timestamp);
     167     SQ_get_column_int(rs, row, 2, &type);
     168
     169     if (operation == 1 && (current_operation == 1 || current_operation == 4)) {
     170       operation = 4;
     171     }
     172     current_operation = operation;
     173
     174     get_operation_name(operation_name, operation);
     175     get_object_type_name(type_name, type);
     176     if (instance<1) {
     177       printf("%5d - Op: %3s Type: %12s Time: %s\n", cont, operation_name,
     178              type_name, ctime(&timestamp));
     179     }
     180     else {
     181       if (instance == cont) {
     182         printf("%5d - Op: %3s Type: %12s Time: %s\n%s\n", cont, operation_name,
     183                type_name, ctime(&timestamp),
     184                SQ_get_column_string_nocopy(rs, row, 3));
     185       }
     186     }
     187     cont++;
     188   }
     189 }
     190
     191 /*
     192   main: Surf entry point.
     193
     194   Self-documenting.
     195 */
     196 int main (int argv, char** argc) {
         /* [<][>][^][v][top][bottom][index][help] */
     197   process_arguments(argv, argc);
     198   read_configuration();
     199   get_db_connections();
     200
     201   surf();
     202
     203   //close_dbs();
     204
     205   return 1;
     206 }

/* [<][>][^][v][top][bottom][index][help] */