ccnd_msg.c

Go to the documentation of this file.
00001 /**
00002  * @file ccnd_msg.c
00003  *
00004  * Logging support for ccnd.
00005  *
00006  * Part of ccnd - the CCNx Daemon.
00007  *
00008  * Copyright (C) 2008, 2009, 2011, 2012 Palo Alto Research Center, Inc.
00009  *
00010  * This work is free software; you can redistribute it and/or modify it under
00011  * the terms of the GNU General Public License version 2 as published by the
00012  * Free Software Foundation.
00013  * This work is distributed in the hope that it will be useful, but WITHOUT ANY
00014  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
00016  * for more details. You should have received a copy of the GNU General Public
00017  * License along with this program; if not, write to the
00018  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  */
00021 
00022 #include <stdio.h>
00023 #include <sys/time.h>
00024 #include <stdarg.h>
00025 #include <time.h>
00026 #include <unistd.h>
00027 
00028 #include <ccn/ccn.h>
00029 #include <ccn/ccnd.h>
00030 #include <ccn/charbuf.h>
00031 #include <ccn/hashtb.h>
00032 #include <ccn/uri.h>
00033 
00034 #include "ccnd_private.h"
00035 
00036 /**
00037  *  Produce ccnd debug output.
00038  *  Output is produced via h->logger under the control of h->debug;
00039  *  prepends decimal timestamp and process identification.
00040  *  Caller should not supply newlines.
00041  *  @param      h  the ccnd handle
00042  *  @param      fmt  printf-like format string
00043  */
00044 void
00045 ccnd_msg(struct ccnd_handle *h, const char *fmt, ...)
00046 {
00047     struct timeval t;
00048     va_list ap;
00049     struct ccn_charbuf *b;
00050     int res;
00051     time_t clock;
00052     if (h == NULL || h->debug == 0 || h->logger == 0)
00053         return;
00054     b = ccn_charbuf_create();
00055     gettimeofday(&t, NULL);
00056     if (((h->debug & 64) != 0) &&
00057         ((h->logbreak-- < 0 && t.tv_sec != h->logtime) ||
00058           t.tv_sec >= h->logtime + 30)) {
00059         clock = t.tv_sec;
00060         ccn_charbuf_putf(b, "%ld.000000 ccnd[%d]: %s ____________________ %s",
00061                          (long)t.tv_sec, h->logpid,
00062                          h->portstr ? h->portstr : "",
00063                          ctime(&clock));
00064         h->logtime = t.tv_sec;
00065         h->logbreak = 30;
00066     }
00067     ccn_charbuf_putf(b, "%ld.%06u ", (long)t.tv_sec, (unsigned)t.tv_usec);
00068     if (h->debug & 32)
00069         ccn_charbuf_putf(b, "%08x.", (unsigned)h->wtnow);
00070     ccn_charbuf_putf(b, "ccnd[%d]: %s\n", h->logpid, fmt);
00071     va_start(ap, fmt);
00072     res = (*h->logger)(h->loggerdata, (const char *)b->buf, ap);
00073     va_end(ap);
00074     ccn_charbuf_destroy(&b);
00075     /* if there's no one to hear, don't make a sound */
00076     if (res < 0)
00077         h->debug = 0;
00078 }
00079 
00080 /**
00081  *  Produce a ccnd debug trace entry.
00082  *  Output is produced by calling ccnd_msg.
00083  *  @param      h  the ccnd handle
00084  *  @param      lineno  caller's source line number (usually __LINE__)
00085  *  @param      msg  a short text tag to identify the entry
00086  *  @param      face    handle of associated face; may be NULL
00087  *  @param      ccnb    points to ccnb-encoded Interest or ContentObject
00088  *  @param      ccnb_size   is in bytes
00089  */
00090 void
00091 ccnd_debug_ccnb(struct ccnd_handle *h,
00092                 int lineno,
00093                 const char *msg,
00094                 struct face *face,
00095                 const unsigned char *ccnb,
00096                 size_t ccnb_size)
00097 {
00098     struct ccn_charbuf *c;
00099     struct ccn_parsed_interest pi;
00100     const unsigned char *nonce = NULL;
00101     size_t nonce_size = 0;
00102     const unsigned char *pubkey = NULL;
00103     size_t pubkey_size = 0;
00104     size_t i;
00105     struct interest_entry *ie = NULL;
00106     int default_lifetime = CCN_INTEREST_LIFETIME_SEC << 12;
00107     intmax_t lifetime = default_lifetime;
00108     
00109     if (h != NULL && h->debug == 0)
00110         return;
00111     if (ccn_parse_interest(ccnb, ccnb_size, &pi, NULL) >= 0) {
00112         pubkey_size = (pi.offset[CCN_PI_E_PublisherIDKeyDigest] -
00113                        pi.offset[CCN_PI_B_PublisherIDKeyDigest]);
00114         pubkey = ccnb + pi.offset[CCN_PI_B_PublisherIDKeyDigest];
00115         lifetime = ccn_interest_lifetime(ccnb, &pi);
00116         ccn_ref_tagged_BLOB(CCN_DTAG_Nonce, ccnb,
00117                   pi.offset[CCN_PI_B_Nonce],
00118                   pi.offset[CCN_PI_E_Nonce],
00119                   &nonce,
00120                   &nonce_size);
00121         ie = hashtb_lookup(h->interest_tab, ccnb, pi.offset[CCN_PI_B_Nonce]);
00122     }
00123     else {
00124         pi.min_suffix_comps = 0;
00125         pi.max_suffix_comps = 32767;
00126         pi.orderpref = 0;
00127         pi.answerfrom = CCN_AOK_DEFAULT;
00128         pi.scope = -1;
00129     }
00130     c = ccn_charbuf_create();
00131     ccn_charbuf_putf(c, "debug.%d %s ", lineno, msg);
00132     if (face != NULL)
00133         ccn_charbuf_putf(c, "%u ", face->faceid);
00134     ccn_uri_append(c, ccnb, ccnb_size, 1);
00135     ccn_charbuf_putf(c, " (%u bytes", (unsigned)ccnb_size);
00136     if (pi.min_suffix_comps != 0 || pi.max_suffix_comps != 32767) {
00137         ccn_charbuf_putf(c, ",c=%d", pi.min_suffix_comps);
00138         if (pi.min_suffix_comps != pi.max_suffix_comps) {
00139             ccn_charbuf_putf(c, ":");
00140             if (pi.max_suffix_comps != 32767)
00141                 ccn_charbuf_putf(c, "%d", pi.max_suffix_comps);
00142         }
00143     }
00144     if (pubkey_size >= 3)
00145         ccn_charbuf_putf(c, ",pb=%02X%02X%02X",
00146                          pubkey[0], pubkey[1], pubkey[2]);
00147     if (pi.orderpref != 0)
00148         ccn_charbuf_putf(c, ",cs=%d", pi.orderpref);
00149     if (pi.answerfrom != CCN_AOK_DEFAULT)
00150         ccn_charbuf_putf(c, ",aok=%#x", pi.answerfrom);
00151     if (pi.scope != -1)
00152         ccn_charbuf_putf(c, ",scope=%d", pi.scope);
00153     if (lifetime != default_lifetime) {
00154         ccn_charbuf_putf(c, ",life=%d.%04d",
00155                          (int)(lifetime >> 12),
00156                          (int)(lifetime & 0xFFF) * 10000 / 4096);
00157     }
00158     if (ie != NULL)
00159         ccn_charbuf_putf(c, ",i=%u", ie->serial);
00160     ccn_charbuf_putf(c, ")");
00161     if (nonce_size > 0) {
00162         const char *p = "";
00163         ccn_charbuf_putf(c, " ");
00164         if (nonce_size == 12)
00165             p = "CCC-P-F-T-NN";
00166         for (i = 0; i < nonce_size; i++)
00167             ccn_charbuf_putf(c, "%s%02X", (*p) && (*p++)=='-' ? "-" : "", nonce[i]);
00168     }
00169     ccnd_msg(h, "%s", ccn_charbuf_as_string(c));
00170     ccn_charbuf_destroy(&c);
00171 }
00172 
00173 /**
00174  * CCND Usage message
00175  */
00176 const char *ccnd_usage_message =
00177     "ccnd - CCNx Daemon\n"
00178     "  options: none\n"
00179     "  arguments: none\n"
00180     "  environment variables:\n"
00181     "    CCND_DEBUG=\n"
00182     "      0 - no messages\n"
00183     "      1 - basic messages (any non-zero value gets these)\n"
00184     "      2 - interest messages\n"
00185     "      4 - content messages\n"
00186     "      8 - matching details\n"
00187     "      16 - interest details\n"
00188     "      32 - gory interest details\n"
00189     "      64 - log occasional human-readable timestamps\n"
00190     "      128 - face registration debugging\n"
00191     "      bitwise OR these together for combinations; -1 gets max logging\n"
00192     "    CCN_LOCAL_PORT=\n"
00193     "      UDP port for unicast clients (default "CCN_DEFAULT_UNICAST_PORT").\n"
00194     "      Also listens on this TCP port for stream connections.\n"
00195     "      Also affects name of unix-domain socket.\n"
00196     "    CCN_LOCAL_SOCKNAME=\n"
00197     "      Name stem of unix-domain socket (default "CCN_DEFAULT_LOCAL_SOCKNAME").\n"
00198     "    CCND_CAP=\n"
00199     "      Capacity limit, in count of ContentObjects.\n"
00200     "      Not an absolute limit.\n"
00201     "    CCND_MTU=\n"
00202     "      Packet size in bytes.\n"
00203     "      If set, interest stuffing is allowed within this budget.\n"
00204     "      Single items larger than this are not precluded.\n"
00205     "    CCND_DATA_PAUSE_MICROSEC=\n"
00206     "      Adjusts content-send delay time for multicast and udplink faces\n"
00207     "    CCND_DEFAULT_TIME_TO_STALE=\n"
00208     "      Default for content objects without explicit FreshnessSeconds\n"
00209     "    CCND_MAX_TIME_TO_STALE=\n"
00210     "      Limit, in seconds, until content becomes stale\n"
00211     "    CCND_KEYSTORE_DIRECTORY=\n"
00212     "      Directory readable only by ccnd where its keystores are kept\n"
00213     "      Defaults to a private subdirectory of /var/tmp\n"
00214     "    CCND_LISTEN_ON=\n"
00215     "      List of ip addresses to listen on; defaults to wildcard\n"
00216     "    CCND_AUTOREG=\n"
00217     "      List of prefixes to auto-register on new faces initiated by peers\n"
00218     "      example: CCND_AUTOREG=ccnx:/like/this,ccnx:/and/this\n"
00219     ;
Generated on Tue Aug 21 14:54:15 2012 for Content-Centric Networking in C by  doxygen 1.6.3