Chapter 17 routing sockets - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Chapter 17 routing sockets

Description:

Chapter 17 routing sockets abstract Introduction datalink socket address structure reading and writing sysctl operation get_ifi_info function interface name and index ... – PowerPoint PPT presentation

Number of Views:727
Avg rating:3.0/5.0
Slides: 22
Provided by: eceUtAcI
Category:

less

Transcript and Presenter's Notes

Title: Chapter 17 routing sockets


1
Chapter 17 routing sockets
2
abstract
  • Introduction
  • datalink socket address structure
  • reading and writing
  • sysctl operation
  • get_ifi_info function
  • interface name and index fuction

3
Introduction
  • Access the Unix routing table within the kernel
  • ioctl command (SIOCADDRT, SIODELRT)
  • netstat read the kernel memory to obtain the
    contents of the routing table
  • Routing daemons (gated)
  • monitor ICMP
  • AF_ROUTE domain gt cleaned up the interface to
    the kernels routing system
  • only type of socket supported in the route domain
    gt raw socket

4
  • Three type of operation on routing socket
  • process can send a message to the kernel by
    writing to a routing socket(addition and deletion
    of routes, Only superuser)
  • process can read a message from the kernel on a
    routing socket (Only superuser)
  • process can use the sysctl function to either
    dump the routing table or to list all the
    configured interface

5
datalink socket address structure
  • Returned by routing socket ltnet/if_dl.hgt

Structure sockaddr_dl uint8_t sdl_len sa_family_
t sdl_family /AF_LINK/ uint16_t
sdl_index/system assigned index/ uint8_t
sdl_type /IFT_ETHER/ uint8_t sdl_nlen /name
length/ uint8_t sdl_alen/link-layer address
length/ uint8_t sdl_slen/ link-layer selector
length / char sdl_data12/minimum work area,
name and
link-layer address/
6
reading and writing
  • After a process create a routing socket, it can
    send commands to the kernel by writing to the
    socket and read information from the kernel by
    reading from the socket
  • Three different structure are exchanged across a
    routing socket
  • rt_msghdr, if_msghdr, ifa_msghdr (figure 17-3)

7
Figure 17-2
8
Data exchanged with kernel across routing socket
for RTM_GET command
9
Figure 17-6
  • include "unproute.h"
  • define BUFLEN (sizeof(struct rt_msghdr) 512)
  • / 8 sizeof(struct sockaddr_in6) 192 /
  • define SEQ 9999
  • int main(int argc, char argv)
  • int sockfd
  • char buf
  • pid_t pid
  • ssize_t n
  • struct rt_msghdr rtm
  • struct sockaddr sa, rti_infoRTAX_MAX
  • struct sockaddr_in sin

10
Figure 17-6(2)
  • if (argc ! 2) err_quit("usage getrt
    ltIPaddressgt")
  • sockfd Socket(AF_ROUTE, SOCK_RAW, 0) / need
    superuser privileges /
  • buf Calloc(1, BUFLEN) / and initialized to 0
    /
  • rtm (struct rt_msghdr ) buf
  • rtm-gtrtm_msglen sizeof(struct rt_msghdr)
    sizeof(struct sockaddr_in)
  • rtm-gtrtm_version RTM_VERSION
  • rtm-gtrtm_type RTM_GET
  • rtm-gtrtm_addrs RTA_DST
  • rtm-gtrtm_pid pid getpid()
  • rtm-gtrtm_seq SEQ
  • sin (struct sockaddr_in ) (rtm 1)
  • sin-gtsin_family AF_INET
  • Inet_pton(AF_INET, argv1,
    sin-gtsin_addr)
  • Write(sockfd, rtm, rtm-gtrtm_msglen)
  • do
  • n Read(sockfd, rtm, BUFLEN)
  • while (rtm-gtrtm_type ! RTM_GET
    rtm-gtrtm_seq ! SEQ

11
Figure 17-6(2)
  • rtm (struct rt_msghdr ) buf
  • sa (struct sockaddr ) (rtm 1)
  • get_rtaddrs(rtm-gtrtm_addrs, sa, rti_info)
  • if ( (sa rti_infoRTAX_DST) ! NULL)
  • printf("dest s\n", Sock_ntop_host(sa,
    sa-gtsa_len))
  • if ( (sa rti_infoRTAX_GATEWAY) ! NULL)
  • printf("gateway s\n", Sock_ntop_host(sa,
    sa-gtsa_len))
  • if ( (sa rti_infoRTAX_NETMASK) ! NULL)
  • printf("netmask s\n", Sock_masktop(sa,
    sa-gtsa_len))
  • if ( (sa rti_infoRTAX_GENMASK) ! NULL)
  • printf("genmask s\n", Sock_masktop(sa,
    sa-gtsa_len))
  • exit(0)

12
rti_info structure filled in by our get_rtaddrs
function
13
Figure 17-9
  • include "unproute.h"
  • define ROUNDUP(a, size) (((a) ((size)-1)) ? (1
    ((a) ((size)-1))) (a))
  • / Step to next socket address structure
  • if sa_len is 0, assume it is sizeof(u_long).
  • /
  • define NEXT_SA(ap) ap (struct sockaddr ) \
  • ((caddr_t) ap (ap-gtsa_len ? ROUNDUP(ap-gtsa_len,
    sizeof (u_long)) \
  • sizeof(u_long)))
  • void get_rtaddrs(int addrs, struct sockaddr sa,
    struct sockaddr rti_info)
  • int i
  • for (i 0 i lt RTAX_MAX i)
  • if (addrs (1 ltlt i))
  • rti_infoi sa
  • NEXT_SA(sa)
  • else rti_infoi NULL

14
sysctl operation
  • Any process can examine both the routing table
    and the interface list.

include ltsys/param.hgt include
ltsys/sysctl.hgt int sysctl(int name, u_int
namelen, void oldp, size_t oldlenp, void newp,
size_t newlen)
returns0 if OK, -1 on error
15
Hierarchical arrangement of sysctl names
16
Sysctl information returned for route domain
17
Information returned for sysctl,CTL_NET,NET_RT_IFL
IST command
One per interface interface name, index, and
hardware address
One per address configured for the interface
18
Figure 17-14
  • include "unproute.h"
  • include ltnetinet/udp.hgt
  • include ltnetinet/ip_var.hgt
  • include ltnetinet/udp_var.hgt / for UDPCTL_xxx
    constants /
  • int main(int argc, char argv)
  • int mib5, val
  • size_t len
  • mib0 CTL_NET
  • mib1 AF_INET
  • mib2 IPPROTO_UDP
  • mib3 UDPCTL_CHECKSUM
  • len sizeof(val)
  • Sysctl(mib, 4, val, len, NULL, 0)
  • printf("udp checksum flag d\n", val)
  • exit(0)

19
interface name and index fuction
  • RFC 2133 defines four function that deal with
    interface names and indexes gtthese are used
    with IPv6 multicasting(chapter 19)
  • each interface has a unique name and a unique
    positive index(0 is never used )

20
include ltnet/if.hgt unsigned int
if_nametoindex(const char ifname)
returnspositive
interface index if OK, 0 on error char
if_indextoname(unsigned int ifindex, char
ifname)
returnspointer to interface name if OK, NULL on
error struct if_nameindex if_nameindex(void)

returnsnonnull pointer if OK, NULL on error void
if_freenameindex(struct if_nameindex ptr)
21
  • if_nameindex return a pointer to an array of
    if_nameindex structure

Struct if_nameindex unsigned int
if_index/1, 2,.../ char if_name/null
terminated name/
Write a Comment
User Comments (0)
About PowerShow.com