## modules/ac/access_control.c

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

## FUNCTIONS

This source file includes following functions.

1. AC_init
2. AC_log
3. access_foreach
4. AC_hostinfo_to_string
5. AC_to_string

    /***************************************
      $Revision: 1.1 $

      Access control module (ac).

      Status: NOT REVUED, NOT TESTED

      ******************/ /******************
      Filename            : access_control.c
      Author              : ottrey@ripe.net
      OSs Tested          : Solaris
      ******************/ /******************
      Copyright (c) 1999                              RIPE NCC

      All Rights Reserved

      Permission to use, copy, modify, and distribute this software and its
      documentation for any purpose and without fee is hereby granted,
      provided that the above copyright notice appear in all copies and that
      both that copyright notice and this permission notice appear in
      supporting documentation, and that the name of the author not be
      used in advertising or publicity pertaining to distribution of the
      software without specific, written prior permission.

      THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
      ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
      AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
      DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
      AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
      OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
      ***************************************/
    #include <stdio.h>
    #include <glib.h>

    /*+ String sizes +*/
    #define STR_S   63
    #define STR_M   255
    #define STR_L   1023
    #define STR_XL  4095
    #define STR_XXL 16383

    /* Global Variables */
    GHashTable *Access_list;

    /* AC_init() */
    /*++++++++++++++++++++++++++++++++++++++
      Initialize the access list.

      More:
      +html+ <PRE>
      Authors:
            ottrey

      +html+ </PRE><DL COMPACT>
      +html+ <DT>Online References:
      +html+ <DD><UL><A HREF="http://www.gtk.org/rdp/glib/glib-hash-tables.html">GLib Hash Tables</A>
      +html+ </UL></DL>

      ++++++++++++++++++++++++++++++++++++++*/
    void AC_init() {
    /* [<][>][^][v][top][bottom][index][help] */
      printf("Initialising access list\n");
      Access_list = g_hash_table_new(g_str_hash, g_str_equal);

    } /* AC_init() */

    /* AC_log() */
    /*++++++++++++++++++++++++++++++++++++++
      Log a host making a query.

      More:
      +html+ <PRE>
      Authors:
            ottrey

      +html+ </PRE><DL COMPACT>
      +html+ <DT>Online References:
      +html+ <DD><UL><A HREF="http://www.gtk.org/rdp/glib/glib-hash-tables.html">GLib Hash Tables</A>
      +html+ </UL></DL>

      ++++++++++++++++++++++++++++++++++++++*/
    void AC_log(char *hostaddress) {
    /* [<][>][^][v][top][bottom][index][help] */
      int *queries_ptr=NULL;
      char *ha;

      printf("hostaddress=%s\n", hostaddress);

      queries_ptr = (int *)g_hash_table_lookup(Access_list, hostaddress);
      if ( queries_ptr != NULL ) {
        printf("found hostaddress\n");
        /* XXX Should do some error checking here? */
        (*queries_ptr)++;
      }
      else {
        printf("didn't find hostaddress\n");
        queries_ptr = (int *)malloc(sizeof(int));
        *queries_ptr = 1;
        /* Create a copy for the list. */
        ha = (char *)calloc(1, strlen(hostaddress)+1);
        strcpy(ha, hostaddress);
        /* Insert the number of queries into the Access List. */
        g_hash_table_insert(Access_list, ha, (void *)queries_ptr);
      }

    } /* AC_log() */

    /* access_foreach() */
    /*++++++++++++++++++++++++++++++++++++++
      Function to add string to the created string.
      It is called via g_hash_table_foreach().

      void *element_data The source name.

      void *result_buf_ptr The string to be populated.

      More:
      +html+ <PRE>
      Authors:
            ottrey

      +html+ </PRE><DL COMPACT>
      +html+ <DT>Online References:
      +html+ <DD><UL>
      +html+ </UL></DL>

      ++++++++++++++++++++++++++++++++++++++*/
    static void access_foreach(void *hostaddress_ptr, void *value_ptr, void *result_buf_ptr) {
    /* [<][>][^][v][top][bottom][index][help] */
      char *hostaddress = (char *)hostaddress_ptr;
      int *queries_ptr = (int *)value_ptr;
      char queries_str_buf[10];
      char *result_buf = (char *)result_buf_ptr;

      strcpy(queries_str_buf, "");
      sprintf(queries_str_buf, "%d", *queries_ptr);

      strcat(result_buf, "\t");
      strcat(result_buf, hostaddress);
      strcat(result_buf, "=");
      strcat(result_buf, queries_str_buf);
      strcat(result_buf, ",\n");

    } /* access_foreach() */

    /* AC_hostinfo_to_string() */
    /*++++++++++++++++++++++++++++++++++++++
      Return information about the host.

      char *hostname The host.

      More:
      +html+ <PRE>
      Authors:
            ottrey

      +html+ </PRE><DL COMPACT>
      +html+ <DT>Online References:
      +html+ <DD><UL><A HREF="http://www.gtk.org/rdp/glib/glib-hash-tables.html">GLib Hash Tables</A>
      +html+ </UL></DL>

      ++++++++++++++++++++++++++++++++++++++*/
    char *AC_hostinfo_to_string(char *hostname) {
    /* [<][>][^][v][top][bottom][index][help] */
      char *result=NULL;
      char result_buf[STR_XL];
      int *queries_ptr;

      strcpy(result_buf, "");

      queries_ptr = (int *)g_hash_table_lookup(Access_list, hostname);
      if (queries_ptr != NULL) {
        sprintf(result_buf, "query_count=%d", *queries_ptr);
      }
      else {
        sprintf(result_buf, "query_count=%d", 0);
      }

      result = (char *)calloc(1, strlen(result_buf)+1);
      strcpy(result, result_buf);

      return result;

    } /* AC_hostinfo_to_string() */

    /* AC_to_string() */
    /*++++++++++++++++++++++++++++++++++++++
      The access list.

      More:
      +html+ <PRE>
      Authors:
            ottrey

      +html+ </PRE><DL COMPACT>
      +html+ <DT>Online References:
      +html+ <DD><UL><A HREF="http://www.gtk.org/rdp/glib/glib-hash-tables.html">GLib Hash Tables</A>
      +html+ </UL></DL>

      ++++++++++++++++++++++++++++++++++++++*/
    char *AC_to_string() {
    /* [<][>][^][v][top][bottom][index][help] */
      char *result=NULL;
      char result_buf[STR_XL];
      int result_len;

      strcpy(result_buf, "");
      strcat(result_buf, "{\n");
      g_hash_table_foreach(Access_list, access_foreach, &result_buf);
      result_len = strlen(result_buf);
      if (result_len == 1) {
        /* If an empty set */
        result_buf[1] = '}';
        result_buf[2] = '\0';
      }
      else {
        result_buf[result_len-2] = '\n';
        result_buf[result_len-1] = '}';
        result_buf[result_len] = '\0';
      }

      result = (char *)calloc(1, result_len+1);
      strcpy(result, result_buf);

      return result;

    } /* AC_to_string() */

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