Back to index

nagios-nrpe  2.13
Functions
utils.h File Reference
#include "../include/config.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void generate_crc32_table (void)
unsigned long calculate_crc32 (char *, int)
void randomize_buffer (char *, int)
int my_tcp_connect (char *, int, int *)
int my_connect (char *, int, int *, char *)
int my_inet_aton (register const char *, struct in_addr *)
void strip (char *)
int sendall (int, char *, int *)
int recvall (int, char *, int *, int)
char * my_strsep (char **, const char *)
void display_license (void)

Function Documentation

unsigned long calculate_crc32 ( char *  ,
int   
)

Definition at line 61 of file utils.c.

                                                            {
       register unsigned long crc;
       int this_char;
       int current_index;

       crc=0xFFFFFFFF;

       for(current_index=0;current_index<buffer_size;current_index++){
              this_char=(int)buffer[current_index];
              crc=((crc>>8) & 0x00FFFFFF) ^ crc32_table[(crc ^ this_char) & 0xFF];
               }

       return (crc ^ 0xFFFFFFFF);
        }

Here is the caller graph for this function:

void display_license ( void  )

Definition at line 419 of file utils.c.

                          {

       printf("This program is released under the GPL (see below) with the additional\n");
       printf("exemption that compiling, linking, and/or using OpenSSL is allowed.\n\n");

       printf("This program is free software; you can redistribute it and/or modify\n");
       printf("it under the terms of the GNU General Public License as published by\n");
       printf("the Free Software Foundation; either version 2 of the License, or\n");
       printf("(at your option) any later version.\n\n");
       printf("This program is distributed in the hope that it will be useful,\n");
       printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
       printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
       printf("GNU General Public License for more details.\n\n");
       printf("You should have received a copy of the GNU General Public License\n");
       printf("along with this program; if not, write to the Free Software\n");
       printf("Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n\n");

       return;
        }

Here is the caller graph for this function:

void generate_crc32_table ( void  )

Definition at line 40 of file utils.c.

                               {
       unsigned long crc, poly;
       int i, j;

       poly=0xEDB88320L;
       for(i=0;i<256;i++){
              crc=i;
              for(j=8;j>0;j--){
                     if(crc & 1)
                            crc=(crc>>1)^poly;
                     else
                            crc>>=1;
                      }
              crc32_table[i]=crc;
                }

       return;
        }

Here is the caller graph for this function:

int my_connect ( char *  ,
int  ,
int *  ,
char *   
)

Definition at line 123 of file utils.c.

                                                            {
       struct sockaddr_in servaddr;
       struct hostent *hp;
       struct protoent *ptrp;
       int result;

       bzero((char *)&servaddr,sizeof(servaddr));
       servaddr.sin_family=AF_INET;
       servaddr.sin_port=htons(port);

       /* try to bypass using a DNS lookup if this is just an IP address */
       if(!my_inet_aton(host_name,&servaddr.sin_addr)){

              /* else do a DNS lookup */
              hp=gethostbyname((const char *)host_name);
              if(hp==NULL){
                     printf("Invalid host name '%s'\n",host_name);
                     return STATE_UNKNOWN;
                      }

              memcpy(&servaddr.sin_addr,hp->h_addr,hp->h_length);
               }

       /* map transport protocol name to protocol number */
       if(((ptrp=getprotobyname(proto)))==NULL){
              printf("Cannot map \"%s\" to protocol number\n",proto);
              return STATE_UNKNOWN;
               }

       /* create a socket */
       *sd=socket(PF_INET,(!strcmp(proto,"udp"))?SOCK_DGRAM:SOCK_STREAM,ptrp->p_proto);
       if(*sd<0){
              printf("Socket creation failed\n");
              return STATE_UNKNOWN;
               }

       /* open a connection */
       result=connect(*sd,(struct sockaddr *)&servaddr,sizeof(servaddr));
       if(result<0){
              switch(errno){  
              case ECONNREFUSED:
                     printf("Connection refused by host\n");
                     break;
              case ETIMEDOUT:
                     printf("Timeout while attempting connection\n");
                     break;
              case ENETUNREACH:
                     printf("Network is unreachable\n");
                     break;
              default:
                     printf("Connection refused or timed out\n");
                      }

              return STATE_CRITICAL;
               }

       return STATE_OK;
        }

Here is the call graph for this function:

Here is the caller graph for this function:

int my_inet_aton ( register const char *  ,
struct in_addr *   
)

