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 interest_entry;
00057 struct pit_face_item;
00058 struct content_tree_node;
00059 struct ccn_forwarding;
00060 struct ccn_strategy;
00061 
00062 //typedef uint_least64_t ccn_accession_t;
00063 typedef unsigned ccn_accession_t;
00064 
00065 /**
00066  * Used for keeping track of interest expiry.
00067  *
00068  * Modulo 2**32, time units and origin are abitrary and private.
00069  */
00070 typedef uint32_t ccn_wrappedtime;
00071 
00072 typedef int (*ccnd_logger)(void *loggerdata, const char *format, va_list ap);
00073 
00074 /**
00075  * We pass this handle almost everywhere within ccnd
00076  */
00077 struct ccnd_handle {
00078     unsigned char ccnd_id[32];      /**< sha256 digest of our public key */
00079     struct hashtb *faces_by_fd;     /**< keyed by fd */
00080     struct hashtb *dgram_faces;     /**< keyed by sockaddr */
00081     struct hashtb *content_tab;     /**< keyed by portion of ContentObject */
00082     struct hashtb *nameprefix_tab;  /**< keyed by name prefix components */
00083     struct hashtb *interest_tab;    /**< keyed by interest msg sans Nonce */
00084     struct ccn_indexbuf *skiplinks; /**< skiplist for content-ordered ops */
00085     unsigned forward_to_gen;        /**< for forward_to updates */
00086     unsigned face_gen;              /**< faceid generation number */
00087     unsigned face_rover;            /**< for faceid allocation */
00088     unsigned face_limit;            /**< current number of face slots */
00089     struct face **faces_by_faceid;  /**< array with face_limit elements */
00090     struct ccn_scheduled_event *reaper;
00091     struct ccn_scheduled_event *age;
00092     struct ccn_scheduled_event *clean;
00093     struct ccn_scheduled_event *age_forwarding;
00094     const char *portstr;            /**< "main" port number */
00095     unsigned ipv4_faceid;           /**< wildcard IPv4, bound to port */
00096     unsigned ipv6_faceid;           /**< wildcard IPv6, bound to port */
00097     nfds_t nfds;                    /**< number of entries in fds array */
00098     struct pollfd *fds;             /**< used for poll system call */
00099     struct ccn_gettime ticktock;    /**< our time generator */
00100     long sec;                       /**< cached gettime seconds */
00101     unsigned usec;                  /**< cached gettime microseconds */
00102     ccn_wrappedtime wtnow;          /**< corresponding wrapped time */
00103     long 
00104     int sliver;                     /**< extra microseconds beyond wtnow */
00105     long starttime;                 /**< ccnd start time, in seconds */
00106     unsigned starttime_usec;        /**< ccnd start time fractional part */
00107     unsigned iserial;
00108     struct ccn_schedule *sched;     /**< our schedule */
00109     struct ccn_charbuf *send_interest_scratch; /**< for use by send_interest */
00110     struct ccn_charbuf *scratch_charbuf; /**< one-slot scratch cache */
00111     struct ccn_indexbuf *scratch_indexbuf; /**< one-slot scratch cache */
00112     /** Next three fields are used for direct accession-to-content table */
00113     ccn_accession_t accession_base;
00114     unsigned content_by_accession_window;
00115     struct content_entry **content_by_accession;
00116     /** The following holds stragglers that would otherwise bloat the above */
00117     struct hashtb *sparse_straggler_tab; /* keyed by accession */
00118     ccn_accession_t accession;      /**< newest used accession number */
00119     ccn_accession_t min_stale;      /**< smallest accession of stale content */
00120     ccn_accession_t max_stale;      /**< largest accession of stale content */
00121     unsigned long capacity;         /**< may toss content if there more than
00122                                      this many content objects in the store */
00123     unsigned long n_stale;          /**< Number of stale content objects */
00124     struct ccn_indexbuf *unsol;     /**< unsolicited content */
00125     unsigned long oldformatcontent;
00126     unsigned long oldformatcontentgrumble;
00127     unsigned long oldformatinterests;
00128     unsigned long oldformatinterestgrumble;
00129     unsigned long content_dups_recvd;
00130     unsigned long content_items_sent;
00131     unsigned long interests_accepted;
00132     unsigned long interests_dropped;
00133     unsigned long interests_sent;
00134     unsigned long interests_stuffed;
00135     unsigned short seed[3];         /**< for PRNG */
00136     int running;                    /**< true while should be running */
00137     int debug;                      /**< For controlling debug output */
00138     ccnd_logger logger;             /**< For debug output */
00139     void *loggerdata;               /**< Passed to logger */
00140     int logbreak;                   /**< see ccn_msg() */
00141     unsigned long logtime;          /**< see ccn_msg() */
00142     int logpid;                     /**< see ccn_msg() */
00143     int mtu;                        /**< Target size for stuffing interests */
00144     int flood;                      /**< Internal control for auto-reg */
00145     struct ccn_charbuf *autoreg;    /**< URIs to auto-register */
00146     int force_zero_freshness;       /**< Simulate freshness=0 on all content */
00147     unsigned interest_faceid;       /**< for self_reg internal client */
00148     const char *progname;           /**< our name, for locating helpers */
00149     struct ccn *internal_client;    /**< internal client */
00150     struct face *face0;             /**< special face for internal client */
00151     struct ccn_charbuf *service_ccnb; /**< for local service discovery */
00152     struct ccn_charbuf *neighbor_ccnb; /**< for neighbor service discovery */
00153     struct ccn_seqwriter *notice;   /**< for notices of status changes */
00154     struct ccn_indexbuf *chface;    /**< faceids w/ recent status changes */
00155     struct ccn_scheduled_event *internal_client_refresh;
00156     struct ccn_scheduled_event *notice_push;
00157     unsigned data_pause_microsec;   /**< tunable, see choose_face_delay() */
00158     int (*noncegen)(struct ccnd_handle *, struct face *, unsigned char *);
00159                                     /**< pluggable nonce generation */
00160     int tts_default;                /**< CCND_DEFAULT_TIME_TO_STALE (seconds) */
00161     int tts_limit;                  /**< CCND_MAX_TIME_TO_STALE (seconds) */
00162 };
00163 
00164 /**
00165  * Each face is referenced by a number, the faceid.  The low-order
00166  * bits (under the MAXFACES) constitute a slot number that is
00167  * unique (for this ccnd) among the faces that are alive at a given time.
00168  * The rest of the bits form a generation number that make the
00169  * entire faceid unique over time, even for faces that are defunct.
00170  */
00171 #define FACESLOTBITS 18
00172 #define MAXFACES ((1U << FACESLOTBITS) - 1)
00173 
00174 struct content_queue {
00175     unsigned burst_nsec;             /**< nsec per KByte, limits burst rate */
00176     unsigned min_usec;               /**< minimum delay for this queue */
00177     unsigned rand_usec;              /**< randomization range */
00178     unsigned ready;                  /**< # that have waited enough */
00179     unsigned nrun;                   /**< # sent since last randomized delay */
00180     struct ccn_indexbuf *send_queue; /**< accession numbers of pending content */
00181     struct ccn_scheduled_event *sender;
00182 };
00183 
00184 enum cq_delay_class {
00185     CCN_CQ_ASAP,
00186     CCN_CQ_NORMAL,
00187     CCN_CQ_SLOW,
00188     CCN_CQ_N
00189 };
00190 
00191 /**
00192  * Face meter index
00193  */
00194 enum ccnd_face_meter_index {
00195     FM_BYTI,
00196     FM_BYTO,
00197     FM_DATI,
00198     FM_INTO,
00199     FM_DATO,
00200     FM_INTI,
00201     CCND_FACE_METER_N
00202 };
00203 
00204 /**
00205  * One of our active faces
00206  */
00207 struct face {
00208     int recv_fd;                /**< socket for receiving */
00209     unsigned sendface;          /**< faceid for sending (maybe == faceid) */
00210     int flags;                  /**< CCN_FACE_* face flags */
00211     int surplus;                /**< sends since last successful recv */
00212     unsigned faceid;            /**< internal face id */
00213     unsigned recvcount;         /**< for activity level monitoring */
00214     struct content_queue *q[CCN_CQ_N]; /**< outgoing content, per delay class */
00215     struct ccn_charbuf *inbuf;
00216     struct ccn_skeleton_decoder decoder;
00217     size_t outbufindex;
00218     struct ccn_charbuf *outbuf;
00219     const struct sockaddr *addr;
00220     socklen_t addrlen;
00221     int pending_interests;
00222     unsigned rrun;
00223     uintmax_t rseq;
00224     struct ccnd_meter *meter[CCND_FACE_METER_N];
00225     unsigned short pktseq;     /**< sequence number for sent packets */
00226 };
00227 
00228 /** face flags */
00229 #define CCN_FACE_LINK   (1 << 0) /**< Elements wrapped by CCNProtocolDataUnit */
00230 #define CCN_FACE_DGRAM  (1 << 1) /**< Datagram interface, respect packets */
00231 #define CCN_FACE_GG     (1 << 2) /**< Considered friendly */
00232 #define CCN_FACE_LOCAL  (1 << 3) /**< PF_UNIX socket */
00233 #define CCN_FACE_INET   (1 << 4) /**< IPv4 */
00234 #define CCN_FACE_MCAST  (1 << 5) /**< a party line (e.g. multicast) */
00235 #define CCN_FACE_INET6  (1 << 6) /**< IPv6 */
00236 #define CCN_FACE_DC     (1 << 7) /**< Direct control face */
00237 #define CCN_FACE_NOSEND (1 << 8) /**< Don't send anymore */
00238 #define CCN_FACE_UNDECIDED (1 << 9) /**< Might not be talking ccn */
00239 #define CCN_FACE_PERMANENT (1 << 10) /**< No timeout for inactivity */
00240 #define CCN_FACE_CONNECTING (1 << 11) /**< Connect in progress */
00241 #define CCN_FACE_LOOPBACK (1 << 12) /**< v4 or v6 loopback address */
00242 #define CCN_FACE_CLOSING (1 << 13) /**< close stream when output is done */
00243 #define CCN_FACE_PASSIVE (1 << 14) /**< a listener or a bound dgram socket */
00244 #define CCN_FACE_NORECV (1 << 15) /**< use for sending only */
00245 #define CCN_FACE_REGOK (1 << 16) /**< Allowed to do prefix registration */
00246 #define CCN_FACE_SEQOK (1 << 17) /** OK to send SequenceNumber link messages */
00247 #define CCN_FACE_SEQPROBE (1 << 18) /** SequenceNumber probe */
00248 #define CCN_FACE_LC    (1 << 19) /** A link check has been issued recently */
00249 #define CCN_NOFACEID    (~0U)    /** denotes no face */
00250 
00251 /**
00252  *  The content hash table is keyed by the initial portion of the ContentObject
00253  *  that contains all the parts of the complete name.  The extdata of the hash
00254  *  table holds the rest of the object, so that the whole ContentObject is
00255  *  stored contiguously.  The internal form differs from the on-wire form in
00256  *  that the final content-digest name component is represented explicitly,
00257  *  which simplifies the matching logic.
00258  *  The original ContentObject may be reconstructed simply by excising this
00259  *  last name component, which is easily located via the comps array.
00260  */
00261 struct content_entry {
00262     ccn_accession_t accession;  /**< assigned in arrival order */
00263     unsigned short *comps;      /**< Name Component byte boundary offsets */
00264     int ncomps;                 /**< Number of name components plus one */
00265     int flags;                  /**< see below */
00266     const unsigned char *key;   /**< ccnb-encoded ContentObject */
00267     int key_size;               /**< Size of fragment prior to Content */
00268     int size;                   /**< Size of ContentObject */
00269     struct ccn_indexbuf *skiplinks; /**< skiplist for name-ordered ops */
00270 };
00271 
00272 /**
00273  * content_entry flags
00274  */
00275 #define CCN_CONTENT_ENTRY_SLOWSEND  1
00276 #define CCN_CONTENT_ENTRY_STALE     2
00277 #define CCN_CONTENT_ENTRY_PRECIOUS  4
00278 
00279 /**
00280  * The sparse_straggler hash table, keyed by accession, holds scattered
00281  * entries that would otherwise bloat the direct content_by_accession table.
00282  */
00283 struct sparse_straggler_entry {
00284     struct content_entry *content;
00285 };
00286 
00287 /**
00288  * State for the strategy engine
00289  *
00290  * This is still quite embryonic.
00291  */
00292 struct ccn_strategy {
00293     struct ccn_scheduled_event *ev; /**< for time-based strategy event */
00294     int state;
00295     ccn_wrappedtime birth;          /**< when interest entry was created */
00296     ccn_wrappedtime renewed;        /**< when interest entry was renewed */
00297     unsigned renewals;              /**< number of times renewed */
00298 };
00299 
00300 struct ielinks;
00301 struct ielinks {
00302     struct ielinks *next;           /**< next in list */
00303     struct ielinks *prev;           /**< previous in list */
00304     struct nameprefix_entry *npe;   /**< owning npe, or NULL for head */
00305 };
00306 
00307 /**
00308  * The interest hash table is keyed by the interest message
00309  *
00310  * The interest message has fields that do not participate in the
00311  * similarity test stripped out - in particular the nonce.
00312  *
00313  */
00314 struct interest_entry {
00315     struct ielinks ll;
00316     struct ccn_strategy strategy;   /**< state of strategy engine */
00317     struct pit_face_item *pfl;      /**< upstream and downstream faces */
00318     struct ccn_scheduled_event *ev; /**< next interest timeout */
00319     const unsigned char *interest_msg; /**< pending interest message */
00320     unsigned size;                  /**< size of interest message */
00321     unsigned serial;                /**< used for logging */
00322 };
00323 
00324 #define TYPICAL_NONCE_SIZE 12       /**< actual allocated size may differ */
00325 /**
00326  * Per-face PIT information
00327  *
00328  * This is used to track the pending interest info that is specific to
00329  * a face.  The list may contain up to two entries for a given face - one
00330  * to track the most recent arrival on that face (the downstream), and
00331  * one to track the most recently sent (the upstream).
00332  */
00333 struct pit_face_item {
00334     struct pit_face_item *next;     /**< next in list */
00335     unsigned faceid;                /**< face id */
00336     ccn_wrappedtime renewed;        /**< when entry was last refreshed */
00337     ccn_wrappedtime expiry;         /**< when entry expires */
00338     unsigned pfi_flags;             /**< CCND_PFI_x */
00339     unsigned char nonce[TYPICAL_NONCE_SIZE]; /**< nonce bytes */
00340 };
00341 #define CCND_PFI_NONCESZ  0x00FF    /**< Mask for actual nonce size */
00342 #define CCND_PFI_UPSTREAM 0x0100    /**< Tracks upstream (sent interest) */
00343 #define CCND_PFI_UPENDING 0x0200    /**< Has been sent upstream */
00344 #define CCND_PFI_SENDUPST 0x0400    /**< Should be sent upstream */
00345 #define CCND_PFI_UPHUNGRY 0x0800    /**< Upstream hungry, cupboard bare */
00346 #define CCND_PFI_DNSTREAM 0x1000    /**< Tracks downstream (recvd interest) */
00347 #define CCND_PFI_PENDING  0x2000    /**< Pending for immediate data */
00348 #define CCND_PFI_SUPDATA  0x4000    /**< Suppressed data reply */
00349 #define CCND_PFI_DCFACE  0x10000    /**< This upstream is a DC face */
00350 
00351 /**
00352  * The nameprefix hash table is keyed by the Component elements of
00353  * the Name prefix.
00354  */
00355 struct nameprefix_entry {
00356     struct ielinks ie_head;      /**< list head for interest entries */
00357     struct ccn_indexbuf *forward_to; /**< faceids to forward to */
00358     struct ccn_indexbuf *tap;    /**< faceids to forward to as tap */
00359     struct ccn_forwarding *forwarding; /**< detailed forwarding info */
00360     struct nameprefix_entry *parent; /**< link to next-shorter prefix */
00361     int children;                /**< number of children */
00362     unsigned flags;              /**< CCN_FORW_* flags about namespace */
00363     int fgen;                    /**< used to decide when forward_to is stale */
00364     unsigned src;                /**< faceid of recent content source */
00365     unsigned osrc;               /**< and of older matching content */
00366     unsigned usec;               /**< response-time prediction */
00367 };
00368 
00369 /**
00370  * Keeps track of the faces that interests matching a given name prefix may be
00371  * forwarded to.
00372  */
00373 struct ccn_forwarding {
00374     unsigned faceid;             /**< locally unique number identifying face */
00375     unsigned flags;              /**< CCN_FORW_* - c.f. <ccn/reg_mgnt.h> */
00376     int expires;                 /**< time remaining, in seconds */
00377     struct ccn_forwarding *next;
00378 };
00379 
00380 /* create and destroy procs for separately allocated meters */
00381 struct ccnd_meter *ccnd_meter_create(struct ccnd_handle *h, const char *what);
00382 void ccnd_meter_destroy(struct ccnd_meter **);
00383 
00384 /* for meters kept within other structures */
00385 void ccnd_meter_init(struct ccnd_handle *h, struct ccnd_meter *m, const char *what);
00386 
00387 /* count something (messages, packets, bytes), getting time info from h */
00388 void ccnd_meter_bump(struct ccnd_handle *h, struct ccnd_meter *m, unsigned amt);
00389 
00390 unsigned ccnd_meter_rate(struct ccnd_handle *h, struct ccnd_meter *m);
00391 uintmax_t ccnd_meter_total(struct ccnd_meter *m);
00392 
00393 
00394 /**
00395  * Refer to doc/technical/Registration.txt for the meaning of these flags.
00396  *
00397  * @def CCN_FORW_ACTIVE         1
00398  * @def CCN_FORW_CHILD_INHERIT  2
00399  * @def CCN_FORW_ADVERTISE      4
00400  * @def CCN_FORW_LAST           8
00401  * @def CCN_FORW_CAPTURE       16
00402  * @def CCN_FORW_LOCAL         32
00403  * @def CCN_FORW_TAP           64
00404  * @def CCN_FORW_CAPTURE_OK   128
00405  */
00406 #define CCN_FORW_PFXO (CCN_FORW_ADVERTISE | CCN_FORW_CAPTURE | CCN_FORW_LOCAL)
00407 #define CCN_FORW_REFRESHED      (1 << 16) /**< private to ccnd */
00408 
00409  
00410 /**
00411  * Determines how frequently we age our forwarding entries
00412  */
00413 #define CCN_FWU_SECS 5
00414 
00415 /*
00416  * Internal client
00417  * The internal client is for communication between the ccnd and other
00418  * components, using (of course) ccn protocols.
00419  */
00420 int ccnd_init_internal_keystore(struct ccnd_handle *);
00421 int ccnd_internal_client_start(struct ccnd_handle *);
00422 void ccnd_internal_client_stop(struct ccnd_handle *);
00423 
00424 /*
00425  * The internal client calls this with the argument portion ARG of
00426  * a face-creation request (/ccnx/CCNDID/newface/ARG)
00427  */
00428 int ccnd_req_newface(struct ccnd_handle *h,
00429                      const unsigned char *msg, size_t size,
00430                      struct ccn_charbuf *reply_body);
00431 
00432 /*
00433  * The internal client calls this with the argument portion ARG of
00434  * a face-destroy request (/ccnx/CCNDID/destroyface/ARG)
00435  */
00436 int ccnd_req_destroyface(struct ccnd_handle *h,
00437                          const unsigned char *msg, size_t size,
00438                          struct ccn_charbuf *reply_body);
00439 
00440 /*
00441  * The internal client calls this with the argument portion ARG of
00442  * a prefix-registration request (/ccnx/CCNDID/prefixreg/ARG)
00443  */
00444 int ccnd_req_prefixreg(struct ccnd_handle *h,
00445                        const unsigned char *msg, size_t size,
00446                        struct ccn_charbuf *reply_body);
00447 
00448 /*
00449  * The internal client calls this with the argument portion ARG of
00450  * a prefix-registration request for self (/ccnx/CCNDID/selfreg/ARG)
00451  */
00452 int ccnd_req_selfreg(struct ccnd_handle *h,
00453                      const unsigned char *msg, size_t size,
00454                      struct ccn_charbuf *reply_body);
00455 
00456 /**
00457  * URIs for prefixes served by the internal client
00458  */
00459 #define CCNDID_LOCAL_URI "ccnx:/%C1.M.S.localhost/%C1.M.SRV/ccnd/KEY"
00460 #define CCNDID_NEIGHBOR_URI "ccnx:/%C1.M.S.neighborhood/%C1.M.SRV/ccnd/KEY"
00461 
00462 /*
00463  * The internal client calls this with the argument portion ARG of
00464  * a prefix-unregistration request (/ccnx/CCNDID/unreg/ARG)
00465  */
00466 int ccnd_req_unreg(struct ccnd_handle *h,
00467                    const unsigned char *msg, size_t size,
00468                    struct ccn_charbuf *reply_body);
00469 
00470 int ccnd_reg_uri(struct ccnd_handle *h,
00471                  const char *uri,
00472                  unsigned faceid,
00473                  int flags,
00474                  int expires);
00475 
00476 struct face *ccnd_face_from_faceid(struct ccnd_handle *, unsigned);
00477 void ccnd_face_status_change(struct ccnd_handle *, unsigned);
00478 int ccnd_destroy_face(struct ccnd_handle *h, unsigned faceid);
00479 void ccnd_send(struct ccnd_handle *h, struct face *face,
00480                const void *data, size_t size);
00481 
00482 /* Consider a separate header for these */
00483 int ccnd_stats_handle_http_connection(struct ccnd_handle *, struct face *);
00484 void ccnd_msg(struct ccnd_handle *, const char *, ...);
00485 void ccnd_debug_ccnb(struct ccnd_handle *h,
00486                      int lineno,
00487                      const char *msg,
00488                      struct face *face,
00489                      const unsigned char *ccnb,
00490                      size_t ccnb_size);
00491 
00492 struct ccnd_handle *ccnd_create(const char *, ccnd_logger, void *);
00493 void ccnd_run(struct ccnd_handle *h);
00494 void ccnd_destroy(struct ccnd_handle **);
00495 extern const char *ccnd_usage_message;
00496 
00497 #endif
Generated on Tue Aug 21 14:54:15 2012 for Content-Centric Networking in C by  doxygen 1.6.3