Check if a String is a Valid IPv6 Address in C
To check if a string is a valid IPv6 address in C, you can use functions like inet_pton
or getaddrinfo
to convert the address from its text form to a binary format. If the conversion is successful, the address is valid. In this tutorial, we cover multiple approaches to validate an IPv6 address using different functions.
Example 1: Using inet_pton
to Validate IPv6 Address
In this example, we will use the inet_pton
function to convert an IPv6 address string into its binary form. A successful conversion indicates that the address is valid.
main.c
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
int main() {
const char *ipv6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
struct in6_addr addr;
// Convert IPv6 address from text to binary form
int result = inet_pton(AF_INET6, ipv6, &addr);
if (result == 1) {
printf("'%s' is a valid IPv6 address.\n", ipv6);
} else if (result == 0) {
printf("'%s' is not a valid IPv6 address.\n", ipv6);
} else {
perror("inet_pton");
}
return 0;
}
Explanation:
- The variable
ipv6
is a constant character pointer that holds the IPv6 address string to be validated. struct in6_addr addr
is declared to store the binary representation of the IPv6 address.- The function
inet_pton
(which stands for “presentation to network”) converts the IPv6 address from its text format to a binary format. It returns 1 if the conversion is successful, 0 if the input is not a valid IPv6 address, and -1 if an error occurs. - Based on the return value of
inet_pton
, the program prints whether the IPv6 address is valid or not.
Output:
'2001:0db8:85a3:0000:0000:8a2e:0370:7334' is a valid IPv6 address.
Example 2: Using getaddrinfo
to Validate IPv6 Address
In this example, we will use the getaddrinfo
function to validate an IPv6 address. By setting the hints to use IPv6 (AF_INET6
), we can check whether the provided address is valid. If getaddrinfo
returns 0, the address is valid.
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
int main() {
const char *ipv6 = "2001:db8::1";
struct addrinfo hints, *res;
int status;
// Set the hints for IPv6
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
// Validate the IPv6 address using getaddrinfo
status = getaddrinfo(ipv6, NULL, &hints, &res);
if (status == 0) {
printf("'%s' is a valid IPv6 address.\n", ipv6);
freeaddrinfo(res);
} else {
printf("'%s' is not a valid IPv6 address: %s\n", ipv6, gai_strerror(status));
}
return 0;
}
Explanation:
- The variable
ipv6
stores the IPv6 address string to be validated. - A
struct addrinfo hints
is declared and initialized to zero usingmemset
to ensure all fields are set to default values. - The member
hints.ai_family
is set toAF_INET6
to indicate that we are interested in IPv6 addresses only. - The
getaddrinfo
function is called with the IPv6 address, NULL for the service, and the hints structure. If it returns 0, the address is valid; otherwise, an error message is provided. - If the address is valid, the allocated memory for the results is freed using
freeaddrinfo
.
Output:
'2001:db8::1' is a valid IPv6 address.
Conclusion
In this tutorial, we explored two methods to check if a string is a valid IPv6 address in C. Using inet_pton
and getaddrinfo
, you can easily validate IPv6 addresses. Both approaches offer reliable ways to ensure that your IPv6 address strings are correctly formatted and valid.