cs 311 – lecture 19 outline internet sockets – gethostname utility – struct hostent –...
Post on 20-Dec-2015
214 views
TRANSCRIPT
CS 311 – Lecture 19 Outline
• Internet Sockets– gethostname utility– struct hostent– inet_addr– Machine byte to Network byte order translation
and vice versa
Lecture 19 1CS311 - Operating Systems I
Internet Sockets
• Processes communicate via network.• Two identify a process on the network we
need– IP address of the process (IPv4 or IPv6)– Port number where the process listens
Lecture 19 2CS311 - Operating Systems I
Getting the IP Address
• To get the IP address in network byte order for the IP that is known use inet_addr().
• Prototype: in_addr_t inet_addr(const char *string)– Returns the 32-bit IP of network byte order
corresponding to the A.B.C.D string.
• If IP not known then we can get that using gethostname() and gethostbyname()
Lecture 19 3CS311 - Operating Systems I
gethostname() and gethostbyname()
• gethostname() returns the host’s name where the process is executing.
• Prototype: int gethostname(char *name, int namelen)
• gethostbyname() returns a pointer to struct hostent
• Protoype: struct hostent * gethostbyname(const char *name)– The only parameter is the hostname.– Searches the /etc/host or the DNS database to get the
structure information.
Lecture 19 4CS311 - Operating Systems I
struct hostent
• The structure has these members– char *h_name - “official” name of the host. – char **h_aliases - alternative names for the host– int h_addrtype - either AF_INET or AF_INET6– int h_length - length, in bytes, of each address.– char **h_addr_list – array of addresses for the
host. – char *h_addr - first host address (or
*h_addr_list[0]).
Lecture 19 5CS311 - Operating Systems I
inet_ntoa() and inet_aton()
• inet_ntoa() - Takes IP address in network-byte order and converts to string format.
• Prototype: char *inet_ntoa(struct in_addr addr)
• inet_aton() – same as inet_addr() but better• Prototype: int inet_aton(const char *addr,
struct in_addr *addr)– Stores the char* addr into struct in_addr
Lecture 19 6CS311 - Operating Systems I
memset() and others• memset() sets a block of memory to the desired
value.• Prototype: void * memset (void * ptr, int value,
size_t num);• memcpy() copies a block of memory to another
block.• Prototype: void * memcpy ( void * destination,
const void * source, size_t num );• memcmp() compares two blocks of memory• Prototype: int memcmp ( const void * ptr1,
const void * ptr2, size_t num );
Lecture 19 7CS311 - Operating Systems I
bzero() and bcopy()
• bzero() same as memset() but initializes a block of memory to zero.
• Prototype: void bzero (void* buffer, size_t length)• bcopy() is the same as memcpy()
Lecture 19 8CS311 - Operating Systems I
Machine-byte to network-byte
• Converting machine-byte order to network order
• Protoype:– in_port_t htons (in_port_t hostShort)– in_addr_t ntohl (in_addr_t networkLong)
• Converting network-byte to machine-byte• Prototype:– in_port_t ntohs (in_port_t networkShort)– in_addr_t htonl (in_addr_t hostLong)
Lecture 19 9CS311 - Operating Systems I