荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: hellsolaris (qq), 信区: Security
标  题: 捉鬼记(3)
发信站: 荔园晨风BBS站 (Sat Oct 25 18:22:26 2003), 站内信件

防火墙模块: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.szu.edu.cn·[FROM: 192.168.44.223]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店