Sponsored Content
Top Forums Programming C program : how to list interfaces with a valid IP ? Post 302158687 by brolon on Wednesday 16th of January 2008 05:05:56 AM
Old 01-16-2008
Quote:
Originally Posted by cbkihong
Having an IP address does not mean the routing is good enough to be able to send or receive anything over the interface.

I have been able to do so with getifaddrs() on Linux. Not sure other Unix flavours. Go search for manpage for that function.
Nice function ! That works but I have a problem with the assignment "ifcur = iflist" : error: incompatible types in assignment ?! I don't understand why.

Code:
	
...
struct ifaddrs*		iflist 
,		        ifcur ;
	
if (getifaddrs (&iflist) < 0)
	printf ("* Erreur getifaddrs ! \n") ;

ifcur = iflist ;
	
freeifaddrs(iflist);
...

 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

interfaces

Hello, which network interface i must sellect during the solaris9 installation le0 or hme0 ? this system is part of the network, it is a standalone system and is not on any domain. thanks for your help, em (1 Reply)
Discussion started by: emsakopa
1 Replies

2. Programming

valid code?

hey , everyone. I have a few questions about pieces of code and was wondering if someone could tell me what exactly they did or if they are even valid. bool1 && bool2 || bool3 <---in what order do these get processed? if (! isdigit(c)) <---What does this do? i = j % 3; <---what does this do?... (4 Replies)
Discussion started by: bebop1111116
4 Replies

3. UNIX for Advanced & Expert Users

find: 0652-019 The status on /interfaces/eu3/hmsl/EBS/20070722 is not valid.

I am getting this error when i issue find command. Any advice. Regards, Vishal (0 Replies)
Discussion started by: vishal_ranjan
0 Replies

4. Solaris

list of physical net interfaces

hi, Can I listdown all available net interfaces on my system like SF4800 or Netra440. I know there are 4 port physically present but I can't see or list them using either sysdef -v prtconf -vp prtdiag -v dladm kstat may be I'm missing switched on these or may some other command... (8 Replies)
Discussion started by: busyboy
8 Replies

5. Homework & Coursework Questions

Valid Name

Could someone help me by midnight tonight!!! Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Insert a reference to the Bourne shell as the command... (0 Replies)
Discussion started by: cody007
0 Replies

6. Ubuntu

How to list my program or package that i compile and installed?

Hi I would like to ask in ubuntu or linux on how to list all my package or software the i installed via source code( compile installed in dir default is /usr/local) just like i solaris in which if you installed a package in ur choosing default root installation dir you can just issue a command... (2 Replies)
Discussion started by: jao_madn
2 Replies

7. Shell Programming and Scripting

Selecting files from a list and passing them to a program using csh

I have a list of files, example as shown below n02-z30-dsr65-ndelt0.25-varp0.002-4x3drw-csq.msf n02-z30-dsr65-ndelt0.25-varp0.004-4x3drw-csq.msf n02-z30-dsr65-ndelt0.25-varp0.006-4x3drw-csq.msf n02-z30-dsr65-ndelt0.25-varp0.008-4x3drw-csq.msf... (8 Replies)
Discussion started by: kristinu
8 Replies

8. Solaris

Interfaces and Virtual-interfaces queries

Hi Al, In course of understanding networking in Solaris, I have these doubts on Interfaces. Please clarify me. I have done fair research in this site and others but could not be clarified. 1. In the "ifconfig -a" command, I see many interfaces and their configurations. But I see many... (1 Reply)
Discussion started by: satish51392111
1 Replies

9. Solaris

can't see other interfaces

Hi Guys, I have a Netra240 server with four interfaces. However, when I ran this command dladm show-dev it showed only one interface bge0. Can someone please explain to me how to fix this problem? Thanks guys. (1 Reply)
Discussion started by: cjashu
1 Replies
GETIFADDRS(3)						     Linux Programmer's Manual						     GETIFADDRS(3)

NAME
getifaddrs, freeifaddrs - get interface addresses SYNOPSIS
#include <sys/types.h> #include <ifaddrs.h> int getifaddrs(struct ifaddrs **ifap); void freeifaddrs(struct ifaddrs *ifa); DESCRIPTION
The getifaddrs() function creates a linked list of structures describing the network interfaces of the local system, and stores the address of the first item of the list in *ifap. The list consists of ifaddrs structures, defined as follows: struct ifaddrs { struct ifaddrs *ifa_next; /* Next item in list */ char *ifa_name; /* Name of interface */ unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */ struct sockaddr *ifa_addr; /* Address of interface */ struct sockaddr *ifa_netmask; /* Netmask of interface */ union { struct sockaddr *ifu_broadaddr; /* Broadcast address of interface */ struct sockaddr *ifu_dstaddr; /* Point-to-point destination address */ } ifa_ifu; #define ifa_broadaddr ifa_ifu.ifu_broadaddr #define ifa_dstaddr ifa_ifu.ifu_dstaddr void *ifa_data; /* Address-specific data */ }; The ifa_next field contains a pointer to the next structure on the list, or NULL if this is the last item of the list. The ifa_name points to the null-terminated interface name. The ifa_flags field contains the interface flags, as returned by the SIOCGIFFLAGS ioctl(2) operation (see netdevice(7) for a list of these flags). The ifa_addr field points to a structure containing the interface address. (The sa_family subfield should be consulted to determine the format of the address structure.) This field may contain a NULL pointer. The ifa_netmask field points to a structure containing the netmask associated with ifa_addr, if applicable for the address family. This field may contain a NULL pointer. Depending on whether the bit IFF_BROADCAST or IFF_POINTOPOINT is set in ifa_flags (only one can be set at a time), either ifa_broadaddr will contain the broadcast address associated with ifa_addr (if applicable for the address family) or ifa_dstaddr will contain the destina- tion address of the point-to-point interface. The ifa_data field points to a buffer containing address-family-specific data; this field may be NULL if there is no such data for this interface. The data returned by getifaddrs() is dynamically allocated and should be freed using freeifaddrs() when no longer needed. RETURN VALUE
On success, getifaddrs() returns zero; on error, -1 is returned, and errno is set appropriately. ERRORS
getifaddrs() may fail and set errno for any of the errors specified for socket(2), bind(2), getsockname(2), recvmsg(2), sendto(2), mal- loc(3), or realloc(3). VERSIONS
The getifaddrs() function first appeared in glibc 2.3, but before glibc 2.3.3, the implementation only supported IPv4 addresses; IPv6 sup- port was added in glibc 2.3.3. Support of address families other than IPv4 is only available on kernels that support netlink. CONFORMING TO
Not in POSIX.1-2001. This function first appeared in BSDi and is present on the BSD systems, but with slightly different semantics docu- mented--returning one entry per interface, not per address. This means ifa_addr and other fields can actually be NULL if the interface has no address, and no link-level address is returned if the interface has an IP address assigned. Also, the way of choosing either ifa_broad- addr or ifa_dstaddr differs on various systems. NOTES
The addresses returned on Linux will usually be the IPv4 and IPv6 addresses assigned to the interface, but also one AF_PACKET address per interface containing lower-level details about the interface and its physical layer. In this case, the ifa_data field may contain a pointer to a struct rtnl_link_stats, defined in <linux/if_link.h>, which contains various interface attributes and statistics. EXAMPLE
The program below demonstrates the use of getifaddrs(), freeifaddrs(), and getnameinfo(3). Here is what we see when running this program on one system: $ ./a.out lo address family: 17 (AF_PACKET) eth0 address family: 17 (AF_PACKET) lo address family: 2 (AF_INET) address: <127.0.0.1> eth0 address family: 2 (AF_INET) address: <10.1.1.4> lo address family: 10 (AF_INET6) address: <::1> eth0 address family: 10 (AF_INET6) address: <fe80::2d0:59ff:feda:eb51%eth0> Program source #include <arpa/inet.h> #include <sys/socket.h> #include <netdb.h> #include <ifaddrs.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(int argc, char *argv[]) { struct ifaddrs *ifaddr, *ifa; int family, s; char host[NI_MAXHOST]; if (getifaddrs(&ifaddr) == -1) { perror("getifaddrs"); exit(EXIT_FAILURE); } /* Walk through linked list, maintaining head pointer so we can free list later */ for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { if (ifa->ifa_addr == NULL) continue; family = ifa->ifa_addr->sa_family; /* Display interface name and family (including symbolic form of the latter for the common families) */ printf("%s address family: %d%s ", ifa->ifa_name, family, (family == AF_PACKET) ? " (AF_PACKET)" : (family == AF_INET) ? " (AF_INET)" : (family == AF_INET6) ? " (AF_INET6)" : ""); /* For an AF_INET* interface address, display the address */ if (family == AF_INET || family == AF_INET6) { s = getnameinfo(ifa->ifa_addr, (family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); if (s != 0) { printf("getnameinfo() failed: %s ", gai_strerror(s)); exit(EXIT_FAILURE); } printf(" address: <%s> ", host); } } freeifaddrs(ifaddr); exit(EXIT_SUCCESS); } SEE ALSO
bind(2), getsockname(2), socket(2), packet(7), ifconfig(8) COLOPHON
This page is part of release 3.44 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. GNU
2012-07-07 GETIFADDRS(3)
All times are GMT -4. The time now is 12:54 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy