[Autosec-devel] sonar/doc/example bare-1.c,NONE,1.1 bare-1.h,NONE,1.1 sonar_inp_list-1.c,NONE,1.1 so
Brought to you by:
red0x
From: red0x <re...@us...> - 2004-04-08 22:08:12
|
Update of /cvsroot/autosec/sonar/doc/example In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21760 Added Files: bare-1.c bare-1.h sonar_inp_list-1.c sonar_inp_list-1.h sonar_inp_list-2.c sonar_inp_list-2.h sonar_inp_list-3.c sonar_inp_list-4.c sonar_inp_list-5.c sonar_inp_list-6.c sonar_inp_list-7.c sonar_inp_list.c sonar_inp_list.h Log Message: Step by step example files for plugin developers --- NEW FILE: sonar_inp_list.h --- /*************************************************************************** * sonar_inp_list.h * IP List Input Plugin Header * Wed Mar 31 19:37:16 2004 * Copyright 2004 red0x * re...@us... ****************************************************************************/ /* * 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 Library 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. */ #ifndef _SONAR_INP_LIST_H #define _SONAR_INP_LIST_H // You need to include this for plugin_t and sonar_t #include <sonar/plugin.h> #include <sys/types.h> #include <unistd.h> #include <sys/time.h> #include <stdarg.h> #include <stdlib.h> /// Buffer size to return to sonar #define BUFSIZE 256 #ifdef __cplusplus extern "C" { #endif void *plugin_init(void *in_data); #ifdef __cplusplus } #endif #endif /* _SONAR_INP_LIST_H */ --- NEW FILE: sonar_inp_list-4.c --- static int plugin_next(char *hostname) { /* don't need to do this, sonar does it */ /* hostname = malloc(); */ if(!hostname) return PLUGIN_ERROR; memset(hostname, 0, BUFSIZE); while(fgets(hostname, BUFSIZE - 1, fp) != NULL) { /* support for comments */ if(hostname[0] == '#') continue; hostname[BUFSIZE] = '\0'; return PLUGIN_OK; } return PLUGIN_EOF; } --- NEW FILE: sonar_inp_list-1.c --- /*************************************************************************** * sonar_inp_list.c * IP List Input Plugin Source * Wed Mar 31 19:37:16 2004 * Copyright 2004 red0x * re...@us... ****************************************************************************/ /* * 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 Library 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 "include/iplist.h" #include <sonar/plugin.h> #include <assert.h> // for assert(3) /// Plugin's plugin_t structure, filled in by plugin_init static plugin_t plugin_context; /// Sonar's sonar_t structure, filled in by sonar, passed to this plugin static sonar_t *sonar_context; /// last error code is stored in the errno. static uint16_t my_errno; // notice everything is declared static. static int plugin_destroy(void); static int plugin_open(void *c_data, int i_data); static int plugin_close(void); static int plugin_next(char *hostname); static void plugin_usage(void); static void plugin_status(void); static int plugin_last_error(void); static FILE *fp; void * plugin_init(void *in_data) { assert(in_data != NULL); // this is assumed sonar_context = (sonar_t *) in_data; // check to see that sonar will provide you with the correct API version if(sonar_context->api < 6) { fprintf(stderr, "Plugin API must be 6 or higher, update sonar or downgrade this plugin\n"); } // this is an input plugin only // if it were input AND output, you could OR // the two together: INPUT_PLUGIN | OUTPUT_PLUGIN plugin_context.type = INPUT_PLUGIN; plugin_context.err = &my_errno; plugin_context.ready = false; // we are not ready // // we should not do this: sonar selects plugins. // plugin_context.selected = false; // plugin_context.version = 6; /* set that we expect to be used by plugin * API version 6 */ // This is how we get accessed from sonar. // for 'R', we are used like this: 'sonar -iR' plugin_context.plugin_char = 'R'; plugin_context.shortname = "random"; // short name plugin_context.longname = "Random Input Plugin"; // description // provide pointers to corresponding functions. plugin_context.plugin_init = &plugin_init; plugin_context.plugin_destroy = &plugin_destroy; // // this function should soon disappear // plugin_context.plugin_args = &plugin_args; // plugin_context.plugin_open = &plugin_open; plugin_context.plugin_close = &plugin_close; plugin_context.plugin_next = &plugin_next; // these are not needed for input plugins plugin_context.plugin_sweep = NULL; plugin_context.plugin_output = NULL; plugin_context.plugin_results = NULL; // these are needed for all plugins. plugin_context.plugin_usage = &plugin_usage; plugin_context.plugin_status = &plugin_status; plugin_context.plugin_last_error = &plugin_last_error; // set the errno to PLUGIN_LOADED. my_errno = PLUGIN_LOADED; // do any local initialization here. return &plugin_context; } --- NEW FILE: sonar_inp_list-5.c --- static int plugin_close(void) { /* close anything you opened */ plugin_context.selected = false; plugin_context.ready = false; if(fp) fclose(fp); return PLUGIN_OK; } --- NEW FILE: sonar_inp_list-1.h --- /*************************************************************************** * sonar_inp_list.h * IP List Input Plugin Header * Wed Mar 31 19:37:16 2004 * Copyright 2004 red0x * re...@us... ****************************************************************************/ /* * 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 Library 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. */ #ifndef _SONAR_INP_LIST_H #define _SONAR_INP_LIST_H #ifdef __cplusplus extern "C" { #endif void *plugin_init(void *in_data); #ifdef __cplusplus } #endif #endif /* _SONAR_INP_LIST_H */ --- NEW FILE: sonar_inp_list.c --- /*************************************************************************** * sonar_inp_list.c * IP List Input Plugin Source * Wed Mar 31 19:37:16 2004 * Copyright 2004 red0x * re...@us... ****************************************************************************/ /* * 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 Library 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 "include/iplist.h" #include <sonar/plugin.h> #include <assert.h> // for assert(3) /// Plugin's plugin_t structure, filled in by plugin_init static plugin_t plugin_context; /// Sonar's sonar_t structure, filled in by sonar, passed to this plugin static sonar_t *sonar_context; /// last error code is stored in the errno. static uint16_t my_errno; // notice everything is declared static. static int plugin_destroy(void); static int plugin_open(void *c_data, int i_data); static int plugin_close(void); static int plugin_next(char *hostname); static void plugin_usage(void); static void plugin_status(void); static int plugin_last_error(void); static FILE *fp; void * plugin_init(void *in_data) { assert(in_data != NULL); // this is assumed sonar_context = (sonar_t *) in_data; // check to see that sonar will provide you with the correct API version if(sonar_context->api < 6) { fprintf(stderr, "Plugin API must be 6 or higher, update sonar or downgrade this plugin\n"); } // this is an input plugin only // if it were input AND output, you could OR // the two together: INPUT_PLUGIN | OUTPUT_PLUGIN plugin_context.type = INPUT_PLUGIN; plugin_context.err = &my_errno; plugin_context.ready = false; // we are not ready // // we should not do this: sonar selects plugins. // plugin_context.selected = false; // plugin_context.version = 6; /* set that we expect to be used by plugin * API version 6 */ // This is how we get accessed from sonar. // for 'R', we are used like this: 'sonar -iR' plugin_context.plugin_char = 'R'; plugin_context.shortname = "random"; // short name plugin_context.longname = "Random Input Plugin"; // description // provide pointers to corresponding functions. plugin_context.plugin_init = &plugin_init; plugin_context.plugin_destroy = &plugin_destroy; // // this function should soon disappear // plugin_context.plugin_args = &plugin_args; // plugin_context.plugin_open = &plugin_open; plugin_context.plugin_close = &plugin_close; plugin_context.plugin_next = &plugin_next; // these are not needed for input plugins plugin_context.plugin_sweep = NULL; plugin_context.plugin_output = NULL; plugin_context.plugin_results = NULL; // these are needed for all plugins. plugin_context.plugin_usage = &plugin_usage; plugin_context.plugin_status = &plugin_status; plugin_context.plugin_last_error = &plugin_last_error; // set the errno to PLUGIN_LOADED. my_errno = PLUGIN_LOADED; // do any local initialization here. return &plugin_context; } /* this is our "destructor" that destroys anything we created */ static int plugin_destroy(void) { /* kill everything in an orderly fashion */ sonar_context = NULL; plugin_context.type = 0; plugin_context.err = NULL; plugin_context.ready = false; plugin_context.selected = false; plugin_context.plugin_init = NULL; plugin_context.plugin_destroy = NULL; plugin_context.plugin_open = NULL; plugin_context.plugin_close = NULL; plugin_context.plugin_next = NULL; plugin_context.plugin_sweep = NULL; plugin_context.plugin_output = NULL; plugin_context.plugin_results = NULL; plugin_context.plugin_usage = NULL; plugin_context.plugin_status = NULL; plugin_context.plugin_last_error = NULL; my_errno = PLUGIN_UNLOADED; if(fp) fclose(fp); fp = NULL; return PLUGIN_UNLOADED; } static int plugin_open(void *c_data, int i_data) { char *optarg = (char *) c_data; plugin_context.selected = true; if(c_data == NULL || i_data == 0) { return PLUGIN_NOFILE; } fp = fopen(optarg, "r"); if(!fp) return PLUGIN_NOFILE; /* report the error */ /* let sonar know you are ready */ plugin_context.ready = true; return BUFSIZE; /* return size of buffer sonar will need */ } static int plugin_next(char *hostname) { /* don't need to do this, sonar does it */ /* hostname = malloc(); */ if(!hostname) return PLUGIN_ERROR; memset(hostname, 0, BUFSIZE); while(fgets(hostname, BUFSIZE - 1, fp) != NULL) { /* support for comments */ if(hostname[0] == '#') continue; hostname[BUFSIZE] = '\0'; return PLUGIN_OK; } return PLUGIN_EOF; } static int plugin_close(void) { /* close anything you opened */ plugin_context.selected = false; plugin_context.ready = false; if(fp) fclose(fp); return PLUGIN_OK; } static void plugin_usage(void) { /* output a few simple usage snippets here, and exit(-1); */ assert(sonar_context != NULL); assert(sonar_context->output != NULL); sonar_context->output("\n iplist input plugin\n"); sonar_context->output(" Usage: -iL,<filename>\n"); /* do not exit so other plugins may print their usage too! */ /* exit(-1); */ } static void plugin_status(void) { /* if you are ready, and selected, output a quick status info here */ assert(sonar_context != NULL); assert(sonar_context->output != NULL); if(plugin_context.ready && plugin_context.selected) sonar_context->output("iplist input loaded::\n"); } static int plugin_last_error(void) { /* return your last error, since my_errno is a uint16_t and the return * type of this function is a int32_t, you could get creative with return * * * * * values and masking ;) */ return my_errno; } --- NEW FILE: sonar_inp_list-2.c --- /* this is our "destructor" that destroys anything we created */ static int plugin_destroy(void) { /* kill everything in an orderly fashion */ sonar_context = NULL; plugin_context.type = 0; plugin_context.err = NULL; plugin_context.ready = false; plugin_context.selected = false; plugin_context.plugin_init = NULL; plugin_context.plugin_destroy = NULL; plugin_context.plugin_open = NULL; plugin_context.plugin_close = NULL; plugin_context.plugin_next = NULL; plugin_context.plugin_sweep = NULL; plugin_context.plugin_output = NULL; plugin_context.plugin_results = NULL; plugin_context.plugin_usage = NULL; plugin_context.plugin_status = NULL; plugin_context.plugin_last_error = NULL; my_errno = PLUGIN_UNLOADED; if(fp) fclose(fp); fp = NULL; return PLUGIN_UNLOADED; } --- NEW FILE: bare-1.h --- /*************************************************************************** * bare.h * Bare Bones Plugin Header * Wed Mar 31 19:37:16 2004 * Copyright 2004 red0x * re...@us... ****************************************************************************/ /* * 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 Library 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. */ #ifndef _BARE_H #define _BARE_H #endif /* _BARE_H */ --- NEW FILE: sonar_inp_list-6.c --- static void plugin_usage(void) { /* output a few simple usage snippets here, and exit(-1); */ assert(sonar_context != NULL); assert(sonar_context->output != NULL); sonar_context->output("\n iplist input plugin\n"); sonar_context->output(" Usage: -iL,<filename>\n"); /* do not exit so other plugins may print their usage too! */ /* exit(-1); */ } static void plugin_status(void) { /* if you are ready, and selected, output a quick status info here */ assert(sonar_context != NULL); assert(sonar_context->output != NULL); if(plugin_context.ready && plugin_context.selected) sonar_context->output("iplist input loaded::\n"); } --- NEW FILE: sonar_inp_list-3.c --- static int plugin_open(void *c_data, int i_data) { char *optarg = (char *) c_data; plugin_context.selected = true; if(c_data == NULL || i_data == 0) { return PLUGIN_NOFILE; } fp = fopen(optarg, "r"); if(!fp) return PLUGIN_NOFILE; /* report the error */ /* let sonar know you are ready */ plugin_context.ready = true; return BUFSIZE; /* return size of buffer sonar will need */ } --- NEW FILE: sonar_inp_list-7.c --- static int plugin_last_error(void) { /* return your last error, since my_errno is a uint16_t and the return * type of this function is a int32_t, you could get creative with return * * * * * values and masking ;) */ return my_errno; } --- NEW FILE: sonar_inp_list-2.h --- /*************************************************************************** * sonar_inp_list.h * IP List Input Plugin Header * Wed Mar 31 19:37:16 2004 * Copyright 2004 red0x * re...@us... ****************************************************************************/ /* * 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 Library 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. */ #ifndef _SONAR_INP_LIST_H #define _SONAR_INP_LIST_H // You need to include this for plugin_t and sonar_t #include <sonar/plugin.h> #include <sys/types.h> #include <unistd.h> #include <sys/time.h> #include <stdarg.h> #include <stdlib.h> /// Buffer size to return to sonar #define BUFSIZE 256 #ifdef __cplusplus extern "C" { #endif void *plugin_init(void *in_data); #ifdef __cplusplus } #endif #endif /* _SONAR_INP_LIST_H */ --- NEW FILE: bare-1.c --- /*************************************************************************** * bare.c * Bare Bones Plugin Source * Wed Mar 31 19:37:16 2004 * Copyright 2004 red0x * re...@us... ****************************************************************************/ /* * 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 Library 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. */ |