Definition at line 186 of file utils.c.

                                                               {
       register unsigned int val;  /* changed from u_long --david */
       register int base, n;
       register char c;
       u_int parts[4];
       register u_int *pp = parts;

       c=*cp;

       for(;;){

              /*
               * Collect number up to ``.''.
               * Values are specified as for C:
               * 0x=hex, 0=octal, isdigit=decimal.
               */
              if (!isdigit((int)c))
                     return (0);
              val=0;
              base=10;

              if(c=='0'){
                     c=*++cp;
                     if(c=='x'||c=='X')
                            base=16,c=*++cp;
                     else
                            base=8;
                      }

              for(;;){
                     if(isascii((int)c) && isdigit((int)c)){
                            val=(val*base)+(c -'0');
                            c=*++cp;
                             } 
                     else if(base==16 && isascii((int)c) && isxdigit((int)c)){
                            val=(val<<4) | (c+10-(islower((int)c)?'a':'A'));
                            c = *++cp;
                             } 
                     else
                            break;
                      }

              if(c=='.'){

                     /*
                      * Internet format:
                      *     a.b.c.d
                      *     a.b.c  (with c treated as 16 bits)
                      *     a.b    (with b treated as 24 bits)
                      */
                     if(pp>=parts+3)
                            return (0);
                     *pp++=val;
                     c=*++cp;
                      } 
              else
                     break;
               }

       /* Check for trailing characters */
       if(c!='\0' && (!isascii((int)c) || !isspace((int)c)))
              return (0);

       /* Concoct the address according to the number of parts specified */
       n=pp-parts+1;
       switch(n){

       case 0:
              return (0);          /* initial nondigit */

       case 1:                            /* a -- 32 bits */
              break;

       case 2:                            /* a.b -- 8.24 bits */
              if(val>0xffffff)
                     return (0);
              val|=parts[0]<<24;
              break;

       case 3:                            /* a.b.c -- 8.8.16 bits */
              if(val>0xffff)
                     return (0);
              val|=(parts[0]<< 24) | (parts[1]<<16);
              break;

       case 4:                            /* a.b.c.d -- 8.8.8.8 bits */
              if(val>0xff)
                     return (0);
              val|=(parts[0]<<24) | (parts[1]<<16) | (parts[2]<<8);
              break;
               }

       if(addr)
              addr->s_addr=htonl(val);

       return (1);
        }

Here is the caller graph for this function:

char* my_strsep ( char **  ,
const char *   
)

Definition at line 377 of file utils.c.

                                                   {
       char *begin, *end;

       begin = *stringp;
       if (begin == NULL)
              return NULL;

       /* A frequent case is when the delimiter string contains only one
          character.  Here we don't need to call the expensive `strpbrk'
          function and instead work using `strchr'.  */
       if(delim[0]=='\0' || delim[1]=='\0'){
              char ch = delim[0];

              if(ch=='\0')
                     end=NULL;
              else{
                     if(*begin==ch)
                            end=begin;
                     else
                            end=strchr(begin+1,ch);
                     }
              }

       else
              /* Find the end of the token.  */
              end = strpbrk (begin, delim);

       if(end){

              /* Terminate the token and set *STRINGP past NUL character.  */
              *end++='\0';
              *stringp=end;
              }
       else
              /* No more delimiters; this is the last token.  */
              *stringp=NULL;

       return begin;
       }

Here is the caller graph for this function:

int my_tcp_connect ( char *  ,
int  ,
int *   
)

Definition at line 113 of file utils.c.

                                                    {
       int result;

       result=my_connect(host_name,port,sd,"tcp");

       return result;
        }

Here is the call graph for this function:

Here is the caller graph for this function:

void randomize_buffer ( char *  ,
int   
)

Definition at line 78 of file utils.c.

                                                   {
       FILE *fp;
       int x;
       int seed;

       /**** FILL BUFFER WITH RANDOM ALPHA-NUMERIC CHARACTERS ****/

       /***************************************************************
          Only use alpha-numeric characters becase plugins usually
          only generate numbers and letters in their output.  We
          want the buffer to contain the same set of characters as
          plugins, so its harder to distinguish where the real output
          ends and the rest of the buffer (padded randomly) starts.
       ***************************************************************/

       /* try to get seed value from /dev/urandom, as its a better source of entropy */
       fp=fopen("/dev/urandom","r");
       if(fp!=NULL){
              seed=fgetc(fp);
              fclose(fp);
               }

       /* else fallback to using the current time as the seed */
       else
              seed=(int)time(NULL);

       srand(seed);
       for(x=0;x<buffer_size;x++)
              buffer[x]=(int)'0'+(int)(72.0*rand()/(RAND_MAX+1.0));

       return;
        }

Here is the caller graph for this function:

int recvall ( int  ,
char *  ,
int *  ,
int   
)

Definition at line 331 of file utils.c.

                                                    {
       int total=0;
       int bytesleft=*len;
       int n=0;
       time_t start_time;
       time_t current_time;
       
       /* clear the receive buffer */
       bzero(buf,*len);

       time(&start_time);

       /* receive all data */
       while(total<*len){

              /* receive some data */
              n=recv(s,buf+total,bytesleft,0);

              /* no data has arrived yet (non-blocking socket) */
              if(n==-1 && errno==EAGAIN){
                     time(&current_time);
                     if(current_time-start_time>timeout)
                            break;
                     sleep(1);
                     continue;
                      }

              /* receive error or client disconnect */
              else if(n<=0)
                     break;

              /* apply bytes we received */
              total+=n;
              bytesleft-=n;
               }

       /* return number of bytes actually received here */
       *len=total;

       /* return <=0 on failure, bytes received on success */
       return (n<=0)?n:total;
        }

Here is the caller graph for this function:

int sendall ( int  ,
char *  ,
int *   
)

Definition at line 302 of file utils.c.

                                       {
       int total=0;
       int bytesleft=*len;
       int n=0;

       /* send all the data */
       while(total<*len){

              /* send some data */
              n=send(s,buf+total,bytesleft,0);

              /* break on error */
              if(n==-1)
                     break;

              /* apply bytes we sent */
              total+=n;
              bytesleft-=n;
               }

       /* return number of bytes actually send here */
       *len=total;

       /* return -1 on failure, 0 on success */
       return n==-1?-1:0;
        }

Here is the caller graph for this function:

void strip ( char *  )

Definition at line 285 of file utils.c.

                        {
       int x;
       int index;

       for(x=strlen(buffer);x>=1;x--){
              index=x-1;
              if(buffer[index]==' ' || buffer[index]=='\r' || buffer[index]=='\n' || buffer[index]=='\t')
                     buffer[index]='\x0';
              else
                     break;
               }

       return;
        }