Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Postgres FD Implementation
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Abuhujair Javed
Postgres FD Implementation
Commits
53d52a68
Commit
53d52a68
authored
Mar 22, 1999
by
Bruce Momjian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleanup of NULL in inet types.
parent
419b91c0
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
81 additions
and
71 deletions
+81
-71
src/backend/utils/adt/mac.c
src/backend/utils/adt/mac.c
+16
-8
src/backend/utils/adt/network.c
src/backend/utils/adt/network.c
+65
-63
No files found.
src/backend/utils/adt/mac.c
View file @
53d52a68
/*
/*
* PostgreSQL type definitions for MAC addresses.
* PostgreSQL type definitions for MAC addresses.
*
*
* $Id: mac.c,v 1.
6 1998/10/26 01:01:36 tgl
Exp $
* $Id: mac.c,v 1.
7 1999/03/22 05:00:57 momjian
Exp $
*/
*/
#include <stdio.h>
#include <stdio.h>
...
@@ -156,7 +156,6 @@ macaddr_in(char *str)
...
@@ -156,7 +156,6 @@ macaddr_in(char *str)
if
(
strlen
(
str
)
>
0
)
if
(
strlen
(
str
)
>
0
)
{
{
count
=
sscanf
(
str
,
"%x:%x:%x:%x:%x:%x"
,
&
a
,
&
b
,
&
c
,
&
d
,
&
e
,
&
f
);
count
=
sscanf
(
str
,
"%x:%x:%x:%x:%x:%x"
,
&
a
,
&
b
,
&
c
,
&
d
,
&
e
,
&
f
);
if
(
count
!=
6
)
if
(
count
!=
6
)
count
=
sscanf
(
str
,
"%x-%x-%x-%x-%x-%x"
,
&
a
,
&
b
,
&
c
,
&
d
,
&
e
,
&
f
);
count
=
sscanf
(
str
,
"%x-%x-%x-%x-%x-%x"
,
&
a
,
&
b
,
&
c
,
&
d
,
&
e
,
&
f
);
...
@@ -168,18 +167,12 @@ macaddr_in(char *str)
...
@@ -168,18 +167,12 @@ macaddr_in(char *str)
count
=
sscanf
(
str
,
"%2x%2x.%2x%2x.%2x%2x"
,
&
a
,
&
b
,
&
c
,
&
d
,
&
e
,
&
f
);
count
=
sscanf
(
str
,
"%2x%2x.%2x%2x.%2x%2x"
,
&
a
,
&
b
,
&
c
,
&
d
,
&
e
,
&
f
);
if
(
count
!=
6
)
if
(
count
!=
6
)
{
elog
(
ERROR
,
"macaddr_in: error in parsing
\"
%s
\"
"
,
str
);
elog
(
ERROR
,
"macaddr_in: error in parsing
\"
%s
\"
"
,
str
);
return
(
NULL
);
}
if
((
a
<
0
)
||
(
a
>
255
)
||
(
b
<
0
)
||
(
b
>
255
)
||
if
((
a
<
0
)
||
(
a
>
255
)
||
(
b
<
0
)
||
(
b
>
255
)
||
(
c
<
0
)
||
(
c
>
255
)
||
(
d
<
0
)
||
(
d
>
255
)
||
(
c
<
0
)
||
(
c
>
255
)
||
(
d
<
0
)
||
(
d
>
255
)
||
(
e
<
0
)
||
(
e
>
255
)
||
(
f
<
0
)
||
(
f
>
255
))
(
e
<
0
)
||
(
e
>
255
)
||
(
f
<
0
)
||
(
f
>
255
))
{
elog
(
ERROR
,
"macaddr_in: illegal address
\"
%s
\"
"
,
str
);
elog
(
ERROR
,
"macaddr_in: illegal address
\"
%s
\"
"
,
str
);
return
(
NULL
);
}
}
}
else
else
{
{
...
@@ -232,6 +225,8 @@ macaddr_out(macaddr *addr)
...
@@ -232,6 +225,8 @@ macaddr_out(macaddr *addr)
bool
bool
macaddr_lt
(
macaddr
*
a1
,
macaddr
*
a2
)
macaddr_lt
(
macaddr
*
a1
,
macaddr
*
a2
)
{
{
if
(
!
PointerIsValid
(
a1
)
||
!
PointerIsValid
(
a2
))
return
FALSE
;
return
((
hibits
(
a1
)
<
hibits
(
a2
))
||
return
((
hibits
(
a1
)
<
hibits
(
a2
))
||
((
hibits
(
a1
)
==
hibits
(
a2
))
&&
lobits
(
a1
)
<
lobits
(
a2
)));
((
hibits
(
a1
)
==
hibits
(
a2
))
&&
lobits
(
a1
)
<
lobits
(
a2
)));
}
}
...
@@ -239,6 +234,8 @@ macaddr_lt(macaddr *a1, macaddr *a2)
...
@@ -239,6 +234,8 @@ macaddr_lt(macaddr *a1, macaddr *a2)
bool
bool
macaddr_le
(
macaddr
*
a1
,
macaddr
*
a2
)
macaddr_le
(
macaddr
*
a1
,
macaddr
*
a2
)
{
{
if
(
!
PointerIsValid
(
a1
)
||
!
PointerIsValid
(
a2
))
return
FALSE
;
return
((
hibits
(
a1
)
<
hibits
(
a2
))
||
return
((
hibits
(
a1
)
<
hibits
(
a2
))
||
((
hibits
(
a1
)
==
hibits
(
a2
))
&&
lobits
(
a1
)
<=
lobits
(
a2
)));
((
hibits
(
a1
)
==
hibits
(
a2
))
&&
lobits
(
a1
)
<=
lobits
(
a2
)));
}
}
...
@@ -246,12 +243,16 @@ macaddr_le(macaddr *a1, macaddr *a2)
...
@@ -246,12 +243,16 @@ macaddr_le(macaddr *a1, macaddr *a2)
bool
bool
macaddr_eq
(
macaddr
*
a1
,
macaddr
*
a2
)
macaddr_eq
(
macaddr
*
a1
,
macaddr
*
a2
)
{
{
if
(
!
PointerIsValid
(
a1
)
||
!
PointerIsValid
(
a2
))
return
FALSE
;
return
((
hibits
(
a1
)
==
hibits
(
a2
))
&&
(
lobits
(
a1
)
==
lobits
(
a2
)));
return
((
hibits
(
a1
)
==
hibits
(
a2
))
&&
(
lobits
(
a1
)
==
lobits
(
a2
)));
}
}
bool
bool
macaddr_ge
(
macaddr
*
a1
,
macaddr
*
a2
)
macaddr_ge
(
macaddr
*
a1
,
macaddr
*
a2
)
{
{
if
(
!
PointerIsValid
(
a1
)
||
!
PointerIsValid
(
a2
))
return
FALSE
;
return
((
hibits
(
a1
)
>
hibits
(
a2
))
||
return
((
hibits
(
a1
)
>
hibits
(
a2
))
||
((
hibits
(
a1
)
==
hibits
(
a2
))
&&
lobits
(
a1
)
>=
lobits
(
a2
)));
((
hibits
(
a1
)
==
hibits
(
a2
))
&&
lobits
(
a1
)
>=
lobits
(
a2
)));
}
}
...
@@ -259,6 +260,8 @@ macaddr_ge(macaddr *a1, macaddr *a2)
...
@@ -259,6 +260,8 @@ macaddr_ge(macaddr *a1, macaddr *a2)
bool
bool
macaddr_gt
(
macaddr
*
a1
,
macaddr
*
a2
)
macaddr_gt
(
macaddr
*
a1
,
macaddr
*
a2
)
{
{
if
(
!
PointerIsValid
(
a1
)
||
!
PointerIsValid
(
a2
))
return
FALSE
;
return
((
hibits
(
a1
)
>
hibits
(
a2
))
||
return
((
hibits
(
a1
)
>
hibits
(
a2
))
||
((
hibits
(
a1
)
==
hibits
(
a2
))
&&
lobits
(
a1
)
>
lobits
(
a2
)));
((
hibits
(
a1
)
==
hibits
(
a2
))
&&
lobits
(
a1
)
>
lobits
(
a2
)));
}
}
...
@@ -266,6 +269,8 @@ macaddr_gt(macaddr *a1, macaddr *a2)
...
@@ -266,6 +269,8 @@ macaddr_gt(macaddr *a1, macaddr *a2)
bool
bool
macaddr_ne
(
macaddr
*
a1
,
macaddr
*
a2
)
macaddr_ne
(
macaddr
*
a1
,
macaddr
*
a2
)
{
{
if
(
!
PointerIsValid
(
a1
)
||
!
PointerIsValid
(
a2
))
return
FALSE
;
return
((
hibits
(
a1
)
!=
hibits
(
a2
))
||
(
lobits
(
a1
)
!=
lobits
(
a2
)));
return
((
hibits
(
a1
)
!=
hibits
(
a2
))
||
(
lobits
(
a1
)
!=
lobits
(
a2
)));
}
}
...
@@ -299,6 +304,9 @@ macaddr_manuf(macaddr *addr)
...
@@ -299,6 +304,9 @@ macaddr_manuf(macaddr *addr)
int
length
;
int
length
;
text
*
result
;
text
*
result
;
if
(
!
PointerIsValid
(
addr
))
return
NULL
;
for
(
manuf
=
manufacturers
;
manuf
->
name
!=
NULL
;
manuf
++
)
for
(
manuf
=
manufacturers
;
manuf
->
name
!=
NULL
;
manuf
++
)
{
{
if
((
manuf
->
a
==
addr
->
a
)
&&
if
((
manuf
->
a
==
addr
->
a
)
&&
...
...
src/backend/utils/adt/network.c
View file @
53d52a68
...
@@ -3,7 +3,7 @@
...
@@ -3,7 +3,7 @@
* is for IP V4 CIDR notation, but prepared for V6: just
* is for IP V4 CIDR notation, but prepared for V6: just
* add the necessary bits where the comments indicate.
* add the necessary bits where the comments indicate.
*
*
* $Id: network.c,v 1.
6 1999/02/24 03:17:05
momjian Exp $
* $Id: network.c,v 1.
7 1999/03/22 05:00:57
momjian Exp $
* Jon Postel RIP 16 Oct 1998
* Jon Postel RIP 16 Oct 1998
*/
*/
...
@@ -50,23 +50,21 @@ network_in(char *src, int type)
...
@@ -50,23 +50,21 @@ network_in(char *src, int type)
int
bits
;
int
bits
;
inet
*
dst
;
inet
*
dst
;
if
(
!
src
)
return
NULL
;
dst
=
palloc
(
VARHDRSZ
+
sizeof
(
inet_struct
));
dst
=
palloc
(
VARHDRSZ
+
sizeof
(
inet_struct
));
if
(
dst
==
NULL
)
if
(
dst
==
NULL
)
{
elog
(
ERROR
,
"unable to allocate memory in network_in()"
);
elog
(
ERROR
,
"unable to allocate memory in network_in()"
);
return
NULL
;
}
/* First, try for an IP V4 address: */
/* First, try for an IP V4 address: */
ip_family
(
dst
)
=
AF_INET
;
ip_family
(
dst
)
=
AF_INET
;
bits
=
inet_net_pton
(
ip_family
(
dst
),
src
,
&
ip_v4addr
(
dst
),
bits
=
inet_net_pton
(
ip_family
(
dst
),
src
,
&
ip_v4addr
(
dst
),
type
?
ip_addrsize
(
dst
)
:
-
1
);
type
?
ip_addrsize
(
dst
)
:
-
1
);
if
((
bits
<
0
)
||
(
bits
>
32
))
if
((
bits
<
0
)
||
(
bits
>
32
))
{
/* Go for an IPV6 address here, before faulting out: */
/* Go for an IPV6 address here, before faulting out: */
elog
(
ERROR
,
"could not parse
\"
%s
\"
"
,
src
);
elog
(
ERROR
,
"could not parse
\"
%s
\"
"
,
src
);
pfree
(
dst
);
return
NULL
;
}
VARSIZE
(
dst
)
=
VARHDRSZ
VARSIZE
(
dst
)
=
VARHDRSZ
+
((
char
*
)
&
ip_v4addr
(
dst
)
-
(
char
*
)
VARDATA
(
dst
))
+
((
char
*
)
&
ip_v4addr
(
dst
)
-
(
char
*
)
VARDATA
(
dst
))
+
ip_addrsize
(
dst
);
+
ip_addrsize
(
dst
);
...
@@ -110,23 +108,16 @@ inet_out(inet *src)
...
@@ -110,23 +108,16 @@ inet_out(inet *src)
tmp
,
sizeof
(
tmp
));
tmp
,
sizeof
(
tmp
));
if
(
dst
==
NULL
)
if
(
dst
==
NULL
)
{
elog
(
ERROR
,
"unable to print address (%s)"
,
strerror
(
errno
));
elog
(
ERROR
,
"unable to print address (%s)"
,
strerror
(
errno
));
return
(
NULL
);
}
}
}
else
else
{
/* Go for an IPV6 address here, before faulting out: */
/* Go for an IPV6 address here, before faulting out: */
elog
(
ERROR
,
"unknown address family (%d)"
,
ip_family
(
src
));
elog
(
ERROR
,
"unknown address family (%d)"
,
ip_family
(
src
));
return
NULL
;
}
dst
=
palloc
(
strlen
(
tmp
)
+
1
);
dst
=
palloc
(
strlen
(
tmp
)
+
1
);
if
(
dst
==
NULL
)
if
(
dst
==
NULL
)
{
elog
(
ERROR
,
"unable to allocate memory in inet_out()"
);
elog
(
ERROR
,
"unable to allocate memory in inet_out()"
);
return
NULL
;
}
strcpy
(
dst
,
tmp
);
strcpy
(
dst
,
tmp
);
return
dst
;
return
dst
;
}
}
...
@@ -146,6 +137,8 @@ cidr_out(inet *src)
...
@@ -146,6 +137,8 @@ cidr_out(inet *src)
bool
bool
network_lt
(
inet
*
a1
,
inet
*
a2
)
network_lt
(
inet
*
a1
,
inet
*
a2
)
{
{
if
(
!
PointerIsValid
(
a1
)
||
!
PointerIsValid
(
a2
))
return
FALSE
;
if
((
ip_family
(
a1
)
==
AF_INET
)
&&
(
ip_family
(
a2
)
==
AF_INET
))
if
((
ip_family
(
a1
)
==
AF_INET
)
&&
(
ip_family
(
a2
)
==
AF_INET
))
{
{
int
order
=
v4bitncmp
(
ip_v4addr
(
a1
),
ip_v4addr
(
a2
),
ip_bits
(
a2
));
int
order
=
v4bitncmp
(
ip_v4addr
(
a1
),
ip_v4addr
(
a2
),
ip_bits
(
a2
));
...
@@ -157,19 +150,23 @@ network_lt(inet *a1, inet *a2)
...
@@ -157,19 +150,23 @@ network_lt(inet *a1, inet *a2)
/* Go for an IPV6 address here, before faulting out: */
/* Go for an IPV6 address here, before faulting out: */
elog
(
ERROR
,
"cannot compare address families %d and %d"
,
elog
(
ERROR
,
"cannot compare address families %d and %d"
,
ip_family
(
a1
),
ip_family
(
a2
));
ip_family
(
a1
),
ip_family
(
a2
));
return
(
FALSE
)
;
return
FALSE
;
}
}
}
}
bool
bool
network_le
(
inet
*
a1
,
inet
*
a2
)
network_le
(
inet
*
a1
,
inet
*
a2
)
{
{
if
(
!
PointerIsValid
(
a1
)
||
!
PointerIsValid
(
a2
))
return
FALSE
;
return
(
network_lt
(
a1
,
a2
)
||
network_eq
(
a1
,
a2
));
return
(
network_lt
(
a1
,
a2
)
||
network_eq
(
a1
,
a2
));
}
}
bool
bool
network_eq
(
inet
*
a1
,
inet
*
a2
)
network_eq
(
inet
*
a1
,
inet
*
a2
)
{
{
if
(
!
PointerIsValid
(
a1
)
||
!
PointerIsValid
(
a2
))
return
FALSE
;
if
((
ip_family
(
a1
)
==
AF_INET
)
&&
(
ip_family
(
a2
)
==
AF_INET
))
if
((
ip_family
(
a1
)
==
AF_INET
)
&&
(
ip_family
(
a2
)
==
AF_INET
))
{
{
return
((
ip_bits
(
a1
)
==
ip_bits
(
a2
))
return
((
ip_bits
(
a1
)
==
ip_bits
(
a2
))
...
@@ -180,19 +177,23 @@ network_eq(inet *a1, inet *a2)
...
@@ -180,19 +177,23 @@ network_eq(inet *a1, inet *a2)
/* Go for an IPV6 address here, before faulting out: */
/* Go for an IPV6 address here, before faulting out: */
elog
(
ERROR
,
"cannot compare address families %d and %d"
,
elog
(
ERROR
,
"cannot compare address families %d and %d"
,
ip_family
(
a1
),
ip_family
(
a2
));
ip_family
(
a1
),
ip_family
(
a2
));
return
(
FALSE
)
;
return
FALSE
;
}
}
}
}
bool
bool
network_ge
(
inet
*
a1
,
inet
*
a2
)
network_ge
(
inet
*
a1
,
inet
*
a2
)
{
{
if
(
!
PointerIsValid
(
a1
)
||
!
PointerIsValid
(
a2
))
return
FALSE
;
return
(
network_gt
(
a1
,
a2
)
||
network_eq
(
a1
,
a2
));
return
(
network_gt
(
a1
,
a2
)
||
network_eq
(
a1
,
a2
));
}
}
bool
bool
network_gt
(
inet
*
a1
,
inet
*
a2
)
network_gt
(
inet
*
a1
,
inet
*
a2
)
{
{
if
(
!
PointerIsValid
(
a1
)
||
!
PointerIsValid
(
a2
))
return
FALSE
;
if
((
ip_family
(
a1
)
==
AF_INET
)
&&
(
ip_family
(
a2
)
==
AF_INET
))
if
((
ip_family
(
a1
)
==
AF_INET
)
&&
(
ip_family
(
a2
)
==
AF_INET
))
{
{
int
order
=
v4bitncmp
(
ip_v4addr
(
a1
),
ip_v4addr
(
a2
),
ip_bits
(
a2
));
int
order
=
v4bitncmp
(
ip_v4addr
(
a1
),
ip_v4addr
(
a2
),
ip_bits
(
a2
));
...
@@ -204,19 +205,24 @@ network_gt(inet *a1, inet *a2)
...
@@ -204,19 +205,24 @@ network_gt(inet *a1, inet *a2)
/* Go for an IPV6 address here, before faulting out: */
/* Go for an IPV6 address here, before faulting out: */
elog
(
ERROR
,
"cannot compare address families %d and %d"
,
elog
(
ERROR
,
"cannot compare address families %d and %d"
,
ip_family
(
a1
),
ip_family
(
a2
));
ip_family
(
a1
),
ip_family
(
a2
));
return
(
FALSE
)
;
return
FALSE
;
}
}
}
}
bool
bool
network_ne
(
inet
*
a1
,
inet
*
a2
)
network_ne
(
inet
*
a1
,
inet
*
a2
)
{
{
if
(
!
PointerIsValid
(
a1
)
||
!
PointerIsValid
(
a2
))
return
FALSE
;
return
(
!
network_eq
(
a1
,
a2
));
return
(
!
network_eq
(
a1
,
a2
));
}
}
bool
bool
network_sub
(
inet
*
a1
,
inet
*
a2
)
network_sub
(
inet
*
a1
,
inet
*
a2
)
{
{
if
(
!
PointerIsValid
(
a1
)
||
!
PointerIsValid
(
a2
))
return
NULL
;
if
((
ip_family
(
a1
)
==
AF_INET
)
&&
(
ip_family
(
a2
)
==
AF_INET
))
if
((
ip_family
(
a1
)
==
AF_INET
)
&&
(
ip_family
(
a2
)
==
AF_INET
))
{
{
return
((
ip_bits
(
a1
)
>
ip_bits
(
a2
))
return
((
ip_bits
(
a1
)
>
ip_bits
(
a2
))
...
@@ -227,13 +233,16 @@ network_sub(inet *a1, inet *a2)
...
@@ -227,13 +233,16 @@ network_sub(inet *a1, inet *a2)
/* Go for an IPV6 address here, before faulting out: */
/* Go for an IPV6 address here, before faulting out: */
elog
(
ERROR
,
"cannot compare address families %d and %d"
,
elog
(
ERROR
,
"cannot compare address families %d and %d"
,
ip_family
(
a1
),
ip_family
(
a2
));
ip_family
(
a1
),
ip_family
(
a2
));
return
(
FALSE
)
;
return
FALSE
;
}
}
}
}
bool
bool
network_subeq
(
inet
*
a1
,
inet
*
a2
)
network_subeq
(
inet
*
a1
,
inet
*
a2
)
{
{
if
(
!
PointerIsValid
(
a1
)
||
!
PointerIsValid
(
a2
))
return
NULL
;
if
((
ip_family
(
a1
)
==
AF_INET
)
&&
(
ip_family
(
a2
)
==
AF_INET
))
if
((
ip_family
(
a1
)
==
AF_INET
)
&&
(
ip_family
(
a2
)
==
AF_INET
))
{
{
return
((
ip_bits
(
a1
)
>=
ip_bits
(
a2
))
return
((
ip_bits
(
a1
)
>=
ip_bits
(
a2
))
...
@@ -244,13 +253,16 @@ network_subeq(inet *a1, inet *a2)
...
@@ -244,13 +253,16 @@ network_subeq(inet *a1, inet *a2)
/* Go for an IPV6 address here, before faulting out: */
/* Go for an IPV6 address here, before faulting out: */
elog
(
ERROR
,
"cannot compare address families %d and %d"
,
elog
(
ERROR
,
"cannot compare address families %d and %d"
,
ip_family
(
a1
),
ip_family
(
a2
));
ip_family
(
a1
),
ip_family
(
a2
));
return
(
FALSE
)
;
return
FALSE
;
}
}
}
}
bool
bool
network_sup
(
inet
*
a1
,
inet
*
a2
)
network_sup
(
inet
*
a1
,
inet
*
a2
)
{
{
if
(
!
PointerIsValid
(
a1
)
||
!
PointerIsValid
(
a2
))
return
NULL
;
if
((
ip_family
(
a1
)
==
AF_INET
)
&&
(
ip_family
(
a2
)
==
AF_INET
))
if
((
ip_family
(
a1
)
==
AF_INET
)
&&
(
ip_family
(
a2
)
==
AF_INET
))
{
{
return
((
ip_bits
(
a1
)
<
ip_bits
(
a2
))
return
((
ip_bits
(
a1
)
<
ip_bits
(
a2
))
...
@@ -261,13 +273,16 @@ network_sup(inet *a1, inet *a2)
...
@@ -261,13 +273,16 @@ network_sup(inet *a1, inet *a2)
/* Go for an IPV6 address here, before faulting out: */
/* Go for an IPV6 address here, before faulting out: */
elog
(
ERROR
,
"cannot compare address families %d and %d"
,
elog
(
ERROR
,
"cannot compare address families %d and %d"
,
ip_family
(
a1
),
ip_family
(
a2
));
ip_family
(
a1
),
ip_family
(
a2
));
return
(
FALSE
)
;
return
FALSE
;
}
}
}
}
bool
bool
network_supeq
(
inet
*
a1
,
inet
*
a2
)
network_supeq
(
inet
*
a1
,
inet
*
a2
)
{
{
if
(
!
PointerIsValid
(
a1
)
||
!
PointerIsValid
(
a2
))
return
NULL
;
if
((
ip_family
(
a1
)
==
AF_INET
)
&&
(
ip_family
(
a2
)
==
AF_INET
))
if
((
ip_family
(
a1
)
==
AF_INET
)
&&
(
ip_family
(
a2
)
==
AF_INET
))
{
{
return
((
ip_bits
(
a1
)
<=
ip_bits
(
a2
))
return
((
ip_bits
(
a1
)
<=
ip_bits
(
a2
))
...
@@ -278,7 +293,7 @@ network_supeq(inet *a1, inet *a2)
...
@@ -278,7 +293,7 @@ network_supeq(inet *a1, inet *a2)
/* Go for an IPV6 address here, before faulting out: */
/* Go for an IPV6 address here, before faulting out: */
elog
(
ERROR
,
"cannot compare address families %d and %d"
,
elog
(
ERROR
,
"cannot compare address families %d and %d"
,
ip_family
(
a1
),
ip_family
(
a2
));
ip_family
(
a1
),
ip_family
(
a2
));
return
(
FALSE
)
;
return
FALSE
;
}
}
}
}
...
@@ -304,36 +319,29 @@ network_host(inet *ip)
...
@@ -304,36 +319,29 @@ network_host(inet *ip)
char
*
ptr
,
char
*
ptr
,
tmp
[
sizeof
(
"255.255.255.255/32"
)];
tmp
[
sizeof
(
"255.255.255.255/32"
)];
if
(
!
PointerIsValid
(
ip
))
return
NULL
;
if
(
ip_type
(
ip
))
if
(
ip_type
(
ip
))
{
elog
(
ERROR
,
"CIDR type has no host part"
);
elog
(
ERROR
,
"CIDR type has no host part"
);
return
NULL
;
}
if
(
ip_family
(
ip
)
==
AF_INET
)
if
(
ip_family
(
ip
)
==
AF_INET
)
{
{
/* It's an IP V4 address: */
/* It's an IP V4 address: */
if
(
inet_net_ntop
(
AF_INET
,
&
ip_v4addr
(
ip
),
32
,
tmp
,
sizeof
(
tmp
))
==
NULL
)
if
(
inet_net_ntop
(
AF_INET
,
&
ip_v4addr
(
ip
),
32
,
tmp
,
sizeof
(
tmp
))
==
NULL
)
{
elog
(
ERROR
,
"unable to print host (%s)"
,
strerror
(
errno
));
elog
(
ERROR
,
"unable to print host (%s)"
,
strerror
(
errno
));
return
(
NULL
);
}
}
}
else
else
{
/* Go for an IPV6 address here, before faulting out: */
/* Go for an IPV6 address here, before faulting out: */
elog
(
ERROR
,
"unknown address family (%d)"
,
ip_family
(
ip
));
elog
(
ERROR
,
"unknown address family (%d)"
,
ip_family
(
ip
));
return
(
NULL
);
}
if
((
ptr
=
strchr
(
tmp
,
'/'
))
!=
NULL
)
if
((
ptr
=
strchr
(
tmp
,
'/'
))
!=
NULL
)
*
ptr
=
0
;
*
ptr
=
0
;
len
=
VARHDRSZ
+
strlen
(
tmp
)
+
1
;
len
=
VARHDRSZ
+
strlen
(
tmp
)
+
1
;
ret
=
palloc
(
len
);
ret
=
palloc
(
len
);
if
(
ret
==
NULL
)
if
(
ret
==
NULL
)
{
elog
(
ERROR
,
"unable to allocate memory in network_host()"
);
elog
(
ERROR
,
"unable to allocate memory in network_host()"
);
return
(
NULL
);
}
VARSIZE
(
ret
)
=
len
;
VARSIZE
(
ret
)
=
len
;
strcpy
(
VARDATA
(
ret
),
tmp
);
strcpy
(
VARDATA
(
ret
),
tmp
);
return
(
ret
);
return
(
ret
);
...
@@ -342,6 +350,9 @@ network_host(inet *ip)
...
@@ -342,6 +350,9 @@ network_host(inet *ip)
int4
int4
network_masklen
(
inet
*
ip
)
network_masklen
(
inet
*
ip
)
{
{
if
(
!
PointerIsValid
(
ip
))
return
0
;
return
ip_bits
(
ip
);
return
ip_bits
(
ip
);
}
}
...
@@ -353,6 +364,9 @@ network_broadcast(inet *ip)
...
@@ -353,6 +364,9 @@ network_broadcast(inet *ip)
char
*
ptr
,
char
*
ptr
,
tmp
[
sizeof
(
"255.255.255.255/32"
)];
tmp
[
sizeof
(
"255.255.255.255/32"
)];
if
(
!
PointerIsValid
(
ip
))
return
NULL
;
if
(
ip_family
(
ip
)
==
AF_INET
)
if
(
ip_family
(
ip
)
==
AF_INET
)
{
{
/* It's an IP V4 address: */
/* It's an IP V4 address: */
...
@@ -364,26 +378,20 @@ network_broadcast(inet *ip)
...
@@ -364,26 +378,20 @@ network_broadcast(inet *ip)
addr
=
htonl
(
ntohl
(
ip_v4addr
(
ip
))
|
mask
);
addr
=
htonl
(
ntohl
(
ip_v4addr
(
ip
))
|
mask
);
if
(
inet_net_ntop
(
AF_INET
,
&
addr
,
32
,
tmp
,
sizeof
(
tmp
))
==
NULL
)
if
(
inet_net_ntop
(
AF_INET
,
&
addr
,
32
,
tmp
,
sizeof
(
tmp
))
==
NULL
)
{
elog
(
ERROR
,
"unable to print address (%s)"
,
strerror
(
errno
));
elog
(
ERROR
,
"unable to print address (%s)"
,
strerror
(
errno
));
return
(
NULL
);
}
}
}
else
else
{
/* Go for an IPV6 address here, before faulting out: */
/* Go for an IPV6 address here, before faulting out: */
elog
(
ERROR
,
"unknown address family (%d)"
,
ip_family
(
ip
));
elog
(
ERROR
,
"unknown address family (%d)"
,
ip_family
(
ip
));
return
(
NULL
);
}
if
((
ptr
=
strchr
(
tmp
,
'/'
))
!=
NULL
)
if
((
ptr
=
strchr
(
tmp
,
'/'
))
!=
NULL
)
*
ptr
=
0
;
*
ptr
=
0
;
len
=
VARHDRSZ
+
strlen
(
tmp
)
+
1
;
len
=
VARHDRSZ
+
strlen
(
tmp
)
+
1
;
ret
=
palloc
(
len
);
ret
=
palloc
(
len
);
if
(
ret
==
NULL
)
if
(
ret
==
NULL
)
{
elog
(
ERROR
,
"unable to allocate memory in network_broadcast()"
);
elog
(
ERROR
,
"unable to allocate memory in network_broadcast()"
);
return
(
NULL
);
}
VARSIZE
(
ret
)
=
len
;
VARSIZE
(
ret
)
=
len
;
strcpy
(
VARDATA
(
ret
),
tmp
);
strcpy
(
VARDATA
(
ret
),
tmp
);
return
(
ret
);
return
(
ret
);
...
@@ -396,30 +404,27 @@ network_network(inet *ip)
...
@@ -396,30 +404,27 @@ network_network(inet *ip)
int
len
;
int
len
;
char
tmp
[
sizeof
(
"255.255.255.255/32"
)];
char
tmp
[
sizeof
(
"255.255.255.255/32"
)];
if
(
!
PointerIsValid
(
ip
))
return
NULL
;
if
(
ip_family
(
ip
)
==
AF_INET
)
if
(
ip_family
(
ip
)
==
AF_INET
)
{
{
/* It's an IP V4 address: */
/* It's an IP V4 address: */
int
addr
=
htonl
(
ntohl
(
ip_v4addr
(
ip
))
&
(
0xffffffff
<<
(
32
-
ip_bits
(
ip
))));
int
addr
=
htonl
(
ntohl
(
ip_v4addr
(
ip
))
&
(
0xffffffff
<<
(
32
-
ip_bits
(
ip
))));
if
(
inet_cidr_ntop
(
AF_INET
,
&
addr
,
ip_bits
(
ip
),
tmp
,
sizeof
(
tmp
))
==
NULL
)
if
(
inet_cidr_ntop
(
AF_INET
,
&
addr
,
ip_bits
(
ip
),
tmp
,
sizeof
(
tmp
))
==
NULL
)
{
elog
(
ERROR
,
"unable to print network (%s)"
,
strerror
(
errno
));
elog
(
ERROR
,
"unable to print network (%s)"
,
strerror
(
errno
));
return
(
NULL
);
}
}
}
else
else
{
/* Go for an IPV6 address here, before faulting out: */
/* Go for an IPV6 address here, before faulting out: */
elog
(
ERROR
,
"unknown address family (%d)"
,
ip_family
(
ip
));
elog
(
ERROR
,
"unknown address family (%d)"
,
ip_family
(
ip
));
return
(
NULL
);
}
len
=
VARHDRSZ
+
strlen
(
tmp
)
+
1
;
len
=
VARHDRSZ
+
strlen
(
tmp
)
+
1
;
ret
=
palloc
(
len
);
ret
=
palloc
(
len
);
if
(
ret
==
NULL
)
if
(
ret
==
NULL
)
{
elog
(
ERROR
,
"unable to allocate memory in network_network()"
);
elog
(
ERROR
,
"unable to allocate memory in network_network()"
);
return
(
NULL
);
}
VARSIZE
(
ret
)
=
len
;
VARSIZE
(
ret
)
=
len
;
strcpy
(
VARDATA
(
ret
),
tmp
);
strcpy
(
VARDATA
(
ret
),
tmp
);
return
(
ret
);
return
(
ret
);
...
@@ -433,32 +438,29 @@ network_netmask(inet *ip)
...
@@ -433,32 +438,29 @@ network_netmask(inet *ip)
char
*
ptr
,
char
*
ptr
,
tmp
[
sizeof
(
"255.255.255.255/32"
)];
tmp
[
sizeof
(
"255.255.255.255/32"
)];
if
(
!
PointerIsValid
(
ip
))
return
NULL
;
if
(
ip_family
(
ip
)
==
AF_INET
)
if
(
ip_family
(
ip
)
==
AF_INET
)
{
{
/* It's an IP V4 address: */
/* It's an IP V4 address: */
int
addr
=
htonl
((
-
1
<<
(
32
-
ip_bits
(
ip
)))
&
0xffffffff
);
int
addr
=
htonl
((
-
1
<<
(
32
-
ip_bits
(
ip
)))
&
0xffffffff
);
if
(
inet_net_ntop
(
AF_INET
,
&
addr
,
32
,
tmp
,
sizeof
(
tmp
))
==
NULL
)
if
(
inet_net_ntop
(
AF_INET
,
&
addr
,
32
,
tmp
,
sizeof
(
tmp
))
==
NULL
)
{
elog
(
ERROR
,
"unable to print netmask (%s)"
,
strerror
(
errno
));
elog
(
ERROR
,
"unable to print netmask (%s)"
,
strerror
(
errno
));
return
(
NULL
);
}
}
}
else
else
{
/* Go for an IPV6 address here, before faulting out: */
/* Go for an IPV6 address here, before faulting out: */
elog
(
ERROR
,
"unknown address family (%d)"
,
ip_family
(
ip
));
elog
(
ERROR
,
"unknown address family (%d)"
,
ip_family
(
ip
));
return
(
NULL
);
}
if
((
ptr
=
strchr
(
tmp
,
'/'
))
!=
NULL
)
if
((
ptr
=
strchr
(
tmp
,
'/'
))
!=
NULL
)
*
ptr
=
0
;
*
ptr
=
0
;
len
=
VARHDRSZ
+
strlen
(
tmp
)
+
1
;
len
=
VARHDRSZ
+
strlen
(
tmp
)
+
1
;
ret
=
palloc
(
len
);
ret
=
palloc
(
len
);
if
(
ret
==
NULL
)
if
(
ret
==
NULL
)
{
elog
(
ERROR
,
"unable to allocate memory in network_netmask()"
);
elog
(
ERROR
,
"unable to allocate memory in network_netmask()"
);
return
(
NULL
);
}
VARSIZE
(
ret
)
=
len
;
VARSIZE
(
ret
)
=
len
;
strcpy
(
VARDATA
(
ret
),
tmp
);
strcpy
(
VARDATA
(
ret
),
tmp
);
return
(
ret
);
return
(
ret
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment