Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9ac0c1e

Browse files
committed
CIDR/INET fixes from D'Arcy.
1 parent7787d75 commit9ac0c1e

File tree

4 files changed

+267
-77
lines changed

4 files changed

+267
-77
lines changed

‎src/backend/utils/adt/inet.c

Lines changed: 42 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*is for IP V4 CIDR notation, but prepared for V6: just
44
*add the necessary bits where the comments indicate.
55
*
6-
*$Id: inet.c,v 1.11 1998/10/2204:58:07 momjian Exp $
6+
*$Id: inet.c,v 1.12 1998/10/2213:16:23 momjian Exp $
77
*Jon Postel RIP 16 Oct 1998
88
*/
99

@@ -43,12 +43,9 @@ static intv4bitncmp(unsigned int a1, unsigned int a2, int bits);
4343
#defineip_v4addr(inetptr) \
4444
(((inet_struct *)VARDATA(inetptr))->addr.ipv4_addr)
4545

46-
/*
47-
*INET address reader.
48-
*/
49-
50-
inet*
51-
inet_in(char*src)
46+
/* Common input routine */
47+
staticinet*
48+
inet_common_in(char*src,inttype)
5249
{
5350
intbits;
5451
inet*dst;
@@ -57,24 +54,39 @@ inet_in(char *src)
5754
if (dst==NULL)
5855
{
5956
elog(ERROR,"unable to allocate memory in inet_in()");
60-
return(NULL);
57+
returnNULL;
6158
}
6259
/* First, try for an IP V4 address: */
6360
ip_family(dst)=AF_INET;
64-
bits=inet_net_pton(ip_family(dst),src,&ip_v4addr(dst),ip_addrsize(dst));
61+
bits=inet_net_pton(ip_family(dst),src,&ip_v4addr(dst),
62+
type ?ip_addrsize(dst) :-1);
6563
if ((bits<0)|| (bits>32))
6664
{
6765
/* Go for an IPV6 address here, before faulting out: */
6866
elog(ERROR,"could not parse \"%s\"",src);
6967
pfree(dst);
70-
return(NULL);
68+
returnNULL;
7169
}
7270
VARSIZE(dst)=VARHDRSZ
7371
+ ((char*)&ip_v4addr(dst)- (char*)VARDATA(dst))
7472
+ip_addrsize(dst);
7573
ip_bits(dst)=bits;
76-
ip_type(dst)=0;
77-
return (dst);
74+
ip_type(dst)=type;
75+
returndst;
76+
}
77+
78+
/* INET address reader. */
79+
inet*
80+
inet_in(char*src)
81+
{
82+
returninet_common_in(src,0);
83+
}
84+
85+
/* CIDR address reader. */
86+
inet*
87+
cidr_in(char*src)
88+
{
89+
returninet_common_in(src,1);
7890
}
7991

8092
/*
@@ -90,8 +102,14 @@ inet_out(inet *src)
90102
if (ip_family(src)==AF_INET)
91103
{
92104
/* It's an IP V4 address: */
93-
if (inet_net_ntop(AF_INET,&ip_v4addr(src),ip_bits(src),
94-
tmp,sizeof(tmp))<0)
105+
if (ip_type(src))
106+
dst=inet_cidr_ntop(AF_INET,&ip_v4addr(src),ip_bits(src),
107+
tmp,sizeof(tmp));
108+
else
109+
dst=inet_net_ntop(AF_INET,&ip_v4addr(src),ip_bits(src),
110+
tmp,sizeof(tmp));
111+
112+
if (dst==NULL)
95113
{
96114
elog(ERROR,"unable to print address (%s)",strerror(errno));
97115
return (NULL);
@@ -101,52 +119,18 @@ inet_out(inet *src)
101119
{
102120
/* Go for an IPV6 address here, before faulting out: */
103121
elog(ERROR,"unknown address family (%d)",ip_family(src));
104-
return(NULL);
122+
returnNULL;
105123
}
106-
if (ip_type(src)==0&&ip_bits(src)==32&& (dst=strchr(tmp,'/'))!=NULL)
107-
*dst=0;
108124
dst=palloc(strlen(tmp)+1);
109125
if (dst==NULL)
110126
{
111127
elog(ERROR,"unable to allocate memory in inet_out()");
112-
return(NULL);
128+
returnNULL;
113129
}
114130
strcpy(dst,tmp);
115-
return(dst);
131+
returndst;
116132
}
117133

