根据域名找到IP地址 getipbyhostname.c

* getipbyhostname.c

cpp 复制代码
#include <netdb.h>  /* gethostbyname */
#include <time.h>   /* time_t, localtime */
#include <errno.h>  /* errno */
#include <netinet/in.h> /* struct in_addr */
#include <arpa/inet.h>  /* inet_ntoa */
#include <string.h> /* strncpy */
#include <stdarg.h> /* va_start, va_end */
#include <stdio.h>

#define E_FAIL -1
#define E_OK 0

#define _FL_ __FILE__,__LINE__

#define E_MESSAGE 10000
#define E_ALLOC  10300
#define E_SOCKFD_SEND  10359
#define E_SOCKFD_RECV  10360
#define E_OSCALL    10201  /* 系统函数(%s)调用出错 */
#define E_FUNCALL   10202  /* 平台函数(%s)调用出错 */

#ifndef pid_t
typedef int pid_t;
#endif
extern  pid_t getpid(void);

int bclerrlog(int appcode, char *file, long line, const char *fmt, ...)
{
  time_t now_time;
  struct tm *info;
  char dt_buf[64];
  char appmsg[512];
  va_list ap;

  va_start(ap, fmt);
  /* bclerreg(appcode, file, line, fmt, ap); */
  vsnprintf(appmsg, 512, fmt, ap);

  time( &now_time );
  info = localtime( &now_time );
  strftime(dt_buf, 80, "%Y-%m-%d %H:%M:%S", info);
  fprintf(stderr, "---- %s ----\n", dt_buf);

  fprintf(stderr, "PID: %d\tFile: %s\tLine: %ld\n", getpid(), file, line);
  fprintf(stderr, "App Error: %d - %s\n", appcode, appmsg);
  fprintf(stderr, "System Error: %d - %s\n", errno, strerror(errno));
  va_end(ap);
}

#if 0
typedef union {
  unsigned char a[4];
  unsigned int m;
} IPAddr_t;
#endif

int main(int argc, char *argv[])
{
  char host_name[256];
  struct hostent *host_entry;
  char *IPbuffer;
  struct in_addr addr;

  /* strncpy(host_name, "p3-sign.douyinpic.com", 256); */
  if (argc < 2) {
    fprintf(stdout, "Usage: %s xx.xx.com", argv[0]);
    return E_OK;
  }
  strncpy(host_name, argv[1], 256);
  printf("Hostname: [%s]\n", host_name);

  /* To retrieve host information */
  host_entry = gethostbyname(host_name);
  if (host_entry == NULL) {
    bclerrlog(E_OSCALL, _FL_, "gethostbyname()");
    return E_FAIL;
  }
  /* To convert an Internet network address into ASCII string */
  for (int i = 0; i < host_entry->h_length; i++) {
    IPbuffer = inet_ntoa(*((struct in_addr*)host_entry->h_addr_list[i]));
    memcpy(&addr, host_entry->h_addr_list[i], sizeof(struct in_addr));
    printf("Host IP#%d: %s\t%08x\n", i, IPbuffer, addr.s_addr);
  }

  return E_OK;
}

$ cc -g getipbyhostname.c -o getipbyhostname

$ ./getipbyhostname p3-sign.douyinpic.com

Hostname: [p3-sign.douyinpic.com]

Host IP#0: 221.231.92.240 f05ce7dd

Host IP#1: 180.101.197.251 fbc565b4

Host IP#2: 116.207.144.248 f890cf74

Host IP#3: 58.215.47.228 e42fd73a

相关推荐
嘻哈baby15 分钟前
游戏/远程桌面的网络延迟优化:从TCP拥塞控制到智能选路
网络·tcp/ip·游戏
shcoc24 分钟前
备用 申请acme 申请ssl
网络·网络协议·ssl
老蒋新思维42 分钟前
创客匠人 2025 万人峰会实录:AI 智能体重构创始人 IP 变现逻辑 —— 从 0 到年入千万的实战路径
大数据·网络·人工智能·tcp/ip·创始人ip·创客匠人·知识变现
Hard but lovely44 分钟前
C/C++ ---条件编译#ifdef
c语言·开发语言·c++
草根站起来1 小时前
局域网内网IP能不能申请SSL证书
服务器·tcp/ip·ssl
EXtreme351 小时前
栈与队列的“跨界”对话:如何用双队列完美模拟栈的LIFO特性?
c语言·数据结构·leetcode·双队列模拟栈·算法思维
南棱笑笑生1 小时前
20251211给飞凌OK3588-C开发板跑飞凌Android14时让OV5645摄像头以1080p录像
c语言·开发语言·rockchip
tang_shou1 小时前
STM32CubeMx使用STM32F4系列芯片实现串口DMA接收
c语言·arm开发·stm32·单片机·嵌入式硬件·mcu·stm32cubemx
南棱笑笑生1 小时前
20251211给飞凌OK3588-C开发板适配Rockchip原厂的Buildroot【linux-6.1】系统时适配adb【type-C0】
linux·c语言·adb·rockchip
YuSun_WK2 小时前
zerotier-cli listpeers/ listnetworks命令以及IP的理解
网络·网络协议·tcp/ip