CSCD433 Advanced Networks Spring 2016 Lecture 16a

Slides:



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

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.
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
TDC561 Network Programming Camelia Zlatea, PhD Week 2 – part II: Socket Application Programming Interface.
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
UDP: User Datagram Protocol. UDP: User Datagram Protocol [RFC 768] r “bare bones”, “best effort” transport protocol r connectionless: m no handshaking.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
ISP – 9 th Recitation Socket programming – Client side.
Protocol Programs that communicate across a network must agree on a protocol on how they will communicate High-level decisions must be made on which program.
1 Application Presentation Session Transport Network Datalink Physical OSI model Application IPv4, IPv6 Device Driver Hardware TCPUDP Internet.
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.
Raw Sockets.
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.
CSCD433 Advanced Networks Fall 2011 Raw vs. Cooked Sockets.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
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.
CSTP FS01CS423 (cotter)1 Protocols 2 References: RFC’s 791, 793, 768, 826.
The Application Layer Application Services (Telnet, FTP, , WWW) Reliable Stream Transport (TCP) Connectionless Packet Delivery Service (IP) Unreliable.
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.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol ,
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
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.
Introduction A Simple Daytime Client A Simple Daytime Server
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.
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.
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
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.
1 School of Computing Science Simon Fraser University CMPT 471: Computer Networking II Introduction Instructor: Dr. Mohamed Hefeeda.
CSCD433 Advanced Networks Winter 2017 Lecture 14
Socket Programming Client/Server.
Sockets and Beginning Network Programming
Read: Chapters 1,2, 3, 4 Communications Client Server Ex: TCP/IP
CS 1652 Jack Lange University of Pittsburgh
Elementary UDP Sockets
Socket Programming (Cont.)
Network Programming CSC- 341
Socket Programming in C
Transport layer API: Socket Programming
Network Programming CSC- 341
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
TCP/IP Socket Programming in C
Elementary UDP Sockets connectionless, unreliable, datagram
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 Spring 2016 Lecture 16a Raw vs. Cooked Sockets C Language

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

Normal Socket Operation (TCP) Create a socket s = socket (AF_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 sent into buffer recv (s, buf, strlen(buf), 0); - Send the buffer to the client send(s, buf, strlen(buf), 0); 3

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

#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 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 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 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 { 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: if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) { .. } Also: IPPROTO_TCP, IPPROTO_UDP and IPPROTO_IP and others

Raw Sockets Operation (ICMP) 05/10/16 Raw Sockets Operation (ICMP) Example Create a socket s = socket (AF_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, use sendto sendto (s, buf, strlen(buf), 0, addr, &len); cs423 - cotter 11 cs423 - cotter 11

Raw Sockets Operation (ICMP) We can start crafting ICMP packets now. But, this way, header data will be constructed by kernel IP code, What if we want to tell kernel that our packet includes the header data already. To do this, we set IP_HDRINCL option for our socket via setsockopt(2) system call: const int on = 1; if (setsockopt(sd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on) < 0) { ... }

Raw Sockets Operation (ICMP) After creating your socket, and building your datagram, you can inject it via sendto(2) and sendmsg(2) system calls. A raw socket is datagram oriented, each send call requires a destination address.

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 cs423 - cotter 14

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 the 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 Size includes the IP header Set IP id field to 0 (tells kernel to set this field) Kernel will calculate IP checksum cs423 - cotter 15 cs423 - cotter 15

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 cs423 - cotter 16 cs423 - cotter 16

Byte Order Issues Packets travelling in network are all big-endian, meaning the most significant bit of the octet is transferred first If host system uses different byte-ordering scheme e.g. i386 architecture is little-endian 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 computer the packet checksum if you choose to construct headers yourself Ready made routines to cut and paste into your code Checksums are computed for IP, ICMP, UDP and TCP packets IP header checksum protects only IPv4 header, while the TCP, DCCP, ICMP, IGMP, and UDP checksums provide end-to-end error detection for both transport header and transport payload data

ICMP Header Fields ICMP packet structure shows checksum Checksum here

Look at Some 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

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