- Forward


Internet Programming Basics
An Introduction with Examples in C


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • An Observation:
    • Regardless of the layer you are working on there are some things they have in common
  • Some Fundamental Issues:
    • Byte Order
    • Addresses
Byte Order
Back SMYC Forward
  • The Issue:
    • In a multi-byte entity, the individual bytes can be stored in any order and different processors use different representations
  • One Implication:
    • If these entities aren't moved from one processor to another there is no reason to care however network programs don't have this luxury
Byte Order (cont.)
Back SMYC Forward
  • The Canonical Representations:
    • Big-Endian - Most significant byte first (i.e., in the smallest address)
    • Little-Endian - Least significant byte first
  • Network Byte Order:
    • The Internet uses big-endian representations
Byte Order (cont.)
Back SMYC Forward
  • Host Order to Network Order:
    • htons() for "short" values (i.e., of type uint16_t)
    • htonl() for "long" values (i.e., of type uint32_t)
  • Network Order to Host Order:
    • ntohs() for "short" values (i.e., of type uint16_t)
    • ntohl() for "long" values (i.e., of type uint32_t)
Byte Order: htons() htons
Back SMYC Forward
uint16_t htons(uint16_t value)
Purpose:
Convert a uint16_t from host order to network order
Details:
value The value in host order
Return The value in network order
#include <arpa/inet.h> arpa/inet.h

Note: These "functions" are commonly defined as macros.

Addresses
Back SMYC Forward
  • Purpose:
    • Every host has (one or more) addresses (associated with a hardware interface) that can be used to identify it
  • IPv4 Addresses:
    • Four bytes (32 bits)
    • Dotted Quads
  • IPv6 Addresses:
    • Sixteen bytes (128 bits)
    • Colon-Seperated Hexadecimals
Address Structures
Back SMYC Forward
// Generic struct sockaddr { sa_family_t sa_family; // AF_UNIX, AF_INET, AF_INET6 char sa_data[14]; }; // IPv4 struct in_addr { in_addr_t s_addr; // 32-bits }; struct sockaddr_in { sa_family_t sin_family; // AF_INET in_port_t sin_port; // Port umber struct in_addr sin_addr; unsigned char __pad[X]; // Pad to size of struct sockaddr }; // IPv6 struct in6_addr { uint8_t s6_addr[16]; // 128 bits }; struct sockaddr_in6 { sa_family_t sin6_family; // AF_INET6 in_port_t sin6_port; // Port umber uint32_t sin6_flowinfo; // Flow information struct in6_addr sin6_addr; uint32_t sin6_scope_id; // Scope ID };
Formatting Addresses: inet_ntop() inet_ntop
Back SMYC Forward
const char *inet_ntop(int domain, const void *net, char *pres, size_t length)
Purpose:
Convert an address from network (binary) to presentation (human-readable)
Details:
domain AF_INET or AF_INET6
net The address to convert
pres The human-readable version
length The length of the human-readable version
Return Pointer to dest on success, NULL on error
#include <arpa/inet.h> arpa/inet.h

Note: This function just converts from one format to another.

Formatting Addresses: inet_pton() inet_pton
Back SMYC Forward
int inet_pton(int domain, const char *pres, void *net)
Purpose:
Convert an address from presentation (human-readable) to network (binary) format
Details:
domain AF_INET or AF_INET6
pres The human-readable version
net The address in binary format
Return 1 on success; 0 on error
#include <arpa/inet.h> arpa/inet.h

Note: This function just converts from one format to another.

Getting Local Addresses: getifaddrs() getifaddrs
Back SMYC Forward
int getifaddrs(struct ifaddrs **first)
Purpose:
Get the addresses of the local hosts network interfaces
Details:
first A pointer to the first address in a linked structure of addresses
Return 0 on success; -1 on error
#include <ifaddrs.h> ifaddrs.h
#include <sys/types.h> sys/types.h

Note: This function reads the information from the file system.

Getting Local Addresses (cont.)
Back SMYC Forward
struct ifaddrs { struct ifaddrs *ifa_next; // Next item in list char *ifa_name; // Name of interface unsigned int ifa_flags; // Flags from SIOCGIFFLAGS struct sockaddr *ifa_addr; // Address of interface struct sockaddr *ifa_netmask; // Netmask of interface union { struct sockaddr *ifu_broadaddr; // Broadcast address of interface struct sockaddr *ifu_dstaddr; // Point-to-point destination address } ifa_ifu; #define ifa_broadaddr ifa_ifu.ifu_broadaddr #define ifa_dstaddr ifa_ifu.ifu_dstaddr void *ifa_data; // Address-specific data };
Getting Local Addresses: freeifaddrs() freeifaddrs
Back SMYC Forward
void freeifaddrs(struct ifaddrs *first)
Purpose:
Free the memory allocated by a call to getifaddrs(struct ifaddrs **first)
Details:
first A pointer to the first address in a linked structure of addresses
#include <ifaddrs.h> ifaddrs.h
#include <sys/types.h> sys/types.h
Getting Local Addresses (cont.)
Back SMYC Forward
unixexamples/internet/my_address.c
 
Getting the Local Host's Name: gethostname() gethostname
Back SMYC Forward
int gethostname(char *name, size_t length)
Purpose:
Access the host name of the local processor
Details:
name The name of the local host
length The maximum length of a host name
Return 0 on success; -1 on error
#define _GNU_SOURCE
#define _POSIX_C_SOURCE
#include <unistd.h> unistd.h

Note: This function reads the information from the file system.

Getting the Local Host's Name (cont.)
Back SMYC Forward
unixexamples/internet/my_name.c
 
There's Always More to Learn
Back -