JunkBox_Lib  1.10.2
ipaddr_tool.c
Go to the documentation of this file.
1 
8 #include "ipaddr_tool.h"
9 
26 {
27  tList* lp;
28  tList* lt;
29 
30  lp = read_tList_file(fn, 2);
31  if (lp==NULL) return NULL;
32 
33  lt = to_ipaddress_list(lp);
34  del_all_tList(&lp);
35 
36  return lt;
37 }
38 
39 
56 {
57  tList* lp;
58  tList* lt;
59 
60  lp = read_tList_fp(fp, 1);
61  if (lp==NULL) return NULL;
62 
63  lt = to_ipaddress_list(lp);
64  del_all_tList(&lp);
65 
66  return lt;
67 }
68 
69 
84 {
85  unsigned char* num;
86  char* addr;
87  tList* ls = NULL;
88  tList* lt = NULL;
89 
90  if (lp==NULL) return NULL;
91  if (lp->ldat.key.buf==NULL) return NULL;
92 
93  while (lp!=NULL) {
94  if (lp->ldat.key.buf!=NULL && strlen((const char*)lp->ldat.key.buf)>0) {
95  if ((lp->ldat.key.buf)[0]!='#') {
96  num = to_address_num8_ipv4((char*)lp->ldat.key.buf, 1);
97  if (num!=NULL) {
98  addr = to_address_char8_ipv4(num);
99  free(num);
100  if (addr!=NULL) {
101  ls = add_tList_node_str(ls, addr, NULL);
102  free(addr);
103  } // ここで addr==NULL となる場合はメモリ不足のみ
104  }
105  else {
106  addr = awk((char*)lp->ldat.key.buf, '/', 1);
107  if (addr!=NULL) {
108  int i = 0;
109  while(addr[i]!='\0' && addr[i]!=',' && addr[i]!=' ') i++;
110  addr[i] = '\0';
111  ls = add_tList_node_str(ls, addr, NULL);
112  free(addr);
113  }
114  }
115  if (lt==NULL) lt = ls;
116  }
117  }
118  lp = lp->next;
119  }
120 
121  return lt;
122 }
123 
124 
143 int is_host_in_list(tList* lp, unsigned char* num, char* hname)
144 {
145  unsigned char* addr;
146 
147  if (lp==NULL) return FALSE;
148  if (lp->ldat.key.buf==NULL) return FALSE;
149  if (hname==NULL && num==NULL) return FALSE;
150 
151  while (lp!=NULL) {
152  if (lp->ldat.key.buf!=NULL && strlen((const char*)lp->ldat.key.buf)>0) {
153  addr = to_address_num8_ipv4((char*)lp->ldat.key.buf, 0);
154  if (addr==NULL && hname!=NULL) {
155  if (!strnrvscmp((const char*)lp->ldat.key.buf, hname, (int)strlen((const char*)lp->ldat.key.buf))) return TRUE;
156  }
157  else if(addr!=NULL && num!=NULL) {
158  if (is_same_network_num_ipv4(addr, num, &(addr[4]))) return TRUE;
159  }
160  }
161  lp = lp->next;
162  }
163 
164  return FALSE;
165 }
166 
167 
181 int is_ipaddr_num_in_list(tList* lp, unsigned char* num)
182 {
183  unsigned char* addr;
184 
185  if (lp==NULL || num==NULL) return FALSE;
186  if (lp->ldat.key.buf==NULL) return FALSE;
187 
188  while (lp!=NULL) {
189  if (lp->ldat.key.buf!=NULL && strlen((const char*)lp->ldat.key.buf)>0) {
190  addr = to_address_num8_ipv4((char*)lp->ldat.key.buf, 1);
191  if (addr!=NULL) {
192  if (is_same_network_num_ipv4(num, addr, &(addr[4]))) return TRUE;
193  }
194  }
195  lp = lp->next;
196  }
197 
198  return FALSE;
199 }
200 
201 
211 void print_address_in_list(FILE* fp, tList* lp)
212 {
213  unsigned char* addr;
214 
215  while (lp!=NULL) {
216  if (lp->ldat.key.buf!=NULL && strlen((const char*)lp->ldat.key.buf)>0) {
217  addr = to_address_num8_ipv4((char*)lp->ldat.key.buf, 1);
218  if (addr!=NULL) {
219  fprintf(fp, "[%d.%d.%d.%d/%d.%d.%d.%d]\n", addr[0],addr[1],addr[2],addr[3],addr[4],addr[5],addr[6],addr[7]);
220  }
221  else { // 恐らくドメイン名
222  fprintf(fp, "[%s]\n", lp->ldat.key.buf);
223  }
224  }
225  lp = lp->next;
226  }
227  return;
228 }
229 
230 
#define TRUE
Definition: common.h:226
#define FALSE
Definition: common.h:223
tList * read_ipaddr_file(char *fn)
Definition: ipaddr_tool.c:25
int is_ipaddr_num_in_list(tList *lp, unsigned char *num)
Definition: ipaddr_tool.c:181
int is_host_in_list(tList *lp, unsigned char *num, char *hname)
Definition: ipaddr_tool.c:143
tList * to_ipaddress_list(tList *lp)
Definition: ipaddr_tool.c:83
tList * read_ipaddr_fp(FILE *fp)
Definition: ipaddr_tool.c:55
void print_address_in_list(FILE *fp, tList *lp)
Definition: ipaddr_tool.c:211
IP Address Tool Header.
#define is_same_network_num_ipv4(a1, a2, m)
Definition: network.h:263
#define to_address_char8_ipv4(a)
[IPv4 アドレス],[ネットマスク](数字8byte)→ IPv4 アドレス(文字列)
Definition: network.h:251
#define to_address_num8_ipv4(a, m)
IPv4 アドレス(文字列)→ [IPv4 アドレス],[ネットマスク](数字8byte)
Definition: network.h:249
void del_all_tList(tList **pp)
リストの全ノードの削除.ポインタ ppのノードを含むリスト全体を削除する.
Definition: tlist.c:769
tList * read_tList_file(const char *fname, int mode)
ファイルから一行ずつ読み込んでリストのキー部に格納.空行はリストに加えない.
Definition: tlist.c:2127
tList * read_tList_fp(FILE *fp, int mode)
ファイルポインタが示すファイルから一行ずつ読み込んでリストのキー部に格納.
Definition: tlist.c:2158
#define add_tList_node_str(p, k, v)
add_tList_node_bystr()
Definition: tlist.h:142
char * awk(char *buf, char cc, int n)
ccを区切り記号として, strのバッファ内の n番目の項目を返す.要 free()
Definition: tools.c:567
int strnrvscmp(const char *s1, const char *s2, int n)
文字列 s1と s2を後ろから n文字比較する.一致するなら 0
Definition: tools.c:674