发信人: tcpip (俺的昵称改了), 信区: cnunix
标  题: 捉鬼记(三)
发信站: 哈工大紫丁香 (Sun Sep 26 15:03:25 1999), 转信

发信人: cpu (奔腾的心), 信区: Solaris
发信站: 华南网木棉站 (Fri Jul 24 11:42:15 1998), 转信

防火墙模块: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;
}
--

        ******************************************************
                
               青岛啤酒,可能是世界上最好的啤酒 。。。 。。。

        ******************************************************
※ 修改:.trueip 于 Sep 26 15:07:10 修改本文.[FROM: dns.mtlab.hit.ed]
--
※ 转寄:.华南网木棉站 bbs.gznet.edu.cn.[FROM: dns.mtlab.hit.ed]

--
☆ 来源:.哈工大紫丁香 bbs.hit.edu.cn.[FROM: trueip.bbs@melon.gzn]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.171毫秒