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-2012 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     int tts_default;                /**< CCND_DEFAULT_TIME_TO_STALE (seconds) */
00147     int tts_limit;                  /**< CCND_MAX_TIME_TO_STALE (seconds) */
00148 };
00149 
00150 /**
00151  * Each face is referenced by a number, the faceid.  The low-order
00152  * bits (under the MAXFACES) constitute a slot number that is
00153  * unique (for this ccnd) among the faces that are alive at a given time.
00154  * The rest of the bits form a generation number that make the
00155  * entire faceid unique over time, even for faces that are defunct.
00156  */
00157 #define FACESLOTBITS 18
00158 #define MAXFACES ((1U << FACESLOTBITS) - 1)
00159 
00160 struct content_queue {
00161     unsigned burst_nsec;             /**< nsec per KByte, limits burst rate */
00162     unsigned min_usec;               /**< minimum delay for this queue */
00163     unsigned rand_usec;              /**< randomization range */
00164     unsigned ready;                  /**< # that have waited enough */
00165     unsigned nrun;                   /**< # sent since last randomized delay */
00166     struct ccn_indexbuf *send_queue; /**< accession numbers of pending content */
00167     struct ccn_scheduled_event *sender;
00168 };
00169 
00170 enum cq_delay_class {
00171     CCN_CQ_ASAP,
00172     CCN_CQ_NORMAL,
00173     CCN_CQ_SLOW,
00174     CCN_CQ_N
00175 };
00176 
00177 /**
00178  * Face meter index
00179  */
00180 enum ccnd_face_meter_index {
00181     FM_BYTI,
00182     FM_BYTO,
00183     FM_DATI,
00184     FM_INTO,
00185     FM_DATO,
00186     FM_INTI,
00187     CCND_FACE_METER_N
00188 };
00189 
00190 /**
00191  * One of our active faces
00192  */
00193 struct face {
00194     int recv_fd;                /**< socket for receiving */
00195     unsigned sendface;          /**< faceid for sending (maybe == faceid) */
00196     int flags;                  /**< CCN_FACE_* face flags */
00197     int surplus;                /**< sends since last successful recv */
00198     unsigned faceid;            /**< internal face id */
00199     unsigned recvcount;         /**< for activity level monitoring */
00200     struct content_queue *q[CCN_CQ_N]; /**< outgoing content, per delay class */
00201     struct ccn_charbuf *inbuf;
00202     struct ccn_skeleton_decoder decoder;
00203     size_t outbufindex;
00204     struct ccn_charbuf *outbuf;
00205     const struct sockaddr *addr;
00206     socklen_t addrlen;
00207     int pending_interests;
00208     unsigned rrun;
00209     uintmax_t rseq;
00210     struct ccnd_meter *meter[CCND_FACE_METER_N];
00211     unsigned short pktseq;     /**< sequence number for sent packets */
00212 };
00213 
00214 /** face flags */
00215 #define CCN_FACE_LINK   (1 << 0) /**< Elements wrapped by CCNProtocolDataUnit */
00216 #define CCN_FACE_DGRAM  (1 << 1) /**< Datagram interface, respect packets */
00217 #define CCN_FACE_GG     (1 << 2) /**< Considered friendly */
00218 #define CCN_FACE_LOCAL  (1 << 3) /**< PF_UNIX socket */
00219 #define CCN_FACE_INET   (1 << 4) /**< IPv4 */
00220 #define CCN_FACE_MCAST  (1 << 5) /**< a party line (e.g. multicast) */
00221 #define CCN_FACE_INET6  (1 << 6) /**< IPv6 */
00222 #define CCN_FACE_DC     (1 << 7) /**< Direct control face */
00223 #define CCN_FACE_NOSEND (1 << 8) /**< Don't send anymore */
00224 #define CCN_FACE_UNDECIDED (1 << 9) /**< Might not be talking ccn */
00225 #define CCN_FACE_PERMANENT (1 << 10) /**< No timeout for inactivity */
00226 #define CCN_FACE_CONNECTING (1 << 11) /**< Connect in progress */
00227 #define CCN_FACE_LOOPBACK (1 << 12) /**< v4 or v6 loopback address */
00228 #define CCN_FACE_CLOSING (1 << 13) /**< close stream when output is done */
00229 #define CCN_FACE_PASSIVE (1 << 14) /**< a listener or a bound dgram socket */
00230 #define CCN_FACE_NORECV (1 << 15) /**< use for sending only */
00231 #define CCN_FACE_REGOK (1 << 16) /**< Allowed to do prefix registration */
00232 #define CCN_FACE_SEQOK (1 << 17) /** OK to send SequenceNumber link messages */
00233 #define CCN_FACE_SEQPROBE (1 << 18) /** SequenceNumber probe */
00234 #define CCN_FACE_LC    (1 << 19) /** A link check has been issued recently */
00235 #define CCN_NOFACEID    (~0U)    /** denotes no face */
00236 
00237 /**
00238  *  The content hash table is keyed by the initial portion of the ContentObject
00239  *  that contains all the parts of the complete name.  The extdata of the hash
00240  *  table holds the rest of the object, so that the whole ContentObject is
00241  *  stored contiguously.  The internal form differs from the on-wire form in
00242  *  that the final content-digest name component is represented explicitly,
00243  *  which simplifies the matching logic.
00244  *  The original ContentObject may be reconstructed simply by excising this
00245  *  last name component, which is easily located via the comps array.
00246  */
00247 struct content_entry {
00248     ccn_accession_t accession;  /**< assigned in arrival order */
00249     unsigned short *comps;      /**< Name Component byte boundary offsets */
00250     int ncomps;                 /**< Number of name components plus one */
00251     int flags;                  /**< see below */
00252     const unsigned char *key;   /**< ccnb-encoded ContentObject */
00253     int key_size;               /**< Size of fragment prior to Content */
00254     int size;                   /**< Size of ContentObject */
00255     struct ccn_indexbuf *skiplinks; /**< skiplist for name-ordered ops */
00256 };
00257 
00258 /**
00259  * content_entry flags
00260  */
00261 #define CCN_CONTENT_ENTRY_SLOWSEND  1
00262 #define CCN_CONTENT_ENTRY_STALE     2
00263 #define CCN_CONTENT_ENTRY_PRECIOUS  4
00264 
00265 /**
00266  * The sparse_straggler hash table, keyed by accession, holds scattered
00267  * entries that would otherwise bloat the direct content_by_accession table.
00268  */
00269 struct sparse_straggler_entry {
00270     struct content_entry *content;
00271 };
00272 
00273 /**
00274  * The propagating interest hash table is keyed by Nonce.
00275  *
00276  * While the interest is pending, the pe is also kept in a doubly-linked
00277  * list off of a nameprefix_entry.
00278  *
00279  * When the interest is consumed, the pe is removed from the doubly-linked
00280  * list and is cleaned up by freeing unnecessary bits (including the interest
00281  * message itself).  It remains in the hash table for a time, in order to catch
00282  * duplicate nonces.
00283  */
00284 struct propagating_entry {
00285     struct propagating_entry *next; /**< next (in arrival order) */
00286     struct propagating_entry *prev; /**< previous (older) */
00287     unsigned flags;             /**< CCN_PR_xxx */
00288     unsigned faceid;            /**< origin of the interest, dest for matches */
00289     int usec;                   /**< usec until timeout */
00290     int sent;                   /**< leading faceids of outbound processed */
00291     struct ccn_indexbuf *outbound; /**< in order of use */
00292     unsigned char *interest_msg; /**< pending interest message */
00293     unsigned size;              /**< size in bytes of interest_msg */
00294     int fgen;                   /**< decide if outbound is stale */
00295 };
00296 // XXX - with new outbound/sent repr, some of these flags may not be needed.
00297 #define CCN_PR_UNSENT   0x01 /**< interest has not been sent anywhere yet */
00298 #define CCN_PR_WAIT1    0x02 /**< interest has been sent to one place */
00299 #define CCN_PR_STUFFED1 0x04 /**< was stuffed before sent anywhere else */
00300 #define CCN_PR_TAP      0x08 /**< at least one tap face is present */
00301 #define CCN_PR_EQV      0x10 /**< a younger similar interest exists */
00302 #define CCN_PR_SCOPE0   0x20 /**< interest scope is 0 */
00303 #define CCN_PR_SCOPE1   0x40 /**< interest scope is 1 (this host) */
00304 #define CCN_PR_SCOPE2   0x80 /**< interest scope is 2 (immediate neighborhood) */
00305 
00306 /**
00307  * The nameprefix hash table is keyed by the Component elements of
00308  * the Name prefix.
00309  */
00310 struct nameprefix_entry {
00311     struct propagating_entry pe_head; /**< list head for propagating entries */
00312     struct ccn_indexbuf *forward_to; /**< faceids to forward to */
00313     struct ccn_indexbuf *tap;    /**< faceids to forward to as tap*/
00314     struct ccn_forwarding *forwarding; /**< detailed forwarding info */
00315     struct nameprefix_entry *parent; /**< link to next-shorter prefix */
00316     int children;                /**< number of children */
00317     unsigned flags;              /**< CCN_FORW_* flags about namespace */
00318     int fgen;                    /**< used to decide when forward_to is stale */
00319     unsigned src;                /**< faceid of recent content source */
00320     unsigned osrc;               /**< and of older matching content */
00321     unsigned usec;               /**< response-time prediction */
00322 };
00323 
00324 /**
00325  * Keeps track of the faces that interests matching a given name prefix may be
00326  * forwarded to.
00327  */
00328 struct ccn_forwarding {
00329     unsigned faceid;             /**< locally unique number identifying face */
00330     unsigned flags;              /**< CCN_FORW_* - c.f. <ccn/reg_mgnt.h> */
00331     int expires;                 /**< time remaining, in seconds */
00332     struct ccn_forwarding *next;
00333 };
00334 
00335 /* create and destroy procs for separately allocated meters */
00336 struct ccnd_meter *ccnd_meter_create(struct ccnd_handle *h, const char *what);
00337 void ccnd_meter_destroy(struct ccnd_meter **);
00338 
00339 /* for meters kept within other structures */
00340 void ccnd_meter_init(struct ccnd_handle *h, struct ccnd_meter *m, const char *what);
00341 
00342 /* count something (messages, packets, bytes), getting time info from h */
00343 void ccnd_meter_bump(struct ccnd_handle *h, struct ccnd_meter *m, unsigned amt);
00344 
00345 unsigned ccnd_meter_rate(struct ccnd_handle *h, struct ccnd_meter *m);
00346 uintmax_t ccnd_meter_total(struct ccnd_meter *m);
00347 
00348 
00349 /**
00350  * Refer to doc/technical/Registration.txt for the meaning of these flags.
00351  *
00352  * @def CCN_FORW_ACTIVE         1
00353  * @def CCN_FORW_CHILD_INHERIT  2
00354  * @def CCN_FORW_ADVERTISE      4
00355  * @def CCN_FORW_LAST           8
00356  * @def CCN_FORW_CAPTURE       16
00357  * @def CCN_FORW_LOCAL         32
00358  * @def CCN_FORW_TAP           64
00359  * @def CCN_FORW_CAPTURE_OK   128
00360  */
00361 #define CCN_FORW_PFXO (CCN_FORW_ADVERTISE | CCN_FORW_CAPTURE | CCN_FORW_LOCAL)
00362 #define CCN_FORW_REFRESHED      (1 << 16) /**< private to ccnd */
00363 
00364  
00365 /**
00366  * Determines how frequently we age our forwarding entries
00367  */
00368 #define CCN_FWU_SECS 5
00369 
00370 /*
00371  * Internal client
00372  * The internal client is for communication between the ccnd and other
00373  * components, using (of course) ccn protocols.
00374  */
00375 int ccnd_init_internal_keystore(struct ccnd_handle *);
00376 int ccnd_internal_client_start(struct ccnd_handle *);
00377 void ccnd_internal_client_stop(struct ccnd_handle *);
00378 
00379 /*
00380  * The internal client calls this with the argument portion ARG of
00381  * a face-creation request (/ccnx/CCNDID/newface/ARG)
00382  */
00383 int ccnd_req_newface(struct ccnd_handle *h,
00384                      const unsigned char *msg, size_t size,
00385                      struct ccn_charbuf *reply_body);
00386 
00387 /*
00388  * The internal client calls this with the argument portion ARG of
00389  * a face-destroy request (/ccnx/CCNDID/destroyface/ARG)
00390  */
00391 int ccnd_req_destroyface(struct ccnd_handle *h,
00392                          const unsigned char *msg, size_t size,
00393                          struct ccn_charbuf *reply_body);
00394 
00395 /*
00396  * The internal client calls this with the argument portion ARG of
00397  * a prefix-registration request (/ccnx/CCNDID/prefixreg/ARG)
00398  */
00399 int ccnd_req_prefixreg(struct ccnd_handle *h,
00400                        const unsigned char *msg, size_t size,
00401                        struct ccn_charbuf *reply_body);
00402 
00403 /*
00404  * The internal client calls this with the argument portion ARG of
00405  * a prefix-registration request for self (/ccnx/CCNDID/selfreg/ARG)
00406  */
00407 int ccnd_req_selfreg(struct ccnd_handle *h,
00408                      const unsigned char *msg, size_t size,
00409                      struct ccn_charbuf *reply_body);
00410 
00411 /**
00412  * URIs for prefixes served by the internal client
00413  */
00414 #define CCNDID_LOCAL_URI "ccnx:/%C1.M.S.localhost/%C1.M.SRV/ccnd/KEY"
00415 #define CCNDID_NEIGHBOR_URI "ccnx:/%C1.M.S.neighborhood/%C1.M.SRV/ccnd/KEY"
00416 
00417 /*
00418  * The internal client calls this with the argument portion ARG of
00419  * a prefix-unregistration request (/ccnx/CCNDID/unreg/ARG)
00420  */
00421 int ccnd_req_unreg(struct ccnd_handle *h,
00422                    const unsigned char *msg, size_t size,
00423                    struct ccn_charbuf *reply_body);
00424 
00425 int ccnd_reg_uri(struct ccnd_handle *h,
00426                  const char *uri,
00427                  unsigned faceid,
00428                  int flags,
00429                  int expires);
00430 
00431 struct face *ccnd_face_from_faceid(struct ccnd_handle *, unsigned);
00432 void ccnd_face_status_change(struct ccnd_handle *, unsigned);
00433 int ccnd_destroy_face(struct ccnd_handle *h, unsigned faceid);
00434 void ccnd_send(struct ccnd_handle *h, struct face *face,
00435                const void *data, size_t size);
00436 
00437 /* Consider a separate header for these */
00438 int ccnd_stats_handle_http_connection(struct ccnd_handle *, struct face *);
00439 void ccnd_msg(struct ccnd_handle *, const char *, ...);
00440 void ccnd_debug_ccnb(struct ccnd_handle *h,
00441                      int lineno,
00442                      const char *msg,
00443                      struct face *face,
00444                      const unsigned char *ccnb,
00445                      size_t ccnb_size);
00446 
00447 struct ccnd_handle *ccnd_create(const char *, ccnd_logger, void *);
00448 void ccnd_run(struct ccnd_handle *h);
00449 void ccnd_destroy(struct ccnd_handle **);
00450 extern const char *ccnd_usage_message;
00451 
00452 #endif
Generated on Sun Apr 22 23:31:52 2012 for Content-Centric Networking in C by  doxygen 1.6.3