⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 firewall.c

📁 Linux下server与client的通信实现。Server支持多线程。附有makefile可供编译。Linux下调试通过。
💻 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 + -