[Autosec-devel] sonar/src util.c,1.31,1.32 sonar.c,1.46,1.47 plugin.h,1.49,1.50 expand.c,1.4,1.5 exp
Brought to you by:
red0x
From: Jacob F. <ph...@us...> - 2004-06-10 22:11:45
|
Update of /cvsroot/autosec/sonar/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1296 Modified Files: util.c sonar.c plugin.h expand.c expand.h Log Message: The string expansion is done (finally) Sorry it took so long. Here's the list of expandable characters: %t - hostname:portnumber %T - hostname:portname %h - hostname %p - portnumber %P - portname %c - timestamp (Day Mon dd hh:mm:ss yyyy) %Y - year (4 digit) %y - year (2 digit) %m - month (numeric) %b - month (short alpha) %B - month (full alpha) %a - day (short alpha) %A - day (full alpha) %d - day (numeric) %H - hour (12 hour) %M - minutes %S - seconds %Q - AM/PM Index: expand.c =================================================================== RCS file: /cvsroot/autosec/sonar/src/expand.c,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** expand.c 23 Apr 2004 04:41:01 -0000 1.4 --- expand.c 10 Jun 2004 22:11:35 -0000 1.5 *************** *** 25,135 **** "\100$ autosec/sonar: $Id$"; ! static char *res, *r; ! static int reslen = 0, curlen = 0; char * ! expand_target(char *format, target_t * target, int port) { ! /* pointers within the format and res string, respectively */ ! char *f, *portname; ! int i; ! /* ! * we're freeing the previous result, so hopefully the caller realizes ! * that the return value is only good until the next call of ! * expand_target() ! * red0x: pharkas, make sure you document this well in the API document ! * as well as the plugin writer's manual. ! */ ! if(res != NULL) ! free(res); ! reslen = sizeof(format) * MALLOC_INC; ! res = (char *) malloc(sizeof(char) * reslen); ! f = format; ! r = res; ! while(*f != '\0') { ! if(*f == '%') { ! switch (*(++f)) { ! /* hostname:portname */ ! case 'T': ! /* skip over the 'T' */ ! f++; ! r += print_hostname(r, target); ! checksize(1); ! *r++ = ':'; ! curlen++; ! r += print_portname(r, port); break; ! /* ip:portnum */ ! case 't': ! f++; ! r += print_ip(r, target); ! checksize(1); ! *r++ = ':'; ! curlen++; ! r += print_port(r, port); break; ! /* ip */ ! case 'i': ! f++; ! r += print_ip(r, target); break; ! /* hostname */ ! case 'h': ! f += 2; ! break; ! /* port number */ ! case 'p': ! f += 2; ! break; ! /* port name */ ! case 'P': ! f += 2; ! break; ! /* year */ ! case 'y': ! f += 2; ! break; ! /* month (numeric) */ ! case 'm': ! f += 2; ! break; ! /* day (numeric?) */ ! case 'd': ! f += 2; ! break; ! /* hour */ ! case 'H': ! f += 2; ! break; ! /* minutes */ ! case 'M': ! f += 2; ! break; ! /* seconds */ ! case 'S': ! f += 2; ! break; ! default: ! checksize(2); ! *(r) = '%'; ! *(++r) = *f++; ! r++; ! curlen += 2; break; } --- 25,200 ---- "\100$ autosec/sonar: $Id$"; ! /******************************************************************************* ! * ! * String expansion characters: ! * %t - hostname:portnumber ! * %T - hostname:portname ! * %h - hostname ! * %p - portnumber ! * %P - portname ! * %c - timestamp (Day Mon dd hh:mm:ss yyyy) ! * %Y - year (4 digit) ! * %y - year (2 digit) ! * %m - month (numeric) ! * %b - month (short alpha) ! * %B - month (full alpha) ! * %a - day (short alpha) ! * %A - day (full alpha) ! * %d - day (numeric) ! * %H - hour (12 hour) ! * %M - minutes ! * %S - seconds ! * %Q - AM/PM ! ******************************************************************************/ char * ! expand_target(char *format, target_t* target, uint16_t port) { ! char *fmtptr; ! char timebuf[TIMEBUF_SIZE]; ! time_t ttime; ! struct tm *curtime; ! struct rb result; ! if(!format) ! return NULL; ! ! ttime=time(NULL); ! curtime=localtime(&ttime); ! memset(timebuf, '\0', TIMEBUF_SIZE); ! ! /* ! * the caller needs to make sure to free the expanded string when they're ! * done with it ! */ ! result.reslen = strlen(format) * MALLOC_INC; ! result.res = (char *) Malloc(sizeof(char) * result.reslen); ! memset(result.res, '\0', result.reslen); ! result.curlen=0; ! fmtptr = format; ! result.resptr = result.res; ! while(*fmtptr != '\0') { ! if(*fmtptr == '%') { ! /* ! * we've found a % character, so decode the following letter and ! * expand it ! */ ! switch (*(++fmtptr)) /* skip over the % */ { ! case 't': /* hostname:port number */ ! fmtptr++; ! print_hostname(target, &result); ! checksize(1, &result); ! *(result.resptr++) = ':'; ! result.curlen++; ! print_number(port, &result); ! case 'T': /* hostname:portname */ ! fmtptr++; ! print_hostname(target, &result); ! checksize(1, &result); ! *(result.resptr++) = ':'; ! result.curlen++; ! print_portname(port, &result); break; ! case 'h': /* hostname */ ! fmtptr++; ! print_hostname(target, &result); break; ! case 'p': /* port number */ ! fmtptr++; ! print_number(port, &result); break; ! case 'P': /* port name */ ! fmtptr++; ! print_portname(port, &result); break; ! case 'c': /* timestamp: Day Mon dd hh:mm:ss yyyy */ ! fmtptr++; ! strftime(timebuf, TIMEBUF_SIZE, "%c", curtime); ! print_string(timebuf, &result); ! memset(timebuf, '\0', TIMEBUF_SIZE); break; ! /* it gets boring after here...*/ ! case 'Y': /* year (4 digit) */ ! fmtptr++; ! strftime(timebuf, TIMEBUF_SIZE, "%Y", curtime); ! print_string(timebuf, &result); ! memset(timebuf, '\0', TIMEBUF_SIZE); break; ! case 'y': /* year (2 digit) */ ! fmtptr++; ! strftime(timebuf, TIMEBUF_SIZE, "%y", curtime); ! print_string(timebuf, &result); ! memset(timebuf, '\0', TIMEBUF_SIZE); break; ! case 'b': /* month (alpha - short) */ ! fmtptr++; ! strftime(timebuf, TIMEBUF_SIZE, "%B", curtime); ! print_string(timebuf, &result); ! memset(timebuf, '\0', TIMEBUF_SIZE); break; ! case 'B': /* month (alpha - full) */ ! fmtptr++; ! strftime(timebuf, TIMEBUF_SIZE, "%B", curtime); ! print_string(timebuf, &result); ! memset(timebuf, '\0', TIMEBUF_SIZE); break; ! case 'm': /* month (numeric) */ ! fmtptr++; ! strftime(timebuf, TIMEBUF_SIZE, "%m", curtime); ! print_string(timebuf, &result); ! memset(timebuf, '\0', TIMEBUF_SIZE); break; ! case 'a': /* day (alpha - short) */ ! fmtptr++; ! strftime(timebuf, TIMEBUF_SIZE, "%a", curtime); ! print_string(timebuf, &result); ! memset(timebuf, '\0', TIMEBUF_SIZE); break; ! case 'A': /* day (alpha - full) */ ! fmtptr++; ! strftime(timebuf, TIMEBUF_SIZE, "%a", curtime); ! print_string(timebuf, &result); ! memset(timebuf, '\0', TIMEBUF_SIZE); break; ! case 'd': /* day (numeric) */ ! fmtptr++; ! strftime(timebuf, TIMEBUF_SIZE, "%d", curtime); ! print_string(timebuf, &result); ! memset(timebuf, '\0', TIMEBUF_SIZE); ! break; ! case 'H': /* hour (12 hour) */ ! fmtptr++; ! strftime(timebuf, TIMEBUF_SIZE, "%I", curtime); ! print_string(timebuf, &result); ! memset(timebuf, '\0', TIMEBUF_SIZE); ! break; ! case 'M': /* minutes */ ! fmtptr++; ! strftime(timebuf, TIMEBUF_SIZE, "%M", curtime); ! print_string(timebuf, &result); ! memset(timebuf, '\0', TIMEBUF_SIZE); ! break; ! case 'S': /* seconds */ ! fmtptr++; ! strftime(timebuf, TIMEBUF_SIZE, "%S", curtime); ! print_string(timebuf, &result); ! memset(timebuf, '\0', TIMEBUF_SIZE); ! break; ! case 'Q': /* AM/PM */ ! fmtptr++; ! strftime(timebuf, TIMEBUF_SIZE, "%p", curtime); ! print_string(timebuf, &result); ! break; ! default: /* unrecognized format */ ! checksize(2, &result); ! *(result.resptr++) = '%'; ! *(result.resptr++) = *fmtptr++; ! result.curlen += 2; break; } *************** *** 137,168 **** else { ! checksize(1); ! *(r++) = *(f++); ! curlen++; } } ! /* terminate the string */ ! checksize(1); ! *(res + curlen + 1) = '\0'; ! return res; } ! static void ! checksize(int i) { ! if(curlen + i >= reslen) { ! while(curlen + i >= (reslen *= MALLOC_INC)) ! { ! /* TODO: use safe realloc */ ! res = (char *) realloc(res, reslen); ! /* res may have been relocated in memory- make sure r is still at ! * the right place */ ! r = res + curlen; ! } } - // curlen+=i; } --- 202,238 ---- else { ! /* the character was not a '%' - copy it direct to the expanded string ! */ ! checksize(1, &result); ! *(result.resptr++) = *(fmtptr++); ! result.curlen++; } } ! /* all done */ ! checksize(1, &result); ! return result.res; } ! /******************************************************************************* ! * check the size of the result string and expand it as needed to fit a string ! * of length len ! ******************************************************************************/ ! void ! checksize(int len, resultBox result) { ! if((result->curlen + len) >= result->reslen) { ! /* expand the string until it fits */ ! while((result->curlen + len) >= (result->reslen *= MALLOC_INC)) ; ! ! result->res = (char *) Realloc(result->res, result->reslen+1); ! /* res may have been relocated in memory- make sure resptr is still ! * at the right place ! */ ! result->resptr = result->res + result->curlen; ! memset(result->resptr, '\0', result->reslen-result->curlen); } } *************** *** 177,231 **** int ! print_hostname(char *result, target_t * t) { ! int i = strlen(t->entry); ! checksize(i); ! snprintf(result, i + 1, "%s", t->entry); ! curlen += i + 1; ! *(result + i) = '\0'; ! return i; } - /* TODO: have this print the actual IP */ int ! print_ip(char *result, target_t * t) { ! return print_hostname(result, t); } int ! print_portname(char *result, int portnum) { ! int i = countdigits(portnum); ! checksize(i); ! snprintf(result, i + 1, "%d", portnum); ! curlen += i + 1; ! *(result + i) = '\0'; ! return i; ! } ! int ! print_port(char *result, int portnum) ! { ! int i = countdigits(portnum); ! checksize(i); ! snprintf(result, i + 1, "%d", portnum); ! curlen += i + 1; ! *(result + i) = '\0'; ! return i; } - /* red0x: I'm wrapping this in ifdef's for later testing */ - #ifdef TEST_EXPAND - int ! main() { ! target_t t; ! char *name = "test"; ! t.entry = name; ! printf("|%s|\n", expand_target("a b c %d %e %T %f %% %T", &t, 80)); ! return 0; } - - #endif /* TEST_EXPAND */ --- 247,307 ---- int ! print_hostname(target_t * t, resultBox result) { ! int strl = strlen(t->addrinfo->ai_canonname); ! checksize(strl+2, result); ! snprintf(result->resptr, strl+1, "%s", t->addrinfo->ai_canonname); ! result->curlen+=strl; ! result->resptr+=strl; ! return strl; } int ! print_portname(int portnum, resultBox result) { ! int strl; ! struct servent *portnam; ! if(portnum<0) ! return 0; ! ! portnam=getservbyport(htons(portnum), NULL); ! if(!portnam || !portnam->s_name) { ! /* we didn't get anything back from getservbyport - print the port ! * number instead ! */ ! return print_number(portnum, result); ! } ! strl=strlen(portnam->s_name); ! checksize(strl+2, result); ! snprintf(result->resptr, strl+1, "%s", portnam->s_name); ! result->curlen+=strl; ! result->resptr+=strl; ! return strl; } int ! print_number(int num, resultBox result) { ! int strl; ! if(num<0) ! return 0; ! strl = countdigits(num); ! checksize(strl+2, result); ! snprintf(result->resptr, strl+1, "%d", num); ! result->curlen+=strl; ! result->resptr+=strl; ! return strl; } int ! print_string(char *str, resultBox result) { ! int strl; ! strl=strlen(str); ! checksize(strl+2, result); ! snprintf(result->resptr, strl+1, "%s", str); ! result->curlen+=strl; ! result->resptr+=strl; ! return strl; } Index: expand.h =================================================================== RCS file: /cvsroot/autosec/sonar/src/expand.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** expand.h 23 Apr 2004 04:31:49 -0000 1.2 --- expand.h 10 Jun 2004 22:11:35 -0000 1.3 *************** *** 22,38 **** #include <stdlib.h> #include "mem.h" #include "plugin.h" ! /* red0x: what does this alter? */ ! #define MALLOC_INC 1.5 ! char *expand_target(char *, target_t *, int); ! static void checksize(int); int countdigits(int); ! int print_hostname(char *, target_t *); ! int print_ip(char *, target_t *); ! int print_port(char *, int); ! int print_portname(char *, int); #endif --- 22,54 ---- #include <stdlib.h> + #include <netdb.h> + #include <netinet/in.h> + #include <time.h> #include "mem.h" #include "plugin.h" ! /* this is the ratio the string is realloc'ed by to hold the expanded ! * text ! */ ! #define MALLOC_INC 1.5 ! #define TIMEBUF_SIZE 40 ! struct rb { ! char *res; ! char *resptr; ! int reslen; ! int curlen; ! }; ! ! typedef struct rb* resultBox; ! ! char *expand_target(char *, target_t *, uint16_t); ! void checksize(int, resultBox); int countdigits(int); ! int print_hostname(target_t *, resultBox); ! int print_ip(target_t *, resultBox); ! int print_portname(int, resultBox); ! int print_number(int, resultBox); ! int print_string(char *, resultBox); #endif Index: sonar.c =================================================================== RCS file: /cvsroot/autosec/sonar/src/sonar.c,v retrieving revision 1.46 retrieving revision 1.47 diff -C2 -d -r1.46 -r1.47 *** sonar.c 8 Jun 2004 23:11:50 -0000 1.46 --- sonar.c 10 Jun 2004 22:11:35 -0000 1.47 *************** *** 39,42 **** --- 39,43 ---- #include "log.h" #include "mem.h" + #include "expand.h" #ifdef HAVE_SYSLOG_H *************** *** 102,105 **** --- 103,107 ---- mthat->dropprivs = &sonar_dropprivs; mthat->find_target = &sonar_find_target; + mthat->expand_target = &expand_target; // no targets yet ;) mthat->vectors = NULL; Index: util.c =================================================================== RCS file: /cvsroot/autosec/sonar/src/util.c,v retrieving revision 1.31 retrieving revision 1.32 diff -C2 -d -r1.31 -r1.32 *** util.c 9 Jun 2004 01:07:44 -0000 1.31 --- util.c 10 Jun 2004 22:11:34 -0000 1.32 *************** *** 31,35 **** #include "log.h" #include "mem.h" ! #include <pthread.h> #include <assert.h> #include <syslog.h> --- 31,35 ---- #include "log.h" #include "mem.h" ! //#include <pthread.h> #include <assert.h> #include <syslog.h> Index: plugin.h =================================================================== RCS file: /cvsroot/autosec/sonar/src/plugin.h,v retrieving revision 1.49 retrieving revision 1.50 diff -C2 -d -r1.49 -r1.50 *** plugin.h 21 May 2004 07:57:21 -0000 1.49 --- plugin.h 10 Jun 2004 22:11:35 -0000 1.50 *************** *** 321,324 **** --- 321,327 ---- char *my_name; + // target string expansion + char *(*expand_target)(char*,target_t*,int); + //! struct for error function pointers struct error_ptrs *err_ptrs; |