Sponsored Content
Top Forums Programming SIOCGARP to display the entire ARP table. Post 302448191 by semash! on Wednesday 25th of August 2010 12:08:38 PM
Old 08-25-2010
SIOCGARP to display the entire ARP table.

Hello everybody,

I have a working code that displays an entry from a given IP address. but, how can i display the entire ARP table? like the option 'arp -a' of the standard linux program.

This is the code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <net/if.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if_arp.h>

char *mac_ntoa(unsigned char *ptr){
static char address[30];
sprintf(address, "%02X:%02X:%02X:%02X:%02X:%02X",
ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
return(address);
}

int main(int argc, char* argv[]){
int s;

struct arpreq req;
struct hostent *hp;
struct sockaddr_in *sin;

char *host = argv[1];

bzero((caddr_t)&req, sizeof(req));

sin = (struct sockaddr_in *)&req.arp_pa;
sin->sin_family = AF_INET; /* Address Family: Internet */
sin->sin_addr.s_addr = inet_addr(host);

if(sin->sin_addr.s_addr ==-1){
if(!(hp = gethostbyname(host))){
fprintf(stderr, "arp: %s ", host);
herror((char *)NULL);
return(-1);
        }
bcopy((char *)hp->h_addr, (char *)&sin->sin_addr, sizeof(sin->sin_addr));
}

if((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
perror("socket() failed.");
exit(-1);
        } /* Socket is opened.*/

strcpy(req.arp_dev, "eth0");

if(ioctl(s, SIOCGARP, (caddr_t)&req) <0){
if(errno == ENXIO){
printf("%s (%s) -- no entry.\n", host, inet_ntoa(sin->sin_addr));
exit(-1);
        } else {
perror("SIOCGARP");
exit(-1);
        }
}
close(s); /* Close the socket, we don't need it anymore. */

printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));

if(req.arp_flags & ATF_COM){
printf("%s ", mac_ntoa(req.arp_ha.sa_data));
        } else {
printf("incomplete");
}

if(req.arp_flags & ATF_PERM){
printf("ATF_PERM");
        }
if(req.arp_flags & ATF_PUBL){
printf("ATF_PUBL");
        }
if(req.arp_flags & ATF_USETRAILERS){
printf("ATF_USETRAILERS");
}

printf("\n");
return(0);
}

I've looked to /usr/src/linux/net/ipv4/arp.c code before posting and can't find anything that tells me how to do it.

Thanks in advance.
 

9 More Discussions You Might Find Interesting

1. Solaris

ARP Cache

Dear all, We are testing two of our servers for mq series connectivity. The scenario is, when one machine is shutting down it's services there are some scripts that do a dns update, which removes the ip address and relates it to the ip address of the other node on our dns server, and the update... (7 Replies)
Discussion started by: earlysame55
7 Replies

2. Red Hat

Arp Problem

Dear All i have a linux proxy server which has RHEL-5 64 bit, it has two interfaces, it has the following details eth0=10.200.14.42 eth3=10.201.14.42 default gateway=10.201.14.254 one static route=192.168.0.0/24 gw 10.200.14.254 i am facing a problem when i ping 10.201.14.42 from... (2 Replies)
Discussion started by: surfer24
2 Replies

3. Web Development

Display the contents of a table.

Dear Friends, Am an newbie to this domain. I have a table which is filled with contents which i need to be updated with,so am trying to design a flow which would read the data from the table and mail it to me at regular intervals. I could make out the flow using mailx command but want to... (3 Replies)
Discussion started by: gokulj
3 Replies

4. UNIX for Advanced & Expert Users

Monitoring the arp table

Man page of netlink says: --------------------------------- NETLINK_ARPD For managing the arp table in user space. ---------------------------------- Is it possible to monitor arp table using Netlink? or it's just for manipulating? (1 Reply)
Discussion started by: xyzt
1 Replies

5. UNIX for Dummies Questions & Answers

How to send html file in a mail not as an attachment but it should display in the mail in table for

Hi The below script working when we are sending the html as attachment can u please guide how to send thesmae data in table form direct in the mail and not in mail attachment . cat Employee.sql SET VERIFY OFF SET PAGESIZE 200 SET MARKUP HTML ON SPOOL ON PREFORMAT OFF ENTMAP ON - HEAD... (0 Replies)
Discussion started by: mani_isha
0 Replies

6. UNIX for Advanced & Expert Users

arp questions

Can someone please explain this output to me. Why doesn't ifconfig show the same info? ~ $ arp -a ? (10.71.0.1) at 00:1b:21:2b:eb:0c on eth0 (4 Replies)
Discussion started by: cokedude
4 Replies

7. AIX

Problem using ioctl (SIOCGIFCONF and SIOCGARP)

I am writing some portable code to print Interface, its IP address, and its MAC (Hardware) address. It is working on three platforms (viz, Linux, Solaris and HP-UX). However, it is creating problems on AIX. The code is as follows: Now the problem is that when I try to execute the... (1 Reply)
Discussion started by: slash_blog
1 Replies