118-
/*
119-
*CIDR uses all of INET's funcs, just has a separate input func.
120-
*/
121-
122-
inet*
123-
cidr_in(char*src)
124-
{
125-
intbits;
126-
inet*dst;
127-
128-
dst=palloc(VARHDRSZ+sizeof(inet_struct));
129-
if (dst==NULL)
130-
{
131-
elog(ERROR,"unable to allocate memory in cidr_in()");
132-
return (NULL);
133-
}
134-
/* First, try for an IP V4 address: */
135-
ip_family(dst)=AF_INET;
136-
bits=inet_net_pton(ip_family(dst),src,&ip_v4addr(dst),ip_addrsize(dst));
137-
if ((bits<0)|| (bits>32))
138-
{
139-
/* Go for an IPV6 address here, before faulting out: */
140-
elog(ERROR,"could not parse \"%s\"",src);
141-
pfree(dst);
142-
return (NULL);
143-
}
144-
VARSIZE(dst)=VARHDRSZ
145-
+ ((char*)&ip_v4addr(dst)- (char*)VARDATA(dst))
146-
+ip_addrsize(dst);
147-
ip_bits(dst)=bits;
148-
return (dst);
149-
}
150134

151135
/* just a stub */
152136
char*
@@ -331,7 +315,7 @@ inet_host(inet *ip)
331315
/* It's an IP V4 address: */
332316
if (inet_net_ntop(AF_INET,&ip_v4addr(ip),32,tmp,sizeof(tmp))<0)
333317
{
334-
elog(ERROR,"unable to printnetmask (%s)",strerror(errno));
318+
elog(ERROR,"unable to printhost (%s)",strerror(errno));
335319
return (NULL);
336320
}
337321
}
@@ -343,7 +327,7 @@ inet_host(inet *ip)
343327
}
344328
if ((ptr=strchr(tmp,'/'))!=NULL)
345329
*ptr=0;
346-
len=VARHDRSZ+strlen(tmp);
330+
len=VARHDRSZ+strlen(tmp)+1;
347331
ret=palloc(len);
348332
if (ret==NULL)
349333
{
@@ -358,7 +342,7 @@ inet_host(inet *ip)
358342
text*
359343
cidr_host(inet*ip)
360344
{
361-
inet_host(ip);
345+
returninet_host(ip);
362346
}
363347

364348
int4
@@ -402,7 +386,7 @@ inet_broadcast(inet *ip)
402386
}
403387
if ((ptr=strchr(tmp,'/'))!=NULL)
404388
*ptr=0;
405-
len=VARHDRSZ+strlen(tmp);
389+
len=VARHDRSZ+strlen(tmp)+1;
406390
ret=palloc(len);
407391
if (ret==NULL)
408392
{
@@ -417,7 +401,7 @@ inet_broadcast(inet *ip)
417401
text*
418402
cidr_broadcast(inet*ip)
419403
{
420-
inet_broadcast(ip);
404+
returninet_broadcast(ip);
421405
}
422406

423407
text*
@@ -447,7 +431,7 @@ inet_netmask(inet *ip)
447431
}
448432
if ((ptr=strchr(tmp,'/'))!=NULL)
449433
*ptr=0;
450-
len=VARHDRSZ+strlen(tmp);
434+
len=VARHDRSZ+strlen(tmp)+1;
451435
ret=palloc(len);
452436
if (ret==NULL)
453437
{
@@ -462,7 +446,7 @@ inet_netmask(inet *ip)
462446
text*
463447
cidr_netmask(inet*ip)
464448
{
465-
inet_netmask(ip);
449+
returninet_netmask(ip);
466450
}
467451

468452
/*

‎src/backend/utils/adt/inet_net_ntop.c

Lines changed: 105 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
#if defined(LIBC_SCCS)&& !defined(lint)
19-
staticconstcharrcsid[]="$Id: inet_net_ntop.c,v 1.2 1998/10/04 15:35:08 momjian Exp $";
19+
staticconstcharrcsid[]="$Id: inet_net_ntop.c,v 1.3 1998/10/22 13:16:25 momjian Exp $";
2020

2121
#endif
2222

@@ -41,10 +41,12 @@ static const char rcsid[] = "$Id: inet_net_ntop.c,v 1.2 1998/10/04 15:35:08 momj
4141

4242
staticchar*inet_net_ntop_ipv4(constu_char*src,intbits,
4343
char*dst,size_tsize);
44+
staticchar*inet_cidr_ntop_ipv4(constu_char*src,intbits,
45+
char*dst,size_tsize);
4446

4547
/*
4648
* char *
47-
*inet_net_ntop(af, src, bits, dst, size)
49+
*inet_cidr_ntop(af, src, bits, dst, size)
4850
*convert network number from network to presentation format.
4951
*generates CIDR style result always.
5052
* return:
@@ -53,21 +55,22 @@ static char *inet_net_ntop_ipv4(const u_char *src, int bits,
5355
*Paul Vixie (ISC), July 1996
5456
*/
5557
char*
56-
inet_net_ntop(intaf,constvoid*src,intbits,char*dst,size_tsize)
58+
inet_cidr_ntop(intaf,constvoid*src,intbits,char*dst,size_tsize)
5759
{
5860
switch (af)
5961
{
6062
caseAF_INET:
61-
return (inet_net_ntop_ipv4(src,bits,dst,size));
63+
return (inet_cidr_ntop_ipv4(src,bits,dst,size));
6264
default:
6365
errno=EAFNOSUPPORT;
6466
return (NULL);
6567
}
6668
}
6769

70+
6871
/*
6972
* static char *
70-
*inet_net_ntop_ipv4(src, bits, dst, size)
73+
*inet_cidr_ntop_ipv4(src, bits, dst, size)
7174
*convert IPv4 network number from network to presentation format.
7275
*generates CIDR style result always.
7376
* return:
@@ -79,7 +82,7 @@ inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
7982
*Paul Vixie (ISC), July 1996
8083
*/
8184
staticchar*
82-
inet_net_ntop_ipv4(constu_char*src,intbits,char*dst,size_tsize)
85+
inet_cidr_ntop_ipv4(constu_char*src,intbits,char*dst,size_tsize)
8386
{
8487
char*odst=dst;
8588
char*t;
@@ -138,3 +141,99 @@ inet_net_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size)
138141
errno=EMSGSIZE;
139142
return (NULL);
140143
}
144+
145+
146+
/*
147+
* char *
148+
* inet_net_ntop(af, src, bits, dst, size)
149+
*convert host/network address from network to presentation format.
150+
*"src"'s size is determined from its "af".
151+
* return:
152+
*pointer to dst, or NULL if an error occurred (check errno).
153+
* note:
154+
*192.5.5.1/28 has a nonzero host part, which means it isn't a network
155+
*as called for by inet_net_pton() but it can be a host address with
156+
*an included netmask.
157+
* author:
158+
*Paul Vixie (ISC), October 1998
159+
*/
160+
char*
161+
inet_net_ntop(intaf,constvoid*src,intbits,char*dst,size_tsize)
162+
{
163+
switch (af)
164+
{
165+
caseAF_INET:
166+
return (inet_net_ntop_ipv4(src,bits,dst,size));
167+
default:
168+
errno=EAFNOSUPPORT;
169+
return (NULL);
170+
}
171+
}
172+
173+
/*
174+
* static char *
175+
* inet_net_ntop_ipv4(src, bits, dst, size)
176+
*convert IPv4 network address from network to presentation format.
177+
*"src"'s size is determined from its "af".
178+
* return:
179+
*pointer to dst, or NULL if an error occurred (check errno).
180+
* note:
181+
*network byte order assumed. this means 192.5.5.240/28 has
182+
*0b11110000 in its fourth octet.
183+
* author:
184+
*Paul Vixie (ISC), October 1998
185+
*/
186+
staticchar*
187+
inet_net_ntop_ipv4(constu_char*src,intbits,char*dst,size_tsize)
188+
{
189+
char*odst=dst;
190+
char*t;
191+
size_tlen=4;
192+
u_intm;
193+
intb,tb;
194+
195+
if (bits<0||bits>32)
196+
{
197+
errno=EINVAL;
198+
return (NULL);
199+
}
200+
if (bits==0)
201+
{
202+
if (size<sizeof"0")
203+
gotoemsgsize;
204+
*dst++='0';
205+
size--;
206+
*dst='\0';
207+
}
208+
209+
/* Format whole octets plus nonzero trailing octets. */
210+
tb= (bits==32) ?31 :bits;
211+
for (b=0;b <= (tb /8)|| (b<len&&*src!=0);b++)
212+
{
213+
if (size<sizeof"255.")
214+
gotoemsgsize;
215+
t=dst;
216+
dst+=SPRINTF((dst,"%u",*src++));
217+
if (b+1 <= (tb /8)|| (b+1<len&&*src!=0))
218+
{
219+
*dst++='.';
220+
*dst='\0';
221+
}
222+
size-= (size_t)(dst-t);
223+
}
224+
225+
/* don't print masklen if 32 bits */
226+
if (bits==32)
227+
returnodst;
228+
229+
/* Format CIDR /width. */
230+
if (size<sizeof"/32")
231+
gotoemsgsize;
232+
dst+=SPRINTF((dst,"/%u",bits));
233+
234+
return (odst);
235+
236+
emsgsize:
237+
errno=EMSGSIZE;
238+
return (NULL);
239+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp