LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-06-2005, 07:37 AM   #1
jagman
LQ Newbie
 
Registered: Feb 2005
Posts: 17

Rep: Reputation: 0
Question sending an arp request


Good day to all of you, i have a question.
How can I make an or send an ARP request in a C program , or in a shell script
 
Old 05-06-2005, 07:57 AM   #2
jagman
LQ Newbie
 
Registered: Feb 2005
Posts: 17

Original Poster
Rep: Reputation: 0
to be more specific, how do i get the MAC address of a client, in a C program or shell script
 
Old 02-27-2006, 02:59 PM   #3
w6nct
LQ Newbie
 
Registered: Feb 2006
Posts: 2

Rep: Reputation: 0
Code example to get ARP cache entry

USAGE:
- Call this function several times, with a slight delay between attempts (e.g., 1 second). Be sure to give up after an excessive number of failed attempts (e.g., 20 attempts), just in case the device never responds to ARPs.
- If no ARP cache entry exists, the function will return an error; but the stack should (transparently) generate an associated ARP Request.
- If needed, I can provide a code example for the calling sequence as well.
- NOTE: I am having a minor issue with this code implementation; but generally it works OK (see LinuxQuestion #2126715 from me, w6nct).


int getMac(char *dev, unsigned long ip, unsigned char *mac)
{
struct sockaddr addr;
struct arpreq myreq;
int sockfd, i;

// default Mac to zero, in case we return an error
for (i = 0; i < 6; i++) {
mac[i] = 0;
}

memset(&addr, '\0', sizeof(addr));
addr.sa_family = AF_INET;
memcpy(&addr.sa_data[2], &ip, 4);

memset(&myreq, '\0', sizeof(myreq));
memcpy(&myreq.arp_pa, &addr, sizeof(addr));
memcpy(&myreq.arp_dev, dev, sizeof(myreq.arp_dev));

if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("getMac: error from socket call (%s, 0x%08X)", dev, ip);
return (-1);
}

// try to get an ARP mapping (if available)
if (ioctl(sockfd, SIOCGARP, (char *) &myreq) < 0) {
printf("getMac: error from ioctl call (%s, 0x%08X)", dev, ip);
close(sockfd);
return (-1);
}

for (i = 0; i < 6; i++)
mac[i] = myreq.arp_ha.sa_data[i];

printf("getMac: %s, 0x%08X, %02X:%02X:%02X:%02X:%02X:%02X",
dev, ip, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

close(sockfd);
return 0;
}

<<< EOM >>>

Last edited by w6nct; 04-03-2006 at 11:30 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
TCP/IP Packet sending Resolve ARP cranium2004 Linux - Networking 3 02-03-2005 09:40 PM
[help - emergency] linux sending arp flood princenux Linux - Security 2 12-10-2004 01:36 PM
ARP request davidho3 Linux - Networking 1 11-06-2004 09:48 AM
ARP request davidho3 Linux - Networking 2 11-05-2004 10:01 AM
Disabling ARP probes after receiving an ARP request AltecLansingMan Linux - Networking 1 03-30-2004 01:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration