Internet Programming Basics
An Introduction with Examples in C |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
htons()
for "short" values (i.e., of type
uint16_t
)htonl()
for "long" values (i.e., of type
uint32_t
)ntohs()
for "short" values (i.e., of type
uint16_t
)ntohl()
for "long" values (i.e., of type
uint32_t
)// 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 };
const char *inet_ntop(int domain, const void *net, char *pres, size_t length)
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 |
Note: This function just converts from one format to another.
int inet_pton(int domain, const char *pres, void *net)
domain
|
AF_INET or AF_INET6 |
pres
|
The human-readable version |
net
|
The address in binary format |
Return | 1 on success; 0 on error |
Note: This function just converts from one format to another.
int getifaddrs(struct ifaddrs **first)
first
|
A pointer to the first address in a linked structure of addresses |
Return | 0 on success; -1 on error |
Note: This function reads the information from the file system.
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 };
#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); }
int gethostname(char *name, size_t length)
name
|
The name of the local host |
length
|
The maximum length of a host name |
Return | 0 on success; -1 on error |
Note: This function reads the information from the file system.
#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; }