1 ioctl Operations Computer Network Programming. 2 Use of ioctl() It is the system interface to set and get attributes of devices, sockets, files, interfaces.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Ipv4 Socket Address Structure struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 address */ /* network byte ordered */ }; struct sockaddr_in { uint8_t sin_len;
CSE 333 – SECTION 8 Networking and sockets. Overview Network Sockets IP addresses and IP address structures in C/C++ DNS – Resolving DNS names Demos.
ADVANCED NETWORK PROGRAMMING BİLGİN METİN ADVANCED SOCKET OPTIONS  IPv4&IPv6 Interoperability  IOCTL Options  Advanced I/O Functions  Nonblocking I/O.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
Network Programming UNIX Internet Socket API. Everything in Unix is a File –When Unix programs do any sort of I/O, they do it by reading or writing to.
תקשורת באינטרנט Tutorial 8. 2 n Socket programming u What is socket ? u Sockets architecture u Types of Sockets u The Socket system calls u Data Transfer.
Tutorial 8 Socket Programming
Non-blocking I/O int flags; int fd; /* file descripter */ void main() { fd = open(“myfile.txt”, R_ONLY); if ((flags = fcntl(fd, F_GETFL, 0)) < 0) /* first.
Broadcast and Multicast. Unicast Host 2Host 1 Broadcast Packet received by every host on network (including the sender!)
Ashutosh Shukla Lecture 3 Network Programming CS 640: Computer Networking.
UNIX Socket Programming CS 6378
1 Networking (Stack and Sockets API). 2 Topic Overview Introduction –Protocol Models –Linux Kernel Support TCP/IP Sockets –Usage –Attributes –Example.
Assignment 3 A Client/Server Application: Chatroom.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
Client-Side Network Programming
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
IT1352-NETWORK PROGRAMMING AND MANAGEMENT
Signal-Driven I/O Concepts and steps for using signal-driven I/O
Sirak Kaewjamnong Computer Network Systems
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
The Application Layer Application Services (Telnet, FTP, , WWW) Reliable Stream Transport (TCP) Connectionless Packet Delivery Service (IP) Unreliable.
Chapter 17 routing sockets. abstract Introduction datalink socket address structure reading and writing sysctl operation get_ifi_info function interface.
UNIX Network Programming1 Chapter 16. ioctl Operations.
Network Programming Eddie Aronovich mail:
Chaptere19 multicasting. contents multicast address multicasting versus broadcasting on a LAN multicasting on a WAN multicast socket option mcast_join.
TELE 402 Lecture 10: Unix domain … 1 Overview Last Lecture –Daemon processes and advanced I/O functions This Lecture –Unix domain protocols and non-blocking.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
Advanced Sockets API-II Vinayak Jagtap
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Introduction to Socket
Socket Programming Lab 1 1CS Computer Networks.
Threads and Locking Ioctl operations. Threads Lightweight processes What’s wrong with processes? –fork() is expensive – 10 to 100 times slower –Inter.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
Chapter 2 Applications and Layered Architectures Sockets.
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Sockets Introduction Socket address structures Value-result arguments
1 Socket Options getsockopt and setsockopt functions Check options and obtain default values Generic socket options IPv4 socket options IPv6 socket options.
UNIX Internet Socket API
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
1 ioctl Function ioctl function get_ifi_info function ARP cache operations: eg. Print H/W addresses.
Ioctl Operations. ioctl Function Interface Configuration  Netstat, ifconfig command 에서 사용.
Socket programming in C. Socket programming with TCP Client must contact server server process must first be running server must have created socket (door)
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Network Programming CSC- 341
Socket Programming in C
Review: TCP Client-Server Interaction
Transport layer API: Socket Programming
Client-side Networking CSE 333 Spring 2018
Socket Programming in C
TCP Sockets Programming
Network Programming Chapter 12
Socket Programming.
Elementary UDP Sockets connectionless, unreliable, datagram
Client-side Networking CSE 333 Summer 2018
Internet Networking recitation #8
Sockets.
Presentation transcript:

1 ioctl Operations Computer Network Programming

2 Use of ioctl() It is the system interface to set and get attributes of devices, sockets, files, interfaces tables like routing table, arp table... The same operations can be performed by Use of special functions such as tcgetattr() Use of routing domain sockets (AF_ROUTE family) Use of fcntl function

3 ioctl function() #include int ioctl (int fd, int request, void *arg); arg pointer type depends on the request. Request can be divided into following categories: - Socket operations - File operations - Interface operations - ARP cache operations - Routing table operations

4 Socket Operations –SIOCATMARK: integer Return if the socket’s read pointer is currently on the out-of-band data –SIOCGPGRP: int Return the process ID or the process group ID that is set to receive the SIGIO or SIGURG signal for this socket – SIOCSPGRP: int Set either the process ID or process group ID to receive the SIGIO or SIGURG signal for this socket

5 File Operations –FIONBIO: integer –Clear or turn on the non-blocking flag for the socket. –FIOASYNC: integer –Clear or turn on the receipt of asynchronous I/O signals (SIGIO) for the socket. –FIONREAD: integer –Return the number of bytes currently available in the socket receive buffer. –FIOSETOWN; integer –Equivalent to SIOCSPGRP for a socket –FIOGETOWN: integer –Equivalent to SIOCGPGRP for a socket

6 Interface Configuration –It is possible to obtain information about all the network interfaces configured on the system –IP address, flags, Bcast address, Interface name,.... –It is possible to set the IP address, bcast address, dest address for ptp links, some flags for the interface

7 Interface Operations REQUEST DescriptionDatatype SIOCGIFCONFget list of all interfacesstruct ifconf SIOCSIFADDRset interface addressstruct ifreq SIOCGIFADDRget interface addressstruct ifreq SIOCSIFFLAGSset interface flags struct ifreq SIOCGIFFLAGSget interface flagsstruct ifreq SIOCSIFDSTADDRset point-to-point addressstruct ifreq SIOCGIFDSTADDRget point-to-point addressstruct ifreq SIOCSIFBRADDRset broadcast addressstruct ifreq SIOCGIFBRADDRget broadcast addressstruct ifreq SIOCGIFNETMASKget subnet maskstruct ifreq SIOCSIFNETMASKset subnet maskstruct ifreq SIOCGIFMETRICget interface metricstruct ifreq SIOCSIFMETRICset interface metricstruct ifreq

8 ifconf and ifreq structures structifconf { intifc_len;/* size of associated buffer */ union { caddr_tifcu_buf; structifreq *ifcu_req; } ifc_ifcu; #defineifc_bufifc_ifcu.ifcu_buf/* buffer address */ #defineifc_reqifc_ifcu.ifcu_req/* array of structures returned */ };

9 structifreq { #defineIFNAMSIZ16 charifr_name[IFNAMSIZ];/* if name, e.g. "en0" */ union { structsockaddr ifru_addr; /* size = 16 bytes */ structsockaddr ifru_dstaddr; charifru_oname[IFNAMSIZ];/* other if name */ structsockaddr ifru_broadaddr; intifru_index;/* interface index */ shortifru_flags; intifru_metric; charifru_data[1];/* interface dependent data */ charifru_enaddr[6]; intif_muxid[2];/* mux id's for arp and ip * } ifr_ifru; #defineifr_addrifr_ifru.ifru_addr/* address */ #defineifr_dstaddrifr_ifru.ifru_dstaddr/* other end of p-to-p link */ #defineifr_onameifr_ifru.ifru_oname/* other if name */ #defineifr_broadaddr ifr_ifru.ifru_broadaddr/* broadcast address */ #defineifr_flagsifr_ifru.ifru_flags/* flags */ #defineifr_metricifr_ifru.ifru_metric/* metric */ #defineifr_dataifr_ifru.ifru_data/* for use by interface */ #defineifr_enaddrifr_ifru.ifru_enaddr/* ethernet address */ #defineifr_index ifr_ifru.ifru_index/* interface index */ };

10 Getting Interface list get_ifi_info () function Will return the list of interface information Each list entry is a structure of type struct ifi_info Will call ioctl() with request type SIOCGIFCONF to get the list –This call will return a an array of ifreq structures: each structure will include info about the interface For each returned ifreq structure about an interface, it can get more information by calling ioctl again with a different request type

11 ifi_info structure #defineIFI_NAME16/* same as IFNAMSIZ in */ #defineIFI_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 */ 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 */ }; get_ifi_info() returns the list of the following structure:

12 Use of get_ifi_info Function int main(int argc, char **argv) { struct ifi_info*ifi, *ifihead; struct sockaddr*sa; u_char*ptr; inti, family, doaliases; if (argc != 3) err_quit("usage: prifinfo "); if (strcmp(argv[1], "inet4") == 0) family = AF_INET; #ifdefIPV6 else if (strcmp(argv[1], "inet6") == 0) family = AF_INET6; #endif else err_quit("invalid "); doaliases = atoi(argv[2]);

13 for (ifihead = ifi = Get_ifi_info(family, doaliases); ifi != NULL; ifi = ifi->ifi_next) { printf("%s: 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"); if ( (i = ifi->ifi_hlen) > 0) { ptr = ifi->ifi_haddr; do { printf("%s%x", (i == ifi->ifi_hlen) ? " " : ":", *ptr++); } while (--i > 0); printf("\n"); }

14 if ( (sa = ifi->ifi_addr) != NULL) printf(" IP addr: %s\n", Sock_ntop_host(sa, sizeof(*sa))); if ( (sa = ifi->ifi_brdaddr) != NULL) printf(" broadcast addr: %s\n", Sock_ntop_host(sa, sizeof(*sa))); if ( (sa = ifi->ifi_dstaddr) != NULL) printf(" destination addr: %s\n", Sock_ntop_host(sa, sizeof(*sa))); } free_ifi_info(ifihead); exit(0); }

15 Example Run aspendos{korpe}:> prifinfo inet4 doaliases lo0: IP addr: le0: IP addr: broadcast addr:

16 Implementation of get_ifi_info() Function struct ifi_info * get_ifi_info(int family, int doaliases) { struct ifi_info *ifi, *ifihead, **ifipnext; int sockfd, len, lastlen, flags, myflags; char *ptr, *buf, lastname[IFNAMSIZ], *cptr; struct ifconf ifc; struct ifreq *ifr, ifrcopy; struct sockaddr_in *sinptr; sockfd = Socket(AF_INET, SOCK_DGRAM, 0); lastlen = 0; len = 100 * sizeof(struct ifreq);/* initial buffer size guess */ for ( ; ; ) { buf = Malloc(len); ifc.ifc_len = len; ifc.ifc_buf = buf; if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) { if (errno != EINVAL || lastlen != 0) err_sys("ioctl error"); } else { if (ifc.ifc_len == lastlen) break;/* success, len has not changed */ lastlen = ifc.ifc_len; } len += 10 * sizeof(struct ifreq);/* increment */ free(buf); } /* continued on the next page */

17 ifihead = NULL; ifipnext = &ifihead; lastname[0] = 0; /* end get_ifi_info1 */ /* include get_ifi_info2 */ for (ptr = buf; ptr < buf + ifc.ifc_len; ) { ifr = (struct ifreq *) ptr; len = max(sizeof(struct sockaddr), ifr->ifr_addr.sa_len); switch (ifr->ifr_addr.sa_family) { case AF_INET6: len = sizeof(struct sockaddr_in6); break; case AF_INET: default: len = sizeof(struct sockaddr); break; } ptr += sizeof(ifr->ifr_name) + len;/* for next one in buffer */

18 if (ifr->ifr_addr.sa_family != family) continue;/* ignore if not desired address family */ myflags = 0; if ( (cptr = strchr(ifr->ifr_name, ':')) != NULL) *cptr = 0;/* replace colon will null */ if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0) { if (doaliases == 0) continue;/* already processed this interface */ myflags = IFI_ALIAS; } memcpy(lastname, ifr->ifr_name, IFNAMSIZ); ifrcopy = *ifr; Ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy); flags = ifrcopy.ifr_flags; if ((flags & IFF_UP) == 0) continue;/* ignore if interface not up */ ifi = Calloc(1, sizeof(struct ifi_info)); *ifipnext = ifi;/* prev points to this new one */ ifipnext = &ifi->ifi_next;/* pointer to next one goes here */

19 ifi->ifi_flags = flags;/* IFF_xxx values */ ifi->ifi_myflags = myflags;/* IFI_xxx values */ memcpy(ifi->ifi_name, ifr->ifr_name, IFI_NAME); ifi->ifi_name[IFI_NAME-1] = '\0'; /* end get_ifi_info2 */ /* include get_ifi_info3 */ switch (ifr->ifr_addr.sa_family) { case AF_INET: sinptr = (struct sockaddr_in *) &ifr->ifr_addr; if (ifi->ifi_addr == NULL) { ifi->ifi_addr = Calloc(1, sizeof(struct sockaddr_in)); memcpy(ifi->ifi_addr, sinptr, sizeof(struct sockaddr_in)); #ifdefSIOCGIFBRDADDR if (flags & IFF_BROADCAST) { Ioctl(sockfd, SIOCGIFBRDADDR, &ifrcopy); sinptr = (struct sockaddr_in *) &ifrcopy.ifr_broadaddr; ifi->ifi_brdaddr = Calloc(1, sizeof(struct sockaddr_in)); memcpy(ifi->ifi_brdaddr, sinptr, sizeof(struct sockaddr_in)); } #endif

20 #ifdefSIOCGIFDSTADDR if (flags & IFF_POINTOPOINT) { Ioctl(sockfd, SIOCGIFDSTADDR, &ifrcopy); sinptr = (struct sockaddr_in *) &ifrcopy.ifr_dstaddr; ifi->ifi_dstaddr = Calloc(1, sizeof(struct sockaddr_in)); memcpy(ifi->ifi_dstaddr, sinptr, sizeof(struct sockaddr_in)); } #endif } break; default: break; } free(buf); return(ifihead);/* pointer to first structure in linked list */ } /* end get_ifi_info */

21 ARP Cache Operations REQUEST DescriptionData Type SIOCSARP add a new entry to arp cache struct arpreq SIOCDARP delete an entry from arp cache struct arpreq SIOCGARP get an entry from arp cachestruct arpreq struct arpreq { struct sockaddr arp_pa; struct sockaddr arp_flags; int arp_flags; }

22 Routing Table Operations REQUEST DescriptionData Type SIOCADDRTadd an entry to the routing table struct rtentry SIOCDELRT delete an entry from routing table struct rtentry struct rtentry defined in You have to be “root” in order to add and delete routing table entry.

23 /dev/kmem and sysctl() There is no ioctl request type for getting the ARP and Routing table entry list. The list can be obtained by reading the kernel’s memory by opening /dev/kmem and reading from it. Or you can use the sysctl() function.