Raw Sockets (+ other) UNPv1 - Stevens, Fenner, Rudoff Linux Socket Programming - Walton.

Slides:



Advertisements
Similar presentations
Florida State UniversityCOP Advanced Unix Programming Raw Sockets Datalink Access Chapters 25, 26.
Advertisements

Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Socket Programming Application Programming Interface.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Tutorial 8 Socket Programming
The Basics of communication LectureII. Processing Techniques.
Additional API functions and data-structures A look at how some information from lower-layers of the TCP/IP protocol stack could be accessed.
SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in Developed at UC Berkeley Objective: to transport.
1 Application Presentation Session Transport Network Datalink Physical OSI model Application IPv4, IPv6 Device Driver Hardware TCPUDP Internet.
Cs423-cotter1 Example Client Program Reference Comer & Stevens, Chapter 7.
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
1 Introduction to Raw Sockets 2 IP address Port address MAC address TCP/IP Stack 67 Bootp DHCP OSPF protocol frame type UDP Port # TCP Port.
CS1652 September 13th, 2012 The slides are adapted from the publisher’s material All material copyright J.F Kurose and K.W. Ross, All Rights.
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
IT1352-NETWORK PROGRAMMING AND MANAGEMENT
9/12/2015B.R1 Socket Abstraction and Interprocess Communication B.Ramamurthy CSE421.
LWIP TCP/IP Stack 김백규.
The Application Layer Application Services (Telnet, FTP, , WWW) Reliable Stream Transport (TCP) Connectionless Packet Delivery Service (IP) Unreliable.
Network Programming Eddie Aronovich mail:
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
Advanced Sockets API-II Vinayak Jagtap
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol ,
TELE 402 Lecture 9: Daemon … 1 by Dr Z. Huang Overview Last Lecture –Broadcast and multicast This Lecture –Daemon processes and advanced I/O functions.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
Introduction to Socket
Socket Programming Lab 1 1CS Computer Networks.
TELE 402 Lecture 12: Signal-Driven I/O & Raw Socket 1 Overview Last Lecture –Advanced UDP sockets and threads –Source: Chapters 22&26 of Stevens’ book.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
1 Socket Options getsockopt and setsockopt functions Check options and obtain default values Generic socket options IPv4 socket options IPv6 socket options.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
CMPT 471 Networking II Network Programming © Janice Regan,
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
UDP : User Datagram Protocol 백 일 우
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Packet Switch Network Server client IP Ether IPTCPData.
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
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.
Socket Abstraction and Interprocess Communication
CSCD433 Advanced Networks Winter 2017 Lecture 14
Sockets and Beginning Network Programming
CS 1652 Jack Lange University of Pittsburgh
Socket Programming in C
CSCD433 Advanced Networks Spring 2016 Lecture 16a
Transport layer API: Socket Programming
Socket Abstraction and Interprocess Communication
User Datagram Protocol (UDP)
Socket Abstraction and Interprocess Communication
Socket Abstraction and Interprocess Communication
Socket Abstraction and Interprocess Communication
Socket Abstraction and Interprocess Communication
Advanced I/O Functions
Socket Abstraction and Interprocess Communication
Internet Networking recitation #8
Socket Programming with UDP
Presentation transcript:

Raw Sockets (+ other) UNPv1 - Stevens, Fenner, Rudoff Linux Socket Programming - Walton

cs423 - cotter2 Other – Readv ( ) and writev ( ) –Read or write data into multiple buffers –Connection-oriented. Recvmsg ( ) and sendmsg ( ) –Most general form of send and receive. Supports multiple buffers and flags. –Connectionless

cs423 - cotter3 readv( ) and writev( ) ssize_t writev ( int filedes, const struct iovec *iov, int iovcnt); ssize_t readv ( int filedes, const struct iovec *iov, int iovcnt); –filedes – socket identifier –iov – pointer to an array of iovec structures –iovcnt – number of iovec structures in the array (16 < iovcnt: Linux 1024) –Return value is # of bytes transferred or -1 or error struct iovec { void * iov_base; // starting address of buffer size_t iov_len; // size of buffer };

cs423 - cotter4 writev( ) example: Write a packet that contains multiple separate data elements –2 character opcode –2 character block count –512 character data block char opcode[2] = “03”; char blkcnt[2] = “17”; char data[512] = “This RFC specifies a standard …”; struct iovec iov[3]; iov[0].iov_base = opcode; iov[0].iov_len = 2; iov[1].iov_base = blkcnt; iov[1].iov_len = 2; iov[2].iov_base = data; iov[3].iov_len = 512; writev (sock, &iov, 3);

cs423 - cotter5 writev( ) example: writev (sock, &iov, 3);

cs423 - cotter6 recvmsg ( ) and sendmsg ( ) ssize_t recvmsg (int sock, struct msghdr *msg, int flags); ssize_t sendvmsg (int sock, struct msghdr *msg, int flags); –Sock – Socket identifier –Msg – struct msghdr that includes message to be sent as well as address and msg_flags –Flags – sockets level flags –Return value is # of bytes transferred or -1 or error

cs423 - cotter7 Sockets level flags FlagDescriptionREC V SEN D MSG_DONTR OUTE MSG_DONTW AIT MSG_PEEK MSG_WAITAL L MSG_OOB Bypass routing table lookup Only this operation is nonblocking Peek at incoming message Wait for all the data Send or receive out-of-band data ******** ******

cs423 - cotter8 Struct msghdr struct msghdr { void *msg_name;// protocol address socklen_tmsg_namelen// size of protocol address struct iovec *msg_iov// scatter / gather array intmsg_iovlen//# of elements in msg_iov void*msg_cntrl// cmsghdr struct socklen_tmsg_cntrllen// length of msg_cntrl intmsg_flags// flags returned by recvmsg };

cs423 - cotter9 Msg_flags FlagReturned by recvmsg msg_flags MSG_EOR MSG OOB **** MSG_BCAST MSG_MCAST MSG_TRUNC MSG_CTRUNC MSG_NOTIFICATION **********

cs423 - cotter10 recvmsg example (ICMP) char recvbuf[BUFSIZE]; char controlbuf[BUFSIZE]; struct msghdr msg; struct iovec iov; sockfd = socket (PF_INET, SOCK_RAW, pr->icmpproto); iov.iov_base = recvbuf; iov.iov_len = sizeof(recvbuf); msg.msg_name = sin; //sockaddr struct, for sender’s IP & port msg.msg_iov = &iov; msg.msg_iovlen = 1; msg.msg_control = controlbuf; for ( ; ; ) { msg.msg_namelen =sizeof(sin); msg.msg_controllen = sizeof(controlbuf); n = recvmsg(sockfd, &msg, 0); :

cs423 - cotter11 What are (standard) sockets? 1.? 2.? 3.? Limitations 1.? 2.? 3.?

cs423 - cotter12 What are Raw Sockets? 1.A way to pass information to network protocols other than TCP or UDP (e.g. ICMP and IGMP) 2.A way to implement new IPv4 protocols 3.A way to build our own packets (be careful here)

cs423 - cotter13 Why Would We Use Them? Allows us to access packets sent over protocols other than TCP / UDP Allows us to process IPv4 protocols in user space –Control, speed, troubleshooting Allow us to implement new IPv4 protocols Allows us to control the IP header –Control option fields (beyond setsockopt() ) –Test / control packet fragmentation

cs423 - cotter14 Limitations? Reliability Loss No Ports Nonstandard communication No Automatic ICMP Raw TCP / UDP unlikely Requires root / admin

cs423 - cotter15 OS Involvement in Sockets User Space Kernel Space Socket AppTCP/IP Stack Linux Socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP) Socket ( AF_INET, SOCK_RAW, IPPROTO_ICMP) Socket ( AF_PACKET, SOCK_RAW, htons(ETH_P_IP)) Identify Socket Type Identify Socket Type Identify Socket Type TCP IP Ethernet

cs423 - cotter16 Normal Socket Operation (TCP) Create a socket –s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP) Bind to a port (optional) –Identify local IP and port desired and create data structure –bind (s, (struct sockaddr *) &sin, sizeof(sin)) Establish a connection to server –Identify server IP and port –connect (s, (struct sockaddr *) &sin, sizeof(sin)) Send / Receive data –Place data to be send into buffer –recv (s, buf, strlen(buf), 0);

cs423 - cotter17 Normal Socket Operation (TCP) User Space Kernel Space Socket AppProtocol Linux socket ( ) Create socket TCP, IP, Internet connect( ) Bind to local port: Connect to remote port send( ) TCP, IP, Internet Pass data thru local stack to remote port OK TCP

cs423 - cotter18 Raw Sockets Operation (ICMP) Create a socket –s = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP) Since there is no port, there is no bind * There is no TCP, so no connection * Send / Receive data –Place data to be sent into buffer –sendto (s, buf, strlen(buf), 0, addr, &len); * More later

cs423 - cotter19 Raw Sockets Operation (ICMP) User Space Kernel Space Socket AppProtocol Linux socket ( ) Create socket sendto( ) IP, Internet Pass data thru local stack to remote host OK ICMP

cs423 - cotter20 Create a Raw Socket s = socket (AF_INET, SOCK_RAW, protocol) –IPPROTO_ICMP, IPPROTO_IP, etc. Can create our own IP header if we wish –const int on = 1; –setsockopt (s, IPPROTO_IP, IP_HDRINCL, &on, sizeof (on)); Can “bind” –Since we have no port, the only effect is to associate a local IP address with the raw socket. (useful if there are multiple local IP addrs and we want to use only 1). Can “connect” –Again, since we have no TCP, we have no connection. The only effect is to associate a remote IP address with this socket.

cs423 - cotter21 Raw Socket Output Normal output performed using sendto or sendmsg. –Write or send can be used if the socket has been connected If IP_HDRINCL not set, starting addr of the data (buf) specifies the first byte following the IP header that the kernel will build. –Size only includes the data above the IP header. If IP_HDRINCL is set, the starting addr of the data identifies the first byte of the IP header. –Size includes the IP header –Set IP id field to 0 (tells kernel to set this field) –Kernel will calculate IP checksum Kernel can fragment raw packets exceeding outgoing MTU

cs423 - cotter22 Raw Socket Input Received TCP / UDP NEVER passed to a raw socket. Most ICMP packets are passed to a raw socket –(Some exceptions for Berkeley-derived implementations) All IGMP packets are passed to a raw socket All IP datagrams with a protocol field that the kernel does not understand (process) are passed to a raw socket. If packet has been fragmented, packet is reassembled before being passed to raw socket

cs423 - cotter23 Conditions that include / exclude passing to specific raw sockets If a nonzero protocol is specified when raw socket is created, datagram protocol must match If raw socket is bound to a specific local IP, then destination IP must match If raw socket is “connected” to a foreign IP address, then the source IP address must match

cs423 - cotter24 Ping – Overview This example modified from code by Walton (Ch 18) Very simple program that uses ICMP to send a ping to another machine over the Internet. Provides the option to send a defined number of packets (or will send a default 25). We will build an ICMP packet (with a proper header, including checksum) that will be updated each time we send a new packet. We will display the raw packet that is received back from our destination host and will interpret some of the data. –(Output format is different from standard ping)

cs423 - cotter25 ICMP Packet header struct icmphdr { u_int8_t type // ICMP message type (0) u_int8_t code // ICMP type sub-code (0) u_int16_t checksumE306, etc. u_int16_t id// echo datagram id (use pid) u_int16_t sequence// echo seq # 1, 2, 3, etc. }; Packet body: : ; ? … B

cs423 - cotter26 myNuPing.c (overview) Global Declarations –Struct packet { }, some variables unsigned short checksum (void *b, int len) –Calculate checksum for ICMP packet (header and data) void display (void *buf, int bytes) –Format a received packet for display. void listener (void) –Separate process to capture responses to pings void ping (struct sockaddr_in *addr) –Create socket and send out pings 1/sec to specified IP addr int main (int count, shar *strings[ ]) –Test for valid instantiation, create addr structure –Fork a separate process (listener) and use existing process for ping

cs423 - cotter27 #defines and checksum calc #define PACKETSIZE 64 struct packet { struct icmphdr hdr; char msg[PACKETSIZE-sizeof(struct icmphdr)]; }; int pid=-1; int loops = 25; struct protoent *proto=NULL; unsigned short checksum(void *b, int len) { unsigned short *buf = b; unsigned int sum=0; unsigned short result; for ( sum = 0; len > 1; len -= 2 ) sum += *buf++; if ( len == 1 ) sum += *(unsigned char*)buf; sum = (sum >> 16) + (sum & 0xFFFF); sum += (sum >> 16); result = ~sum; return result; }

cs423 - cotter28 display - present echo info void display(void *buf, int bytes) { int i; struct iphdr *ip = buf; struct icmphdr *icmp = buf+ip->ihl*4; printf(" \n"); for ( i = 0; i < bytes; i++ ) { if ( !(i & 15) ) printf("\n%04X: ", i); printf("%02X ", ((unsigned char*)buf)[i]); } printf("\n"); printf("IPv%d: hdr-size=%d pkt-size=%d protocol=%d TTL=%d src=%s ", ip->version, ip->ihl*4, ntohs(ip->tot_len), ip->protocol, ip->ttl, inet_ntoa(ip->saddr)); printf("dst=%s\n", inet_ntoa(ip->daddr)); if ( icmp->un.echo.id == pid ) { printf("ICMP: type[%d/%d] checksum[%d] id[%d] seq[%d]\n", icmp->type, icmp->code, ntohs(icmp->checksum), icmp->un.echo.id, icmp->un.echo.sequence); }

cs423 - cotter29 Listener - separate process to listen for and collect messages- void listener(void) { int sd, i; struct sockaddr_in addr; unsigned char buf[1024]; sd = socket(PF_INET, SOCK_RAW, proto->p_proto); if ( sd < 0 ) { perror("socket"); exit(0); } for (i = 0; i < loops; i++) { int bytes, len=sizeof(addr); bzero(buf, sizeof(buf)); bytes = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr *) &addr, &len); if ( bytes > 0 ) display(buf, bytes); else perror("recvfrom"); } exit(0); }

