[cvs] / netsukuku / src / andna_cache.h Repository:
ViewVC logotype

View of /netsukuku/src/andna_cache.h

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.33 - (download) (as text) (annotate)
Wed Jan 31 14:18:16 2007 UTC (3 years, 7 months ago) by alpt
Branch: MAIN
CVS Tags: HEAD
Changes since 1.32: +18 -10 lines
* file HACKING cleaned
* map_pack() coded
* map_lid_add(), map_lid_del() coded
    1 /* This file is part of Netsukuku
    2  * (c) Copyright 2005 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
    3  *
    4  * This source code is free software; you can redistribute it and/or
    5  * modify it under the terms of the GNU General Public License as published
    6  * by the Free Software Foundation; either version 2 of the License,
    7  * or (at your option) any later version.
    8  *
    9  * This source code is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   12  * Please refer to the GNU Public License for more details.
   13  *
   14  * You should have received a copy of the GNU Public License along with
   15  * this source code; if not, write to:
   16  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   17  */
   18 
   19 #ifndef ANDNA_CACHE_H
   20 #define ANDNA_CACHE_H
   21 
   22 #include "inet.h"
   23 #include "crypto.h"
   24 #include "endianness.h"
   25 #include "llist.c"
   26 #include "snsd_cache.h"
   27 
   28 /*\
   29  *
   30  * ANDNA definitions
   31  *
   32 \*/
   33 
   34 #define ANDNA_MAX_BACKUP_GNODES   2
   35 #define ANDNA_MAX_QUEUE     5
   36 #define ANDNA_MAX_HNAME_LEN   512 /* (null terminator included) */
   37 #define ANDNA_MAX_HOSTNAMES   256 /* Max number of hnames per node */
   38 #define ANDNA_MAX_RHC_HNAMES    512 /* Max number of hnames kept in
   39                the resolved_hnames cache* */
   40 #define ANDNA_EXPIRATION_TIME   2592000 /* 30 days (in seconds)*/
   41 #define ANDNA_UPDATE_TIME   129600  /* 1.5 days */
   42 #define ANDNA_MIN_UPDATE_TIME   3600  /* The minum amount of time to
   43                be waited before sending an
   44                update of the hname. */
   45 
   46 #define ANDNA_PRIVKEY_BITS    1024
   47 #define ANDNA_SKEY_MAX_LEN    900
   48 #define ANDNA_PKEY_LEN      140
   49 #define ANDNA_HASH_SZ     (MAX_IP_SZ)
   50 #define ANDNA_SIGNATURE_LEN   128
   51 
   52 /* Returns the number of nodes to be used in a backup_gnode */
   53 #define ANDNA_BACKUP_NODES(seeds) ({(seeds) > 8 ?       \
   54             ((seeds)*32)/MAXGROUPNODE : (seeds);})
   55 
   56 #ifdef DEBUG
   57   #undef ANDNA_EXPIRATION_TIME
   58   #define ANDNA_EXPIRATION_TIME 100
   59   #undef ANDNA_UPDATE_TIME
   60   #define ANDNA_UPDATE_TIME 30
   61   #undef ANDNA_MIN_UPDATE_TIME
   62   #define ANDNA_MIN_UPDATE_TIME 2
   63 #endif
   64 
   65 /*\
   66  *
   67  * * *  Cache stuff  * * *
   68  *
   69 \*/
   70 
   71 /* * andna_cache flags * */
   72 #define ANDNA_BACKUP    1   /* We are a backup_node */
   73 #define ANDNA_COUNTER   (1<<1)    /* We are a counter_node */
   74 #define ANDNA_ROUNDED   (1<<2)    /* We are a rounded_hash_node */
   75 #define ANDNA_FULL    (1<<3)    /* Queue full */
   76 #define ANDNA_UPDATING    (1<<4)    /* The hname is being updated
   77                right now */
   78 
   79 /*
   80  * andna_cache_queue
   81  *
   82  * The queue of the andna_cache. (see below).
   83  */
   84 struct andna_cache_queue
   85 {
   86   LLIST_HDR (struct andna_cache_queue);
   87 
   88   time_t    timestamp;
   89   u_short   hname_updates;    /* numbers of hname's updates */
   90   char    pubkey[ANDNA_PKEY_LEN];
   91 
   92   u_short   snsd_counter;   /* # of `snsd' nodes */
   93   snsd_service  *service;
   94 };
   95 typedef struct andna_cache_queue andna_cache_queue;
   96 
   97 /*
   98  * andna_cache
   99  *
  100  * It keeps the entries of the hostnames registered by other nodes.
  101  */
  102 struct andna_cache
  103 {
  104   LLIST_HDR (struct andna_cache);
  105 
  106   u_int     hash[MAX_IP_INT]; /* hostname's hash */
  107   char    flags;
  108 
  109   u_short   queue_counter;
  110   andna_cache_queue *acq;     /* The queue of the registration.
  111                The first is the active one */
  112 };
  113 typedef struct andna_cache andna_cache;
  114 
  115 /* part of the counter cache, see below */
  116 struct counter_c_hashes
  117 {
  118   LLIST_HDR (struct counter_c_hashes);
  119 
  120   time_t    timestamp;
  121   u_short   hname_updates;
  122   int   hash[MAX_IP_INT];
  123 };
  124 typedef struct counter_c_hashes counter_c_hashes;
  125 INT_INFO counter_c_hashes_body_iinfo = { 2,
  126            { INT_TYPE_32BIT, INT_TYPE_16BIT },
  127            { 0, sizeof(time_t) },
  128            { 1, 1 }
  129                };
  130 
  131 /*
  132  * counter_c
  133  * Counter node's cache.
  134  *
  135  * All the infos regarding a particular register_node are stored here. For
  136  * example, we need to know how many hostnames he already registered.
  137  */
  138 struct counter_c
  139 {
  140   LLIST_HDR (struct counter_c);
  141 
  142   char            pubkey[ANDNA_PKEY_LEN];
  143   char    flags;
  144 
  145   u_short   hashes;     /* The number of hashes in cch */
  146   counter_c_hashes *cch;      /* The hashes of the hnames */
  147 };
  148 typedef struct counter_c counter_c;
  149 INT_INFO counter_c_body_iinfo = { 1,
  150           { INT_TYPE_16BIT },
  151           { ANDNA_PKEY_LEN+sizeof(char) },
  152           { 1 }
  153         };
  154 
  155 /*
  156  * lcl_cache_keyring
  157  *
  158  * The lcl keyring is used to store the RSA keys used to complete some of the
  159  * ANDNA requests, (f.e. registering or updating a hname).
  160  */
  161 typedef struct
  162 {
  163   u_int   skey_len;
  164   u_int   pkey_len;
  165 
  166   u_char    *privkey;   /* secret key packed */
  167   u_char    *pubkey;    /* pubkey packed */
  168 
  169   RSA   *priv_rsa;    /* key pair unpacked */
  170 } lcl_cache_keyring;
  171 
  172 
  173 /*
  174  * lcl_cache
  175  *
  176  * The Local Andna Cache keeps all the hostnames which have been register by
  177  * localhost (ourself).
  178  */
  179 struct lcl_cache
  180 {
  181   LLIST_HDR (struct lcl_cache);
  182 
  183   char    *hostname;    /* The registered hostname */
  184   u_int   hash;     /* 32bit hash of the md5 hash
  185                of the hname */
  186   u_short   hname_updates;    /* How many updates we've done
  187                for this hostname */
  188   time_t          timestamp;    /* the last time when the hname
  189                was updated. If it is 0, the
  190                hname has still to be
  191                registered */
  192 
  193   u_short   snsd_counter;   /* # of `snsds' */
  194   snsd_service  *service;
  195 
  196   char    flags;
  197 };
  198 typedef struct lcl_cache lcl_cache;
  199 
  200 
  201 /*
  202  * resolved_hnames_cache
  203  *
  204  * This cache keeps info on the already resolved hostnames, so we won't have
  205  * to resolve them soon again.
  206  * In order to optimize the search we order the linked list by the time
  207  * of hname resolution. The last hname which has been searched/resolved is
  208  * always moved at the head of the llist, in this way, at the end of the llist
  209  * there is the hname which has been searched for the first time but has been
  210  * ignored until now.
  211  * When the cache is full, the hname which is at the end of the llist is
  212  * removed to empty new space.
  213  * The hname which have the `timestamp' expired are removed too.
  214  */
  215 struct resolved_hnames_cache
  216 {
  217   LLIST_HDR (struct resolved_hnames_cache);
  218 
  219   u_int   hash;   /* 32bit hash of the md5 hash of the
  220              hname */
  221   char    flags;
  222 
  223   time_t    timestamp;  /* the last time when the hname
  224              was updated. With this we know that
  225              at timestamp+ANDNA_EXPIRATION_TIME
  226              this cache will expire. */
  227 
  228   u_short   snsd_counter;
  229   snsd_service  *service;
  230 };
  231 typedef struct resolved_hnames_cache rh_cache;
  232 
  233 
  234 /*\
  235  *
  236  *  * * *  Global vars   * * *
  237  *
  238 \*/
  239 andna_cache *andna_c;
  240 int andna_c_counter;
  241 
  242 counter_c *andna_counter_c;
  243 int cc_counter;
  244 
  245 lcl_cache_keyring lcl_keyring;
  246 lcl_cache *andna_lcl;
  247 int lcl_counter;
  248 
  249 rh_cache *andna_rhc;
  250 int rhc_counter;
  251 
  252 
  253 /*\
  254  *
  255  *  *  *  *  Package stuff  *  *  *
  256  *
  257 \*/
  258 
  259 
  260 /*
  261  *  * * * lcl cache package * * *
  262  */
  263 
  264 struct lcl_keyring_pkt_hdr
  265 {
  266   u_int   skey_len;
  267   u_int   pkey_len;
  268 }_PACKED_;
  269 /*
  270  * the rest of the pkt is:
  271  *
  272  *  char    privkey[hdr.skey_len];
  273  *  char    pubkey[hdr.pkey_len];
  274  */
  275 INT_INFO lcl_keyring_pkt_hdr_iinfo = { 2,
  276              { INT_TYPE_32BIT, INT_TYPE_32BIT },
  277              { 0, sizeof(u_int) },
  278              { 1, 1 }
  279            };
  280 #define LCL_KEYRING_HDR_PACK_SZ(khdr) (sizeof(struct lcl_keyring_pkt_hdr) + \
  281           (khdr)->skey_len + (khdr)->pkey_len)
  282 
  283 /*
  284  * The local cache pkt is used to pack the entire local cache to save it in a
  285  * file or to send it to a node.
  286  */
  287 struct lcl_cache_pkt_hdr
  288 {
  289   u_short   tot_caches;   /* How many lcl structs there
  290                are in the pkt's body */
  291 }_PACKED_;
  292 INT_INFO lcl_cache_pkt_hdr_iinfo = { 1, { INT_TYPE_16BIT }, { 0 }, { 1 } };
  293 #define LCL_CACHE_HDR_PACK_SZ   (sizeof(struct lcl_cache_pkt_hdr))
  294 
  295 /*
  296  * The body is:
  297  *
  298  * struct lcl_cache_pkt_body {
  299  *  u_short   hname_updates;
  300  *  time_t          timestamp;
  301  *  char    hostname[strlen(hostname)+1];  * null terminated *
  302  * } body[ hdr.tot_caches ];
  303  *
  304  */
  305 #define LCL_CACHE_BODY_PACK_SZ(hname_len)   ((hname_len) + sizeof(u_short) \
  306               + sizeof(time_t))
  307 INT_INFO lcl_cache_pkt_body_iinfo = { 2, { INT_TYPE_16BIT, INT_TYPE_32BIT },
  308               { 0, sizeof(u_short) },
  309               { 1, 1 }
  310             };
  311 
  312 /*
  313  *  * * * andna cache package * * *
  314  */
  315 
  316 /*
  317  * the body of the acq_pkt is:
  318  * struct acq_pkt_body {
  319  *  time_t    timestamp;
  320  *  u_short   hname_updates;
  321  *  char    pubkey[ANDNA_PKEY_LEN];
  322  *
  323  *  u_short   snsd_counter;
  324  *  char    snsd_service_pack[SNSD_SERVICE_PACK_SZ];
  325  * };
  326  */
  327 INT_INFO acq_body_iinfo = { 3,
  328           { INT_TYPE_32BIT, INT_TYPE_16BIT, INT_TYPE_16BIT },
  329           { 0, sizeof(time_t),
  330             sizeof(time_t) + sizeof(u_short) + ANDNA_PKEY_LEN },
  331           { 1, 1, 1 }
  332         };
  333 #define ACQ_BODY_PACK_SZ    (sizeof(time_t) + sizeof(u_short)*2 + \
  334            ANDNA_PKEY_LEN)
  335 #define ACQ_PACK_SZ(snsd_pack_sz) (ACQ_BODY_PACK_SZ + (snsd_pack_sz))
  336 
  337 struct andna_cache_pkt_hdr
  338 {
  339   u_short   tot_caches;
  340 }_PACKED_;
  341 INT_INFO andna_cache_pkt_hdr_iinfo = { 1, { INT_TYPE_16BIT }, { 0 }, { 1 } };
  342 /*
  343  * The body is:
  344  * struct andna_cache_pack {
  345  *  u_int   hash[MAX_IP_INT];
  346  *  char    flags;
  347  *  u_short   queue_counter;
  348  *  char    acq_pack[ACQ_PACK_SZ*queue_counter];
  349  * } acache_pack[hdr.tot_caches];
  350  */
  351 INT_INFO andna_cache_body_iinfo = { 1,
  352             { INT_TYPE_16BIT },
  353             { MAX_IP_SZ+sizeof(char) },
  354             { 1 }
  355           };
  356 #define ACACHE_BODY_PACK_SZ   (ANDNA_HASH_SZ + sizeof(char) +   \
  357              sizeof(u_short))
  358 #define ACACHE_PACK_SZ(acq_pack_sz) ((acq_pack_sz) + ACACHE_BODY_PACK_SZ)
  359 
  360 /*
  361  * If the acache pack will be sent on a network packet, the `acq->timestamp'
  362  * will be the difference of the current time with the same `acq->timestamp',
  363  * in this way the node which receives the packet will add its current time to
  364  * `acq->timestamp'. This is necessary because the sending and receiving node
  365  * don't have the clock synced. Note that the rtt isn't considered because it
  366  * is generally very small and the ANDNA times don't need an accurate
  367  * precision, f.e. the expiration time is three days long.
  368  * If the pack is saved on a file, then `acq->timestamp' remains the same.
  369  * Problem: if the clock is changed, acq->timestamp will refer to the old
  370  * clock.
  371  */
  372 #define ACACHE_PACK_FILE    1
  373 #define ACACHE_PACK_PKT     2
  374 
  375 
  376 /*
  377  * The counter cache pkt is similar to the andna_cache_pkt, it is completely
  378  * arranged in the same way.
  379  */
  380 struct counter_c_pkt_hdr
  381 {
  382   u_short   tot_caches;
  383 }_PACKED_;
  384 INT_INFO counter_c_pkt_hdr_iinfo = { 1, { INT_TYPE_16BIT }, { 0 }, { 1 } };
  385 #define COUNTER_CACHE_HASHES_PACK_SZ  (sizeof(time_t) + sizeof(u_short) + \
  386             ANDNA_HASH_SZ)
  387 #define COUNTER_CACHE_BODY_PACK_SZ  (ANDNA_PKEY_LEN + sizeof(char)    +   \
  388             sizeof(u_short))
  389 #define COUNTER_CACHE_PACK_SZ(hashes) ((COUNTER_CACHE_HASHES_PACK_SZ*(hashes))\
  390            + COUNTER_CACHE_BODY_PACK_SZ)
  391 
  392 /*
  393  *  * * * Resolved hostnames cache pkt. * * *
  394  */
  395 
  396 struct rh_cache_pkt_hdr
  397 {
  398   u_short   tot_caches;   /* How many lcl structs there
  399                are in the pkt's hdr */
  400 }_PACKED_;
  401 INT_INFO rh_cache_pkt_hdr_iinfo = { 1, { INT_TYPE_16BIT }, { 0 }, { 1 } };
  402 /*
  403  * The body is:
  404  * struct rh_cache_pkt_body {
  405  *  u_int   hash;
  406  *  char    flags;
  407  *  time_t    timestamp;
  408  *
  409  *  u_short   snsd_counter;
  410  *  char    snsd_service_pack[SNSD_SERVICE_PACK_SZ];
  411  * } body[ hdr.tot_caches ];
  412  */
  413 #define RH_CACHE_BODY_PACK_SZ(snsd_pack_sz) (sizeof(u_int)+sizeof(char)+ \
  414              sizeof(time_t)+sizeof(u_short)+\
  415              (snsd_pack_sz))
  416 INT_INFO rh_cache_pkt_body_iinfo = { 3,
  417             { INT_TYPE_32BIT, INT_TYPE_32BIT, INT_TYPE_16BIT },
  418             { 0, sizeof(u_int)+sizeof(char),
  419               sizeof(u_int)+sizeof(char)+sizeof(time_t) },
  420             { 1, 1, 1 }
  421            };
  422 
  423 
  424 /*\
  425  *
  426  * * * Functions' declaration * * *
  427  *
  428 \*/
  429 
  430 void andna_caches_init(int family);
  431 
  432 void lcl_new_keyring(lcl_cache_keyring *keyring);
  433 void lcl_destroy_keyring(lcl_cache_keyring *keyring);
  434 lcl_cache *lcl_cache_new(char *hname);
  435 void lcl_cache_free(lcl_cache *alcl);
  436 void lcl_cache_destroy(lcl_cache *head, int *counter);
  437 lcl_cache *lcl_cache_find_hname(lcl_cache *head, char *hname);
  438 lcl_cache *lcl_cache_find_hash(lcl_cache *alcl, u_int hash);
  439 lcl_cache *lcl_get_registered_hnames(lcl_cache *alcl);
  440 
  441 andna_cache_queue *ac_queue_findpubk(andna_cache *ac, char *pubk);
  442 andna_cache_queue *ac_queue_add(andna_cache *ac, char *pubkey);
  443 void ac_queue_del(andna_cache *ac, andna_cache_queue *acq);
  444 void ac_queue_del_expired(andna_cache *ac);
  445 void ac_queue_destroy(andna_cache *ac);
  446 andna_cache *andna_cache_findhash(int hash[MAX_IP_INT]);
  447 andna_cache *andna_cache_gethash(int hash[MAX_IP_INT]);
  448 andna_cache *andna_cache_addhash(int hash[MAX_IP_INT]);
  449 int andna_cache_del_ifexpired(andna_cache *ac);
  450 void andna_cache_del_expired(void);
  451 void andna_cache_destroy(void);
  452 
  453 counter_c_hashes *cc_hashes_add(counter_c *cc, int hash[MAX_IP_INT]);
  454 void cc_hashes_del(counter_c *cc, counter_c_hashes *cch);
  455 int counter_c_del_ifexpired(counter_c *cc);
  456 void cc_hashes_del_expired(counter_c *cc);
  457 void cc_hashes_destroy(counter_c *cc);
  458 counter_c_hashes *cc_findhash(counter_c *cc, int hash[MAX_IP_INT]);
  459 counter_c *counter_c_findpubk(char *pubk);
  460 counter_c *counter_c_add(inet_prefix *rip, char *pubkey);
  461 void counter_c_del_expired(void);
  462 void counter_c_destroy(void);
  463 
  464 rh_cache *rh_cache_new(char *hname, time_t timestamp);
  465 rh_cache *rh_cache_add_hash(u_int hash, time_t timestamp);
  466 rh_cache *rh_cache_add(char *hname, time_t timestamp);
  467 rh_cache *rh_cache_find_hash(u_int hash);
  468 rh_cache *rh_cache_find_hname(char *hname);
  469 void rh_cache_del(rh_cache *rhc);
  470 void rh_cache_del_expired(void);
  471 void rh_cache_flush(void);
  472 
  473 char *pack_lcl_keyring(lcl_cache_keyring *keyring, size_t *pack_sz);
  474 int unpack_lcl_keyring(lcl_cache_keyring *keyring, char *pack, size_t pack_sz);
  475 
  476 char *pack_lcl_cache(lcl_cache *local_cache, size_t *pack_sz);
  477 lcl_cache *unpack_lcl_cache(char *pack, size_t pack_sz, int *counter);
  478 
  479 char *pack_andna_cache(andna_cache *acache, size_t *pack_sz, int pack_type);
  480 andna_cache *unpack_andna_cache(char *pack, size_t pack_sz, int *counter,
  481         int pack_type);
  482 
  483 char *pack_counter_cache(counter_c *countercache,  size_t *pack_sz);
  484 counter_c *unpack_counter_cache(char *pack, size_t pack_sz, int *counter);
  485 
  486 char *pack_rh_cache(rh_cache *rhcache, size_t *pack_sz);
  487 rh_cache *unpack_rh_cache(char *pack, size_t pack_sz, int *counter);
  488 
  489 int save_lcl_keyring(lcl_cache_keyring *keyring, char *file);
  490 int load_lcl_keyring(lcl_cache_keyring *keyring, char *file);
  491 
  492 int save_lcl_cache(lcl_cache *lcl, char *file);
  493 lcl_cache *load_lcl_cache(char *file, int *counter);
  494 
  495 int save_andna_cache(andna_cache *acache, char *file);
  496 andna_cache *load_andna_cache(char *file, int *counter);
  497 
  498 int save_counter_c(counter_c *countercache, char *file);
  499 counter_c *load_counter_c(char *file, int *counter);
  500 
  501 int save_rh_cache(rh_cache *rh, char *file);
  502 rh_cache *load_rh_cache(char *file, int *counter);
  503 
  504 int load_hostnames(char *file, lcl_cache **old_alcl_head, int *old_alcl_counter);
  505 int load_snsd(char *file, lcl_cache *alcl_head);
  506 
  507 int add_resolv_conf(char *hname, char *file);
  508 int del_resolv_conf(char *hname, char *file);
  509 
  510 #endif    /*ANDNA_CACHE_H*/

alpt (at) freaknet (dot) org
ViewVC Help
Powered by ViewVC 1.1-dev