?? firewall.c
字號:
#define _GNU_SOURCE#include <stdio.h>#include <ctype.h>#include <stdlib.h>#include <string.h>#include <server.h>#include <firewall.h>#define TRUE 1#define FALSE 0struct ElementList *parseEntry (char **line, int* errno) { *errno = 0; /* No error yet */ char *position = *line; struct ElementList *item; char *tmp; int finished = FALSE; struct ElementList *head = NULL; struct ElementList *previous = NULL; /* ignore white space */ while (isblank(*position)) { position++; } /* capture special case of * */ if (*position == '*') { item = malloc (sizeof (struct ElementList)); if (!item) { fprintf (stderr, "Couldn't allocate memory!\n"); exit (1); } item->element = strdup ("*"); /* to be able to use free later */ item->next = NULL; position ++; *line = position; return (item); } /* now check entries */ while (!finished) { tmp = position; while (*tmp && (!isblank (*tmp)) && (*tmp != ',')) { tmp++; } if (tmp == position) { *errno = 1; /* fprintf (stderr, "Illegal format in string %s\n", position); */ return NULL; } /* create new entry */ item = malloc (sizeof (struct ElementList)); if (!item) { fprintf (stderr, "Couldn't allocate memory!\n"); exit (1); } item->element = malloc (tmp -position + 1); if (!(item->element)) { fprintf (stderr, "Couldn't allocate memory!\n"); exit (1); } item->element = strncpy (item->element, position, tmp -position ); item->element[tmp - position] = '\0'; item->next = NULL; /* printf ("Added string %s\n", item->element);*/ /* add to list */ if (previous) { previous->next = item; } if (!head) { head = item; } previous = item; position = tmp; if (*position == ',') { position++; while (isblank(*position)) { position++; } } else { finished = TRUE; } } *line = position; return (head);}int extractNumber (char **s) { int n = 0; while (isdigit (**s)) { n = 10*n + (**s - '0'); (*s)++; } if (n >= 256) { return -1; } return (n);}int isIPAddress (char *s) { if (extractNumber (&s) == -1) { return FALSE; } if (*s != '.') { return FALSE; } s++; if (extractNumber (&s) == -1) { return FALSE; } if (*s != '.') { return FALSE; } s++; if (extractNumber (&s) == -1) { return FALSE; } if (*s != '.') { return FALSE; } s++; if (extractNumber (&s) == -1) { return FALSE; } return TRUE;}void freeElementList (struct ElementList *list) { struct ElementList *tmp; while (list) { tmp = list->next; free (list->element); free (list); list = tmp; }}struct ConfigLine *parseLine (char *line, int *errno) { struct ConfigLine *entry; struct ElementList *tmp; char *IPAddressString; *errno = 0; entry = malloc (sizeof (struct ConfigLine)); if (!entry) { fprintf (stderr, "Out of memory! \n"); exit (1); } entry->uids = parseEntry (&line, errno); if (!entry->uids) { free (entry); *errno = ILLEGAL_UID; return NULL; } entry->programs = parseEntry (&line, errno); if (!entry->programs) { freeElementList (entry->uids); free (entry); *errno = ILLEGAL_PROGRAM; return NULL; } entry->IPAddresses = parseEntry (&line, errno); if (!entry->IPAddresses) { freeElementList (entry->uids); freeElementList (entry->programs); free (entry); *errno = ILLEGAL_IP_ADDRESS; return NULL; } tmp = entry->IPAddresses; while (tmp) { IPAddressString = tmp->element; if (strcmp (IPAddressString, "*") && !isIPAddress (IPAddressString)){ freeElementList (entry->uids); freeElementList (entry->programs); freeElementList (entry->IPAddresses); free (entry); *errno = ILLEGAL_IP_ADDRESS; return NULL; } tmp = tmp->next; } entry->ports = parseEntry (&line, errno); if (!entry->ports) { freeElementList (entry->uids); freeElementList (entry->programs); freeElementList (entry->IPAddresses); free (entry); *errno = ILLEGAL_PORT; return NULL; } return entry;} int strCompare (char *s1, char* s2) { int result = strcmp (s1, s2); if (result != 0) { if (strcmp (s1, "*") == 0) { return 1; } else if (strcmp (s2, "*") == 0) { return -1; } } return result;} int compareEntry (struct ConfigEntry *entry1, struct ConfigEntry *entry2) { int result; result = strCompare (entry1->uid, entry2->uid); if (result != 0 ) { return result; } result = strCompare (entry1->program, entry2->program); if (result != 0 ) { return result; } result = strCompare (entry1->IPAddress, entry2->IPAddress); if (result != 0 ) { return result; } if (entry1->port < entry2->port) { return -1; } else if (entry1->port > entry2->port) { return 1; } else { return 0; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -