Linux C Network Programming-Get local IP address, mac, get corresponding IP via domain name

Source: Internet
Author: User
Tags sin socket error

Get the local IP address, Mac, get the corresponding IP through the domain name,

Is the more common operation that network programming may encounter, so it is summarized as follows (3 functions are encapsulated),

Directly on the code :

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include < unistd.h> #include <netdb.h> #include <net/if.h> #include <arpa/inet.h> #include <sys/ioctl.h > #include <sys/types.h> #include <sys/socket.h> #define Mac_size18#define ip_size16//function Declareint get_ip_by_domain (const char *domain, char *ip); Gets Ipint get_local_mac (const char *eth_inf, char *mac) according to the domain name; Gets the native Macint get_local_ip (const char *eth_inf, char *ip); Gets the native ip/****** main test **********/int main (void) {char Ip[ip_size];char mac[mac_size];const char *test_domain = "www.b Aidu.com "; const char *test_eth =" eth0 "; Get_ip_by_domain (Test_domain, IP);p rintf ("%s IP:%s\n ", Test_domain, IP); get_ Local_mac (Test_eth, Mac);p rintf ("Local%s Mac:%s\n", Test_eth, Mac), Get_local_ip (Test_eth, IP);p rintf ("Local%s IP:%s\ N ", Test_eth, IP); return 0;} Get Ipint get_ip_by_domain (const char *domain, char *ip) based on domain name {char **pptr;struct hostent *hptr;hptr =gethostbyname (domain), if (NULL = = hptr) {printf ("GetHostByName error for host:%s/n", domain); return-1;} for (pptr = hptr->h_addr_list; *pptr! = null; pptr++) {if (Null! = Inet_ntop (Hptr->h_addrtype, *pptr, IP, ip_size)) { return 0; Get only the first ip}}return-1;} Get native Macint get_local_mac (const char *eth_inf, char *mac) {struct ifreq ifr;int sd;bzero (&ifr, sizeof (struct ifreq)) ; if (SD = socket (af_inet, sock_stream, 0)) < 0) {printf ("Get%s MAC address socket creat error\n", eth_inf); return-1;} strncpy (Ifr.ifr_name, Eth_inf, sizeof (Ifr.ifr_name)-1), if (IOCTL (SD, SIOCGIFHWADDR, &AMP;IFR) < 0) {printf ("Get%s MAC address error\n ", eth_inf); return-1;} snprintf (Mac, Mac_size, "%02x:%02x:%02x:%02x:%02x:%02x", (unsigned char) ifr.ifr_hwaddr.sa_data[0], (unsigned char) IFR.IFR_HWADDR.SA_DATA[1], (unsigned char) ifr.ifr_hwaddr.sa_data[2], (unsigned char) ifr.ifr_hwaddr.sa_data[3], ( unsigned char) ifr.ifr_hwaddr.sa_data[4], (unsigned char) ifr.ifr_hwaddr.sa_data[5]); close (SD); return 0;} Get native IpintGET_LOCAL_IP (const char *eth_inf, char *ip) {int sd;struct sockaddr_in sin;struct ifreq ifr;sd = socket (Af_inet, SOCK_DGRAM , 0); if ( -1 = = SD) {printf ("Socket Error:%s\n", Strerror (errno)); return-1;} strncpy (Ifr.ifr_name, Eth_inf, Ifnamsiz); ifr.ifr_name[ifnamsiz-1] = 0;//if error:no such Deviceif (IOCTL (SD, SIOCGIFAD DR, &AMP;IFR) < 0) {printf ("IOCTL error:%s\n", Strerror (errno)); Close (SD); return-1;} memcpy (&sin, &ifr.ifr_addr, sizeof (SIN)), snprintf (IP, ip_size, "%s", Inet_ntoa (SIN.SIN_ADDR)), close (SD); return 0;}
Test RunThe results are as follows:

Linux C Network Programming-Get local IP address, mac, get corresponding IP via domain name

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.