/* * UPFRAME - An Extendible Framework for the Reception and Processing of UDP Data * Template for UPFRAME plugin with traffic shaper * Copyright (C) 2003-2004 ETH DDosVax team and Caspar Schlegel * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */ #include "libread.h" // fetch the read functions #include #include #include #include #include #include #include "bufferpacket.h" // data types for payload #include "udppacket.h" // packets #include "log.h" #include "getenvs.h" // environment variable handling int myreconnect = 0; /* default values for command line switches and environment variables */ char *restartCommand = NULL; char *path = "/tmp/nerf"; int logfacility = LOG_TO_STDOUT; char *logfile = NULL; int timeout = 60; // watchdog timeout struct sigaction oldsa; /* prototypes */ inline int process(bufferPacketp_t packet, int result); static void sighupHandler(int signal __attribute__ ((unused))); /* we get sent a signal when the Mgmt Process exits. In this case we wait and try to reconnect. */ static void sighupHandler(int signal __attribute__ ((unused))) { oldsa.sa_handler(signal); // stack signal handlers... myreconnect = 1; } // process data inline int process(bufferPacketp_t packet, int result) { // insert payload here return 0; } int main(int argc, char **argv) { int ret; struct sigaction sa; whandler_t whandler; bufferPacketp_t rec=NULL; struct timeval wait; unsigned int len; int loss; getEnvs(NULL, &path); // get environment variable for fifopath /* initialize logging */ logwriteSetMedium(logfacility, logfile, "skeleton"); logwrite(LOG_DEBUG, "skeleton started"); // and log while(1) { // a plugin normally just keeps running... printf("Announcing..."); /* Try to announce. If there is no Mgmt Process, keep trying */ while ((ret = readerAnnounce(path, restartCommand, timeout, &whandler, "skeleton")) == -2) { sleep(1); }; // install signal handler sa.sa_handler = sighupHandler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sigaction(SIGHUP, &sa, &oldsa); printf(" done.\n"); /* while there's no data: */ while ( (ret = readerAdvance()) != READOK) { if (restartCommand != NULL) { /* serve watchdog */ watchdogSendHeartBeat(&whandler); } wait.tv_sec = 1; wait.tv_usec = 0; select(0, NULL, NULL, NULL, &wait); } // now we've finished preparing, start main loop. while(1) { ret = readerGet(&rec, &len); // get bufferPacket if (restartCommand != NULL) { /* serve watchdog */ watchdogSendHeartBeat(&whandler); } if (ret != READOK) { // error occured during readerGet if (ret == READERR) { exit(1); } // in all other cases we've to advance to the next segment. // for this we need the Mgmt Process therefore we check if // we got the reconnect signal/ if (myreconnect) { break; // finished reading block now go back for reannounce } if (ret == READSTALE) { loss = READSTALE; // data lost: block no more there } // else: READNODATA if ( (ret = readerAdvance()) == READSEQ) { // block loss detected in sequence number loss = READSEQ; } else if (ret == READERR) { // error in communication logwrite(LOG_DEBUG, "communication error, sleeping 1 sec at %s, %d", __FILE__, __LINE__); wait.tv_sec = 1; wait.tv_usec = 0; select(0, NULL, NULL, NULL, &wait); } else if (ret == READNOMGMT) { // error in communication, reannounce break; // go back to reannounce } else if (ret == READNODATA) { // no data wait.tv_sec = 0; wait.tv_usec = 100; select(0, NULL, NULL, NULL, &wait); } } else { // data is ok. process(rec, loss); // process read block loss = READOK; } } } }