8. Shell Programming and Scripting

Bash script to display result in table

My script gives the following result. Is it possible to display the same in table format ? 1. rex_best Latest feeds are not avaialable. The last feed was generated on 2012-05-17 File Name = ekb_best_20120517_010949_665.tar.gz The Number of entry elements = 4209539 2. rex_genre Latest... (2 Replies)
Discussion started by: kishorekumar87
2 Replies

9. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies
sendfile(3EXT)						    Extended Library Functions						    sendfile(3EXT)

NAME
sendfile - send files over sockets or copy files to files SYNOPSIS
cc [ flag... ] file... -lsendfile [ library... ] #include <sys/sendfile.h> ssize_t sendfile(int out_fd, int in_fd, off_t *off, size_t len); DESCRIPTION
The sendfile() function copies data from in_fd to out_fd starting at offset off and of length len bytes. The in_fd argument should be a file descriptor to a regular file opened for reading. See open(2). The out_fd argument should be a file descriptor to a regular file opened for writing or to a connected AF_INET or AF_INET6 socket of SOCK_STREAM type. See socket(3SOCKET). The off argument is a pointer to a vari- able holding the input file pointer position from which the data will be read. After sendfile() has completed, the variable will be set to the offset of the byte following the last byte that was read. The sendfile() function does not modify the current file pointer of in_fd, but does modify the file pointer for out_fd if it is a regular file. The sendfile() function can also be used to send buffers by pointing in_fd to SFV_FD_SELF. RETURN VALUES
Upon successful completion, sendfile() returns the total number of bytes written to out_fd and also updates the offset to point to the byte that follows the last byte read. Otherwise, it returns -1, and errno is set to indicate the error. ERRORS
The sendfile() function will fail if: EAFNOSUPPORT The implementation does not support the specified address family for socket. EAGAIN Mandatory file or record locking is set on either the file descriptor or output file descriptor if it points at regular files. O_NDELAY or O_NONBLOCK is set, and there is a blocking record lock. An attempt has been made to write to a stream that cannot accept data with the O_NDELAY or the O_NONBLOCK flag set. EBADF The out_fd or in_fd argument is either not a valid file descriptor, out_fd is not opened for writing. or in_fd is not opened for reading. EINVAL The offset cannot be represented by the off_t structure, or the length is negative when cast to ssize_t. EIO An I/O error occurred while accessing the file system. ENOTCONN The socket is not connected. EOPNOTSUPP The socket type is not supported. EPIPE The out_fd argument is no longer connected to the peer endpoint. EINTR A signal was caught during the write operation and no data was transferred. USAGE
The sendfile() function has a transitional interface for 64-bit file offsets. See lf64(5). EXAMPLES
Example 1 Sending a Buffer Over a Socket The following example demonstrates how to send the buffer buf over a socket. At the end, it prints the number of bytes transferred over the socket from the buffer. It assumes that addr will be filled up appropriately, depending upon where to send the buffer. int tfd; off_t baddr; struct sockaddr_in sin; char buf[64 * 1024]; in_addr_t addr; size_t len; tfd = socket(AF_INET, SOCK_STREAM, 0); if (tfd == -1) { perror("socket"); exit(1); } sin.sin_family = AF_INET; sin.sin_addr.s_addr = addr; /* Fill in the appropriate address. */ sin.sin_port = htons(2345); if (connect(tfd, (struct sockaddr *)&sin, sizeof(sin))<0) { perror("connect"); exit(1); } baddr = (off_t)buf; len = sizeof(buf); while (len > 0) { ssize_t res; res = sendfile(tfd, SFV_FD_SELF, &baddr, len); if (res == -1) if (errno != EINTR) { perror("sendfile"); exit(1); } else continue; len -= res; } Example 2 Transferring Files to Sockets The following program demonstrates a transfer of files to sockets: int ffd, tfd; off_t off; struct sockaddr_in sin; in_addr_t addr; int len; struct stat stat_buf; ssize_t len; ffd = open("file", O_RDONLY); if (ffd == -1) { perror("open"); exit(1); } tfd = socket(AF_INET, SOCK_STREAM, 0); if (tfd == -1) { perror("socket"); exit(1); } sin.sin_family = AF_INET; sin.sin_addr = addr; /* Fill in the appropriate address. */ sin.sin_port = htons(2345); if (connect(tfd, (struct sockaddr *) &sin, sizeof(sin)) <0) { perror("connect"); exit(1); } if (fstat(ffd, &stat_buf) == -1) { perror("fstat"); exit(1); } len = stat_buf.st_size; while (len > 0) { ssize_t res; res = sendfile(tfd, ffd, &off, len); if (res == -1) if (errno != EINTR) { perror("sendfile"); exit(1); } else continue; len -= res; } ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Evolving | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
open(2), libsendfile(3LIB), sendfilev(3EXT), socket(3SOCKET), attributes(5), lf64(5) SunOS 5.11 31 May 2006 sendfile(3EXT)
All times are GMT -4. The time now is 06:27 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy