Unix Network Programming Part 2: Elementary Sockets 8.5.2005 Jani Peusaari.

Slides:



Advertisements
Similar presentations
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;
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()
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
CSCE 515: Computer Network Programming Socket Wenyuan Xu Department of Computer Science and Engineering.
Sockets Programming Introduction © Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
Tutorial 8 Socket Programming
CS 311 – Lecture 19 Outline Internet Sockets – gethostname utility – struct hostent – inet_addr – Machine byte to Network byte order translation and vice.
#include DatatypeDescription int8_t uint8_t int16_t uint16_t int32_t uint32_t Signed 8-bit integer Unsigned 8-bit integer Signed 16-bit integer Unsigned.
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
Introduction to Project 1 Web Client and Server Jan 2006.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE453 – Introduction to Computer Networks Lecture 15 – Transport Layer (II)
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Operating Systems Chapter 9 Distributed Communication.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
Client-Side Network Programming
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.
Introduction to Socket Programming Advisor: Quincy Wu Speaker: Kuan-Ta Lu Date: Nov. 25, 2010.
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.
1 CMPT 471 Networking II Transport Layer Network Programming © Janice Regan, 2013.
Technical Details for sockaddr_in Structure Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
UNIT8: SOCKETS Topics Introduction to sockets Socket Addresses
Lecture 15 Overview.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Introduction to Socket
Socket Programming Lab 1 1CS Computer Networks.
1 Sockets Programming Socket to me!. 2 Network Application Programming Interface (API) The services provided by the operating system that provide the.
1 CS716 Advanced Computer Networks By A. Wahid Shaikh.
Socket address structures Byte ordering IP address conversion
Sockets Introduction Socket address structures Value-result arguments
Introduction to Sockets
Network Programming Berkeley Sockets (BSD)
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.
1 Network Programming. 2 Background Important guidelines –Use conductor.tamucc.edu or any LINUX machine to develop your network applications –Do not use.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Advanced UNIX programming Fall 2002 Instructor: Ashok Srinivasan Lecture 17 Acknowledgements: The syllabus and power point presentations are modified versions.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
CSE 333 – SECTION 8 Client-Side Network Programming.
Onward with Chat! Networking CS 3470, Section 1.
Network Programming CSC- 341
Socket Programming (Cont.)
Network Programming CSC- 341
Chapter4 Elementary TCP Socket
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Transport layer API: Socket Programming
Chapter 3 Sockets Introduction
Advanced Network Programming spring 2007
Lecture 2 Socket Programming
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Sockets.
Today’s topic: Basic TCP API
Presentation transcript:

Unix Network Programming Part 2: Elementary Sockets Jani Peusaari

Contents Socket address structures Byte Ordering & Manipulation Streams Necessary information on how to create a socket and how they work. Protocol dependency issues and introduction to performance.

Notes In the book: – {} means a structure, e.g. in_addr{} – Solid boxes demonstrate system functions – Boxes with dashed lines demonstate authors own functions ’const’ pointers as function arguments mean their contents are not changed in the function

Socket address structures Socket functions work through a reference to a protocol dependent address structure (sockaddr_ + protocol suffix) Structures are used to pass information between the process and the kernel Structures reside only on a given host, and are not passed in actual communication

struct in_addr { in_addr_ts_addr; }; struct sockaddr_in { uint8_tsin_len; sa_family_tsin_family; // AF_INET in_port_tsin_port; struct in_addrsin_addr; char sin_zero };

Structure differences POSIX only requires sin_family, sin_addr and sin_port A POSIX compliant implementation may have additional fields Almost all have implementations have a sin_zero field so that structures are at least 16 bytes (128 bits) long

Types POSIX defines different variable types that are used to define structures, to make architecture independent representations Integer format: int _t e.g. uint8_t, int16_t For backward compatibility reasons, some structure members can be other structures, that are again typedef’d as e.g. uint32_t Also u_char, u_long for backward reasons

Different Protocol Support All socket functions must be usable with different protocol families Socket address structures are of different size Solution: Generic socket address structure All functions require that structures are cast into a pointer to this generic structure, e.g.: bind(sockfd, (struct sockaddr *)serv, sizeof(serv)) Kernel checks protocol family

From IPv4 to IPv6 SIN6_LEN constant defined if sin6_len field present in socket address structure Family is AF_INET6 (In IPv4 AF_INET) Structure designed to be 64-bit aligned New generic socket address structure defined: struct sockaddr_storage

struct in6_addr { uint8_ts6_addr[16]; }; struct sockaddr_in { uint8_tsin6_len; sa_family_tsin6_family; // AF_INET6 in_port_tsin6_port; uint32_tsin6_flowinfo; struct in6_addrsin6_addr; uint32_tsin6_scope_id; };

New Generic Socket Address Structure The old one only 16 bytes long New one is defined to be as long as the largest possible structure in the system Support for alignment requirements If things were designed now, all would be cast as (void *), length field would be a requirement...

Value-Result Arguments Socket Address Structures are commonly passed as a reference Some arguments used in two ways E.g. int accept(int s, struct sockaddr *addr, socklen_t *addrlen); *addrlen is a value when called, result when function returns (especially for variable length socket address structures)

Byte Ordering Systems use different byte orders, known as little-endian and big-endian Network byte order, host byte order Network byte order is big-endian POSIX requires that certain fields in socket address structures must be maintained in network byte order User is responsible for this

Byte ordering functions Host to Network functions uint16_t htons(uint16_t host16bitvalue); uint32_t htonl(uint32_t host32bitvalue); Network to Host functions uint16_t ntohs(uint16_t net16bitvalue); uint32_t ntohl(uint32_t net32bitvalue);

Address/ASCII conversion Functions that convert from/to ASCII strings to/from network byte ordered binary values E.g. ” ” to 32-bit network byte ordered binary value ASCII / Network (inet_aton, inet_ntoa) Presentation / Numeric (inet_pton, inet_ntop)

#include int inet_aton(const char *strptr, struct in_addr *addrptr); in_addr_t inet_addr(const char *strptr); // DONT USE char *inet_ntoa(struct in_addr inaddr);

#include int inet_pton(int family, const char *strptr, void *addrptr); const char *inet_ntop(int family, const void *addrptr, char *strptr, size_t len);

Problems Older functions only work with IPv4, and have some special qualities E.g. what inet_addr returns on error Newer functions require that we know the address family – Family as argument – Structure member names

Streams File reading functions, e.g. read/write are also used with stream sockets Unlike with files, these functions may return less bytes read/written than requested, but it is not an error The book demonstrates functions, which correctly handle stream sockets

Solutions presented The book shows sock_* functions, an approach for family independent code Instead of calling e.g. inet_ntop directly, call a matching sock_ntop function, which looks inside the socket address structure for family, and call inet_ntop accordingly Approach that simplifies portability between families

Supporting functions Functions that operate on multibyte data, without interpreting the data, without assuming null terminates the data 4.2BSD (b*) and ANSI C (mem*) functions Needed for zeroying socket address structures, copying structures / structure members

#include void bzero(void *dest, size_t nbytes); void bcopy(const void *src, void *dest, size_t nbytes); int bcmp(const void *ptr1, const void *ptr2, size_t nbytes); #include void *memset(void *dest, int c, size_t len); void *memcpy(void *dest, const void *src, size_t nbytes); int memcmp(const void *ptr1, const void *ptr2, size_t nbytes);

Portable code Compile time tests to check socket address structures Always check socket address structure size (e.g. sizeof(sockaddr_in)) Use newer inet_* functions that support both IPv4 and IPv6 addresses – Possibly with wrappers to handle address families in an independent way