Created
November 4, 2018 00:40
-
-
Save hirokuma/ccb2beaedb8dbfc047f0e06c14e4776e to your computer and use it in GitHub Desktop.
inet_pton()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
#include <string.h> | |
#include <arpa/inet.h> | |
int main(void) | |
{ | |
const char IPADDR[] = "192.168.10.5"; | |
int retval; | |
//uint8_t buf[sizeof(struct in_addr)]; | |
//uint8_t buf[sizeof(struct in6_addr)]; | |
struct in6_addr addr; | |
/* | |
--------------------------------- | |
typedef uint32_t in_addr_t; | |
struct in_addr { | |
in_addr_t s_addr; | |
}; | |
--------------------------------- | |
struct in6_addr { | |
unsigned char s6_addr[16]; | |
}; | |
--------------------------------- | |
*/ | |
retval = inet_pton(AF_INET, IPADDR, &addr); | |
switch (retval) { | |
case 1: | |
printf("OK\n "); | |
for (size_t lp = 0; lp < sizeof(struct in_addr); lp++) { | |
printf("%02x", addr.s6_addr[lp]); | |
} | |
printf("\n"); | |
break; | |
case 0: | |
printf("not match\n"); | |
break; | |
case -1: | |
printf("NG\n"); | |
break; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment