Use ioctl to set the mac address to obtain the current IP address, subnet mask, and other information.

Source: Internet
Author: User
 #include <sys/types.h>  /* for socket(2) and related bits and pieces */
#include <sys/socket.h> /* for socket(2) */
#include <net/if.h> /* for struct ifreq */
#include <net/if_arp.h> /* for ARPHRD_ETHER */
#include <sys/ioctl.h> /* for IOCTL's */
#include <stdio.h> /* for fprintf etc */
#include <unistd.h> /* for close */


int main(int argc,char **argv)
{
struct ifreq ifr;
int skfd;

if (argc<3) {
fprintf(stderr,"usage:/n%s interface hwaddr/n",argv[1]);
return 1;
}

/* Fill in the structure */
snprintf(ifr.ifr_name, IFNAMSIZ, "%s", argv[1]);
ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;

/* TODO: write some code to parse argv[2] into ifr.ifr_hwaddr.sa_data */
/* memcpy(&ifr.ifr_hwaddr.sa_data, argv[2], sizeof(ether.address)); */
/* Create a socket fd */
skfd = socket(PF_INET,SOCK_STREAM,0);

/* call the IOCTL */
if (ioctl(skfd, SIOCSIFHWADDR, &ifr) < 0) {
perror("ioctl(SIOCSIFHWADDR)");
return 1;
}

/* cleanup */
close(skfd);

/* we're out of here! */
return 0;
}

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>

static void usage(){
printf("usage : ipconfig interface /n");
exit(0);
}

int main(int argc,char **argv)
{
struct sockaddr_in *addr;
struct sockaddr sa;
struct ifreq ifr;
char *name,*address;
int sockfd;
unsigned char mac[6];

if(argc != 2)
usage();
else
name = argv[1];

sockfd = socket(AF_INET,SOCK_DGRAM,0);
strncpy(ifr.ifr_name,name,IFNAMSIZ-1);

if(ioctl(sockfd,SIOCGIFADDR,&ifr) == -1)
perror("ioctl error"),exit(1);

addr = (struct sockaddr_in *)&(ifr.ifr_addr);
address = inet_ntoa(addr->sin_addr);
printf("inet addr: %s ",address);

if(ioctl(sockfd,SIOCGIFBRDADDR,&ifr) == -1)
perror("ioctl error"),exit(1);

addr = (struct sockaddr_in *)&ifr.ifr_broadaddr;
address = inet_ntoa(addr->sin_addr);
printf("broad addr: %s ",address);

if(ioctl(sockfd,SIOCGIFNETMASK,&ifr) == -1)
perror("ioctl error"),exit(1);

addr = (struct sockaddr_in *)&ifr.ifr_addr;
address = inet_ntoa(addr->sin_addr);
printf("inet mask: %s ",address);

printf("/n");
if(ioctl(sockfd, SIOCGIFHWADDR, &ifr) == -1)
perror("ioctl error"), exit(1);

memcpy(&sa, &ifr.ifr_addr, sizeof(struct sockaddr_in));
memcpy(mac, sa.sa_data, sizeof(mac));
fprintf(stdout, "%s mac: %.2X:%.2X:%.2X:%.2X:%.2X:%.2X/n",
name, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

exit(0);
}












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.