Esempio n. 1
0
void print_addrinfo( struct addrinfo* ai ) {
   if( LOGGER_LEVEL_INFO <= logger_get_level() ) {
      if( NULL != ai )
      {
         LOGGER_info("FAMILY:    0x%02x", ai->ai_family);
         LOGGER_info("SOCKTYPE:  0x%02x", ai->ai_socktype);
         LOGGER_info("PROTOCOL:  0x%02x", ai->ai_protocol);
         LOGGER_info("FLAGS:     0x%02x", ai->ai_flags);
         LOGGER_info("CANONNAME: %s", ai->ai_canonname);
         LOGGER_info("ADDR_LEN:  %d", ai->ai_addrlen);
         switch( ai->ai_addr->sa_family ) {
            case AF_INET:
               LOGGER_info("ADDR_FAMILY: 0x%02x (INET)", ai->ai_addr->sa_family);
               print_array(LOGGER_LEVEL_INFO, "ADDR_DATA:   %s", ai->ai_addr->sa_data, ai->ai_addrlen);
            break;
            case AF_INET6:
               LOGGER_info("ADDR_FAMILY: 0x%02x (INET6)", ai->ai_addr->sa_family);
               print_array(LOGGER_LEVEL_INFO, "ADDR_DATA:   %s", ai->ai_addr->sa_data, ai->ai_addrlen);
            break;
            default:
               LOGGER_info("ADDR_FAMILY: 0x%02x (other)", ai->ai_addr->sa_family);
               print_array(LOGGER_LEVEL_INFO, "ADDR_DATA:   %s", ai->ai_addr->sa_data, ai->ai_addrlen);
            break;
         }
         LOGGER_info("--------------");

         print_addrinfo( ai->ai_next );
      }
   }
   return;
}
Esempio n. 2
0
void print_array( int log_level, char* fmt, const char* s, int l) {
   if( log_level <= logger_get_level() ) {
      int i = 0;
      int len = 3*l + 1;
      char a[len];
      char* t = a;
      for( i=0; i < l; ++i ) {
         sprintf( t, "%02x ", s[i]);
         t += 3;
      }
      a[len] = '\0';
      LOGGER_info( fmt, a );
   }
}
Esempio n. 3
0
/**
 * Parses a comma-delimited string and sets the appropriate log filter levels.
 *
 * The individual levels CAN start with - or + to indicate whether to remove
 * or add the level.
 * @param[out] filter Which filter to update.
 * @param str
 * String with the log filter levels.
 */
static void logger_set_filter(uint64_t *filter, const char *str)
{
    char word[MAX_BUF];
    const char *cp;
    size_t pos;
    int8_t oper;
    logger_level level;

    pos = 0;

    while ((cp = string_get_word(str, &pos, ',', word,
            sizeof(word), 0)) != NULL) {
        oper = -1;

        if (*cp == '-' || *cp == '+') {
            oper = *cp == '+';
            cp += 1;
        }

        if (strcasecmp(cp, "all") == 0) {
            if (oper == -1) {
                oper = *filter == 0;
            }

            if (oper == 1) {
                *filter = ~0;
            } else {
                *filter = 0;
            }

            continue;
        }

        level = logger_get_level(cp);

        if (level >= LOG_MAX) {
            continue;
        }

        if (oper == -1) {
            *filter ^= 1U << level;
        } else if (oper == 0) {
            *filter &= ~(1U << level);
        } else if (oper == 1) {
            *filter |= 1U << level;
        }
    }
}
bool    cmd_show_logger(vty_t* vty, variant_stack_t* params)
{
    if(logger_is_enabled())
    {
        vty_write(vty, "logging enable\n");
    }
    else
    {
        vty_write(vty, "no logging enable\n");
    }

    int hist_size = logger_get_buffer_size();
    if(hist_size > 0)
    {
        vty_write(vty, "logging history %d\n", hist_size);
    }
    else
    {
        vty_write(vty, "no logging history\n");
    }

    for(int i = 0; i < logger_services_size; i++)
    {
        logger_service_t* s = *(logger_services + i);
        if(s->enabled)
        {
            switch(logger_get_level(s->service_id))
            {
            case LOG_LEVEL_BASIC:
                vty_write(vty, "logging class %s info\n", s->service_name);
                break;
            case LOG_LEVEL_ERROR:
                vty_write(vty, "logging class %s error\n", s->service_name);
                break;
            case LOG_LEVEL_ADVANCED:
                vty_write(vty, "logging class %s advanced\n", s->service_name);
                break;
            case LOG_LEVEL_DEBUG:
                vty_write(vty, "logging class %s debug\n", s->service_name);
                break;
            }
        }
    }
}
Esempio n. 5
0
int socket_dispatch_inet(dh_t dh, int max_packets, pcap_handler packet_handler, u_char* user_args)
{
   LOGGER_trace("Enter");

   int      socket = dh.fd;
   int      i;
   int32_t  nPackets = 0;
   uint8_t  buffer[g_options.snapLength];

   struct pcap_pkthdr hdr;

   for ( i = 0
         ; i < max_packets || 0 == max_packets || -1 == max_packets
         ; ++i)
   {
      // peek socket buffer to get hole message length
      switch(hdr.len = recv(socket, NULL, 0, MSG_PEEK|MSG_TRUNC)) {
         case 0: {
                    perror("socket: recv(); connection shutdown");
                    return -1;
                 }

         case -1: {
                     if (EAGAIN == errno || EWOULDBLOCK == errno) {
                        return nPackets;
                     }
                     else {
                        perror("socket: recv()");
                        return -1;
                     }
                  }

         //default:
         // everything is all right
         // further processing
      }

      // recv is blocking; until connection is closed
      // TODO: check handling
      // recv is enough here, because we are just interested of the packet
      // we will not send anything back
      switch(hdr.caplen = recv(socket, buffer, sizeof(buffer), 0)) {
         case 0:
         {
            perror("socket: recv(); connection shutdown");
            return -1;
         }

         case -1:
         {
            if (EAGAIN == errno || EWOULDBLOCK == errno) {
                return nPackets;
            }
            else {
               perror("socket: recv()");
               return -1;
            }
         }

         //default:
         // everything is all right
         // futher processing
      }


      LOGGER_info("bytes received: (%d)", hdr.len);
      LOGGER_info("bytes captured: (%d)", hdr.caplen);
      if( LOGGER_LEVEL_DEBUG <= logger_get_level() ){
         int i = 0;
         for( i=0; i < hdr.caplen; ++i ) {
            LOGGER_debug( "%02x ", buffer[i]);
         }
      }

      // get timestamp
      gettimeofday(&hdr.ts, NULL);

      // print received data
      // be aware of the type casts need
      packet_handler(user_args, &hdr, buffer);
      ++nPackets;
   }

   LOGGER_trace("Return");
   return nPackets;
}