In message <[EMAIL PROTECTED]> Andy Angrick wrote:
> With a lot of research and great help from a few on the list (thanks Luc)..
> this is what i finally came up with for find out the IP and MAC address of
> an interface in case anyone is interested.
...
> int main(void){
> printf("Hardware: %s\n",gethwaddr("eth0"));
> printf("IP: %s\n",getipaddr("eth0"));
> }
warning: control reaches end of non-void function
>
> char *gethwaddr(char *device)
> {
> int fd,i;
warning: unused variable `i'
> static char ethaddr[8];
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> struct ifreq ifr;
> unsigned char *txt;
>
> if((fd=socket(AF_INET,SOCK_DGRAM,0))<0)
> return 0;
> memset(&ifr,0,sizeof(ifr));
> strncpy(ifr.ifr_name,device,sizeof(ifr.ifr_name));
>
> if(ioctl(fd,SIOCGIFHWADDR,(char *)&ifr)==-1)
> {
> close(fd);
> return 0;
> }
> close(fd);
> txt=(unsigned char *)&ifr.ifr_hwaddr.sa_data;
> sprintf(ethaddr, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X", txt[0]&0x000000ff,\
*** ERROR *** This format outputs 19 characters (including the final '\0')
into a character array which can only hold 8 chars...
> txt[1]&0x000000ff,txt[2]&0x000000ff,txt[3]&0x000000ff,\
> txt[4]&0x000000ff,txt[5]&0x000000ff);
??? What is the "&0xFF" needed for? You are using unsigned chars, and
an unsigned format string???
> return ethaddr;
> }
>
> char *getipaddr(char *device)
> {
> int fd,i;
warning: unused variable `i'
> static char ipaddr[14];
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> struct ifreq ifr;
> unsigned long int myip;
warning: unused variable `myip'
> char tmp[14]="",str[14]="";
warning: unused variable `str'
warning: unused variable `tmp'
> unsigned char *txt;
>
> if((fd=socket(AF_INET,SOCK_DGRAM,0))<0)
> return 0;
> memset(&ifr,0,sizeof(ifr));
>
> strncpy(ifr.ifr_name,device,sizeof(ifr.ifr_name));
> ifr.ifr_addr.sa_family = AF_INET;
>
> if(ioctl(fd,SIOCGIFADDR,(char *)&ifr)==-1)
> {
> close(fd);
> return 0;
> }
> close(fd);
> txt=(unsigned char *)&ifr.ifr_addr.sa_data;
> sprintf(ipaddr, "%d.%d.%d.%d", txt[2],txt[3],txt[4],txt[5]);
*** ERROR *** This format outputs up to 16 characters (including the final '\0')
into a character array which can only hold 14 chars...
> return ipaddr;
> }
Ummmm... you better fix them bugs...
Wolfgang Denk
--
Software Engineering: Embedded and Realtime Systems, Embedded Linux
Phone: (+49)-8142-4596-87 Fax: (+49)-8142-4596-88 Email: [EMAIL PROTECTED]
Drun'? 'm not drun'! You woudn' dare call m' drun' if I was sober!
- Terry Pratchett, _Men at Arms_
--
To unsubscribe from this list, send a message to [EMAIL PROTECTED]
with the command "unsubscribe linux-embedded" in the message body.
For more information, see <http://waste.org/mail/linux-embedded>.