CSCD433 Advanced Networks Winter 2017 Lecture 14

Slides:



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

Sockets: Network IPC Internet Socket UNIX Domain Socket.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
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.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
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.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Sockets Programming Introduction © Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
Tutorial 8 Socket Programming
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
UNIX Socket Programming CS 6378
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
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.
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.
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.
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
IT1352-NETWORK PROGRAMMING AND MANAGEMENT
UNIX Network Programming1 UNIX Network Programming 2nd Edition.
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.
Ports Port - A 16-bit number that identifies the application process that receives an incoming message. Reserved ports or well-known ports (0 to 1023)
Remote Shell CS230 Project #4 Assigned : Due date :
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
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.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
Introduction A Simple Daytime Client A Simple Daytime Server
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.
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
Introduction to Sockets
UNIX Internet Socket API
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
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.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
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 Programming Client/Server.
Assignment 3 A Client/Server Application: Chatroom
UNIX Sockets COS 461 Precept 1.
CS 1652 Jack Lange University of Pittsburgh
Elementary UDP Sockets
Network Programming CSC- 341
Socket Programming in C
Network Programming with Sockets
CSCD433 Advanced Networks Spring 2016 Lecture 16a
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Tutorial on Socket Programming
Transport layer API: Socket Programming
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
Socket Programming in C
TCP/IP Socket Programming in C
CSCD433 Advanced Networks Winter 2019 Lecture 14
Socket Programming(1/2)
Internet Networking recitation #8
Sockets.
Today’s topic: Basic TCP API
Presentation transcript:

CSCD433 Advanced Networks Winter 2017 Lecture 14 Raw vs. Cooked Sockets C Language

Introduction Raw Sockets in C Review of Sockets in C Look at how to write Raw Sockets in C Contrast with Java

Sockets in C Normal socket operation in C is more detailed More steps to implement Client/Server connection Does not take care of details like address translation Programmers need to know more about network connections

Normal Socket Operations Look at TCP Client/Server example in C Then, look at a Raw Socket implementation in C For fun, see a Java Raw Socket example

Using Regular C Sockets Steps to create a socket in C for Server vs. Client Server operations Create a Socket Fill in the Socket Address structure Set the Port Number / IP Address Name a Socket Create a Socket Queue Accepte Connections Do something with the Connection – read or write data Close a Socket Client operations Requeste Connections Use data from Server

Creating a Socket in C socket() system call Socket system call returns a descriptor similar to low-level file descriptor Socket created is one end point of a communication channel #include <sys/types.h> #include <sys/socket.h> int socket (int domain, int type, int protocol);

Fill in Address For AF_INET socket (Ipv4 socket) struct sockaddr_in { short int sin_family; /* AF_INET */ unsigned short int sin_port; /* Port number */ struct in_addr sin_addr; /* IP address */ }; struct in_addr { unsigned long int s_addr; };

Name a Socket AF_INET sockets are associated with an IP/ port number bind() system call assigns address specified in parameter, address, to unnamed socket and a port number #include <sys/socket.h> int bind(int socket, const struct sockaddr *address, size_t address_len);

Create a Socket Queue A server program must create a queue to store pending requests listen() system call. Allows incoming connections to be pending while server is busy dealing with previous client Return values same as for bind #include <sys/socket.h> int listen ( int socket, int backlog );

Accept Connections Wait for connections to socket by using accept() system call Creates a new socket to communicate with the client and returns its descriptor #include <sys/socket.h> int accept(int socket, struct sockaddr *address, size_t *address_len);

Sending or Writing to Socket sendmsg (fd, msgstruct, flags); fd is valid file descriptor from socket call msgstruct which is used to pass information to kernel like destination address and the buffer the flags can be passed as 0 write (fd, data, length); write is the “normal” write function; can be used with both files and sockets

Sending or Writing to Socket send (fd, data, length, flags); sendto (fd, data, length, flags, destaddress, addresslen); send() is used for TCP SOCK_STREAM connected sockets, and sendto() is used for UDP SOCK_DGRAM unconnected datagram sockets or RAW sockets With unconnected sockets, you must specify destination of a packet each time you send one

Receiving or Reading From Socket recv (int s, void *buf, size_t len, int flags); recvfrom (int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen); recv() is for TCP SOCK_STREAM sockets recvfrom() is for UDP SOCK_DGRAM or RAW sockets Both functions take socket descriptor s, a pointer to buffer buf, size (in bytes) of buffer, len, and a set of flags that control how the functions work recvfrom() takes a struct sockaddr*, from that will tell you where the data came from, and will fill in fromlen with the size of struct sockaddr

Receiving or Reading from Socket recvmsg (int sockfd, struct msghdr *msg, int flags); read (int fd, void *buf, size_t count); recvmsg() offers some additional features that recv() does not ancillary data fields in the msghdr read () attempts to read up to count bytes from file descriptor fd into the buffer starting at buf File descriptor can also be a socket descriptor

Client-Server Request Flow

TCP Date and Time Server

TCP Date and Time Server Server starts Waits for a client to contact it Client connects – TCP C socket Server gets the Current Date and Tim Writes it to the Client Client displays it and closes connection

#include <sys/socket.h> #include <netinet/in.h> ... int main (int argc, char *argv[]) { int listenfd = 0, connfd = 0; struct sockaddr_in serv_addr; // Will hold Server address and port number char sendBuff[1025]; time_t ticks; listenfd = socket (AF_INET, SOCK_STREAM, 0); // Create a TCP socket memset (&serv_addr, '0', sizeof(serv_addr)); memset (sendBuff, '0', sizeof(sendBuff)); serv_addr.sin_family = AF_INET; // Set the Socket type serv_addr.sin_addr.s_addr = htonl (INADDR_ANY); // Set server address serv_addr.sin_port = htons(5000); // Set server port Server Side: Date and Time Server

ticks = time(NULL); // Read the time and date Server Side: Date and Time Server bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); // Bind & Listen on port listen(listenfd, 10); // while(1) { connfd = accept(listenfd, (struct sockaddr*) // Accept connection from client NULL, NULL); ticks = time(NULL); // Read the time and date snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks)); //Put in buffer write(connfd, sendBuff, strlen(sendBuff)); // Write to socket close(connfd); // Close the connection sleep(1); }

include <sys/socket.h> #include <sys/types.h> … Client: Date and Time include <sys/socket.h> #include <sys/types.h> … int main(int argc, char *argv[]) { int sockfd = 0, n = 0; char recvBuff[1024]; struct sockaddr_in serv_addr; if (argc != 2) // Give it IP address in command line printf("\n Usage: %s <ip of server> \n",argv[0]); return 1; } memset(recvBuff, '0',sizeof(recvBuff)); // Set up Receive buffer if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) // Open TCP Socket printf("\n Error : Could not create socket \n"); memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET; // Set socket family type serv_addr.sin_port = htons(5000); // Set port number

if (inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0) { // Convert into network address printf("\n inet_pton error occured\n"); return 1; } if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) //Connect to socket at server addr { printf("\n Error : Connect Failed \n"); while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0) // Read from the socket into buffer recvBuff[n] = 0; if(fputs(recvBuff, stdout) == EOF) // Write the buffer to the screen printf("\n Error : Fputs error\n"); if(n < 0) printf("\n Read error \n"); return 0; Client: Date and Time

Demo

Defining a Raw Socket in C Just like normal sockets, we create raw sockets with the socket(2) system call: int socket(int domain, int type, int protocol) However, type and protocol parameters are set to SOCK_RAW and protocol name accordingly, this example is for ICMP: if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) { .. } Also: IPPROTO_TCP, IPPROTO_UDP and IPPROTO_IP and others

Raw Sockets Two ways to define a raw socket 1. Let kernel fill in the IP header and we just create transport protocol header or 2. We are responsible for the whole packet and create both the IP header and underlying protocol headers

Creating Raw Sockets int sockfd; sockfd = socket(AF_INET, SOCK_RAW, protocol); 1. This creates a raw socket and lets the kernel fill in the ip header, we will fill in the protocol header for ICMP or IGMP or TCP or UDP - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - – - - - - - - const int on = 1; setsockopt (sockfd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on); 2. Setting option, IP_HDRINCL allows us to create our own IP header and not let the kernel do it for us IPPROTO_ICMP IPPROTO_IGMP

Raw Sockets Operation (ICMP) User Space Kernel Space Socket App Protocol Linux Create socket ICMP socket ( ) OK Pass data thru local stack to remote host IP, Internet sendto( ) OK 26

Raw Socket Output Normal output performed using sendto or sendmsg 05/10/16 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 data (buf) specifies first byte following IP header that kernel builds If IP_HDRINCL is set, starting addr of data identifies first byte of IP header, that we build 27 cs423 - cotter 27

Raw Socket Input Most ICMP packets are passed to raw socket 05/10/16 Raw Socket Input Most ICMP packets are passed to raw socket Some exceptions for Berkeley-derived implementations All IGMP packets are passed to a raw socket All IP datagrams with protocol field that 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 28 cs423 - cotter 28

