TELE 402 Lecture 6: Name and address conversions 1 Overview Last Lecture –Socket Options and elementary UDP sockets This Lecture –Name and address conversions.

Slides:



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

Topics: –DNS system –Gathering machine information How to find out the machines ip address, name, OS, version, etc.
CSE 333 – SECTION 8 Networking and sockets. Overview Network Sockets IP addresses and IP address structures in C/C++ DNS – Resolving DNS names Demos.
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()
I/O Models Satish Krishnan. I/O Models Blocking I/O Non-blocking I/O I/O Multiplexing Signal driven I/O Asynchronous I/O.
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.
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.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
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.
1 Advanced Name and Address Conversions getaddrinfo, getnameinfo, gai_strerror, freeaddrinfo host_serv, tcp_connect, tcp_listen, udp_client, udp_connect,
Introduction to Project 1 Web Client and Server Jan 2006.
Client Software Design Objectives: Understand principles of C/S design, with focus on clients Review Windows implementations of Socket functions.
ECE 4110 – Internetwork Programming Client-Server Model.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
Netprog: DNS and name lookups1 Address Conversion Functions and The Domain Name System Refs: Chapter 9 RFC 1034 RFC 1035.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
Client-Side Network Programming
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Class A Addresses The 1 st bit of a class A address is 0 The 1 st byte has a value from (128.x.x.x would not be a class A) 127.x.x.x is reserved.
Elementary Name and Address Conversions
More on Socket API. More on Socket I/O Functions  Scatter read and gather write.
Elementary Name and Address Conversions
Name and Address Conversions Chap 11. Domain Name System  A lookup mechanism for translating objects into other objects  A globally distributed, loosely.
API Extension Discussion for MIF-enabled Hosts IETF#77 Dapeng Liu, Zhen Cao.
Netprog: Advanced Sockets Programming1 Advanced Sockets Programming Ref: Chapter 7,11,21,22.
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.
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.
Advanced UNIX programming Fall 2002 Instructor: Ashok Srinivasan Lecture 25 Acknowledgements: The syllabus and power point presentations are modified versions.
EGEE-III INFSO-RI Enabling Grids for E-sciencE EGEE and gLite are registered trademarks gLite middleware IPv6 compliance (sub-task.
Research and developments Introduction to IPv6 Lana Abadie, Februar 5 th 2008.
Sockets Introduction Socket address structures Value-result arguments
Introduction to Sockets
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
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.
Chapter 11 Advanced Name and Address Conversion. Introduction gethostbyname, gethostbyaddr: protocol dependent getaddrinfo: –a function providing protocol.
API Extension Discussion for MIF-enabled Hosts IETF#77 Dapeng Liu, Zhen Cao.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
CSE 333 – SECTION 8 Client-Side Network Programming.
Name and Address Conversions
Name/Address conversion:
UNIX Sockets COS 461 Precept 1.
Advanced Computer Networks
CS 105 “Tour of the Black Holes of Computing”
Class A Addresses The 1st bit of a class A address is 0
Week 13 - Friday CS222.
Chapter4 Elementary TCP Socket
Name and Address Conversions Part I
Things that are nice to know when you’re doing this project
Client-side Networking CSE 333 Spring 2018
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
Advanced Sockets Programming
Chapter 11 Name and Address Conversions (Part II)
Server-side Programming CSE 333 Autumn 2018
Server-side Programming CSE 333 Summer 2018
IP Addresses, DNS CSE 333 Summer 2018
Client-side Networking CSE 333 Summer 2018
Network Programming: Part I CSCI 380: Operating Systems
Client-side Networking CSE 333 Autumn 2018
Client-side Networking CSE 333 Winter 2019
Internet Networking recitation #8
Today’s topic: Basic TCP API
Client-side Networking CSE 333 Spring 2019
Presentation transcript:

TELE 402 Lecture 6: Name and address conversions 1 Overview Last Lecture –Socket Options and elementary UDP sockets This Lecture –Name and address conversions –Source: Chapter 11 of Stevens’ book Next Lecture –IPv6 –Source: Chapter 12 of Stevens’ book

TELE 402 Lecture 6: Name and address conversions 2 Domain Name System Convert between numeric addresses and names –Files, NIS, DNS –Functions: gethostbyname and gethostbyaddr DNS resolver and name servers

TELE 402 Lecture 6: Name and address conversions 3 gethostbyname 1 Prototype struct hostent *gethostbyname(const char *hostname) –Returns: nonnull pointer if OK, NULL on error with h_errno set. struct hostent {char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; }

TELE 402 Lecture 6: Name and address conversions 4 gethostbyname 2 Prototype (cont) –Note: the error code is put into h_errno, and the error message can be printed out using either hstrerror(h_errno) or herror(char *). –Performs a query for an A record in DNS May be withdrawn in the future

TELE 402 Lecture 6: Name and address conversions 5 hostent structure

TELE 402 Lecture 6: Name and address conversions 6 gethostbyaddr 1 Prototype struct hostent *gethostbyaddr(const char *addr, size_t len, int family) –Returns: nonnull pointer if OK, NULL on error with h_errno set. –The same hostent structure is returned, but the field of interest is h_name

TELE 402 Lecture 6: Name and address conversions 7 Gethostbyaddr 2 Prototype (cont) –addr is not a char *, but is a pointer to an in_addr structure containing the IPv4 address. –len is the size of the structure. –The family argument is AF_INET.

TELE 402 Lecture 6: Name and address conversions 8 Reentrant functions 1 gethostbyname and gethostbyaddr are not reentrant –Uses a static structure (hostent) to store the result (part of BIND code) –Problem for threads and signal handlers

TELE 402 Lecture 6: Name and address conversions 9 Reentrant functions 2 main() { struct hostent *hptr;... signal(SIGALRM, sig_alrm);... hptr = gethostbyname(...);... } void sig_alrm(int signo){ struct host ent *hptr;... hptr = gethostbyname(...);... }

TELE 402 Lecture 6: Name and address conversions 10 Reentrant functions 3 struct hostent *gethostbyname_r(const char *hostname, struct host *result, char *buf, int buflen, int *h_errnop) struct hostent *gethostbyaddr_r(const char *addr, int len, int type, struct hostent *result, char *buf, int buflen, int *h_errnop) Returns: nonnull pointer if OK, NULL on error

TELE 402 Lecture 6: Name and address conversions 11 Reentrant functions 4 There are two ways to make reentrant functions –The caller can prepare the necessary memory space and pass it to the function. The caller should also free the memory space later –The function allocates the required memory space dynamically, fills the memory space, and returns the pointer of the memory space to the caller. The caller should call some function to release the memory space later; otherwise there will be memory leakage

TELE 402 Lecture 6: Name and address conversions 12 Reentrant functions 5 There is a reentrant problem with the variable errno. –Every process has only one copy of the variable –But a signal handler may interfere with the value of the variable –Better to return the error number from functions

TELE 402 Lecture 6: Name and address conversions 13 Get local host name 1 uname returns the name of the current host int uname(struct utsname *name) –Returns: nonnegative value if OK, -1 on error –It is often used along with gethostbyname struct utsname {char sysname[]; char nodename[]; char release[]; char version[]; char machine[];}

TELE 402 Lecture 6: Name and address conversions 14 Get local host name 2 gethostname returns the name of current host int gethostname(char *name, size_t namelen) –Returns: 0 if OK, -1 on error

TELE 402 Lecture 6: Name and address conversions 15 getservbyname Converts a service name to a port number struct servent *getservbyname(const char *servname, const char *protoname) struct servent {char *s_name; char **s_aliases; int s_port; char *s_proto;} Returns: nonnull pointer if OK, NULL on error protoname can be NULL or point to “udp” or “tcp”

TELE 402 Lecture 6: Name and address conversions 16 getservbyport Converts a port number to a service name struct servent *getservbyport(int port, const char *protname) Returns: nonnull pointer if OK, NULL on error

TELE 402 Lecture 6: Name and address conversions 17 getaddrinfo 1 int getaddrinfo(const char *hostname, const char *service, const struct addrinfo *hints, struct addrinfo **result); Returns: 0 if OK, nonzero on error hostname is either a hostname or an address string service is either a service name or a decimal port number string

TELE 402 Lecture 6: Name and address conversions 18 getaddrinfo 2 hints is either a null pointer or a pointer to an addrinfo structure that the caller fills in with hints about the types of information that the caller wants returned struct addrinfo {int ai_flags; int ai_family; int ai_socktype; int ai_protocol; size_t ai_addrlen; char *ai_canonname; struct sockaddr *ai_addr; struct addrinfo *ai_next;}

TELE 402 Lecture 6: Name and address conversions 19 getaddrinfo 3 ai_flags –AI_PASSIVE, AI_CANONNAME ai_family –AF_xxx ai_socktype –SOCK_DGRAM, SOCK_STREAM ai_protocol –IPROTO_UDP, IPROTO_TCP

TELE 402 Lecture 6: Name and address conversions 20 getaddrinfo 4 ai_addrlen –length of ai_addr ai_canonname ai_addr –socket structure containing information ai_next –pointer to the next addrinfo structure or NULL

TELE 402 Lecture 6: Name and address conversions 21 getaddrinfo 5

TELE 402 Lecture 6: Name and address conversions 22 getaddrinfo 6 If hints is NULL, addrinfo assumes a value of 0 for ai_flags, as_socktype, ai_protocol, and AF__UNSPEC for ai_family Multiple structures returned if: –Multiple addresses for hostname –Service is provided by multiple socket types, depending on the ai_socktype hint Order not determined

TELE 402 Lecture 6: Name and address conversions 23 getaddrinfo 7 What to do with the returned results –Arguments for socket are ai_family, ai_socktype, ai_protocol –2nd and 3rd arguments to connect or bind are ai_addr and ai_len

TELE 402 Lecture 6: Name and address conversions 24 getaddrinfo 8

TELE 402 Lecture 6: Name and address conversions 25 gai_strerror const char *gai_strerror(int error)

TELE 402 Lecture 6: Name and address conversions 26 freeaddrinfo All storage returned by getaddrinfo is dynamically allocated. void freeaddrinfo(struct addrinfo *ai) ai should point to the first addrinfo structure

TELE 402 Lecture 6: Name and address conversions 27 Summary of getaddrinfo

TELE 402 Lecture 6: Name and address conversions 28 Functions based on getaddrinfo 1 Stevens has several functions based on getaddrinfo Allow programs to be protocol independent

TELE 402 Lecture 6: Name and address conversions 29 Functions based on getaddrinfo 2 host_serv (lib/host_serv.c) struct addrinfo host_serv(const char *hostname, const char *service, int family, int socktype) –Returns an addrinfo structure for the given host and service –It always returns a canonical name tcp_connect (lib/tcp_connect.c) int tcp_connect(const char *hostname, const char *service); –Returns an connected socket descriptor

TELE 402 Lecture 6: Name and address conversions 30 Functions based on getaddrinfo 3 tcp_listen (lib/tcp_listen.c) int tcp_listen(const char *hostname, const char *service, socklen_t *addrlenp) –Returns a listening socket descriptor udp_client (lib/udp_client.c) int udp_client(const char *hostname, const char *service, void **saptr, socklen_t *lenp) –Returns an unconnected socket descriptor

TELE 402 Lecture 6: Name and address conversions 31 Functions based on getaddrinfo 4 udp_connect (lib/udp_connect.c) int udp_connect(const char *hostname, const char *service) –Returns a connected socket descriptor udp_server (lib/udp_server.c) int udp_server(const char *hostname, const char *service, socklen_t *lenptr) –Returns an unconnected socket descriptor

TELE 402 Lecture 6: Name and address conversions 32 getnameinfo 1 int getnameinfo(const struct sockaddr *sockaddr, socklen_t addrlen, char *host, size_t hostlen, char *serv, size_t servlen, int flags) Returns: 0 if OK, -1 on error

TELE 402 Lecture 6: Name and address conversions 33 getnameinfo 2 Flags for getnameinfo