Hello,
Thanking the people who answer in advance. =)
I fixed my problem by the line from char * mac_str = NULL; to char mac_str[1];
My questions:
1) With * mac_str = NULL I get an segmentation error. Why is that? Why can't the variable be overwritten by the C function ether_ntoa_r ??
2) my fix seems odd for some way: char mac_str[1] can only hold one character.
why can it store my whole MAC address? I am puzzled.
My program which these questions are related to. It's to retrieve the IP and MAC addres from my networkcard. I have to type sudo ./ipaddr eth0 to get it to work
Code:// Headers needed #include <net/if.h> #include<netinet/if_ether.h> #include <net/if_arp.h> #include <netpacket/packet.h> #include <net/ethernet.h> /* the L2 protocols */ #include <asm/types.h> #include <linux/sockios.h> #include<stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <arpa/inet.h> //htons #include <sys/ioctl.h> #include <netinet/ether.h> //ether_ntoa_r int main(int argc, char *argv[]) { struct ifreq ifr; int fd; memset(&ifr, 0, sizeof(ifr)); if(argc == 1) { printf("U moet een netwerk interface naam als urgument meegeven. Bijv. eth0\n"); exit(1); } // setup ifr for ioctl strncpy (ifr.ifr_name, argv[1], sizeof(ifr.ifr_name) - 1); ifr.ifr_name[sizeof(ifr.ifr_name)-1] = '\0'; // create socket fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); if ( fd == -1) { fprintf(stderr," Kan socket niet aanmaken. errno code: %s \n", strerror(errno)); return -1; } // IP adres ophalen if (ioctl(fd, SIOCGIFADDR, &ifr) == -1) { perror("Fout ophalen IP adres"); close(fd); return (-1); } // MAC adres ophalen if (ioctl(fd, SIOCGIFADDR, &ifr) == -1) { perror("Fout ophalen MAC adres"); close(fd); return (-1); } // close socket if created locally close(fd); printf("IP adres van %s is %s\n",argv[1],inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr)); char mac_str[1]; // mac adres omzetten naar colon format ether_ntoa_r((const struct ether_addr *)&(ifr.ifr_hwaddr.sa_data),mac_str); printf("En het Mac adres is %s\n",mac_str); exit(0); }



1Likes
LinkBack URL
About LinkBacks


