Moved/Created proto_intostr() and proto_strtoint() to utils.c.

This allows to update dump_transmit_options() to use the log module to dump data.
This commit is contained in:
Franck Joncourt
2013-04-30 22:22:03 +02:00
parent 10a4e1f675
commit d93648cf99
5 changed files with 110 additions and 105 deletions
+4 -71
View File
@@ -45,8 +45,6 @@
#define FWKNOPRC_MODE (S_IRUSR|S_IWUSR) /*!< mode used to create an fwknoprc file with the open function */
#define PARAM_YES_VALUE "Y" /*!< String which represents a YES value for a parameter in fwknoprc */
#define PARAM_NO_VALUE "N" /*!< String which represents a NO value for a parameter in fwknoprc */
#define ARRAY_SIZE(t) (sizeof(t) / sizeof(t[0])) /*!< Macro to get the number of elements of an array */
/**
* Structure to handle a variable in an rcfile
@@ -222,12 +220,12 @@ is_rc_section(const char* line, uint16_t line_size, char* rc_section, uint16_t r
}
/**
* Grab a variable and its value from a rc line.
* @brief Grab a variable and its value from a rc line.
*
* \param line Line to parse for a variable
* \param param Parameter structure where to store the variable name and its value
* @param line Line to parse for a variable
* @param param Parameter structure where to store the variable name and its value
*
* \return 0 if no variable has been found, 1 otherwise.
* @return 0 if no variable has been found, 1 otherwise.
*/
static int
is_rc_param(const char *line, rc_file_param_t *param)
@@ -263,71 +261,6 @@ is_rc_param(const char *line, rc_file_param_t *param)
return 1;
}
/* Convert a protocol string to its intger value.
*/
static int
proto_strtoint(const char *pr_str)
{
if (strcasecmp(pr_str, "udpraw") == 0)
return(FKO_PROTO_UDP_RAW);
else if (strcasecmp(pr_str, "udp") == 0)
return(FKO_PROTO_UDP);
else if (strcasecmp(pr_str, "tcpraw") == 0)
return(FKO_PROTO_TCP_RAW);
else if (strcasecmp(pr_str, "tcp") == 0)
return(FKO_PROTO_TCP);
else if (strcasecmp(pr_str, "icmp") == 0)
return(FKO_PROTO_ICMP);
else if (strcasecmp(pr_str, "http") == 0)
return(FKO_PROTO_HTTP);
else
return(-1);
}
/**
* \brief Return a prototype string according to a prototype integer value
*
* This function checks the prototype integer is valid, and write the prototype
* string associated.
*
* \param proto Prototype inetger value (UDP_RAW, UDP, TCPRAW...)
* \param proto_str Buffer to write the prototype string
* \param proto_size size of the prototype string buffer
*
* \return 1 if the digest integer value is not supported, 0 otherwise
*/
static int
proto_inttostr(unsigned int proto, char* pr_str, size_t pr_size)
{
uint8_t proto_not_valid = 0;
memset(pr_str, 0, pr_size);
switch (proto)
{
case FKO_PROTO_UDP_RAW:
strlcpy(pr_str, "UDPRAW", pr_size);
break;
case FKO_PROTO_UDP:
strlcpy(pr_str, "UDP", pr_size);
break;
case FKO_PROTO_TCP_RAW:
strlcpy(pr_str, "TCPRAW", pr_size);
break;
case FKO_PROTO_TCP:
strlcpy(pr_str, "TCP", pr_size);
break;
case FKO_PROTO_ICMP:
strlcpy(pr_str, "ICMP", pr_size);
break;
default:
proto_not_valid = 1;
break;
}
return proto_not_valid;
}
/* Assign path to fwknop rc file
*/
static void
+10 -29
View File
@@ -32,36 +32,18 @@
#include "spa_comm.h"
#include "utils.h"
static void
print_proto(const int proto)
{
switch (proto) {
case FKO_PROTO_UDP:
printf("udp");
break;
case FKO_PROTO_TCP_RAW:
printf("tcpraw");
break;
case FKO_PROTO_TCP:
printf("tcp");
break;
case FKO_PROTO_ICMP:
printf("icmp");
break;
case FKO_PROTO_HTTP:
printf("http");
break;
}
return;
}
static void
dump_transmit_options(const fko_cli_options_t *options)
{
printf("Generating SPA packet:\n protocol: ");
print_proto(options->spa_proto),
printf("\n port: %d\n", options->spa_dst_port);
printf(" IP/host: %s\n", options->spa_server_str);
char proto_str[PROTOCOL_BUFSIZE]; /* Protocol string */
proto_inttostr(options->spa_proto, proto_str, sizeof(proto_str));
log_msg(LOG_VERBOSITY_INFO, "Generating SPA packet:");
log_msg(LOG_VERBOSITY_INFO, " protocol: %s", proto_str);
log_msg(LOG_VERBOSITY_INFO, " port: %d", options->spa_dst_port);
log_msg(LOG_VERBOSITY_INFO, " IP/host: %s", options->spa_server_str);
return;
}
@@ -644,8 +626,7 @@ send_spa_packet(fko_ctx_t ctx, fko_cli_options_t *options)
errno = 0;
if (options->verbose)
dump_transmit_options(options);
dump_transmit_options(options);
if (options->spa_proto == FKO_PROTO_TCP || options->spa_proto == FKO_PROTO_UDP)
{
+83
View File
@@ -35,6 +35,25 @@
static void *get_in_addr(struct sockaddr *sa);
/**
* Structure to handle a protocol string and its associated integer value
*/
typedef struct fko_protocol
{
char str[PROTOCOL_BUFSIZE]; /*!< String which represents a protocol value for the FKO library */
int val; /*!< Value of the protocol according to the FKO library */
} fko_protocol_t;
static fko_protocol_t fko_protocol_array[] =
{
{ "udpraw", FKO_PROTO_UDP_RAW },
{ "udp", FKO_PROTO_UDP },
{ "tcpraw", FKO_PROTO_TCP_RAW },
{ "tcp", FKO_PROTO_TCP },
{ "icmp", FKO_PROTO_ICMP },
{ "http", FKO_PROTO_HTTP }
};
/* Generic hex dump function.
*/
void
@@ -224,4 +243,68 @@ resolve_dest_adr(const char *dns_str, struct addrinfo *hints, char *ip_str, size
return error;
}
/**
* @brief Return a protocol string according to a protocol integer value
*
* This function checks if the protocol integer is valid, and write the protocol
* string associated.
*
* @param proto protocol inetger value (UDP_RAW, UDP, TCPRAW...)
* @param proto_str Buffer to write the protocol string
* @param proto_size size of the protocol string buffer
*
* @return -1 if the protocol integer value is not supported, 0 otherwise
*/
short
proto_inttostr(int proto, char *proto_str, size_t proto_size)
{
short proto_error = -1;
unsigned char ndx_proto; /* Index for the fko_protocol_t structure */
/* Initialize the protocol string */
memset(proto_str, 0, proto_size);
/* Look into the fko_protocol_array to find out the right protocol */
for (ndx_proto = 0 ; ndx_proto < ARRAY_SIZE(fko_protocol_array) ; ndx_proto++)
{
/* If the protocol matches, grab it */
if (fko_protocol_array[ndx_proto].val == proto)
{
strlcpy(proto_str, fko_protocol_array[ndx_proto].str, proto_size);
proto_error = 0;
break;
}
}
return proto_error;
}
/**
* @brief Convert a protocol string to its integer value.
*
* @param pr_str Protocol string (UDP_RAW, UDP, TCPRAW...)
*
* @return -1 if the protocol string is not supported, otherwise the protocol value
*/
short
proto_strtoint(const char *pr_str)
{
unsigned char ndx_proto; /* Index for the fko_protocol_t structure */
int proto_int = -1; /* Protocol integer value */
/* Look into the fko_protocol_array to find out the right protocol */
for (ndx_proto = 0 ; ndx_proto < ARRAY_SIZE(fko_protocol_array) ; ndx_proto++)
{
/* If the protocol matches, grab it */
if (strcasecmp(pr_str, fko_protocol_array[ndx_proto].str) == 0)
{
proto_int = fko_protocol_array[ndx_proto].val;
break;
}
}
return proto_int;
}
/***EOF***/
+9 -5
View File
@@ -39,12 +39,16 @@
#include <sys/socket.h>
#include <netdb.h>
#define PROTOCOL_BUFSIZE 16 /*!< Maximum number of chars for a protocol string (TCP for example) */
/* Prototypes
*/
void hex_dump(const unsigned char *data, const int size);
int is_base64(const unsigned char *buf, const unsigned short int len);
int set_file_perms(const char *file);
int verify_file_perms_ownership(const char *file);
int resolve_dest_adr(const char *dns_str, struct addrinfo *hints, char *ip_str, size_t ip_bufsize);
void hex_dump(const unsigned char *data, const int size);
int is_base64(const unsigned char *buf, const unsigned short int len);
int set_file_perms(const char *file);
int verify_file_perms_ownership(const char *file);
int resolve_dest_adr(const char *dns_str, struct addrinfo *hints, char *ip_str, size_t ip_bufsize);
short proto_inttostr(int proto, char *proto_str, size_t proto_size);
short proto_strtoint(const char *pr_str);
#endif /* UTILS_H */
+4
View File
@@ -134,6 +134,10 @@ enum {
/* Some convenience macros */
/* Get the number of elements of an array
*/
#define ARRAY_SIZE(t) (sizeof(t) / sizeof(t[0]))
/* Characters allowed between a config parameter and its value.
*/
#define IS_CONFIG_PARAM_DELIMITER(x) (x == ' ' || x == '\t' || x == '=');