发信人: HPVC()
整理人: williamlong(1999-12-13 19:02:34), 站内信件
|
【 以下文字转载自 Solaris 讨论区 】 【 原文由 cpu 所发表 】 防火墙模块:ipauth.c
/************************************************************************/ /* ipauth.c, by digger */ /* ipauth read the file that include all IPs that authorized to access */ /* some services of localhost, the format is just like: */ /************************************************************************/ /* # this is one comments line begin with "#" */ /* 172.18.85.0 # allow subnet /* 172.18.86.146 */ /* 172.18.86.145 */ /* ... */ /************************************************************************/ /* function InitAuthIP read the authorized IP into memory array, and */ /* function IPIsAuthed check if the given IP is authorized */ /************************************************************************/
# include <stdio.h> # include <sys/types.h> # include <string.h> # include <sys/socket.h> # include <netinet/in.h> # include <arpa/inet.h>
# define MAXHOSTS 32 # define TRUE 0 # define FALSE -1
u_long AuthedIP[MAXHOSTS]; /* authorized IPs */ int AuthedIPNum; /* number of authorized IPs */
void InitAuthIP(char *file) /* read IP from file into memory array */ { FILE *fp; char sBuf[64]; char *tmp; char *s; u_long IP;
if ((fp = fopen(file,"r")) == NULL) { fprintf(stderr, "fopen %s error, terminated\n", file); exit(-1); } AuthedIPNum = 0; while (AuthedIPNum < MAXHOSTS && !feof(fp) && fgets(sBuf, 64, fp)) { tmp = sBuf; s = strtok(tmp, " \t\r\n"); if (s == NULL) continue; /* ignore empty line */ if (s[0] == '#') continue; /* ignore commits line */ if ((IP = inet_addr(s)) != -1) { AuthedIP[AuthedIPNum ++] = IP; } } if (AuthedIPNum == 0) { /* default Authorized IP */ AuthedIP[0] = inet_addr("127.0.0.1"); AuthedIPNum ++ ; } fclose(fp); }
int IPIsAuthed(u_long IP) { int i; for (i = 0;i < AuthedIPNum;i ++) { if ((AuthedIP[i] & (u_long)255) == 0) { /* subnet */ if ((AuthedIP[i] & IP) == AuthedIP[i]) break; } else if (AuthedIP[i] == IP) { /* ip */ break; } } if (i == AuthedIPNum) return FALSE; else return TRUE; }
-- ※ 来源:.广州网易 BBS bbs.nease.net.[FROM: 202.96.190.124]
|
|