Socket address structures Byte ordering IP address conversion

Slides:



Advertisements
Similar presentations
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Advertisements

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.
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C.
Name and Address Conversions© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
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.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
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.
Winsock programming.  TCP/IP UDP TCP  Winsock #include wsock32.lib.
Unix Network Programming Part 2: Elementary Sockets Jani Peusaari.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Network Programming Sockets and Winsock. Please Be Responsible We all know that the Internet is full of security holes –most of them do not require any.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
ECE453 – Introduction to Computer Networks Lecture 15 – Transport Layer (II)
Operating Systems Chapter 9 Distributed Communication.
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
Introduction to Socket Programming Advisor: Quincy Wu Speaker: Kuan-Ta Lu Date: Nov. 25, 2010.
Elementary Name and Address Conversions
Computer Systems II CSC 2405 Network Programming.
Sirak Kaewjamnong Computer Network Systems
The Application Layer Application Services (Telnet, FTP, , WWW) Reliable Stream Transport (TCP) Connectionless Packet Delivery Service (IP) Unreliable.
1 Internet Socket programming Behzad Akbari. 2 Sharif University of Technology, Kish Island Campus What is an API? API – stands for Application Programming.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C.
Technical Details for sockaddr_in Structure Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Network Programming with Sockets Reading: Stevens 3rd ed., Ch. 3-6, or 2 nd ed. Beej's Guide to Network Programming 1.
Elementary Name and Address Conversions
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
Introduction to Socket
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
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.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
Sockets Introduction Socket address structures Value-result arguments
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
1 Network Programming. 2 Background Important guidelines –Use conductor.tamucc.edu or any LINUX machine to develop your network applications –Do not use.
In unistd.h : int gethostname(char * name, int maxlen) Purpose : Find the computer's name.
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.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
Client-Server model. Socket programming 
Name and Address Conversions
Socket programming Péter Verhás August 2002
Jim Fawcett CSE 681 – Software Modeling & Analysis Fall 2002
Socket Programming (Cont.)
Network Programming CSC- 341
Network Programming with Sockets
Sockets.
Transport layer API: Socket Programming
Recitation 11 – 4/29/01 Outline Sockets Interface
Chapter 3 Sockets Introduction
Introduction to Socket Programming
28.
Network Programming Chapter 12
Tutorial on Socket Programming
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Chapter 3 소켓 주소 구조체 다루기 u-Network Design Lab 2.
Internet Networking recitation #8
in unistd.h: int gethostname(char * name, int maxlen)
Presentation transcript:

Socket Address Structures Chapter 03. Socket Address Structures

Socket address structures Byte ordering IP address conversion Goal Socket address structures Byte ordering IP address conversion Domain name conversion

Def. of socket address structures (1/6) Sockets use the socket address structure to pass and to receive addresses Several types depending on address family example) TCP/IP  SOCKADDR_IN, IrDA  SOCKADDR_IRDA Basic type is SOCKADDR structure

Def. of socket address structures (2/6) SOCKADDR structure sa_family 16-bit integer value identifying the protocol family being used e.g.) TCP/IP  AF_INET sa_data Address information used in the protocol family e.g.) TCP/IP  IP address and port number struct sockaddr { u_short sa_family; char sa_data[14]; }; typedef struct sockaddr SOCKADDR;

Def. of socket address structures (3/6) SOCKADDR_IN structure sin_addr 32-bit in_addr structure struct sockaddr_in { short sin_family; // AF_INET u_short sin_port; // port number struct in_addr sin_addr; // IP address char sin_zero[8]; // always 0 }; typedef struct sockaddr_in SOCKADDR_IN;

Def. of socket address structures (4/6) IN_ADDR structure struct in_addr { union { struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b; struct { u_short s_w1,s_w2; } S_un_w; u_long S_addr; } S_un; #define s_addr S_un.S_addr }; typedef struct in_addr IN_ADDR;

Def. of socket address structures (5/6) comparison of socket address structures SOCKADDR{} SOCKADDR_IN{} SOCKADDR_IRDA{} sa_family (2) sin_family (2) irdaAddressFamily (2) sa_data (14) sin_port (2) irdaDeviceID (4) sin_addr (4) irdaServiceName (25) sin_zero (8)

Def. of socket address structures (6/6) Use of socket address structure example1) Example 2) SOCKADDR_IN addr1; // initialize socket address structure ... f((SOCKADDR *)&addr1, ...); SOCKADDR_IN addr2; g((SOCKADDR *)&addr2, ...); // use socket address structure ...

Byte ordering function calls (1/6) Two ways to store data in their memories big-endian: the high-order octet is stored in the lowest memory location little-endian: the high-order octet is stored in the highest memory location 0x12 0x34 0x56 0x78 Big-endian Little-endian 0x1000 0x1001 0x1002 0x1003   

Byte ordering function calls (2/6) What if byte ordering is not considered in the below? End system router data IP address Port number ? (a) (b) (c)

Byte ordering function calls (3/6) Cases that byte ordering must be considered in network applications Address information for protocol (a) IP address  big-endian (b) port number  big-endian Data that applications send and receive (c) big-endian or little-endian Ref.  network byte ordering : big-endian  host byte ordering : byte ordering that system uses

Byte ordering function calls (4/6) Byte ordering functions(unix compatible) Byte ordering functions(Winsock extended) u_short htons (u_short hostshort); // host-to-network-short u_long htonl (u_long hostlong); // host-to-network-long u_short ntohs (u_short netshort); // network-to-host-short u_long ntohl (u_long netlong); // network-to-host-long int WSAHtons (SOCKET s, u_short hostshort, u_short* lpnetshort); int WSAHtonl (SOCKET s, u_long hostlong, u_long* lpnetlong); int WSANtohs (SOCKET s, u_short netshort, u_short* lphostshort); int WSANtohl (SOCKET s, u_long netlong, u_long* lphostlong);

Byte ordering function calls (5/6) Use of byte ordering functions Data to pass Use data in application hton*( ) ntoh*( ) Data from socket call Socket system call Socket system call

Byte ordering function calls (6/6) Byte ordering of SOCKADDR_IN structure sin_family (2) sin_port sin_addr (4) sin_zero (8) SOCKADDR_IN{} Host byte ordering Network byte ordering

Byte ordering function calls (6/6) ByteOrder.cpp

Function calls to convert IP address (1/4) Example.

Function calls to convert IP address(2/4) IP address coversion function calls Converts an IP address in ASCII dotted-decimal format to 32-bit binary format (network byte ordering) Converts 32-bit binary format (network byte ordering) to an IP address in ASCII dotted-decimal format unsigned long inet_addr (const char* cp); char* inet_ntoa (struct in_addr in); // network-to-ascii

Function calls to convert IP address(3/4) Use ① of byte ordering and IP address conversion function calls Case that address information is passed to socket system call after socket address structure initialization (usually sender side) f( ) is socket system call // initialize socket address structure SOCKADDR_IN addr; ZeroMemory(&addr, sizeof(addr)); // initialize to 0 addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr("147.46.114.70"); addr.sin_port = htons(9010); // socket system call f((SOCKADDR *)&addr, ...);

Function calls to convert IP address (4/4) Use ② of byte ordering and IP address conversion function calls Case that application prints address information after socket system call (usually receiver side) g( ) is socket system call // declaration of socket address structure SOCKADDR_IN addr; // socket system call g((SOCKADDR *)&addr, ...); // print IP address and port number printf("IP address=%s, port number=%d\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));

Byte ordering function calls (6/6) IPAddr.cpp

coversion between domain name and IP address (1/6) Example: Domain name  IP address

coversion between domain name and IP address (2/6) Domain name  IP address /* domain name  IP address(network byte ordering) */ struct hostent* gethostbyname ( const char* name // domain name ); /* address(network byte ordering)  domain name */ struct hostent* gethostbyaddr ( const char* addr, // network byte ordered IP address int len, // length of IP address(e.g.: 4) int type // address family(e.g.: AF_INET)

coversion between domain name and IP address(3/6) hostent struct struct hostent { char * h_name; // official name of host char ** h_aliases; // alias list short h_addrtype; // host address type short h_length; // length of address char ** h_addr_list; // list of addresses #define h_addr h_addr_list[0] // address, for backward compatibility }; typedef struct hostent HOSTENT;

coversion between domain name and IP address(4/6) hostent struct (cont’d) HOSTENT{} h_name official domain name\0 h_aliases alias #1\0 h_addrtype AF_INET alias #2\0 h_length 4 NULL h_addr_list IN_ADDR{} IP address #1 IP address #2 NULL h_length = 4

coversion between domain name and IP address(5/6) User defined function ① // domain name -> IP address BOOL GetIPAddr(char *name, IN_ADDR *addr) { HOSTENT *ptr = gethostbyname(name); if(ptr == NULL){ err_display("gethostbyname()"); return FALSE; } memcpy(addr, ptr->h_addr, ptr->h_length); return TRUE;

coversion between domain name and IP address (6/6) User defined function ② // IP address -> domain name BOOL GetDomainName(IN_ADDR addr, char *name) { HOSTENT *ptr = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET); if(ptr == NULL){ err_display("gethostbyaddr()"); return FALSE; } strcpy(name, ptr->h_name); return TRUE;

Byte ordering function calls (6/6) INameResolution.cpp