Fix code for windows

This commit is contained in:
brenosilva 2012-06-16 19:33:22 +00:00
parent 5b46e6c614
commit 2791e7766b
5 changed files with 62 additions and 43 deletions

View File

@ -37,7 +37,7 @@ CPTTree *CPTCreateRadixTree(apr_pool_t *pool) {
return tree; return tree;
} }
void ConvertIPNetmask(uint8_t *buffer, uint8_t netmask, uint16_t ip_bitmask) { void ConvertIPNetmask(unsigned char *buffer, unsigned char netmask, unsigned int ip_bitmask) {
int aux = 0, bytes = 0; int aux = 0, bytes = 0;
int mask = 0, mask_bit = 0; int mask = 0, mask_bit = 0;
@ -72,7 +72,7 @@ TreeNode *CPTCreateNode(apr_pool_t *pool) {
return node; return node;
} }
CPTData *CPTCreateCPTData(uint8_t netmask, apr_pool_t *pool) { CPTData *CPTCreateCPTData(unsigned char netmask, apr_pool_t *pool) {
CPTData *prefix_data = apr_palloc(pool, sizeof(CPTData)); CPTData *prefix_data = apr_palloc(pool, sizeof(CPTData));
@ -87,8 +87,8 @@ CPTData *CPTCreateCPTData(uint8_t netmask, apr_pool_t *pool) {
return prefix_data; return prefix_data;
} }
TreePrefix *InsertDataPrefix(TreePrefix *prefix, uint8_t *ipdata, uint16_t ip_bitmask, TreePrefix *InsertDataPrefix(TreePrefix *prefix, unsigned char *ipdata, unsigned int ip_bitmask,
uint8_t netmask, apr_pool_t *pool) { unsigned char netmask, apr_pool_t *pool) {
if(prefix == NULL) if(prefix == NULL)
return NULL; return NULL;
@ -104,8 +104,8 @@ TreePrefix *InsertDataPrefix(TreePrefix *prefix, uint8_t *ipdata, uint16_t ip_bi
return prefix; return prefix;
} }
TreePrefix *CPTCreatePrefix(uint8_t *ipdata, uint16_t ip_bitmask, TreePrefix *CPTCreatePrefix(unsigned char *ipdata, unsigned int ip_bitmask,
uint8_t netmask, apr_pool_t *pool) { unsigned char netmask, apr_pool_t *pool) {
TreePrefix *prefix = NULL; TreePrefix *prefix = NULL;
int bytes = ip_bitmask/8; int bytes = ip_bitmask/8;
@ -162,7 +162,7 @@ void CPTAppendToCPTDataList(CPTData *new, CPTData **list) {
return; return;
} }
int TreePrefixContainNetmask(TreePrefix *prefix, uint8_t netmask) { int TreePrefixContainNetmask(TreePrefix *prefix, unsigned char netmask) {
CPTData *prefix_data = NULL; CPTData *prefix_data = NULL;
if (prefix == NULL) { if (prefix == NULL) {
@ -179,7 +179,7 @@ int TreePrefixContainNetmask(TreePrefix *prefix, uint8_t netmask) {
return 0; return 0;
} }
int CheckBitmask(uint8_t netmask, uint16_t ip_bitmask) { int CheckBitmask(unsigned char netmask, unsigned int ip_bitmask) {
switch(netmask) { switch(netmask) {
@ -198,7 +198,7 @@ int CheckBitmask(uint8_t netmask, uint16_t ip_bitmask) {
return 0; return 0;
} }
TreeNode *CPTCreateHead(TreePrefix *prefix, TreeNode *node, CPTTree *tree, uint8_t netmask, uint16_t ip_bitmask) { TreeNode *CPTCreateHead(TreePrefix *prefix, TreeNode *node, CPTTree *tree, unsigned char netmask, unsigned int ip_bitmask) {
if(tree == NULL) if(tree == NULL)
return NULL; return NULL;
@ -216,7 +216,7 @@ TreeNode *CPTCreateHead(TreePrefix *prefix, TreeNode *node, CPTTree *tree, uint8
return node; return node;
node->count++; node->count++;
node->netmasks = apr_palloc(tree->pool, (node->count * sizeof(uint8_t))); node->netmasks = apr_palloc(tree->pool, (node->count * sizeof(unsigned char)));
if(node->netmasks) if(node->netmasks)
node->netmasks[0] = netmask; node->netmasks[0] = netmask;
@ -243,7 +243,7 @@ TreeNode *SetParentNode(TreeNode *node, TreeNode *new_node, CPTTree *tree) {
} }
int InsertNetmask(TreeNode *node, TreeNode *parent, TreeNode *new_node, int InsertNetmask(TreeNode *node, TreeNode *parent, TreeNode *new_node,
CPTTree *tree, uint8_t netmask, uint8_t bitlen) { CPTTree *tree, unsigned char netmask, unsigned char bitlen) {
int i; int i;
if (netmask != NETMASK_256-1 && netmask != NETMASK_128) { if (netmask != NETMASK_256-1 && netmask != NETMASK_128) {
@ -258,7 +258,7 @@ int InsertNetmask(TreeNode *node, TreeNode *parent, TreeNode *new_node,
} }
node->count++; node->count++;
node->netmasks = apr_palloc(tree->pool, (node->count * sizeof(uint8_t))); node->netmasks = apr_palloc(tree->pool, (node->count * sizeof(unsigned char)));
if(node->netmasks == NULL) if(node->netmasks == NULL)
return 0; return 0;
@ -286,12 +286,12 @@ int InsertNetmask(TreeNode *node, TreeNode *parent, TreeNode *new_node,
return 0; return 0;
} }
TreeNode *CPTAddElement(uint8_t *ipdata, uint16_t ip_bitmask, CPTTree *tree, uint8_t netmask) { TreeNode *CPTAddElement(unsigned char *ipdata, unsigned int ip_bitmask, CPTTree *tree, unsigned char netmask) {
uint8_t *buffer = NULL; unsigned char *buffer = NULL;
uint8_t bitlen = 0; unsigned char bitlen = 0;
int bit_validation = 0, test_bit = 0; int bit_validation = 0, test_bit = 0;
int i = 0, j = 0, temp = 0; int i = 0, j = 0, temp = 0;
uint16_t x, y; unsigned int x, y;
TreeNode *node = NULL, *new_node = NULL; TreeNode *node = NULL, *new_node = NULL;
TreeNode *parent = NULL, *i_node = NULL; TreeNode *parent = NULL, *i_node = NULL;
TreeNode *bottom_node = NULL; TreeNode *bottom_node = NULL;
@ -407,7 +407,7 @@ TreeNode *CPTAddElement(uint8_t *ipdata, uint16_t ip_bitmask, CPTTree *tree, uin
node->count++; node->count++;
new_node = node; new_node = node;
node->netmasks = apr_palloc(tree->pool, (node->count * sizeof(uint8_t))); node->netmasks = apr_palloc(tree->pool, (node->count * sizeof(unsigned char)));
if ((node->count -1) == 0) { if ((node->count -1) == 0) {
node->netmasks[0] = netmask; node->netmasks[0] = netmask;
@ -474,7 +474,7 @@ TreeNode *CPTAddElement(uint8_t *ipdata, uint16_t ip_bitmask, CPTTree *tree, uin
i++; i++;
} }
i_node->netmasks = apr_palloc(tree->pool, (node->count - i) * sizeof(uint8_t)); i_node->netmasks = apr_palloc(tree->pool, (node->count - i) * sizeof(unsigned char));
if(i_node->netmasks == NULL) { if(i_node->netmasks == NULL) {
return NULL; return NULL;
@ -514,7 +514,7 @@ TreeNode *CPTAddElement(uint8_t *ipdata, uint16_t ip_bitmask, CPTTree *tree, uin
return new_node; return new_node;
} }
int TreeCheckData(TreePrefix *prefix, CPTData *prefix_data, uint16_t netmask) { int TreeCheckData(TreePrefix *prefix, CPTData *prefix_data, unsigned int netmask) {
for(prefix_data != NULL; ; prefix_data = prefix_data->next) { for(prefix_data != NULL; ; prefix_data = prefix_data->next) {
if (prefix_data->netmask == netmask) { if (prefix_data->netmask == netmask) {
@ -525,7 +525,7 @@ int TreeCheckData(TreePrefix *prefix, CPTData *prefix_data, uint16_t netmask)
return 0; return 0;
} }
int TreePrefixNetmask(modsec_rec *msr, TreePrefix *prefix, uint16_t netmask, int flag) { int TreePrefixNetmask(modsec_rec *msr, TreePrefix *prefix, unsigned int netmask, int flag) {
CPTData *prefix_data = NULL; CPTData *prefix_data = NULL;
int ret = 0; int ret = 0;
@ -564,8 +564,8 @@ int TreePrefixNetmask(modsec_rec *msr, TreePrefix *prefix, uint16_t netmask, int
return ret; return ret;
} }
TreeNode *CPTRetriveNode(modsec_rec *msr, uint8_t *buffer, uint16_t ip_bitmask, TreeNode *node) { TreeNode *CPTRetriveNode(modsec_rec *msr, unsigned char *buffer, unsigned int ip_bitmask, TreeNode *node) {
uint16_t x, y; unsigned int x, y;
if(node == NULL) { if(node == NULL) {
if (msr->txcfg->debuglog_level >= 9) { if (msr->txcfg->debuglog_level >= 9) {
@ -610,7 +610,7 @@ TreeNode *CPTRetriveParentNode(TreeNode *node) {
return node; return node;
} }
TreeNode *CPTFindElementIPNetblock(modsec_rec *msr, uint8_t *ipdata, uint8_t ip_bitmask, TreeNode *node) { TreeNode *CPTFindElementIPNetblock(modsec_rec *msr, unsigned char *ipdata, unsigned char ip_bitmask, TreeNode *node) {
TreeNode *netmask_node = NULL; TreeNode *netmask_node = NULL;
int mask = 0, bytes = 0; int mask = 0, bytes = 0;
int i = 0, j = 0; int i = 0, j = 0;
@ -690,10 +690,10 @@ TreeNode *CPTFindElementIPNetblock(modsec_rec *msr, uint8_t *ipdata, uint8_t ip_
return CPTFindElementIPNetblock(msr, ipdata, ip_bitmask, netmask_node->parent); return CPTFindElementIPNetblock(msr, ipdata, ip_bitmask, netmask_node->parent);
} }
TreeNode *CPTFindElement(modsec_rec *msr, uint8_t *ipdata, uint16_t ip_bitmask, CPTTree *tree) { TreeNode *CPTFindElement(modsec_rec *msr, unsigned char *ipdata, unsigned int ip_bitmask, CPTTree *tree) {
TreeNode *node = NULL; TreeNode *node = NULL;
int mask = 0, bytes = 0; int mask = 0, bytes = 0;
uint8_t temp_data[NETMASK_256-1]; unsigned char temp_data[NETMASK_256-1];
if (tree == NULL) { if (tree == NULL) {
if (msr->txcfg->debuglog_level >= 9) { if (msr->txcfg->debuglog_level >= 9) {
@ -771,7 +771,7 @@ TreeNode *CPTFindElement(modsec_rec *msr, uint8_t *ipdata, uint16_t ip_bitmask,
return CPTFindElementIPNetblock(msr, temp_data, ip_bitmask, node); return CPTFindElementIPNetblock(msr, temp_data, ip_bitmask, node);
} }
TreeNode *CPTIpMatch(modsec_rec *msr, uint8_t *ipdata, CPTTree *tree, int type) { TreeNode *CPTIpMatch(modsec_rec *msr, unsigned char *ipdata, CPTTree *tree, int type) {
if(tree == NULL) { if(tree == NULL) {
if (msr->txcfg->debuglog_level >= 9) { if (msr->txcfg->debuglog_level >= 9) {
@ -807,8 +807,8 @@ TreeNode *CPTIpMatch(modsec_rec *msr, uint8_t *ipdata, CPTTree *tree, int type)
} }
TreeNode *TreeAddIP(const char *buffer, CPTTree *tree, int type) { TreeNode *TreeAddIP(const char *buffer, CPTTree *tree, int type) {
uint32_t ip, ret; unsigned long ip, ret;
uint8_t netmask_v4 = NETMASK_32, netmask_v6 = NETMASK_128; unsigned char netmask_v4 = NETMASK_32, netmask_v6 = NETMASK_128;
char ip_strv4[NETMASK_32], ip_strv6[NETMASK_128]; char ip_strv4[NETMASK_32], ip_strv6[NETMASK_128];
struct in_addr addr4; struct in_addr addr4;
struct in6_addr addr6; struct in6_addr addr6;
@ -820,7 +820,7 @@ TreeNode *TreeAddIP(const char *buffer, CPTTree *tree, int type) {
switch(type) { switch(type) {
case IPV4_TREE: case IPV4_TREE:
bzero(&addr4, sizeof(addr4)); memset(&addr4, 0, sizeof(addr4));
memset(ip_strv4, 0x0, NETMASK_32); memset(ip_strv4, 0x0, NETMASK_32);
strncpy(ip_strv4, buffer, sizeof(ip_strv4) - 2); strncpy(ip_strv4, buffer, sizeof(ip_strv4) - 2);
@ -849,10 +849,10 @@ TreeNode *TreeAddIP(const char *buffer, CPTTree *tree, int type) {
tree->count++; tree->count++;
return CPTAddElement((uint8_t *)&ip, NETMASK_32, tree, netmask_v4); return CPTAddElement((unsigned char *)&ip, NETMASK_32, tree, netmask_v4);
case IPV6_TREE: case IPV6_TREE:
bzero(&addr6, sizeof(addr6)); memset(&addr6, 0, sizeof(addr6));
memset(ip_strv6, 0x0, NETMASK_128); memset(ip_strv6, 0x0, NETMASK_128);
strncpy(ip_strv6, buffer, sizeof(ip_strv6) - 2); strncpy(ip_strv6, buffer, sizeof(ip_strv6) - 2);
@ -878,7 +878,7 @@ TreeNode *TreeAddIP(const char *buffer, CPTTree *tree, int type) {
tree->count++; tree->count++;
return CPTAddElement((uint8_t *)&addr6.s6_addr, NETMASK_128, tree, netmask_v6); return CPTAddElement((unsigned char *)&addr6.s6_addr, NETMASK_128, tree, netmask_v6);
default: default:
return NULL; return NULL;
} }

View File

@ -16,7 +16,6 @@
#define __MSC_TREE_H__ #define __MSC_TREE_H__
#include "modsecurity.h" #include "modsecurity.h"
#include <stdint.h>
typedef struct CPTData CPTData; typedef struct CPTData CPTData;
typedef struct TreePrefix TreePrefix; typedef struct TreePrefix TreePrefix;
@ -45,20 +44,20 @@ typedef struct TreeRoot TreeRoot;
#define NETMASK_2 0x2 #define NETMASK_2 0x2
struct CPTData { struct CPTData {
uint8_t netmask; unsigned char netmask;
struct CPTData *next; struct CPTData *next;
}; };
struct TreePrefix { struct TreePrefix {
uint8_t *buffer; unsigned char *buffer;
uint16_t bitlen; unsigned int bitlen;
CPTData *prefix_data; CPTData *prefix_data;
}; };
struct TreeNode { struct TreeNode {
uint16_t bit; unsigned int bit;
int count; int count;
uint8_t *netmasks; unsigned char *netmasks;
TreePrefix *prefix; TreePrefix *prefix;
struct TreeNode *left, *right; struct TreeNode *left, *right;
struct TreeNode *parent; struct TreeNode *parent;
@ -75,8 +74,8 @@ struct TreeRoot {
CPTTree *ipv6_tree; CPTTree *ipv6_tree;
}; };
CPTTree DSOLOCAL *CPTCreateRadixTree(apr_pool_t *pool); CPTTree DSOLOCAL *CPTCreateRadixTree(apr_pool_t *pool);
TreeNode DSOLOCAL *CPTIpMatch(modsec_rec *, uint8_t *, CPTTree *, int); TreeNode DSOLOCAL *CPTIpMatch(modsec_rec *msr, unsigned char *ipdata, CPTTree *tree, int type);
TreeNode DSOLOCAL *TreeAddIP(const char *, CPTTree *, int); TreeNode DSOLOCAL *TreeAddIP(const char *buffer, CPTTree *tree, int type);
#endif /*__MSC_TREE_H__ */ #endif /*__MSC_TREE_H__ */

View File

@ -571,6 +571,24 @@ char *file_basename(apr_pool_t *mp, const char *filename) {
} }
#ifdef WIN32 #ifdef WIN32
char *strcasestr(const char *haystack, const char *needle) {
char aux, lower_aux;
int length;
if ((aux = *needle++) != 0) {
aux = (char)tolower((unsigned char)aux);
length = strlen(needle);
do {
do {
if ((lower_aux = *haystack++) == 0)
return NULL;
} while ((char)tolower((unsigned char)lower_aux) != aux);
} while (strncasecmp(haystack, needle, length) != 0);
haystack--;
}
return ((char *)haystack);
}
int inet_pton(int family, const char *src, void *dst) { int inet_pton(int family, const char *src, void *dst) {
struct addrinfo addr; struct addrinfo addr;
struct sockaddr_in *in = NULL; struct sockaddr_in *in = NULL;

View File

@ -29,7 +29,9 @@
#include "re.h" #include "re.h"
#ifdef WIN32 #ifdef WIN32
#include <ws2tcpip.h>
int DSOLOCAL inet_pton(int family, const char *src, void *dst); int DSOLOCAL inet_pton(int family, const char *src, void *dst);
char DSOLOCAL *strcasestr(const char *haystack, const char *needle);
#endif #endif
int DSOLOCAL normalize_path_inplace(unsigned char *input, int len, int win, int *changed); int DSOLOCAL normalize_path_inplace(unsigned char *input, int len, int win, int *changed);

View File

@ -333,7 +333,7 @@ static int msre_op_ipmatchFromFile_execute(modsec_rec *msr, msre_rule *rule, msr
return 0; return 0;
} }
if (CPTIpMatch(msr, (uint8_t *)&in.s_addr, rtree->ipv4_tree, IPV4_TREE) != NULL) { if (CPTIpMatch(msr, (unsigned char *)&in.s_addr, rtree->ipv4_tree, IPV4_TREE) != NULL) {
*error_msg = apr_psprintf(msr->mp, "IPmatchFromFile \"%s\" matched at %s.", var->value, var->name); *error_msg = apr_psprintf(msr->mp, "IPmatchFromFile \"%s\" matched at %s.", var->value, var->name);
return 1; return 1;
} }
@ -347,7 +347,7 @@ static int msre_op_ipmatchFromFile_execute(modsec_rec *msr, msre_rule *rule, msr
return 0; return 0;
} }
if (CPTIpMatch(msr, (uint8_t *)&in6.s6_addr, rtree->ipv6_tree, IPV6_TREE) != NULL) { if (CPTIpMatch(msr, (unsigned char *)&in6.s6_addr, rtree->ipv6_tree, IPV6_TREE) != NULL) {
*error_msg = apr_psprintf(msr->mp, "IPmatchFromFile \"%s\" matched at %s.", var->value, var->name); *error_msg = apr_psprintf(msr->mp, "IPmatchFromFile \"%s\" matched at %s.", var->value, var->name);
return 1; return 1;
} }