1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/*
* Bump in the Host (BIH)
* http://code.google.com/p/bump-in-the-host/source/
* ----------------------------------------------------------
*
* Copyrighted (C) 2010,2011 by the China Mobile Communications
* Corporation <bih.cmcc@gmail.com>;
* See the COPYRIGHT file for full details.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
*/
#ifdef __KERNEL__
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/skbuff.h>
#include <linux/slab.h>
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#endif
#include "voslist.h"
#include "map.h"
#define NIPQUAD(addr) \
((unsigned char *)&addr)[0], \
((unsigned char *)&addr)[1], \
((unsigned char *)&addr)[2], \
((unsigned char *)&addr)[3]
struct pool_node
{
struct listnode *addr;
struct listnode *currnode;
unsigned int curraddr;
}ADDRPOOL;
int pool_addr_add(unsigned int first,unsigned int last);
int pool_addr_add2(char *string);
int pool_addr_del(unsigned int first,unsigned int last);
int pool_addr_del2(char *string);
char *pool_addr_show(void);
unsigned int pool_addr_assign(struct in6_addr *in6addr);
/*
Insert an address block(from 'first' to 'last') into the address pool.
*/
int pool_addr_add(unsigned int first,unsigned int last)
{
struct pool_node *node=&ADDRPOOL;
struct listnode *listnode;
if(ntohl(first)>ntohl(last))
{
lprintf(0,"argument error\n");
return(-1);
}
listnode=listnode_find(node->addr,first);
if(listnode)
{
listnode->data=(void *)last;
return(0);
}
listnode=listnode_add(&(node->addr),first,(void *)last,0);
if(listnode==NULL)
{
lprintf(0,"listnode_add error \n");
return(-1);
}
return(0);
}
/*
Insert an address block into the address pool, not numbers but characters.
*/
int pool_addr_add2(char *string)
{
unsigned int first,last;
char *pstr,*firststr,*laststr;
int ret,slen,num=0,pstrlen,firststrlen,laststrlen;
if(string==NULL)
{
lprintf(0,"arguments error\n");
return(-1);
}
slen=strlen(string);
if(string[slen-1]=='\n')
{
slen--;
string[slen]=0;
}
if(string[slen-1]=='\r')
{
slen--;
string[slen]=0;
}
ret=0;
while(msg_field2(string,slen,num++,&pstr,&pstrlen,',')==0)
{
if(msg_field2(pstr,pstrlen,0,&firststr,&firststrlen,'-')==0&&
msg_field2(pstr,pstrlen,1,&laststr,&laststrlen,'-')==0)
{
first=sk_inet_addr(buff2String(firststr,firststrlen));
last=sk_inet_addr(buff2String(laststr,laststrlen));
ret=pool_addr_add(first,last);
if(ret!=0)
lprintf(0,"\"%.*s\" error\n",pstrlen,pstr);
}
else
lprintf(0,"addr(%.*s) error\n",pstrlen,pstr);
}
return(0);
}
/*
Delete the address block from 'first' to 'last'.
*/
int pool_addr_del(unsigned int first,unsigned int last)
{
struct pool_node *node=&ADDRPOOL;
struct listnode *listnode;
if(ntohl(first)>ntohl(last))
{
lprintf(0,"argument error\n");
return(-1);
}
listnode=listnode_find(node->addr,first);
if(listnode&&listnode->data==(void *)last)
{
listnode_del(&(node->addr),first,NULL);
return(0);
}
lprintf(0,"\"%u.%u.%u.%u-%u.%u.%u.%u\" is not exist\n",NIPQUAD(first),NIPQUAD(last));
return(-1);
}
/*
Delete the address block.
*/
int pool_addr_del2(char *string)
{
unsigned int first,last;
char *pstr,*firststr,*laststr;
int ret,slen,num=0,pstrlen,firststrlen,laststrlen;
if(string==NULL)
{
lprintf(0,"arguments error\n");
return(-1);
}
slen=strlen(string);
if(string[slen-1]=='\n')
{
slen--;
string[slen]=0;
}
if(string[slen-1]=='\r')
{
slen--;
string[slen]=0;
}
ret=0;
while(msg_field2(string,slen,num++,&pstr,&pstrlen,',')==0)
{
if(msg_field2(pstr,pstrlen,0,&firststr,&firststrlen,'-')==0&&
msg_field2(pstr,pstrlen,1,&laststr,&laststrlen,'-')==0)
{
first=sk_inet_addr(buff2String(firststr,firststrlen));
last=sk_inet_addr(buff2String(laststr,laststrlen));
ret=pool_addr_del(first,last);
if(ret!=0)
lprintf(0,"\"%.*s\" error\n",pstrlen,pstr);
}
else
lprintf(0,"addr(%.*s) error\n",pstrlen,pstr);
}
return(0);
}
/*
Print the information of the address block.
*/
char *pool_addr_show(void)
{
struct listnode *node,*head=ADDRPOOL.addr;
unsigned int first,last;
SHOW2BUF_DEF;
SHOW2BUF_INIT();
node=head;
while(node)
{
first=node->key;
last=(unsigned int)(node->data);
if(node!=head)
SHOW2BUF(",");
SHOW2BUF("%u.%u.%u.%u-%u.%u.%u.%u %u.%u.%u.%u",NIPQUAD(first),NIPQUAD(last),NIPQUAD(ADDRPOOL.curraddr));
node=node->next;
}
SHOW2BUF_RET;
}
/*
If there is a new IPv6 address arrival:
1. If this IPv6 address already exists in the mapping relationship, return the IPv4 address;
2. Otherwise, check the address block to find a free IPv4 address to make a mapping relationship with this IPv6 address;
3. If there is no free IPv4 address, then check the next address block, and repeat step 2;
4. If there is no free IPv4 address in all the address blocks, start from the first block and repeate step 2;
5. If there is no free IPv4 address in the address pool, print the error message 'There is on free ip address'.
*/
unsigned int pool_addr_assign(struct in6_addr *in6addr)
{
struct pool_node *node=&ADDRPOOL;
struct listnode *list_node;
unsigned int addr,first_addr,last_addr;
struct map_node *mapnode;
if((node==NULL)||(node->addr==NULL))
{
lprintf(0,"argument error\n");
return(0);
}
if(node->currnode==NULL)
node->currnode=node->addr;
list_node=node->currnode;
if(in6addr)
{
mapnode=map_find(*in6addr);
if(mapnode)
return(mapnode->id);
}
do
{
first_addr=list_node->key;
last_addr=(unsigned int)(list_node->data);
if(node->curraddr==0)
{
node->curraddr=first_addr;
addr=node->curraddr;
}
else
{
addr=htonl(ntohl(node->curraddr)+1);
}
do
{
if(ntohl(addr)>ntohl(last_addr))
addr=first_addr;
if(map_search(addr)==NULL)
{
node->currnode=list_node;
node->curraddr=addr;
if(in6addr)
map_add(addr,*in6addr);
return(addr);
}
else
{
addr=htonl(ntohl(addr)+1);
}
}
while(addr!=node->curraddr);
list_node=list_node->next;
if(list_node==NULL)
list_node=node->addr;
}
while(list_node!=node->currnode);
lprintf(0,"There is no free ip address\n");
return(0);
}
|