cs423 - cotter30 ping - Create message and send it void ping(struct sockaddr_in *addr) { const int val=255; int i, j, sd, cnt=1; struct packet pckt; struct sockaddr_in r_addr; sd = socket(PF_INET, SOCK_RAW, proto->p_proto); if ( sd < 0 ) { perror("socket"); return; } if ( setsockopt(sd, SOL_IP, IP_TTL, &val, sizeof(val)) != 0) perror("Set TTL option"); if ( fcntl(sd, F_SETFL, O_NONBLOCK) != 0 ) perror("Request nonblocking I/O");

cs423 - cotter31 ping (cont) for (j = 0; j < loops; j++) { // send pings 1 per second int len=sizeof(r_addr); printf("Msg #%d\n", cnt); if ( recvfrom(sd, &pckt, sizeof(pckt), 0, (struct sockaddr *)&r_addr, &len) > 0 ) printf("***Got message!***\n"); bzero(&pckt, sizeof(pckt)); pckt.hdr.type = ICMP_ECHO; pckt.hdr.un.echo.id = pid; for ( i = 0; i < sizeof(pckt.msg)-1; i++ ) pckt.msg[i] = i+'0'; pckt.msg[i] = 0; pckt.hdr.un.echo.sequence = cnt++; pckt.hdr.checksum = checksum(&pckt, sizeof(pckt)); if (sendto(sd, &pckt, sizeof(pckt), 0, (struct sockaddr *) addr, sizeof(*addr)) <= 0) perror("sendto"); sleep(1); }

cs423 - cotter32 myNuPing.c – main() int main(int count, char *argv[]) { struct hostent *hname; struct sockaddr_in addr; loops = 0; if ( count != 3 ) { printf("usage: %s \n", argv[0]); exit(0); } if (count == 3) // WE HAVE SPECIFIED A MESSAGE COUNT loops = atoi(argv[2]); if ( count > 1 ) { pid = getpid(); proto = getprotobyname("ICMP"); hname = gethostbyname(argv[1]); bzero(&addr, sizeof(addr)); addr.sin_family = hname->h_addrtype; addr.sin_port = 0; addr.sin_addr.s_addr = *(long*)hname->h_addr; if ( fork() == 0 ) listener(); else ping(&addr); wait(0); } else printf("usage: myping \n"); return 0; }

cs423 - cotter33 “Ping” Output [root]#./myNuPing Msg # : CC F BE : E4 06 DF : A 3B 3C 3D 3E 3F : A 4B 4C 4D 4E 4F : A 5B 5C 5D 5E 5F : IPv4: hdr-size=20 pkt-size=84 protocol=1 TTL=128 src= dst= ICMP: type[0/0] checksum[58374] id[2015] seq[1] Msg #2 ***Got message!*** : CC F BD : E3 06 DF : A 3B 3C 3D 3E 3F : A 4B 4C 4D 4E 4F : A 5B 5C 5D 5E 5F : IPv4: hdr-size=20 pkt-size=84 protocol=1 TTL=128 src= dst= ICMP: type[0/0] checksum[58118] id[2015] seq[2] [root]#

cs423 - cotter34 Summary Raw Sockets allow access to Protocols other than the standard TCP and UDP Performance and capabilities may be OS dependent. –Some OSs block the ability to send packets that originate from raw sockets (although reception may be permitted). Raw sockets remove the burden of the complex TCP/IP protocol stack, but they also remove the safeguards and support that those protocols provide