Code Search for Developers
 
 
  

linkstats_passthru.c from EmStar at Krugle


Show linkstats_passthru.c syntax highlighted

/*
 *
 * Copyright (c) 2003 The Regents of the University of California.  All 
 * rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Neither the name of the University nor the names of its
 *   contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
 

/*
 * linkstats_upper.c: The "upper" half of linkstats, i.e. the data
 * interface that we provide to users of the linkstats service.
 *
 * $Id: linkstats_passthru.c,v 1.6 2004/10/14 01:24:24 girod Exp $
 */

char linkstats_upper_c_cvsid[] = "$Id: linkstats_passthru.c,v 1.6 2004/10/14 01:24:24 girod Exp $";

#include <stdio.h>
#include <stdlib.h>

#include "linkstats_i.h"


/********************************************************************/


/*
 * This callback is called whenever a packet arrives up from the
 * underlying datalink interfaces
 */
static int 
linkstats_from_lower(link_pass_ctx_t *ctx, link_pkt_t *link_pkt, int data_len)
{
  neighbor_list_t *nl = (neighbor_list_t *) link_pass_data(ctx);
  linkstats_pkt_t *linkstats_pkt = (linkstats_pkt_t *) link_pkt->data;
  link_pkt_t up_link_hdr = *link_pkt;
  buf_t *up_pkt;
  
  elog(LOG_DEBUG(70), "calling linkstats_reciever(), with pkt type: %d",
       link_pkt->type);
  
  /*
   * Make sure the packet has the right packet type.  Since we
   * specified the desired packet type when we registered to receive
   * packets, this test should never fail.
   */
  if (link_pkt->type != PKT_TYPE_LINK_STATS) {
    elog(LOG_WARNING, "got a packet not meant for us - filter failed!");
    goto done;
  }

  /*
   * Make sure the data portion of the packet is at least the size of
   * a linkstats header
   */
  if (data_len < sizeof(linkstats_pkt_t)) {
    elog(LOG_ERR, "got a short linkstats packet (%d bytes < %d)!",
         data_len, sizeof(linkstats_pkt_t));
    goto done;
  }

  /* We now know that the data pointed to by linkstats_pkt is valid */
  elog(LOG_DEBUG(3), "got linkstats packet from node [%d] with seqno:"
       " %llu, with inner type: %d",  linkstats_pkt->src_id, 
       linkstats_pkt->seqno, linkstats_pkt->inner_type);

  /* Update packet statistics for this neighbor */
  update_stats(nl, link_pkt, linkstats_pkt);
  
  /* 
   * Send it up the stack, modifying *only* the protocol number in the
   * link header 
   */
  up_link_hdr.type = linkstats_pkt->inner_type;
  
  up_pkt = buf_new();

  /* first construct the link header */
  bufcpy(up_pkt, &up_link_hdr, sizeof(up_link_hdr));

  /* followed by user data */
  bufcpy(up_pkt, linkstats_pkt->data, data_len - sizeof(linkstats_pkt_t));

  /* send it up */
  elog(LOG_DEBUG(5), "sending packet up to the application");
  link_pass_to_upper(ctx, up_pkt);

 done:
  return EVENT_RENEW;
}


/*
 * Callback when the queue of packets written to our local device file
 * is serviced
 */
static int linkstats_from_upper(link_pass_ctx_t *ctx, link_pkt_t *link_pkt_in,
				int data_len)
{
  neighbor_list_t *nl = (neighbor_list_t *) link_pass_data(ctx);

  /* pkt_out is the packet we're getting ready to transmit */
  buf_t *pkt_out = buf_new();

  /* Create the linkstats-specific header */
  linkstats_pkt_t linkstats_hdr = {
    src_id: my_node_id,
    inner_type: link_pkt_in->type,
    seqno: ++(nl->last_seqno_sent)
  };
  
  /* Create our *outgoing* link header */
  link_pkt_t link_hdr = *link_pkt_in;
  link_hdr.type = PKT_TYPE_LINK_STATS;
  
  /*
   * Construct the packet: outer link header, inner linkstats header, 
   * user payload
   */
  bufcpy(pkt_out, &link_hdr,  sizeof(link_hdr));
  bufcpy(pkt_out, &linkstats_hdr, sizeof(linkstats_hdr));
  bufcpy(pkt_out, link_pkt_in->data, data_len);
  
  elog(LOG_DEBUG(3), "LinkStats packet sent; src_id: %u, inner pkt "
       "type: %u, seqno: %llu", linkstats_hdr.src_id, 
       linkstats_hdr.inner_type, linkstats_hdr.seqno);
 
  /* Send to the lower interface */
  link_pass_to_lower(ctx, pkt_out);
  
  /* Even if the passthru is blocked, our packet is queued, so we
   * can forget about it.  So we always return 0 here to register
   * that we're prepared to process the next message */
  return 0;
}


/*
 * Register the linkstats device that other processes open to send 
 * packets and gather statistics on the link.
 */
void linkstats_register_passthru(neighbor_list_t *nl)
{ 
  /* set options for the link provider interface */
  link_pass_opts_t opts = {
    description: "Linkstats Adaptor",
    uses: {
      name: nl->link_uses,
      data: nl,
      q_opts: {
	inq_len: 200,
	//outq_len: 1
      },
      pkt_type: PKT_TYPE_LINK_STATS
    },

    provides: {
      name: nl->link_provides,
      if_class: nl->link_class,
      data: nl
    },

    pkt_from_lower: linkstats_from_lower,
    pkt_from_upper: linkstats_from_upper,

    mtu_adjust: -sizeof(linkstats_pkt_t)
  };

  /* try to register the packetdev as a link interface */
  if (link_pass_new(&opts, &(nl->pass_context)) < 0) {
    elog(LOG_CRIT, "can't create passthru %s: %m",
         link_name(&opts.provides, NULL));
    exit(1);
  }
  
  elog(LOG_INFO, "created upper interface, %s", link_pass_name(nl->pass_context, NULL));
}





See more files for this project here

EmStar

EmStar is a software system for developing and deploying wireless sensor networks involving Linux-based platforms. As the wireless sensor network community has attempted to deploy more complex designs---large-scale, long-lived systems that need self-organization and adaptivity---a number of difficult software design issues have arisen. Advances in software design have not kept pace with the capabilities of hardware. This is because designing for an adaptive, efficient, and useful sensor network has turned out to be surprisingly complex and difficult. EmStar is a Linux-based software framework, whose goal is to dramatically reduce this complexity, enabling work to be shared and reused, and simplifying and speeding the design of new sensor network applications.

Project homepage: http://cvs.cens.ucla.edu/emstar/
Programming language(s): C,Shell Script
License: other

  linkstats_i.h
  linkstats_main.c
  linkstats_passthru.c
  linkstats_status.c
  linkstats_util.c