JMU
Internet Programming Basics
An Introduction with Examples in C


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Motivation
Byte Order
Byte Order (cont.)
Byte Order (cont.)
Byte Order: htons() htons
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
Address Structures
  // 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
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
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
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.)
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
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.)
unixexamples/internet/my_address.c
        #include <arpa/inet.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int
main(void)
{
  struct ifaddrs     *first, *current;
  struct sockaddr_in *address;
    
  getifaddrs(&first); // Gets a linked structure of interface addresses

  current = first;
  while (current)
    {
      if ((current->ifa_addr) && (current->ifa_addr->sa_family == AF_INET))
        {
          address = (struct sockaddr_in *)current->ifa_addr;
          printf("%s: %s\n", current->ifa_name, inet_ntoa(address->sin_addr));
        }

      current = current->ifa_next;
    }

  freeifaddrs(first); // Free the memory allocated by getifaddrs()
  exit(0);
}

        
Getting the Local Host's Name: gethostname() gethostname
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.)
unixexamples/internet/my_name.c
        #define _GNU_SOURCE
#define  _POSIX_C_SOURCE 20112L
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int
main(void)
{
  char   name[NI_MAXHOST];

  memset(&name, 0, sizeof(name)); 
  gethostname(name, NI_MAXHOST);
  printf("Host: %s\n", name);
  return 0;
}