📄 snot_parse_rules.c
字号:
goto ignore;
}
if ((token = strtok(NULL, " ")) == NULL)
{
printf("parse_rules: [line %d]: Truncated, ignoring\n",line);
goto ignore;
}
}
else if ((token = strtok(NULL, " ")) != NULL)
{
// construct source address list using the rule's string
if(parse_ip(token,&(r->srcaddr),&(r->srcnum),line))
{
goto ignore;
}
}
else
{
printf("parse_rules: [line %d]: Truncated, ignoring\n",line);
goto ignore;
}
/* get the source port number */
if ((token = strtok(NULL, " ")) != NULL)
{
if(!(STRCASECMP(token, "any")))
{
r->srcport = ANY;
}
else
{
if(*token == '!')
{
r->srcportnot = 1;
token++;
}
else
{
r->srcportnot = 0;
}
r->srcport = atoi(token);
}
}
else
{
printf("parse_rules: [line %d]: Truncated, ignoring\n",line);
goto ignore;
}
/* get the direction */
if ((token = strtok(NULL, " ")) != NULL)
{
if((!(STRCASECMP(token, "->")))||(!(STRCASECMP(token, "<>"))))
{
if(!(STRCASECMP(token, "->")))
{
r->bidirectional = FALSE;
}
else
{
r->bidirectional = TRUE;
}
}
}
else
{
printf("parse_rules: [line %d]: Truncated, ignoring\n",line);
goto ignore;
}
/* get the dest IP */
/* first check if its been overridden */
if (globdst != NULL)
{
// construct destination address list using the input argument
if(parse_ip(globdst,&(r->destaddr),&(r->destnum),line))
{
goto ignore;
}
if ((token = strtok(NULL, " ")) == NULL)
{
printf("parse_rules: [line %d]: Truncated, ignoring\n",line);
goto ignore;
}
}
else if ((token = strtok(NULL, " ")) != NULL)
{
// construct destination address list using the rule string
if(parse_ip(token,&(r->destaddr),&(r->destnum),line))
{
goto ignore;
}
}
else
{
printf("parse_rules: [line %d]: Truncated, ignoring\n",line);
goto ignore;
}
/* get the dest port number */
if ((token = strtok(NULL, " ")) != NULL)
{
if(!(STRCASECMP(token, "any")))
{
r->destport = ANY;
}
else
{
if(*token == '!')
{
r->destportnot = 1;
token++;
}
else
{
r->destportnot = 0;
}
r->destport = atoi(token);
}
}
else
{
printf("parse_rules: [line %d]: Truncated, ignoring\n",line);
goto ignore;
}
if (r->proto == 0)
{
printf("parse_rules: [line %d]: Invalid Rule, ignoring\n",line);
goto ignore;
}
/* now work out the crap at the end */
// begin to construct option_list in the rule structure
if((token = strtok(optstring, "(")) != NULL)
{
/* if((olhead = malloc(sizeof(struct option_list))) == NULL)
{
printf("parse_rules: [line %d]: malloc failed\n", line);
return(1);
}
*/
olhead = NULL;
/* current = olhead;
current->option_string = NULL;
current->next = NULL; */
/* Make sure there is at least one option in here */
if ((token = strtok(NULL, ";")) == NULL)
{
printf("parse_rules: [line %d]: Malformed options string, ignoring\n",line);
goto ignore;
}
/* skip any spaces */
while(isspace(*token))
{
token++;
if(*token == '\0')
{
printf("parse_rules: [line %d]: Malformed options string, ignoring\n",line);
goto ignore;
}
}
/* Loop through all the options, and make a linked list of them */
while((token != NULL)&&(*token != ')'))
{
if((olcur = malloc(sizeof(struct option_list))) == NULL)
{
printf("parse_rules: [line %d]: malloc failed\n", line);
return(1);
}
/* token is a option field now */
olcur->option_string = strdup(token);
if ((token = strtok(NULL, ";")) != NULL)
{
/* skip any spaces */
while(isspace(*token))
{
token++;
if(*token == '\0')
{
printf("parse_rules: [Line %d]: Malformed options string, ignoring\n",line);
goto ignore;
}
}
/* Stick it on the front of the list */
olcur->next = olhead;
olhead = olcur;
/*
if((current->next = malloc(sizeof(struct option_list)))==NULL)
{
printf("parse_rules: [line %d]: malloc failed\n", line);
return(1);
}
current = current->next;
*/
}
else
{
printf("parse_rules: [line %d]: Missing ; terminator in options string, ignoring\n",line);
goto ignore;
}
}
/* current->option_string = NULL;
current->next = NULL;
*/
olcur = olhead;
/* Blank out the array */
memset(r->optionlist, 0, (sizeof(char *) * OPTION_MAX));
/* Search the list for our option strings, and make the option array */
while(olcur != NULL)
{
if(!(STRCASECMP("nocase",olcur->option_string)))
{
/* special case for the only option with no argument */
r->optionlist[NOCASE] = (char *)1;
}
else if((arg = strchr(olcur->option_string, ':')) != NULL)
{
token = strtok(olcur->option_string, ":");
arg++;
while(isspace(*arg))
{
arg++;
}
if(*arg == '\0')
{
printf("parse_rules: [line %d]: Malformed options string, ignoring\n",line);
goto ignore;
}
/* if there is a trailing bracket, chop it off */
/* if(temp = strrchr(arg, ')'))
{
*temp = '\0';
}
*/ /* arg now points to the argument */
found = 0;
for(i=0;(i!=OPTION_MAX);i++)
{
if(!(STRCASECMP(option_text[i],token)))
{
found = 1;
if(r->optionlist[i] != NULL)
{
/* some tool has duped the option strings, just take
the last one */
free(r->optionlist[i]);
}
/* special code to handle each type of option */
switch (i)
{
case MSG:
r->optionlist[i] = strdup(arg);
break;
/* These take u_char */
case ITYPE:
case ICODE:
case TTL:
case TOS:
if ( (unsigned char)atoi(arg) > 255 )
{
printf("parse_rules: [line %d]: option %s greater than 255, ignoring\n",line, option_text[i]);
goto ignore;
}
if ((uc = malloc(sizeof(unsigned char))) == NULL)
{
printf("parse_rules: [line %d]: malloc failed..\n");
return(1);
}
*uc = (unsigned char)atoi(arg);
r->optionlist[i] = uc;
break;
/* These take u_short */
case ICMP_ID:
case ICMP_SEQ:
case ID:
if ( atoi(arg) > 65535 )
{
printf("parse_rules: [line %d]: option %s greater than allowed size 65,535, ignoring\n",line, option_text[i]);
goto ignore;
}
if ((us = malloc(sizeof(unsigned short))) == NULL)
{
printf("parse_rules: [line %d]: malloc failed..\n");
return(1);
}
*us = atoi(arg);
r->optionlist[i] = (char *)us;
break;
/* These take u_int_32 */
case SEQ:
case ACK:
if ((ul = malloc(sizeof(unsigned long))) == NULL)
{
printf("parse_rules: [line %d]: malloc failed..\n");
return(1);
}
*ul = (unsigned long)atol(arg);
r->optionlist[i] = (char *)ul;
case DEPTH:
case OFFSET:
if ((mi = malloc(sizeof(int))) == NULL)
{
printf("parse_rules: [line %d]: malloc failed..\n");
return(1);
}
*mi = atoi(arg);
r->optionlist[i] = (char *)mi;
break;
case IPOPTS:
if((ia = malloc(IPOPTS_MAX)) == NULL)
{
printf("parse_rules: [line %d]: malloc failed..\n");
return(1);
}
memset(ia, FALSE, IPOPTS_MAX);
while((tok2 = strtok(NULL, ",")) != NULL)
{
while(isspace(*tok2))
{
tok2++;
}
if(*tok2 == '\0')
{
printf("parse_rules: [line %d]: Malformed ipopts string, ignoring\n",line);
goto ignore;
}
temp = tok2;
/* Chop trailing spaces */
while(*temp != '\0')
{
if(isspace(*temp))
{
*temp = '\0';
}
temp++;
}
found2 = FALSE;
for(j=0;(j!=IPOPTS_MAX);j++)
{
if(!(STRCASECMP(ipopts_text[j],tok2)))
{
ia[j] = TRUE;
found2 = TRUE;
}
}
if (found2 == FALSE)
{
printf("parse_rules: [line %d]: Illegal ipopts string %s, ignoring\n", line, tok2);
goto ignore;
}
}
r->optionlist[i] = ia;
break;
case DSIZE:
if((ds = malloc(sizeof(struct dsize))) == NULL)
{
printf("parse_rules: [line %d]: malloc failed..\n");
return(1);
}
if(*arg == '<')
{
((struct dsize *)ds)->gtlt = DSIZELT;
arg++;
}
else if(*arg == '>')
{
((struct dsize *)ds)->gtlt = DSIZEGT;
arg++;
}
else
// This is an exact match
{
((struct dsize *)ds)->gtlt = DSIZEEQ;
}
((struct dsize *)ds)->size = atoi(arg);
if (((struct dsize *)ds)->size > (1500-(LIBNET_PACKET)))
{
printf("parse_rules: [line %d]: dsize too large, ignoring\n",line);
goto ignore;
}
r->optionlist[i] = ds;
break;
case FRAGBITS:
while(isspace(*arg))
{
arg++;
}
if((flags = malloc(sizeof(int))) == NULL)
{
printf("parse_rules: [line %d]: malloc failed..\n");
return(1);
}
*flags = 0;
while(*arg != '\0')
{
switch(*arg)
{
case 'r':
case 'R':
*flags |= FRAGR;
break;
case 'd':
case 'D':
*flags |= FRAGD;
break;
case 'm':
case 'M':
*flags |= FRAGM;
break;
case '*':
*flags |= FRAGSTAR;
break;
case '+':
*flags |= FRAGPLUS;
break;
case '-':
*flags |= FRAGMINUS;
break;
default:
printf("parse_rules: [line %d]: Illegal fragmentation flags char \"%c\", ignoring\n", line, *arg);
goto ignore;
break;
}
arg++;
}
if(*flags == 0)
{
printf("parse_rules: [line %d]: No fragmentation flags provided, ignoring\n",line);
goto ignore;
}
r->optionlist[i] = (char *)flags;
break;
case FLAGS:
while(isspace(*arg))
{
arg++;
}
if((flags = malloc(sizeof(int))) == NULL)
{
printf("parse_rules: [line %d]: malloc failed..\n");
return(1);
}
*flags = 0;
while(*arg != '\0')
{
switch(*arg)
{
case 'f':
case 'F':
*flags |= TFIN;
break;
case 's':
case 'S':
*flags |= TSYN;
break;
case 'r':
case 'R':
*flags |= TRST;
break;
case 'p':
case 'P':
*flags |= TPSH;
break;
case 'a':
case 'A':
*flags |= TACK;
break;
case 'u':
case 'U':
*flags |= TURG;
break;
case '2':
*flags |= TRES2;
break;
case '1':
*flags |= TRES1;
break;
case '+':
*flags |= TPLUS;
break;
case '*':
*flags |= TSTAR;
break;
case '-':
*flags |= TMINUS;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -