Sponsored Content
Full Discussion: SIOCSARP: Invalid Argument.
Top Forums Programming SIOCSARP: Invalid Argument. Post 302447674 by semash! on Tuesday 24th of August 2010 03:13:47 AM
Old 08-24-2010
Hey fpmurphy, thanks for your quick reply. I made some changes in the code and got it working.

I think the errors were on mac_aton() function, the way i used the flags and in the lack of a defined device (arp_dev[16] in the arpreq structure).

With all of that changed, this is the working code, i think it would be of help for someone:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <net/if.h>
#include <errno.h>
#include <net/if_arp.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <sys/socket.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);
} /* End of mac_ntoa */

int mac_aton(char *addr, unsigned char *ptr){
int i, v[6];
if((i = sscanf(addr, "%x:%x:%x:%x:%x:%x", &v[0], &v[1], &v[2], &v[3],
&v[4], &v[5])) !=6){
fprintf(stderr, "arp: invalid Ethernet address '%s'\n", addr);
return(1);
        } /* End of If*/
for(i = 0; i < 6; i++){
ptr[i] = v[i];
        } /* End of For */
return(0);
}

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

if(argc < 3 || argc > 4){
fprintf(stderr, "usage: %s <ip_addr> <hw_addr> [temp|pub|perm|trail]\n",
argv[0]);
fprintf(stderr, "default: temp.\n");
exit(-1);
        } /* End of If */

int s, flags;
char *host = argv[1];

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

bzero((caddr_t)&req, sizeof(req)); /* caddr_t is not really needed. */

sin = (struct sockaddr_in *)&req.arp_pa;
sin->sin_family = AF_INET;
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);
        } /* End of If */
bcopy((char *)hp->h_addr, (char *)&sin->sin_addr, sizeof(sin->sin_addr));
        } /* End of If */

if(mac_aton(argv[2], req.arp_ha.sa_data)){ /* If address is valid... */
return(-1);
}

argc -=2;
argv +=2;

flags = ATF_PERM | ATF_COM;

while(argc-- > 0){
if(!(strncmp(argv[0], "temp", 4))){
flags &= ~ATF_PERM;
        } else if(!(strncmp(argv[0], "pub", 3))){
flags |= ATF_PUBL;
        } else if(!(strncmp(argv[0], "trail", 5))){
flags |= ATF_USETRAILERS;
        } else if(!(strncmp(argv[0], "dontpub", 7))){ /* Not working yet */
flags |= ATF_DONTPUB;
        } else if(!(strncmp(argv[0], "perm", 4))){
flags = ATF_PERM;
        } else {
flags &= ~ATF_PERM;
        } /* End of Else*/
argv++;
        }/* End of While */

req.arp_flags = flags; /* Finally, asign the flags to the structure */
strcpy(req.arp_dev, "eth0"); /* Asign the device.  */

if((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
perror("socket() failed.");
exit(-1);
        } /* End of If */

if(ioctl(s, SIOCSARP, (caddr_t)&req) <0){ /* caddr_t not really needed. */
perror(host);
exit(-1);
        } /* End of If */

printf("ARP cache entry successfully added.\n");
close(s);
return(0);
}

Thank you very much for your help. Smilie

Last edited by semash!; 08-25-2010 at 01:18 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

stty: : Invalid argument

Hello Everyone; I have a script that is throwing the following message: stty: : Invalid argument The line that gives the message is the following, sailormoon$ scp home/voice.xml newwave@silvermoon:/newwave/config/radius stty: : Invalid argument voice.xml | ... (2 Replies)
Discussion started by: tony3101
2 Replies

2. Solaris

ps: 65535 is an invalid non-numeric argument for -p option

I want to figure out what is the reason of error message I have in Solaris 10. Why Solaris 10 dosn't recognize 65535? ps: 65535 is an invalid non-numeric argument for -p option usage: ps 'format' is one or more of: Thank you (5 Replies)
Discussion started by: gogogo
5 Replies

3. Programming

error "Invalid argument" returned after call sched_setscheduler

the code is below and the was run on Solaris 9. ----------------------------- struct sched_param param; param.sched_priority = 99; if(sched_setscheduler(0, SCHED_RR, &param) == -1) { perror("setting priority"); exit(1); } ------------------------------- after the... (1 Reply)
Discussion started by: robin.zhu
1 Replies

4. UNIX for Dummies Questions & Answers

msgrcv : Invalid argument

Hi All, Please guide me how to get rid : msgrcv : Invalid argument. I am using message queues: msgsnd and msgrcv, I am able to send through msgsnd and receive through msgrcv, but at times i get the belo error. msgrcv : Invalid argument. (1 Reply)
Discussion started by: answers
1 Replies

5. Solaris

Invalid Argument and glassfish

I tried to install glassfish on Solaris 10 and it worked fine on other instances. I got the below message bash-3.00# ./sjsas-9_1_01-solaris-sparc.bin -console bash: ./sjsas-9_1_01-solaris-sparc.bin: Invalid argument I logged on as root and the file has execute permission. So strange. Do... (1 Reply)
Discussion started by: Andrew2008
1 Replies

6. IP Networking

sendto invalid argument

Hi I lost a lot of time in understanding the message "sendto Invalid argument" when I execute the following code. This code is a simple UDP sender improved with some reliability feature. My goal is to send a file. I've reported only the code which may be useful. Can anyone help me? Thank you... (0 Replies)
Discussion started by: Puntino
0 Replies

7. UNIX for Dummies Questions & Answers

Need FIX for: RTNETLINK answers: Invalid argument

OK...I'm using the latest version of Fedora 10. My network connection was working fine, and I had several network LAN shares on my desktop. Then I rebooted the system without dismounting those shares first. ooops. <:( ...When the system came back up, my network connection was gone. All... (2 Replies)
Discussion started by: Pudnik
2 Replies

8. UNIX for Advanced & Expert Users

invalid argument in semctl()

When I am using the function semctl() it is giving me error as the INVALID ARGUMENT. Can any body give me the possible reasons??? (2 Replies)
Discussion started by: asimibm
2 Replies

9. UNIX for Advanced & Expert Users

NFS : Invalid argument (Remote share mounting issue)

Hi Guru's, I am unable to mount NFS share on unix system (DG/UX) which is nfs client. Error: mount: /nfsshare: Invalid argument mount: giving up on: /mountpoint i tried following command mount -t nfs remotehost:/nfsshare /mountpoint Error: (5 Replies)
Discussion started by: Justin John
5 Replies

10. UNIX for Beginners Questions & Answers

Mount error in Linux: invalid argument

hi i have an android phone which i think is bricked, so wanted to see what the actual issue is, I flashed it with TWRP recovery image and launched terminal i, was able understand that mount is not happening, it says invalid argument and no such file or directory. Below is what i get while... (32 Replies)
Discussion started by: nanz143
32 Replies
getipnodebyname(3SOCKET)				     Sockets Library Functions					  getipnodebyname(3SOCKET)

