ccnd_private.h

Go to the documentation of this file.
00001 /**
00002  * @file ccnd_private.h
00003  *
00004  * Private definitions for ccnd - the CCNx daemon.
00005  * Data structures are described here so that logging and status
00006  * routines can be compiled separately.
00007  *
00008  * Part of ccnd - the CCNx Daemon.
00009  *
00010  * Copyright (C) 2008-2011 Palo Alto Research Center, Inc.
00011  *
00012  * This work is free software; you can redistribute it and/or modify it under
00013  * the terms of the GNU General Public License version 2 as published by the
00014  * Free Software Foundation.
00015  * This work is distributed in the hope that it will be useful, but WITHOUT ANY
00016  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
00017  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
00018  * for more details. You should have received a copy of the GNU General Public
00019  * License along with this program; if not, write to the
00020  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021  * Boston, MA 02110-1301, USA.
00022  */
00023  
00024 #ifndef CCND_PRIVATE_DEFINED
00025 #define CCND_PRIVATE_DEFINED
00026 
00027 #include <poll.h>
00028 #include <stdarg.h>
00029 #include <stddef.h>
00030 #include <stdint.h>
00031 #include <sys/socket.h>
00032 #include <sys/types.h>
00033 
00034 #include <ccn/ccn_private.h>
00035 #include <ccn/coding.h>
00036 #include <ccn/reg_mgmt.h>
00037 #include <ccn/schedule.h>
00038 #include <ccn/seqwriter.h>
00039 
00040 /*
00041  * These are defined in other ccn headers, but the incomplete types suffice
00042  * for the purposes of this header.
00043  */
00044 struct ccn_charbuf;
00045 struct ccn_indexbuf;
00046 struct hashtb;
00047 struct ccnd_meter;
00048 
00049 /*
00050  * These are defined in this header.
00051  */
00052 struct ccnd_handle;
00053 struct face;
00054 struct content_entry;
00055 struct nameprefix_entry;
00056 struct propagating_entry;
00057 struct content_tree_node;
00058 struct ccn_forwarding;
00059 
00060 //typedef uint_least64_t ccn_accession_t;
00061 typedef unsigned ccn_accession_t;
00062 
00063 typedef int (*ccnd_logger)(void *loggerdata, const char *format, va_list ap);
00064 
00065 /**
00066  * We pass this handle almost everywhere within ccnd
00067  */
00068 struct ccnd_handle {
00069     unsigned char ccnd_id[32];      /**< sha256 digest of our public key */
00070     struct hashtb *faces_by_fd;     /**< keyed by fd */
00071     struct hashtb *dgram_faces;     /**< keyed by sockaddr */
00072     struct hashtb *content_tab;     /**< keyed by portion of ContentObject */
00073     struct hashtb *nameprefix_tab;  /**< keyed by name prefix components */
00074     struct hashtb *propagating_tab; /**< keyed by nonce */
00075     struct ccn_indexbuf *skiplinks; /**< skiplist for content-ordered ops */
00076     unsigned forward_to_gen;        /**< for forward_to updates */
00077     unsigned face_gen;              /**< faceid generation number */
00078     unsigned face_rover;            /**< for faceid allocation */
00079     unsigned face_limit;            /**< current number of face slots */
00080     struct face **faces_by_faceid;  /**< array with face_limit elements */
00081     struct ccn_scheduled_event *reaper;
00082     struct ccn_scheduled_event *age;
00083     struct ccn_scheduled_event *clean;
00084     struct ccn_scheduled_event *age_forwarding;
00085     const char *portstr;            /**< "main" port number */
00086     unsigned ipv4_faceid;           /**< wildcard IPv4, bound to port */
00087     unsigned ipv6_faceid;           /**< wildcard IPv6, bound to port */
00088     nfds_t nfds;                    /**< number of entries in fds array */
00089     struct pollfd *fds;             /**< used for poll system call */
00090     struct ccn_gettime ticktock;    /**< our time generator */
00091     long sec;                       /**< cached gettime seconds */
00092     unsigned usec;                  /**< cached gettime microseconds */
00093     long starttime;                 /**< ccnd start time, in seconds */
00094     unsigned starttime_usec;        /**< ccnd start time fractional part */
00095     struct ccn_schedule *sched;     /**< our schedule */
00096     struct ccn_charbuf *scratch_charbuf; /**< one-slot scratch cache */
00097     struct ccn_indexbuf *scratch_indexbuf; /**< one-slot scratch cache */
00098     /** Next three fields are used for direct accession-to-content table */
00099     ccn_accession_t accession_base;
00100     unsigned content_by_accession_window;
00101     struct content_entry **content_by_accession;
00102     /** The following holds stragglers that would otherwise bloat the above */
00103     struct hashtb *sparse_straggler_tab; /* keyed by accession */
00104     ccn_accession_t accession;      /**< newest used accession number */
00105     ccn_accession_t min_stale;      /**< smallest accession of stale content */
00106     ccn_accession_t max_stale;      /**< largest accession of stale content */
00107     unsigned long capacity;         /**< may toss content if there more than
00108                                      this many content objects in the store */
00109     unsigned long n_stale;          /**< Number of stale content objects */
00110     struct ccn_indexbuf *unsol;     /**< unsolicited content */
00111     unsigned long oldformatcontent;
00112     unsigned long oldformatcontentgrumble;
00113     unsigned long oldformatinterests;
00114     unsigned long oldformatinterestgrumble;
00115     unsigned long content_dups_recvd;
00116     unsigned long content_items_sent;
00117     unsigned long interests_accepted;
00118     unsigned long interests_dropped;
00119     unsigned long interests_sent;
00120     unsigned long interests_stuffed;
00121     unsigned short seed[3];         /**< for PRNG */
00122     int running;                    /**< true while should be running */
00123     int debug;                      /**< For controlling debug output */
00124     ccnd_logger logger;             /**< For debug output */
00125     void *loggerdata;               /**< Passed to logger */
00126     int logbreak;                   /**< see ccn_msg() */
00127     unsigned long logtime;          /**< see ccn_msg() */
00128     int logpid;                     /**< see ccn_msg() */
00129     int mtu;                        /**< Target size for stuffing interests */
00130     int flood;                      /**< Internal control for auto-reg */
00131     struct ccn_charbuf *autoreg;    /**< URIs to auto-register */
00132     int force_zero_freshness;       /**< Simulate freshness=0 on all content */
00133     unsigned interest_faceid;       /**< for self_reg internal client */
00134     const char *progname;           /**< our name, for locating helpers */
00135     struct ccn *internal_client;    /**< internal client */
00136     struct face *face0;             /**< special face for internal client */
00137     struct ccn_charbuf *service_ccnb; /**< for local service discovery */
00138     struct ccn_charbuf *neighbor_ccnb; /**< for neighbor service discovery */
00139     struct ccn_seqwriter *notice;   /**< for notices of status changes */
00140     struct ccn_indexbuf *chface;    /**< faceids w/ recent status changes */
00141     struct ccn_scheduled_event *internal_client_refresh;
00142     struct ccn_scheduled_event *notice_push;
00143     unsigned data_pause_microsec;   /**< tunable, see choose_face_delay() */
00144     void (*appnonce)(struct ccnd_handle *, struct face *, struct ccn_charbuf *);
00145                                     /**< pluggable nonce generation */
00146 };
00147 
00148 /**
00149  * Each face is referenced by a number, the faceid.  The low-order
00150  * bits (under the MAXFACES) constitute a slot number that is
00151  * unique (for this ccnd) among the faces that are alive at a given time.
00152  * The rest of the bits form a generation number that make the
00153  * entire faceid unique over time, even for faces that are defunct.
00154  */
00155 #define FACESLOTBITS 18
00156 #define MAXFACES ((1U << FACESLOTBITS) - 1)
00157 
00158 struct content_queue {
00159     unsigned burst_nsec;             /**< nsec per KByte, limits burst rate */
00160     unsigned min_usec;               /**< minimum delay for this queue */
00161     unsigned rand_usec;              /**< randomization range */
00162     unsigned ready;                  /**< # that have waited enough */
00163     unsigned nrun;                   /**< # sent since last randomized delay */
00164     struct ccn_indexbuf *send_queue; /**< accession numbers of pending content */
00165     struct ccn_scheduled_event *sender;
00166 };
00167 
00168 enum cq_delay_class {
00169     CCN_CQ_ASAP,
00170     CCN_CQ_NORMAL,
00171     CCN_CQ_SLOW,
00172     CCN_CQ_N
00173 };
00174 
00175 /**
00176  * Face meter index
00177  */
00178 enum ccnd_face_meter_index {
00179     FM_BYTI,
00180     FM_BYTO,
00181     FM_DATI,
00182     FM_INTO,
00183     FM_DATO,
00184     FM_INTI,
00185     CCND_FACE_METER_N
00186 };
00187 
00188 /**
00189  * One of our active faces
00190  */
00191 struct face {
00192     int recv_fd;                /**< socket for receiving */
00193     unsigned sendface;          /**< faceid for sending (maybe == faceid) */
00194     int flags;                  /**< CCN_FACE_* face flags */
00195     int surplus;                /**< sends since last successful recv */
00196     unsigned faceid;            /**< internal face id */
00197     unsigned recvcount;         /**< for activity level monitoring */
00198     struct content_queue *q[CCN_CQ_N]; /**< outgoing content, per delay class */
00199     struct ccn_charbuf *inbuf;
00200     struct ccn_skeleton_decoder decoder;
00201     size_t outbufindex;
00202     struct ccn_charbuf *outbuf;
00203     const struct sockaddr *addr;
00204     socklen_t addrlen;
00205     int pending_interests;
00206     unsigned rrun;
00207     uintmax_t rseq;
00208     struct ccnd_meter *meter[CCND_FACE_METER_N];
00209     unsigned short pktseq;     /**< sequence number for sent packets */
00210 };
00211 
00212 /** face flags */
00213 #define CCN_FACE_LINK   (1 << 0) /**< Elements wrapped by CCNProtocolDataUnit */
00214 #define CCN_FACE_DGRAM  (1 << 1) /**< Datagram interface, respect packets */
00215 #define CCN_FACE_GG     (1 << 2) /**< Considered friendly */
00216 #define CCN_FACE_LOCAL  (1 << 3) /**< PF_UNIX socket */
00217 #define CCN_FACE_INET   (1 << 4) /**< IPv4 */
00218 #define CCN_FACE_MCAST  (1 << 5) /**< a party line (e.g. multicast) */
00219 #define CCN_FACE_INET6  (1 << 6) /**< IPv6 */
00220 #define CCN_FACE_DC     (1 << 7) /**< Direct control face */
00221 #define CCN_FACE_NOSEND (1 << 8) /**< Don't send anymore */
00222 #define CCN_FACE_UNDECIDED (1 << 9) /**< Might not be talking ccn */
00223 #define CCN_FACE_PERMANENT (1 << 10) /**< No timeout for inactivity */
00224 #define CCN_FACE_CONNECTING (1 << 11) /**< Connect in progress */
00225 #define CCN_FACE_LOOPBACK (1 << 12) /**< v4 or v6 loopback address */
00226 #define CCN_FACE_CLOSING (1 << 13) /**< close stream when output is done */
00227 #define CCN_FACE_PASSIVE (1 << 14) /**< a listener or a bound dgram socket */
00228 #define CCN_FACE_NORECV (1 << 15) /**< use for sending only */
00229 #define CCN_FACE_REGOK (1 << 16) /**< Allowed to do prefix registration */
00230 #define CCN_FACE_SEQOK (1 << 17) /** OK to send SequenceNumber link messages */
00231 #define CCN_FACE_SEQPROBE (1 << 18) /** SequenceNumber probe */
00232 #define CCN_NOFACEID    (~0U)    /** denotes no face */
00233 
00234 /**
00235  *  The content hash table is keyed by the initial portion of the ContentObject
00236  *  that contains all the parts of the complete name.  The extdata of the hash
00237  *  table holds the rest of the object, so that the whole ContentObject is
00238  *  stored contiguously.  The internal form differs from the on-wire form in
00239  *  that the final content-digest name component is represented explicitly,
00240  *  which simplifies the matching logic.
00241  *  The original ContentObject may be reconstructed simply by excising this
00242  *  last name component, which is easily located via the comps array.
00243  */
00244 struct content_entry {
00245     ccn_accession_t accession;  /**< assigned in arrival order */
00246     unsigned short *comps;      /**< Name Component byte boundary offsets */
00247     int ncomps;                 /**< Number of name components plus one */
00248     int flags;                  /**< see below */
00249     const unsigned char *key;   /**< ccnb-encoded ContentObject */
00250     int key_size;               /**< Size of fragment prior to Content */
00251     int size;                   /**< Size of ContentObject */
00252     struct ccn_indexbuf *skiplinks; /**< skiplist for name-ordered ops */
00253 };
00254 
00255 /**
00256  * content_entry flags
00257  */
00258 #define CCN_CONTENT_ENTRY_SLOWSEND  1
00259 #define CCN_CONTENT_ENTRY_STALE     2
00260 #define CCN_CONTENT_ENTRY_PRECIOUS  4
00261 
00262 /**
00263  * The sparse_straggler hash table, keyed by accession, holds scattered
00264  * entries that would otherwise bloat the direct content_by_accession table.
00265  */
00266 struct sparse_straggler_entry {
00267     struct content_entry *content;
00268 };
00269 
00270 /**
00271  * The propagating interest hash table is keyed by Nonce.
00272  *
00273  * While the interest is pending, the pe is also kept in a doubly-linked
00274  * list off of a nameprefix_entry.
00275  *
00276  * When the interest is consumed, the pe is removed from the doubly-linked
00277  * list and is cleaned up by freeing unnecessary bits (including the interest
00278  * message itself).  It remains in the hash table for a time, in order to catch
00279  * duplicate nonces.
00280  */
00281 struct propagating_entry {
00282     struct propagating_entry *next;
00283     struct propagating_entry *prev;
00284     unsigned flags;             /**< CCN_PR_xxx */
00285     unsigned faceid;            /**< origin of the interest, dest for matches */
00286     int usec;                   /**< usec until timeout */
00287     int sent;                   /**< leading faceids of outbound processed */
00288     struct ccn_indexbuf *outbound; /**< in order of use */
00289     unsigned char *interest_msg; /**< pending interest message */
00290     unsigned size;              /**< size in bytes of interest_msg */
00291     int fgen;                   /**< decide if outbound is stale */
00292 };
00293 // XXX - with new outbound/sent repr, some of these flags may not be needed.
00294 #define CCN_PR_UNSENT   0x01 /**< interest has not been sent anywhere yet */
00295 #define CCN_PR_WAIT1    0x02 /**< interest has been sent to one place */
00296 #define CCN_PR_STUFFED1 0x04 /**< was stuffed before sent anywhere else */
00297 #define CCN_PR_TAP      0x08 /**< at least one tap face is present */
00298 #define CCN_PR_EQV      0x10 /**< a younger similar interest exists */
00299 #define CCN_PR_SCOPE0   0x20 /**< interest scope is 0 */
00300 #define CCN_PR_SCOPE1   0x40 /**< interest scope is 1 (this host) */
00301 #define CCN_PR_SCOPE2   0x80 /**< interest scope is 2 (immediate neighborhood) */
00302 
00303 /**
00304  * The nameprefix hash table is keyed by the Component elements of
00305  * the Name prefix.
00306  */
00307 struct nameprefix_entry {
00308     struct propagating_entry pe_head; /**< list head for propagating entries */
00309     struct ccn_indexbuf *forward_to; /**< faceids to forward to */
00310     struct ccn_indexbuf *tap;    /**< faceids to forward to as tap*/
00311     struct ccn_forwarding *forwarding; /**< detailed forwarding info */
00312     struct nameprefix_entry *parent; /**< link to next-shorter prefix */
00313     int children;                /**< number of children */
00314     unsigned flags;              /**< CCN_FORW_* flags about namespace */
00315     int fgen;                    /**< used to decide when forward_to is stale */
00316     unsigned src;                /**< faceid of recent content source */
00317     unsigned osrc;               /**< and of older matching content */
00318     unsigned usec;               /**< response-time prediction */
00319 };
00320 
00321 /**
00322  * Keeps track of the faces that interests matching a given name prefix may be
00323  * forwarded to.
00324  */
00325 struct ccn_forwarding {
00326     unsigned faceid;             /**< locally unique number identifying face */
00327     unsigned flags;              /**< CCN_FORW_* - c.f. <ccn/reg_mgnt.h> */
00328     int expires;                 /**< time remaining, in seconds */
00329     struct ccn_forwarding *next;
00330 };
00331 
00332 /* create and destroy procs for separately allocated meters */
00333 struct ccnd_meter *ccnd_meter_create(struct ccnd_handle *h, const char *what);
00334 void ccnd_meter_destroy(struct ccnd_meter **);
00335 
00336 /* for meters kept within other structures */
00337 void ccnd_meter_init(struct ccnd_handle *h, struct ccnd_meter *m, const char *what);
00338 
00339 /* count something (messages, packets, bytes), getting time info from h */
00340 void ccnd_meter_bump(struct ccnd_handle *h, struct ccnd_meter *m, unsigned amt);
00341 
00342 unsigned ccnd_meter_rate(struct ccnd_handle *h, struct ccnd_meter *m);
00343 uintmax_t ccnd_meter_total(struct ccnd_meter *m);
00344 
00345 
00346 /**
00347  * Refer to doc/technical/Registration.txt for the meaning of these flags.
00348  *
00349  * @def CCN_FORW_ACTIVE         1
00350  * @def CCN_FORW_CHILD_INHERIT  2
00351  * @def CCN_FORW_ADVERTISE      4
00352  * @def CCN_FORW_LAST           8
00353  * @def CCN_FORW_CAPTURE       16
00354  * @def CCN_FORW_LOCAL         32
00355  * @def CCN_FORW_TAP           64
00356  * @def CCN_FORW_CAPTURE_OK   128
00357  */
00358 #define CCN_FORW_PFXO (CCN_FORW_ADVERTISE | CCN_FORW_CAPTURE | CCN_FORW_LOCAL)
00359 #define CCN_FORW_REFRESHED      (1 << 16) /**< private to ccnd */
00360 
00361  
00362 /**
00363  * Determines how frequently we age our forwarding entries
00364  */
00365 #define CCN_FWU_SECS 5
00366 
00367 /*
00368  * Internal client
00369  * The internal client is for communication between the ccnd and other
00370  * components, using (of course) ccn protocols.
00371  */
00372 int ccnd_init_internal_keystore(struct ccnd_handle *);
00373 int ccnd_internal_client_start(struct ccnd_handle *);
00374 void ccnd_internal_client_stop(struct ccnd_handle *);
00375 
00376 /*
00377  * The internal client calls this with the argument portion ARG of
00378  * a face-creation request (/ccnx/CCNDID/newface/ARG)
00379  */
00380 int ccnd_req_newface(struct ccnd_handle *h,
00381                      const unsigned char *msg, size_t size,
00382                      struct ccn_charbuf *reply_body);
00383 
00384 /*
00385  * The internal client calls this with the argument portion ARG of
00386  * a face-destroy request (/ccnx/CCNDID/destroyface/ARG)
00387  */
00388 int ccnd_req_destroyface(struct ccnd_handle *h,
00389                          const unsigned char *msg, size_t size,
00390                          struct ccn_charbuf *reply_body);
00391 
00392 /*
00393  * The internal client calls this with the argument portion ARG of
00394  * a prefix-registration request (/ccnx/CCNDID/prefixreg/ARG)
00395  */
00396 int ccnd_req_prefixreg(struct ccnd_handle *h,
00397                        const unsigned char *msg, size_t size,
00398                        struct ccn_charbuf *reply_body);
00399 
00400 /*
00401  * The internal client calls this with the argument portion ARG of
00402  * a prefix-registration request for self (/ccnx/CCNDID/selfreg/ARG)
00403  */
00404 int ccnd_req_selfreg(struct ccnd_handle *h,
00405                      const unsigned char *msg, size_t size,
00406                      struct ccn_charbuf *reply_body);
00407 
00408 /**
00409  * URIs for prefixes served by the internal client
00410  */
00411 #define CCNDID_LOCAL_URI "ccnx:/%C1.M.S.localhost/%C1.M.SRV/ccnd/KEY"
00412 #define CCNDID_NEIGHBOR_URI "ccnx:/%C1.M.S.neighborhood/%C1.M.SRV/ccnd/KEY"
00413 
00414 /*
00415  * The internal client calls this with the argument portion ARG of
00416  * a prefix-unregistration request (/ccnx/CCNDID/unreg/ARG)
00417  */
00418 int ccnd_req_unreg(struct ccnd_handle *h,
00419                    const unsigned char *msg, size_t size,
00420                    struct ccn_charbuf *reply_body);
00421 
00422 int ccnd_reg_uri(struct ccnd_handle *h,
00423                  const char *uri,
00424                  unsigned faceid,
00425                  int flags,
00426                  int expires);
00427 
00428 struct face *ccnd_face_from_faceid(struct ccnd_handle *, unsigned);
00429 void ccnd_face_status_change(struct ccnd_handle *, unsigned);
00430 int ccnd_destroy_face(struct ccnd_handle *h, unsigned faceid);
00431 void ccnd_send(struct ccnd_handle *h, struct face *face,
00432                const void *data, size_t size);
00433 
00434 /* Consider a separate header for these */
00435 int ccnd_stats_handle_http_connection(struct ccnd_handle *, struct face *);
00436 void ccnd_msg(struct ccnd_handle *, const char *, ...);
00437 void ccnd_debug_ccnb(struct ccnd_handle *h,
00438                      int lineno,
00439                      const char *msg,
00440                      struct face *face,
00441                      const unsigned char *ccnb,
00442                      size_t ccnb_size);
00443 
00444 struct ccnd_handle *ccnd_create(const char *, ccnd_logger, void *);
00445 void ccnd_run(struct ccnd_handle *h);
00446 void ccnd_destroy(struct ccnd_handle **);
00447 extern const char *ccnd_usage_message;
00448 
00449 #endif
Generated on Fri May 13 16:27:01 2011 for Content-Centric Networking in C by  doxygen 1.6.3