Show idr_main.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.
*
*/
/*
* idr.c: Reliable NeighborCast.
* A fast, best effort mechanism for broadcast reliability
* For more information about RNC, see
* http://lecs.cs.ucla.edu/~vladimir/idr
*/
char idr_main_c_cvsid[] = "$Id: idr_main.c,v 1.5 2003/02/15 23:45:48 "\
" vladimir Exp $";
#include <stdio.h>
#include <stdlib.h>
#include <emrun/emrun.h>
#include <link/link.h>
#include <link/neighbor.h>
#include "idr_i.h"
#define MINUTE 60000 // 60.000 milliseconds
void log_packets(idr_state_t *s);
/*****************************************************************/
/*
* Callback when we are asked to shut down by emrun
*/
static void idr_shutdown(void *data)
{
idr_state_t *s = (idr_state_t *)data;
if (s==NULL) {
printf("NULL POINTER!!!\n");
}
elog(LOG_CRIT, "idr daemon shutting down");
log_packets(s);
exit(0);
}
// extra pathname for output filenames
#define MAX_EXTRA_NAME 30
char extra_name[MAX_EXTRA_NAME+1]="/tmp";
static void usage(void)
{
fprintf(stderr,
"idrd release: %s\n"
"\n"
"usage: idrd [-i link index] [-a ack period] [-p output path]"\
" [-d dump timer interval]\n"
"\n"
"-i: Integer link index for underlying network interface"\
" to use --\n"
" e.g., '0' uses /dev/link/0/data\n"
" default index is first-available\n"
"-P: Pot value\n"
"-u: output number\n"
"\n"
,CVSTAG
);
exit(1);
}
int main(int argc, char *argv[])
{
int POT=50;
idr_state_t f;
int use_default_link = 1, arg;
emrun_opts_t emrun_opts = {
shutdown: idr_shutdown,
data: (void *) &f
};
// neighbor options
neighbor_opts_t n_opts = {
link_opts: {
link_index: LINK_INDEX_AUTO
},
new_list: new_neighbor_list,
data: (void *) &f
};
// generic initialization common to all programs
misc_init(&argc, argv, CVSTAG);
memset(&f, 0, sizeof(f));
f.pot=POT;
//********** Parse command-line arguments ************
opterr = 0;
while ((arg = getopt(argc, argv, "hi:P:u:")) != EOF) {
switch (arg) {
case 'h':
usage();
break;
case 'i':
if (!isdigit(*optarg)) {
elog_g(LOG_CRIT, "invalid link index '%s'", optarg);
exit(1);
} else {
use_default_link = 0;
idr_open_link(&f, atoi(optarg), POT);
}
break;
case 'P':
if (!isdigit(*optarg)) {
elog_g(LOG_CRIT, "invalid pot value '%s'", optarg);
exit(1);
} else {
POT = atoi(optarg);
}
break;
case 'u':
if (!isdigit(*optarg)) {
elog_g(LOG_CRIT, "invalid out value '%s'", optarg);
exit(1);
} else {
f.pot=atoi(optarg);
}
break;
case '?':
elog_g(LOG_CRIT, "invalid option '-%c' (use -h for help)", optopt);
exit(1);
break;
default:
usage();
break;
}
}
// advance past parsed options
argc -= optind;
argv += optind;
elog_g(LOG_INFO, "using output path name %s", extra_name);
// If no link interfaces were explicitly given with -i arguments,
// open the default interface
if (use_default_link)
idr_open_link(&f, LINK_INDEX_AUTO, POT);
// make sure there's at least one interface open
if (f.num_ifaces == 0) {
elog_g(LOG_EMERG, "idr has no interfaces in use!");
exit(1);
}
// Create the upper packetdev interface
idr_register_packetdev(&f);
// Create the conf interface
idr_conf_init(&f);
// Initialize the util stuff
idr_util_init(&f);
/* Open a link to the neighborlist service */
if (g_neighbors(&n_opts, NULL) < 0) {
elog(LOG_CRIT, "couldn't open neighborlist %s: %m",
link_name(&n_opts.link_opts));
exit(1);
} else {
elog(LOG_NOTICE, "opened neighbor list %s successfully",
link_name(&n_opts.link_opts));
}
// Start the event loop running - it should never exit (the shutdown
// handler is called when the program is supposed to stop)
emrun_init(&emrun_opts); /* this should be the last initialization */
g_main();
elog_g(LOG_CRIT, "event loop terminated abnormally!");
return 0;
}
void log_packets(idr_state_t *s)
{
char buf[255]={0};
int j, fd;
if (my_node_id==1) // bail if I am the sink
return;
j=sprintf(buf, "/tmp/idrd_p_%d_%d", my_node_id, s->pot);
if ((fd=open(buf, O_WRONLY|O_CREAT|O_APPEND, 0644)) < 0) {
elog(LOG_ERR, "Unable to open %s\n", buf);
perror("open");
return;
}
memset(&buf, 0, sizeof(buf));
j=sprintf(buf, "%d\n",s->status.retno);
write(fd, &buf, j);
write(fd, (char *)"\n", 1);
close(fd);
}
See more files for this project here