Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
babeld
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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
babeld
Commits
03d6d27a
Commit
03d6d27a
authored
May 17, 2012
by
Grégoire Henry
Committed by
Juliusz Chroboczek
May 23, 2012
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add IPv4 support in kernel_routes() on BSD
parent
a02bc458
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
75 additions
and
49 deletions
+75
-49
kernel_socket.c
kernel_socket.c
+75
-49
No files found.
kernel_socket.c
View file @
03d6d27a
...
@@ -532,82 +532,104 @@ static void
...
@@ -532,82 +532,104 @@ static void
print_kernel_route
(
int
add
,
struct
kernel_route
*
route
)
print_kernel_route
(
int
add
,
struct
kernel_route
*
route
)
{
{
char
ifname
[
IFNAMSIZ
];
char
ifname
[
IFNAMSIZ
];
char
addr_prefix
[
INET6_ADDRSTRLEN
];
char
addr_gw
[
INET6_ADDRSTRLEN
];
if
(
!
inet_ntop
(
AF_INET6
,
route
->
prefix
,
if
(
!
if_indextoname
(
route
->
ifindex
,
ifname
))
addr_prefix
,
sizeof
(
addr_prefix
))
||
memcpy
(
ifname
,
"unk"
,
4
);
!
inet_ntop
(
AF_INET6
,
route
->
gw
,
addr_gw
,
sizeof
(
addr_gw
))
||
!
if_indextoname
(
route
->
ifindex
,
ifname
))
{
fprintf
(
stderr
,
"Couldn't format kernel route for printing."
);
// return;
}
fprintf
(
stderr
,
fprintf
(
stderr
,
"%s kernel route: dest: %s
/%d
gw: %s metric: %d if: %s(%d)
\n
"
,
"%s kernel route: dest: %s gw: %s metric: %d if: %s(%d)
\n
"
,
add
==
RTM_ADD
?
"Add"
:
add
==
RTM_ADD
?
"Add"
:
add
==
RTM_DELETE
?
"Delete"
:
"Change"
,
add
==
RTM_DELETE
?
"Delete"
:
"Change"
,
addr_prefix
,
route
->
plen
,
addr_gw
,
route
->
metric
,
ifname
,
format_prefix
(
route
->
prefix
,
route
->
plen
),
route
->
ifindex
format_address
(
route
->
gw
),
route
->
metric
,
ifname
,
route
->
ifindex
);
);
}
}
static
int
static
int
parse_kernel_route
(
const
struct
rt_msghdr
*
rtm
,
struct
kernel_route
*
route
)
parse_kernel_route
(
const
struct
rt_msghdr
*
rtm
,
struct
kernel_route
*
route
)
{
{
struct
sockaddr
*
sa
;
void
*
rta
=
(
void
*
)
rtm
+
sizeof
(
struct
rt_msghdr
);
void
*
rta
=
(
void
*
)
rtm
+
sizeof
(
struct
rt_msghdr
);
struct
sockaddr_in6
*
sin6
;
char
addr
[
INET6_ADDRSTRLEN
];
if
(
ifindex_lo
<
0
)
{
ifindex_lo
=
if_nametoindex
(
"lo0"
);
if
(
ifindex_lo
<=
0
)
return
-
1
;
}
memset
(
route
,
0
,
sizeof
(
*
route
));
memset
(
route
,
0
,
sizeof
(
*
route
));
route
->
metric
=
0
;
route
->
metric
=
0
;
route
->
ifindex
=
rtm
->
rtm_index
;
route
->
ifindex
=
rtm
->
rtm_index
;
/* filter out kernel route (cache) */
if
((
rtm
->
rtm_flags
&
RTF_IFSCOPE
)
!=
0
)
return
-
1
;
/* filter out our own route */
if
((
rtm
->
rtm_flags
&
RTF_PROTO2
)
!=
0
)
return
-
1
;
/* Prefix */
if
(
!
(
rtm
->
rtm_addrs
&
RTA_DST
))
if
(
!
(
rtm
->
rtm_addrs
&
RTA_DST
))
return
-
1
;
return
-
1
;
sin6
=
(
struct
sockaddr_in6
*
)
rta
;
sa
=
(
struct
sockaddr
*
)
rta
;
rta
+=
ROUNDUP
(
sa
->
sa_len
);
if
(
sa
->
sa_family
==
AF_INET6
)
{
struct
sockaddr_in6
*
sin6
=
(
struct
sockaddr_in6
*
)
sa
;
memcpy
(
route
->
prefix
,
&
sin6
->
sin6_addr
,
16
);
if
(
IN6_IS_ADDR_LINKLOCAL
(
&
sin6
->
sin6_addr
)
if
(
IN6_IS_ADDR_LINKLOCAL
(
&
sin6
->
sin6_addr
)
||
IN6_IS_ADDR_MC_LINKLOCAL
(
&
sin6
->
sin6_addr
))
||
IN6_IS_ADDR_MC_LINKLOCAL
(
&
sin6
->
sin6_addr
))
return
-
1
;
return
-
1
;
if
((
rtm
->
rtm_flags
&
RTF_PROTO2
)
!=
0
)
}
else
if
(
sa
->
sa_family
==
AF_INET
)
{
struct
sockaddr_in
*
sin
=
(
struct
sockaddr_in
*
)
sa
;
#ifdef __APPLE__
if
(
IN_LINKLOCAL
(
ntohl
(
sin
->
sin_addr
.
s_addr
)))
return
-
1
;
return
-
1
;
memcpy
(
&
route
->
prefix
,
&
sin6
->
sin6_addr
,
16
);
#endif
rta
+=
ROUNDUP
(
sizeof
(
struct
sockaddr_in6
));
if
(
IN_MULTICAST
(
ntohl
(
sin
->
sin_addr
.
s_addr
)))
return
-
1
;
v4tov6
(
route
->
prefix
,
(
unsigned
char
*
)
&
sin
->
sin_addr
);
}
else
{
return
-
1
;
}
/* Gateway */
if
(
!
(
rtm
->
rtm_addrs
&
RTA_GATEWAY
))
if
(
!
(
rtm
->
rtm_addrs
&
RTA_GATEWAY
))
return
-
1
;
return
-
1
;
sa
=
(
struct
sockaddr
*
)
rta
;
sin6
=
(
struct
sockaddr_in6
*
)
rta
;
rta
+=
ROUNDUP
(
sa
->
sa_len
);
if
(
sa
->
sa_family
==
AF_INET6
)
{
struct
sockaddr_in6
*
sin6
=
(
struct
sockaddr_in6
*
)
sa
;
memcpy
(
route
->
gw
,
&
sin6
->
sin6_addr
,
16
);
if
(
IN6_IS_ADDR_LINKLOCAL
(
&
sin6
->
sin6_addr
))
{
if
(
IN6_IS_ADDR_LINKLOCAL
(
&
sin6
->
sin6_addr
))
{
route
->
ifindex
=
IN6_LINKLOCAL_IFINDEX
(
sin6
->
sin6_addr
);
route
->
ifindex
=
IN6_LINKLOCAL_IFINDEX
(
sin6
->
sin6_addr
);
SET_IN6_LINKLOCAL_IFINDEX
(
sin6
->
sin6_addr
,
0
);
SET_IN6_LINKLOCAL_IFINDEX
(
sin6
->
sin6_addr
,
0
);
}
}
memcpy
(
&
route
->
gw
,
&
sin6
->
sin6_addr
,
16
);
}
else
if
(
sa
->
sa_family
==
AF_INET
)
{
rta
+=
ROUNDUP
(
sizeof
(
struct
sockaddr_in6
));
struct
sockaddr_in
*
sin
=
(
struct
sockaddr_in
*
)
sa
;
v4tov6
(
route
->
gw
,
(
unsigned
char
*
)
&
sin
->
sin_addr
);
}
if
(
route
->
ifindex
==
ifindex_lo
)
return
-
1
;
if
(
!
(
rtm
->
rtm_addrs
&
RTA_NETMASK
))
{
/* Netmask */
route
->
plen
=
0
;
if
((
rtm
->
rtm_addrs
&
RTA_NETMASK
)
!=
0
)
{
sa
=
(
struct
sockaddr
*
)
rta
;
rta
+=
ROUNDUP
(
sa
->
sa_len
);
if
(
!
v4mapped
(
route
->
prefix
))
{
struct
sockaddr_in6
*
sin6
=
(
struct
sockaddr_in6
*
)
sa
;
route
->
plen
=
mask2len
(
sin6
->
sin6_addr
.
__u6_addr
.
__u6_addr8
,
16
);
}
else
{
}
else
{
sin6
=
(
struct
sockaddr_in6
*
)
rta
;
struct
sockaddr_in
*
sin
=
(
struct
sockaddr_in
*
)
sa
;
route
->
plen
=
mask2len
(
&
sin6
->
sin6_addr
);
route
->
plen
=
mask2len
((
unsigned
char
*
)
&
sin
->
sin_addr
,
4
);
inet_ntop
(
AF_INET6
,
&
sin6
->
sin6_addr
,
addr
,
sizeof
(
addr
));
rta
+=
ROUNDUP
(
sizeof
(
struct
sockaddr_in6
));
}
}
if
(
rtm
->
rtm_flags
&
RTF_HOST
)
route
->
plen
=
128
;
if
(
ifindex_lo
<
0
)
{
ifindex_lo
=
if_nametoindex
(
"lo0"
);
if
(
ifindex_lo
<=
0
)
return
-
1
;
}
}
if
(
v4mapped
(
route
->
prefix
))
route
->
plen
+=
96
;
if
(
route
->
ifindex
==
ifindex_lo
)
if
(
rtm
->
rtm_flags
&
RTF_HOST
)
route
->
plen
=
128
;
return
-
1
;
return
0
;
return
0
;
}
}
int
int
...
@@ -622,7 +644,7 @@ kernel_routes(struct kernel_route *routes, int maxroutes)
...
@@ -622,7 +644,7 @@ kernel_routes(struct kernel_route *routes, int maxroutes)
mib
[
0
]
=
CTL_NET
;
mib
[
0
]
=
CTL_NET
;
mib
[
1
]
=
PF_ROUTE
;
mib
[
1
]
=
PF_ROUTE
;
mib
[
2
]
=
0
;
mib
[
2
]
=
0
;
mib
[
3
]
=
AF_
INET6
;
/* Address family */
mib
[
3
]
=
AF_
UNSPEC
;
/* Address family */
mib
[
4
]
=
NET_RT_DUMP
;
/* Dump the kernel routing table */
mib
[
4
]
=
NET_RT_DUMP
;
/* Dump the kernel routing table */
mib
[
5
]
=
0
;
/* No flags */
mib
[
5
]
=
0
;
/* No flags */
...
@@ -748,6 +770,10 @@ kernel_addresses(char *ifname, int ifindex, int ll,
...
@@ -748,6 +770,10 @@ kernel_addresses(char *ifname, int ifindex, int ll,
struct
sockaddr_in
*
sin
=
(
struct
sockaddr_in
*
)
ifap
->
ifa_addr
;
struct
sockaddr_in
*
sin
=
(
struct
sockaddr_in
*
)
ifap
->
ifa_addr
;
if
(
ll
)
if
(
ll
)
goto
next
;
goto
next
;
#ifdef __APPLE__
if
(
IN_LINKLOCAL
(
htonl
(
sin
->
sin_addr
.
s_addr
)))
goto
next
;
#endif
memcpy
(
routes
[
i
].
prefix
,
v4prefix
,
12
);
memcpy
(
routes
[
i
].
prefix
,
v4prefix
,
12
);
memcpy
(
routes
[
i
].
prefix
+
12
,
&
sin
->
sin_addr
,
4
);
memcpy
(
routes
[
i
].
prefix
+
12
,
&
sin
->
sin_addr
,
4
);
routes
[
i
].
plen
=
128
;
routes
[
i
].
plen
=
128
;
...
...
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