Byte Order Issues Packets containing integers traveling in network are all big-endian, meaning most significant bit of the octet is transferred first Includes port numbers and ip addresses If host system uses different byte-ordering scheme e.g. i386 architecture is little-endian Includes data in address fields Data must be converted to network byte order or vice versa On receive path, and if host byte ordering scheme is different from network byte order, Data must be converted from big-endian to little-endian On send path, reverse operation Little-endian to big-endian

Byte Ordering Functions htons(3) and htonl(3) convert 16 bit and 32 bit quantities from host byte order into the network byte order respectively Similarly, ntohs(3) and ntohl(3) macros convert 16- bit and 32-bit quantities from network byte order into the host byte order. On machines which have a byte order same as the network order, routines are defined as null macros

Compute Internet Checksum You will also have to compute the packet checksum if you choose to construct headers yourself including ICMP Ready made routines to cut and paste into your code Checksums are normally computed for IP, ICMP, UDP and TCP packets

ICMP Header Fields ICMP packet structure shows checksum Checksum here

Raw ICMP Packet Example We will look at a simpler version of an ICMP program using Raw Sockets the Ping program !!!

Ping.c Example static int in_cksum(unsigned short *buf, int sz) { The checksum is calculated by forming the ones' complement of the ones' complement sum of the header's 16-bit words } …... static void ping(const char *host) { struct hostent *h; // structure stores, host name, address, any aliases for host struct sockaddr_in pingaddr; // create the socket address for ping target struct icmp *pkt; // ICMP Header int pingsock, c; char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; // max length if ((pingsock = socket (AF_INET, SOCK_RAW, 1)) < 0) { // Create socket, 1 == ICMP/ perror("ping: creating a raw socket"); exit(1); }

Ping.c pingaddr.sin_family = AF_INET; // Set domain if (!(h = gethostbyname(host))) { // get IP address fprintf(stderr, "ping: unknown host %s\n", host); exit(1); } memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr)); //Set up host addr hostname = h->h_name;

ping.c Build the icmp packet + checksum, send it pkt = (struct icmp *) packet; memset(pkt, 0, sizeof(packet)); pkt->icmp_type = ICMP_ECHO; //Set ICMP type pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet)); c = sendto(pingsock, packet, sizeof(packet), 0, (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));

ping.c /* listen for replies */ while (1) { struct sockaddr_in from; size_t fromlen = sizeof(from); if ((c = recvfrom(pingsock, packet, sizeof(packet), 0, (struct sockaddr *) &from, &fromlen)) < 0) { if (errno == EINTR) continue; perror("ping: recvfrom"); }

ping.c if (c >= 76) { /* ip + icmp */ struct iphdr *iphdr = (struct iphdr *) packet; pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */ if (pkt->icmp_type == ICMP_ECHOREPLY) break; } printf("%s is alive!\n", hostname); return;

ping.c int main () { ping ("www.yahoo.com"); }

Look at Some More Examples This URL provides good examples for each type of common network traffic and how to spoof an IP address: http://www.enderunix.org/docs/en/rawipspoof/

import jpcap.JpcapHandler; import jpcap.Jpcap; import jpcap.Packet; Contrast with Java import jpcap.JpcapHandler; import jpcap.Jpcap; import jpcap.Packet; public class JpcapTip implements JpcapHandler { public void handlePacket(Packet packet){ System.out.println(packet); } public static void main(String[] args) throws java.io.IOException{ String[] devices = Jpcap.getDeviceList(); for (int i = 0; i < devices.length; i++) { System.out.println(devices[i]); String deviceName = devices[0]; Jpcap jpcap = Jpcap.openDevice(deviceName, 1028, false, 1); jpcap.loopPacket(-1, new JpcapTip());

JPCAP Example The output of executing the test class looks like this It's shortened for space: ARP REQUEST 00:06:5b:01:b2:4d(192.168.15.79) 00:00:00:00:00:00(192.168.15.34)‏ 1052251329:525479 192.168.15.103->255.255.255.255 protocol(17) priority(0) hop(offset(0) ident(59244) UDP 1211 1211

References Raw Socket Sources Opensource Guide to Raw Sockets – low level http://opensourceforu.com/2015/03/a-guide-to-using-raw- sockets/ Micro HowTo Raw Sockets http://www.microhowto.info/howto/send_an_arbitrary_ip v4_datagram_using_a_raw_socket_in_c.html Spoofing Raw Sockets Interface http://www.enderunix.org/docs/en/rawipspoof/ Regular Sockets Beej;s Guide to Network Programming http://beej.us/guide/bgnet/output/html/multipage /index.html

Summary Raw Sockets in C more direct in native code You have to do more work in defining headers yourself for injecting packets But, you have more control Gives power to you, the programmer!!!

New Assignment Assignment 4, Packet sniffer