NAME
getipnodebyname, getipnodebyaddr, freehostent - get IP node entry SYNOPSIS
cc [ flag... ] file... -lsocket -lnsl [ library... ] #include <sys/socket.h> #include <netdb.h> struct hostent *getipnodebyname(const char *name, int af, int flags, int *error_num); struct hostent *getipnodebyaddr(const void *src, size_t len, int af, int *error_num); void freehostent(struct hostent *ptr); PARAMETERS
af Address family flags Various flags name Name of host error_num Error storage src Address for lookup len Length of address ptr Pointer to hostent structure DESCRIPTION
The getipnodebyname() function searches the ipnodes database from the beginning. The function finds the first h_name member that matches the hostname specified by name. The function takes an af argument that specifies the address family. The address family can be AF_INET for IPv4 addresses or AF_INET6 for IPv6 addresses. The flags argument determines what results are returned based on the value of flags. If the flags argument is set to 0 (zero), the default operation of the function is specified as follows: o If the af argument is AF_INET, a query is made for an IPv4 address. If successful, IPv4 addresses are returned and the h_length member of the hostent structure is 4. Otherwise, the function returns a NULL pointer. o If the af argument is AF_INET6, a query is made for an IPv6 address. If successful, IPv6 addresses are returned and the h_length mem- ber of the hostent structure is 16. Otherwise, the function returns a NULL pointer. The flags argument changes the default actions of the function. Set the flags argument with a logical OR operation on any of combination of the following values: AI_V4MAPPED AI_ALL AI_ADDRCONFIG The special flags value, AI_DEFAULT, should handle most applications. Porting simple applications to use IPv6 replaces the call hptr = gethostbyname(name); with hptr = getipnodebyname(name, AF_INET6, AI_DEFAULT, &error_num); The flags value 0 (zero) implies a strict interpretation of the af argument: o If flags is 0 and af is AF_INET, the caller wants only IPv4 addresses. A query is made for A records. If successful, IPv4 addresses are returned and the h_length member of the hostent structure is 4. Otherwise, the function returns a NULL pointer. o If flags is 0 and af is AF_INET6, the caller wants only IPv6 addresses. A query is made for AAAA records. If successful, IPv6 addresses are returned and the h_length member of the hostent structure is 16. Otherwise, the function returns a NULL pointer. Logically OR other constants into the flags argument to modify the behavior of the getipnodebyname() function. o If the AI_V4MAPPED flag is specified with af set to AF_INET6, the caller can accept IPv4-mapped IPv6 addresses. If no AAAA records are found, a query is made for A records. Any A records found are returned as IPv4-mapped IPv6 addresses and the h_length is 16. The AI_V4MAPPED flag is ignored unless af equals AF_INET6. o The AI_ALL flag is used in conjunction with the AI_V4MAPPED flag, exclusively with the IPv6 address family. When AI_ALL is logically ORed with AI_V4MAPPED flag, the caller wants all addresses: IPv6 and IPv4-mapped IPv6 addresses. A query is first made for AAAA records and, if successful, IPv6 addresses are returned. Another query is then made for A records. Any A records found are returned as IPv4-mapped IPv6 addresses and the h_length is 16. Only when both queries fail does the function return a NULL pointer. The AI_ALL flag is ignored unless af is set to AF_INET6. o The AI_ADDRCONFIG flag specifies that a query for AAAA records should occur only when the node is configured with at least one IPv6 source address. A query for A records should occur only when the node is configured with at least one IPv4 source address. For exam- ple, if a node is configured with no IPv6 source addresses, af equals AF_INET6, and the node name queried has both AAAA and A records, then: o A NULL pointer is returned when only the AI_ADDRCONFIG value is specified. o The A records are returned as IPv4-mapped IPv6 addresses when the AI_ADDRCONFIG and AI_V4MAPPED values are specified. The special flags value, AI_DEFAULT, is defined as #define AI_DEFAULT (AI_V4MAPPED | AI_ADDRCONFIG) The getipnodebyname() function allows the name argument to be a node name or a literal address string: a dotted-decimal IPv4 address or an IPv6 hex address. Applications do not have to call inet_pton(3SOCKET) to handle literal address strings. Four scenarios arise based on the type of literal address string and the value of the af argument. The two simple cases occur when name is a dotted-decimal IPv4 address and af equals AF_INET and when name is an IPv6 hex address and af equals AF_INET6. The members of the returned hostent structure are: h_name Pointer to a copy of the name argument h_aliases NULL pointer. h_addrtype Copy of the af argument. h_length 4 for AF_INET or 16 for AF_INET6. h_addr_list Array of pointers to 4-byte or 16-byte binary addresses. The array is terminated by a NULL pointer. RETURN VALUES
Upon successful completion, getipnodebyname() and getipnodebyaddr() return a hostent structure. Otherwise they return NULL. The hostent structure does not change from the existing definition when used with gethostbyname(3NSL). For example, host entries are repre- sented by the struct hostent structure defined in <netdb.h>: struct hostent { char *h_name; /* canonical name of host */ char **h_aliases; /* alias list */ int h_addrtype; /* host address type */ int h_length; /* length of address */ char **h_addr_list; /* list of addresses */ }; An error occurs when name is an IPv6 hex address and af equals AF_INET. The return value of the function is a NULL pointer and error_num equals HOST_NOT_FOUND. The getipnodebyaddr() function has the same arguments as the existing gethostbyaddr(3NSL) function, but adds an error number. As with getipnodebyname(), getipnodebyaddr() is thread-safe. The error_num value is returned to the caller with the appropriate error code to sup- port thread-safe error code returns. The following error conditions can be returned for error_num: HOST_NOT_FOUND Host is unknown. NO_DATA No address is available for the name specified in the server request. This error is not a soft error. Another type of name server request might be successful. NO_RECOVERY An unexpected server failure occurred, which is a non-recoverable error. TRY_AGAIN This error is a soft error that indicates that the local server did not receive a response from an authori- tative server. A retry at some later time might be successful. One possible source of confusion is the handling of IPv4-mapped IPv6 addresses and IPv4-compatible IPv6 addresses, but the following logic should apply: 1. If af is AF_INET6, and if len equals 16, and if the IPv6 address is an IPv4-mapped IPv6 address or an IPv4-compatible IPv6 address, then skip over the first 12 bytes of the IPv6 address, set af to AF_INET, and set len to 4. 2. If af is AF_INET, lookup the name for the given IPv4 address. 3. If af is AF_INET6, lookup the name for the given IPv6 address. 4. If the function is returning success, then the single address that is returned in the hostent structure is a copy of the first argument to the function with the same address family that was passed as an argument to this function. All four steps listed are performed in order. This structure, and the information pointed to by this structure, are dynamically allocated by getipnodebyname() and getipnodebyaddr(). The freehostent() function frees this memory. EXAMPLES
Example 1: Getting the Canonical Name, Aliases, and Internet IP Addresses for a Given Hostname The following is a sample program that retrieves the canonical name, aliases, and all Internet IP addresses, both version 6 and version 4, for a given hostname. #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> main(int argc, const char **argv) { char abuf[INET6_ADDRSTRLEN]; int error_num; struct hostent *hp; char **p; if (argc != 2) { (void) printf("usage: %s hostname0, argv[0]); exit(1); } /* argv[1] can be a pointer to a hostname or literal IP address */ hp = getipnodebyname(argv[1], AF_INET6, AI_ALL | AI_ADDRCONFIG | AI_V4MAPPED, &error_num); if (hp == NULL) { if (error_num == TRY_AGAIN) { printf("%s: unknown host or invalid literal address " "(try again later)0, argv[1]); } else { printf("%s: unknown host or invalid literal address0, argv[1]); } exit(1); } for (p = hp->h_addr_list; *p != 0; p++) { struct in6_addr in6; char **q; bcopy(*p, (caddr_t)&in6, hp->h_length); (void) printf("%s%s", inet_ntop(AF_INET6, (void *)&in6, abuf, sizeof(abuf)), hp->h_name); for (q = hp->h_aliases; *q != 0; q++) (void) printf(" %s", *q); (void) putchar(''); } freehostent(hp); exit(0); } ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Evolving | +-----------------------------+-----------------------------+ |MT-Level |Safe | +-----------------------------+-----------------------------+ SEE ALSO
getaddrinfo(3SOCKET), gethostbyname(3NSL), htonl(3SOCKET), inet(3SOCKET), netdb.h(3HEAD), hosts(4), ipnodes(4), nsswitch.conf(4), attributes(5) NOTES
No enumeration functions are provided for IPv6. Existing enumeration functions such as sethostent(3NSL) do not work in combination with the getipnodebyname() and getipnodebyaddr() functions. All the functions that return a struct hostent must always return the canonical in the h_name field. This name, by definition, is the well- known and official hostname shared between all aliases and all addresses. The underlying source that satisfies the request determines the mapping of the input name or address into the set of names and addresses in hostent. Different sources might make such as determination in different ways. If more than one alias and more than one address in hostent exist, no pairing is implied between the alias and address. The current implementations of these functions return or accept only addresses for the Internet address family (type AF_INET) or the Inter- net address family Version 6 (type AF_INET6). The form for an address of type AF_INET is a struct in_addr defined in <netinet/in.h>. The form for an address of type AF_INET6 is a struct in6_addr, also defined in <netinet/in.h>. The functions described in inet_ntop(3SOCKET) and inet_pton(3SOCKET) that are illustrated in the EXAMPLES section are helpful in constructing and manipulating addresses in either of these forms. SunOS 5.10 5 Apr 2004 getipnodebyname(3SOCKET)
All times are GMT -4. The time now is 08:29 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy