1 / 10

#include<sys/types.h>

#include<sys/types.h>. Byte Ordering. 16-bit integer is made up of 2 bytes. 2 ways to store the bytes in memory Little-endian byte order: low order byte at the beginning address Big-endian byte order: high-order byte at the starting address. Network byte order and host byte order.

everett
Download Presentation

#include<sys/types.h>

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. #include<sys/types.h>

  2. Byte Ordering • 16-bit integer is made up of 2 bytes. 2 ways to store the bytes in memory • Little-endian byte order: low order byte at the beginning address • Big-endian byte order: high-order byte at the starting address

  3. Network byte order and host byte order • TCP/IP specifies a standard representation for binary integers used in protocol headers called Network byte order that represents integers with the most significant byte first • We refer to the byte ordering used by given system as the host byte order

  4. Byte-ordering functions • Following function converts integers • htons--from host to network byte order short unit16_t htons(unit16_t value); • ntohs--from network to host byte order short unit16_t ntohs(unit16_t value); • htonl—from host to network byte order long unit32_t htonl(unit32_t value); • ntohs--from network to host byte order long unit32_t ntohl(unit32_t value);

  5. bzero function • Syntax void bzero( void *dest, size_t nbytes); • It is provided by most the system to set nbytes of memory pointed by dest as 0. • Example bzero( &servaddr, sizeof(servaddr)); • Will set servaddr ( server address struct) as 0

  6. Group of address conversion functions int inet_aton( const char *strptr, struct in_addr *addrptr); Returns; 1 if string was valid, 0 on error convert from ASCII string to network byte order binary values char * inet_ntoa( struct inaddr inaddr); Return: pointer to dotted-decimal string Convert from network byte order binary value to dotted decimal string These two functions convert IPv4 address

  7. int inet_pton( int family, const char* strptr, void *addrptr); • Function will convert IP address in string( strptr) to the numeric value as in address struct( addptr). Return 1 if OK, return –1 on error

  8. Const char *inet_ntop(int family, const void *addptr, char *strptr, size_t len); • return pointer if K, NULL on error • Does the reverse conversion, from numeric (addptr) to presentation (strptr). The len is the size of the destination, to prevent the function from overflowing the caller’s buffer.

  9. Wrapper functions • In any real world program it is essential to check every function call for error return. Since terminating on an error is the common case, we can shorten our programs by defining a wrapper function that performs the actual function call, tests, the return value, and terminates on an error • s = Socket(AF_INET, SOCK_STREAM, 0);

  10. Wrapper function Socket() int Socket( int family, int type, int protocol) { int n; if( n = socket( family, type, protocol)) < 0) { printf(“socket error\n”); exit(0); } return n; }

More Related