Skip to content

Instantly share code, notes, and snippets.

@hirokuma
Created November 4, 2018 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirokuma/ccb2beaedb8dbfc047f0e06c14e4776e to your computer and use it in GitHub Desktop.
Save hirokuma/ccb2beaedb8dbfc047f0e06c14e4776e to your computer and use it in GitHub Desktop.
inet_pton()
#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