2. 16.1 Introduction
• The ioctl function has traditionally been the system interface
used for everything that didn’t fit into some other nicely defined
category.
• Nevertheless numerous ioctls remain for implementation-
dependent features related to network programming: obtaining
the interface information, accessing the routing table, ARP cache
• A common use of ioctl by network program ( typically server ) is
to obtain information on all the host’s interfaces when the
program starts : the interface addresses, whether the interface
supports broadcasting, whether the interface supports
multicasting, and so on.
UNIX Network Programming
2
3. 16.2 ioctl Function
#include <unistd.h>
int ioctl ( int fd, int request, . . . /* void *arg */ );
Returns: 0 if OK, -1 on error
– The third argument is always a pointer, but the type of pointer
depends on the request.
– We can divide the requests related to networking into sex
categories.
• socket operations
• file operations
• interface operations
• ARP cache operations
• routing table operations
• streams system
UNIX Network Programming
3
5. 16.3 Socket Operations
• SIOCATMASK
• return through the integer pointed to by the third argument a nonzero value if
the socket’s read pointer is currently at the out-of-band mark, of a zero value
if the read pointer is not at the out-of-band mark.
• SIOCGPGRP
• return through the integer pointed to by the third argument either the process
ID or the process group ID that is set to receive the SIGIO or SIGURG signal
for this socket.
• SIOCSPGRP
• set either the process ID or the process group ID to receive the SIGIO or
SIGURG signal for this socket from the integer pointed to by third argument.
UNIX Network Programming
5
6. 16.4 File Operations
• FIONBIO
• the nonblocking flag for the socket is cleared or turned on, depending whether
the third argument to ioclt points to a zero or nonzero value, respectively.
• FIOASYNC
• the flag that governs the receipt of asynchronous I/O signals(SIGIO) for the
socket is cleared or turned on, depending whether the third argument to ioctl
points a zero or nonzero value, respectively.
• FIONREAD
• return in the integer pointed to by the third argument to ioctl the number of
bytes currently in the socket receive buffer.
• FIOSETOWN - Equivalent to SIOCSPGRP for a socket
• FIOGETOWN - Equivalent to SIOCGPGRP for a socket
UNIX Network Programming
6
7. 16.5 Interface Configuration
• From the kernel, get all the network interfaces configured on the
system
• In order to do it, use ioctl with SIOCGIFCONF
• Before calling ioctl we allocate a buffer and an ifconf structure
and the initialize the latter.
ifreq{}
ifc_len 1 0 2 4
ifc_req
F ig u re 1 6 .3 In it ia liz a t io n o f i f c o n f s tru c tu re b e fo re SIOCGIFCONF.
UNIX Network Programming
7
8. 16.5 Interface Configuration
ifreq{}
ifc_len 64
ifc_req
ifr_name[]
ifreq{}
socket
ad d ress
stru ctu re
ifr_name[]
ifreq{}
socket
ad d ress
stru ctu re
F i g u r e 1 6 . 4 V a lu e s r e t u rn e d b y SIOCGIFCONF.
UNIX Network Programming
8
9. 16.5 Interface Configuration
struct ifconf {
int ifc_len; /* Size of buffer. */
union {
caddr_t ifcu_buf;
struct ifreq *ifcu_req;
} ifc_ifcu;
};
#define ifc_buf ifc_ifcu.ifcu_buf /* Buffer address. */
#define ifc_req ifc_ifcu.ifcu_req /* Array of structures. */
#define IFNAMSIZ16
struct ifreq {
char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "en0". */
union {
struct sockaddr ifru_addr;
struct sockaddr ifru_dstaddr;
struct sockaddr ifru_broadaddr;
short int ifru_flags;
int ifru_matric;
caddr_t ifru_data;
} ifr_ifru;
};
#define ifr_addr ifr_ifru.ifru_addr /* address */
#define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-p lnk */
#define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */
#define ifr_flags ifr_ifru.ifru_flags /* flags */
#define ifr_metric ifr_ifru.ifru_ivalue /* metric */
#define ifr_data ifr_ifru.ifru_data /* for use by interface */
UNIX Network Programming
9
10. 16.6 get_cli_info function
• Since many programs need to know all the interfaces on a
system, we will develop a function of our own named
get_ifi_info that returns a linked list of structures, one for each
interface that is currently “up”.
UNIX Network Programming
10
11. Figure 16.5 unpifi.h header.
#include "unp.h"
#include <net/if.h>
#define IFI_NAME 16 /* same as IFNAMSIZ in <net/if.h> */
#define IFI_HADDR 8 /* allow for 64-bit EUI-64 in future */
struct ifi_info {
char ifi_name[IFI_NAME]; /* interface name, null terminated */
u_char ifi_haddr[IFI_HADDR]; /* hardware address */
u_short ifi_hlen; /* #bytes in hardware address: 0, 6, 8 */
short ifi_flags; /* IFF_xxx constants from <net/if.h> */
short ifi_myflags; /* our own IFI_xxx flags */
struct sockaddr *ifi_addr; /* primary address */
struct sockaddr *ifi_brdaddr;/* broadcast address */
struct sockaddr *ifi_dstaddr; /* destination address */
struct ifi_info *ifi_next; /* next of these structures */
};
#define IFI_ALIAS 1 /* ifi_addr is an alias */
/* function prototypes */
struct ifi_info *get_ifi_info(int, int);
struct ifi_info *Get_ifi_info(int, int);
void free_ifi_info(struct ifi_info *);
UNIX Network Programming
11
12. Figure 16.6 prifinfo program that calls our get_ifi_info function. (1/3)
#include "unpifi.h"
int
main(int argc, char **argv)
{
struct ifi_info *ifi, *ifihead;
struct sockaddr *sa;
u_char *ptr;
int i, family, doaliases;
if (argc != 3)
err_quit("usage: prifinfo <inet4|inet6> <doaliases>");
if (strcmp(argv[1], "inet4") == 0)
family = AF_INET;
#ifdef IPV6
else if (strcmp(argv[1], "inet6") == 0)
family = AF_INET6;
#endif
else
err_quit("invalid <address-family>");
doaliases = atoi(argv[2]);
UNIX Network Programming
12
13. Figure 16.6 prifinfo program that calls our get_ifi_info function. (2/3)
for (ifihead = ifi = Get_ifi_info(family, doaliases);
ifi != NULL; ifi = ifi->ifi_next) {
printf("%s: <", ifi->ifi_name);
if (ifi->ifi_flags & IFF_UP) printf("UP ");
if (ifi->ifi_flags & IFF_BROADCAST) printf("BCAST ");
if (ifi->ifi_flags & IFF_MULTICAST) printf("MCAST ");
if (ifi->ifi_flags & IFF_LOOPBACK) printf("LOOP ");
if (ifi->ifi_flags & IFF_POINTOPOINT) printf("P2P ");
printf(">n");
UNIX Network Programming
13
14. Figure 16.6 prifinfo program that calls our get_ifi_info function. (3/3)
if ( (i = ifi->ifi_hlen) > 0) {
ptr = ifi->ifi_haddr;
do {
printf("%s%x", (i == ifi->ifi_hlen) ? " " : ":", *ptr++);
} while (--i > 0);
printf("n");
}
if ( (sa = ifi->ifi_addr) != NULL)
printf(" IP addr: %sn",
Sock_ntop_host(sa, sizeof(*sa)));
if ( (sa = ifi->ifi_brdaddr) != NULL)
printf(" broadcast addr: %sn",
Sock_ntop_host(sa, sizeof(*sa)));
if ( (sa = ifi->ifi_dstaddr) != NULL)
printf(" destination addr: %sn",
Sock_ntop_host(sa, sizeof(*sa)));
}
free_ifi_info(ifihead);
exit(0);
}
UNIX Network Programming
14
21. Figure 16.10 free_ifi_info function: free dynamic memory allocated by
get _ifi_info.
void
free_ifi_info(struct ifi_info *ifihead)
{
struct ifi_info *ifi, *ifinext;
for (ifi = ifihead; ifi != NULL; ifi = ifinext) {
if (ifi->ifi_addr != NULL)
free(ifi->ifi_addr);
if (ifi->ifi_brdaddr != NULL)
free(ifi->ifi_brdaddr);
if (ifi->ifi_dstaddr != NULL)
free(ifi->ifi_dstaddr);
ifinext = ifi->ifi_next; /* can't fetch ifi_next after free() */
free(ifi); /* the ifi_info{} itself */
}
}
UNIX Network Programming
21
22. 16.7 Interface Operations
• SIOCGIFADDR
– return the unicast address in the ifr_addr member.
• SIOCSIFADDR
– sets the interface address from the ifr_addr member.
• SIOCGIFFLAGS
– return the interface flags in the ifr_flags member.
• SIOCSIFFLAGS
– sets the interface flags from the ifr_flags member.
• SIOCGIFDSTADDR
– return the point-to-point address in the ifr_dstaddr member.
• SIOCSIFDSTADDR
– set the point-to-point address from ifr_dstaddr member.
UNIX Network Programming
22
23. 16.7 Interface Operations
• SIOCGIFBRDADDR
– return the broadcast address in the ifr_broadaddr emember.
• SIOCSIFBRDADDR
– set the broadcast address from the ifr_boradaddr member.
• SIOCGIFNETMASK
– return the subnet mask in the ifr_addr member.
• SIOCSIFNETMASK
– set the subnet mask from the ifr_addr member.
• SIOCGIFMETRIC
– return the interface metric in the ifr_metric member.
• SIOCSIFMETRIC
– set the interface routing metric from the ifr_metric member.
UNIX Network Programming
23
24. 16.8 ARP Cache Operations
struct arpreq {
struct sockaddr arp_pa; /* protocol addesss */
struct sockaddr arp_ha; /* hardware address */
int arp_flags; /* flags */
};
#define ATF_INUSE 0x01 /* entry in use */
#define ATF_COM 0x02 /* completed entry (hardware addr valid) */
#define ATF_PERM 0x04 /* permanent entry */
#define ATF_PUBL 0x08 /* published entry (respond for other host) */
• SIOCSARP
– add a new entry to the ARP cache or modify an existing
entry.
• SIOCDARP
– delete an entry from the ARP cache.
• SIOCGARP
– get an entry from the ARP cache.
UNIX Network Programming 24
26. 16.9 Routing Table Operations
• Two ioctl requests are provided to operate on the routing table.
• These two requests require that the third argument to ioctl be a
pointer to an rtentry structure,which is defined by including the
<net/route.h> header.
• Only the superuser can issue these requests.
• SIOCADDRT - add an entry to the routing table.
• SIOCDELRT - delete an entry to the routing table.
UNIX Network Programming
26