Some code refactoring

remotes/origin/master-1.0.x
Anoop Saldanha 16 years ago committed by Victor Julien
parent 4edcc530ec
commit fbc4d11ed4

@ -653,7 +653,9 @@ error:
}
/* XXX error handling */
int DetectAddressParse2(DetectAddressGroupsHead *gh, DetectAddressGroupsHead *ghn, char *s,int negate) {
int DetectAddressParse2(DetectAddressGroupsHead *gh,
DetectAddressGroupsHead *ghn,
char *s, int negate) {
int i, x;
int o_set = 0, n_set = 0;
int depth = 0;
@ -880,6 +882,7 @@ int DetectAddressGroupParse(DetectAddressGroupsHead *gh, char *str) {
/* free the temp negate head */
DetectAddressGroupsHeadFree(ghn);
return 0;
error:
DetectAddressGroupsHeadFree(ghn);
return -1;
@ -887,14 +890,15 @@ error:
DetectAddressGroupsHead *DetectAddressGroupsHeadInit(void) {
DetectAddressGroupsHead *gh = malloc(sizeof(DetectAddressGroupsHead));
if (gh == NULL) {
if (gh == NULL)
return NULL;
}
memset(gh, 0, sizeof(DetectAddressGroupsHead));
#ifdef DEBUG
detect_address_group_head_init_cnt++;
detect_address_group_head_memory += sizeof(DetectAddressGroupsHead);
#endif
return gh;
}

@ -36,7 +36,11 @@ DetectProto *DetectProtoInit(void) {
return dp;
}
/* free a DetectAddressGroup object */
/**
* \brief Free a DetectAddressGroup object
*
* \param dp Pointer to the DetectProto instance to be freed
*/
void DetectProtoFree(DetectProto *dp) {
if (dp == NULL)
return;
@ -44,24 +48,33 @@ void DetectProtoFree(DetectProto *dp) {
free(dp);
}
/**
* \brief Parses a protocol sent as a string.
*
* \param dp Pointer to the DetectProto instance which will be updated with the
* incoming protocol information.
* \param str Pointer to the string containing the protocol name.
*
* \retval 0 Always return 0.
*/
int DetectProtoParse(DetectProto *dp, char *str) {
int proto;
if (strcasecmp(str, "tcp") == 0) {
proto = IPPROTO_TCP;
dp->proto[(proto/8)] |= 1<<(proto%8);
dp->proto[proto / 8] |= 1 << (proto % 8);
} else if (strcasecmp(str, "udp") == 0) {
proto = IPPROTO_UDP;
dp->proto[(proto/8)] |= 1<<(proto%8);
dp->proto[proto / 8] |= 1 << (proto % 8);
} else if (strcasecmp(str, "icmp") == 0) {
proto = IPPROTO_ICMP;
dp->proto[(proto/8)] |= 1<<(proto%8);
dp->proto[proto / 8] |= 1 << (proto % 8);
} else if (strcasecmp(str, "ip") == 0) {
dp->flags |= DETECT_PROTO_ANY;
memset(&dp->proto, 0xFF, sizeof(dp->proto));
} else {
proto = atoi(str);
dp->proto[(proto/8)] |= 1<<(proto%8);
dp->proto[proto / 8] |= 1 << (proto % 8);
}
return 0;

@ -267,26 +267,33 @@ int SigParseAddress(Signature *s, const char *addrstr, char flag) {
if (strcasecmp(addrstr, "any") == 0)
s->flags |= SIG_FLAG_SRC_ANY;
if (DetectAddressGroupParse(&s->src,addr) < 0) {
if (DetectAddressGroupParse(&s->src, addr) < 0)
goto error;
}
} else {
if (strcasecmp(addrstr, "any") == 0)
s->flags |= SIG_FLAG_DST_ANY;
if (DetectAddressGroupParse(&s->dst,addr) < 0) {
if (DetectAddressGroupParse(&s->dst, addr) < 0)
goto error;
}
}
return 0;
error:
return -1;
}
/* http://www.iana.org/assignments/protocol-numbers
/**
* \brief Parses the protocol supplied by the Signature.
*
* http://www.iana.org/assignments/protocol-numbers
*
* \param s Pointer to the Signature instance to which the parsed
* protocol has to be added.
* \param protostr Pointer to the character string containing the protocol name.
*
* \retval 0 On successfully parsing the protocl sent as the argument.
* \retval -1 On failure
*/
int SigParseProto(Signature *s, const char *protostr) {
int r = DetectProtoParse(&s->proto, (char *)protostr);
@ -349,6 +356,17 @@ int SigParsePort(Signature *s, const char *portstr, char flag) {
return 0;
}
/**
* \brief Parses the action that has been used by the Signature and allots it
* to its Signatue instance.
*
* \param s Pointer to the Signatue instance to which the action belongs.
* \param action Pointer to the action string used by the Signature.
*
* \retval 0 On successfully parsing the action string and adding it to the
* Signature.
* \retval -1 On failure.
*/
int SigParseAction(Signature *s, const char *action) {
if (strcasecmp(action, "alert") == 0) {
s->action = ACTION_ALERT;
@ -409,9 +427,8 @@ int SigParseBasics(Signature *s, char *sigstr, char ***result) {
if (SigParseAddress(s, arr[CONFIG_SRC], 0) < 0)
goto error;
/* For "ip" we parse the ports as well, even though they will
be just "any". We do this for later sgh building for the
tcp and udp protocols. */
/* For "ip" we parse the ports as well, even though they will be just "any".
* We do this for later sgh building for the tcp and udp protocols. */
if (strcasecmp(arr[CONFIG_PROTO],"tcp") == 0 ||
strcasecmp(arr[CONFIG_PROTO],"udp") == 0 ||
strcasecmp(arr[CONFIG_PROTO],"ip") == 0) {
@ -456,7 +473,7 @@ int SigParse(DetectEngineCtx *de_ctx, Signature *s, char *sigstr) {
}
/* cleanup */
if (basics) {
if (basics != NULL) {
int i = 0;
while (basics[i] != NULL) {
free(basics[i]);
@ -464,6 +481,7 @@ int SigParse(DetectEngineCtx *de_ctx, Signature *s, char *sigstr) {
}
free(basics);
}
return ret;
}
@ -495,6 +513,15 @@ void SigFree(Signature *s) {
free(s);
}
/**
* \brief Parses a signature and adds it to the Detection Engine Context
*
* \param de_ctx Pointer to the Detection Engine Context
* \param sigstr Pointer to a character string containing the signature to be
* parsed
*
* \retval Pointer to the Signature instance on success; NULL on failure
*/
Signature *SigInit(DetectEngineCtx *de_ctx, char *sigstr) {
Signature *sig = SigAlloc();
if (sig == NULL)

@ -594,9 +594,10 @@ static int SignatureIsIPOnly(DetectEngineCtx *de_ctx, Signature *s) {
if (sm == NULL)
goto iponly;
for ( ; sm != NULL ; sm = sm->next)
for ( ;sm != NULL ;sm = sm->next) {
if (!( sigmatch_table[sm->type].flags & SIGMATCH_IPONLY_COMPAT))
return 0;
}
iponly:
if (!(de_ctx->flags & DE_QUIET)) {
@ -630,7 +631,14 @@ static int SignatureIsInspectingPayload(DetectEngineCtx *de_ctx, Signature *s) {
return 0;
}
/* add all signatures to their own source address group */
/**
* \brief Add all signatures to their own source address group
*
* \param de_ctx Pointer to the Detection Engine Context
*
* \retval 0 on success
* \retval -1 on failure
*/
int SigAddressPrepareStage1(DetectEngineCtx *de_ctx) {
Signature *tmp_s = NULL;
DetectAddressGroup *gr = NULL;

Loading…
Cancel
Save