Skip to content

Instantly share code, notes, and snippets.

@GunjiD
Last active July 28, 2020 07:30
Show Gist options
  • Save GunjiD/9fadd1e83e40892c1ad03821407c7081 to your computer and use it in GitHub Desktop.
Save GunjiD/9fadd1e83e40892c1ad03821407c7081 to your computer and use it in GitHub Desktop.
簡易ntop実装
#include <string.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
/*
https://code.woboq.org/userspace/glibc/resolv/inet_ntop.c.html#15tmp
*/
void ip_ntop(uint32_t src, char *dest){
u_char addr[16];
sprintf(addr,"%u.%u.%u.%u",(src >> 0) & 0xFF,(src >> 8) & 0xFF,(src >> 16) & 0xFF,(src >> 24) & 0xFF);
strcpy(dest, addr);
}
int main(int argc, char *argv[]){
uint32_t address = 2600511680; //IP 想定
char ip[16];
ip_ntop(address, ip);
printf("%s\n", ip);
return 0;
}
/*
printf("%ld", sizeof "255.255.255.255");
結果
16
255.255.255.255 は 16バイト
*/
@GunjiD
Copy link
Author

GunjiD commented Jul 28, 2020

$ ./a.out 
192.168.0.155

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment