From 31990741fbf7dec987b8319cad65ff942dd4b957 Mon Sep 17 00:00:00 2001 From: victor Date: Tue, 1 Jun 2004 09:42:35 +0000 Subject: ip/mask git-svn-id: http://opensvn.csie.org/pttbbs/branches/victor.screen@2041 63ad8ddf-47c3-0310-b6dd-a9e9d9715204 --- include/proto.h | 1 + include/pttstruct.h | 8 +++++--- mbbsd/cache.c | 33 ++++++++++++++++++--------------- mbbsd/mbbsd.c | 23 ++++++----------------- mbbsd/stuff.c | 16 ++++++++++++++++ mbbsd/talk.c | 2 +- 6 files changed, 47 insertions(+), 36 deletions(-) diff --git a/include/proto.h b/include/proto.h index a3e6c557..efed5fd4 100644 --- a/include/proto.h +++ b/include/proto.h @@ -483,6 +483,7 @@ void out_lines(const char *str, int line); #define pressanykey() vmsg_lines(b_lines, NULL) int vmsg_lines(int lines, const char *msg); int log_user(const char *fmt, ...); +unsigned int ipstr2int(char *ip); time_t gettime(int line, time_t dt, char* head); void setcalfile(char *buf, char *userid); void stand_title(char *title); diff --git a/include/pttstruct.h b/include/pttstruct.h index 7a419c42..80eef33e 100644 --- a/include/pttstruct.h +++ b/include/pttstruct.h @@ -435,9 +435,11 @@ typedef struct { } GV2; /* fromcache */ - char domain[MAX_FROM][50]; - char replace[MAX_FROM][50]; - int top; + unsigned int home_ip[MAX_FROM]; + unsigned int home_mask[MAX_FROM]; + char home_desc[MAX_FROM][32]; + int home_num; + int max_user; time_t max_time; time_t Fuptime; diff --git a/mbbsd/cache.c b/mbbsd/cache.c index 842cedad..dcf0c039 100644 --- a/mbbsd/cache.c +++ b/mbbsd/cache.c @@ -868,25 +868,28 @@ reload_fcache(void) FILE *fp; SHM->Fbusystate = 1; - bzero(SHM->domain, sizeof(SHM->domain)); if ((fp = fopen("etc/domain_name_query", "r"))) { - char buf[256], *po; + char buf[256], *ip, *mask; - SHM->top = 0; + SHM->home_num = 0; while (fgets(buf, sizeof(buf), fp)) { - if (buf[0] && buf[0] != '#' && buf[0] != ' ' && - buf[0] != '\n') { - sscanf(buf, "%s", SHM->domain[SHM->top]); // XXX check buffer size - po = buf + strlen(SHM->domain[SHM->top]); - while (*po == ' ' || *po == '\t') - po++; - strncpy(SHM->replace[SHM->top], po, 49); - SHM->replace[SHM->top] - [strlen(SHM->replace[SHM->top]) - 1] = 0; - (SHM->top)++; - if (SHM->top == MAX_FROM) - break; + if (!buf[0] || buf[0] == '#' || buf[0] == ' ' || buf[0] == '\n') { + continue; + } + ip = strtok(buf, " \t"); + if ((mask = strchr(ip, '/')) != NULL) { + SHM->home_ip[SHM->home_num] = ipstr2int(ip); + SHM->home_mask[SHM->home_num] = atoi(mask); } + else + SHM->home_ip[SHM->home_num] = ipstr2int(ip); + ip = strtok(NULL, " \t"); + strncpy(SHM->home_desc[SHM->home_num], ip, sizeof(SHM->home_desc[SHM->home_num])); + SHM->home_desc[SHM->home_num] + [strlen(SHM->home_desc[SHM->home_num]) - 1] = 0; + (SHM->home_num)++; + if (SHM->home_num == MAX_FROM) + break; } fclose(fp); } diff --git a/mbbsd/mbbsd.c b/mbbsd/mbbsd.c index 151bf6e9..f3468cbb 100644 --- a/mbbsd/mbbsd.c +++ b/mbbsd/mbbsd.c @@ -696,25 +696,14 @@ del_distinct(char *fname, char *line) static int where(char *from) { - register int i = 0, count = 0, j; - - for (j = 0; j < SHM->top; j++) { - char *token = strtok(SHM->domain[j], "&"); - - i = 0; - count = 0; - while (token) { - if (strstr(from, token)) - count++; - token = strtok(NULL, "&"); - i++; + register int i; + + for (i = 0; i < SHM->home_num; i++) { + if ((SHM->home_ip[i] & SHM->home_mask[i]) == (ipstr2int(from) & SHM->home_mask[i])) { + return i; } - if (i == count) - break; } - if (i != count) - return 0; - return j; + return 0; } #endif diff --git a/mbbsd/stuff.c b/mbbsd/stuff.c index 8e6bc461..9f00435f 100644 --- a/mbbsd/stuff.c +++ b/mbbsd/stuff.c @@ -317,6 +317,22 @@ belong(char *filelist, char *key) return rc; } +unsigned int +ipstr2int(char *ip) +{ + unsigned int i, val = 0; + char *p = strtok(ip, "."); + val = atoi(p); + for (i = 0; i < 3; i++) { + p = strtok(NULL, "."); + val *= 256; + if (p == NULL) + continue; + val += atoi(p); + } + return val; +} + #ifndef _BBS_UTIL_C_ /* getdata_buf */ time_t gettime(int line, time_t dt, char*head) diff --git a/mbbsd/talk.c b/mbbsd/talk.c index b710c66d..86981eac 100644 --- a/mbbsd/talk.c +++ b/mbbsd/talk.c @@ -1584,7 +1584,7 @@ descript(int show_mode, userinfo_t * uentp, time_t diff) return (((uentp->pager != 2 && uentp->pager != 3 && diff) || HAS_PERM(PERM_SYSOP)) ? #ifdef WHERE - uentp->from_alias ? SHM->replace[uentp->from_alias] : + uentp->from_alias ? SHM->home_desc[uentp->from_alias] : uentp->from #else uentp->from -- cgit v1.2.3