Getting interface names using code


 
Thread Tools Search this Thread
Operating Systems Solaris Getting interface names using code
# 8  
Old 07-10-2008
might be better in the high level programming forum.
# 9  
Old 07-10-2008
There is very little error checking in this, and I have not filtered out the loopback interface.

compiles as is, but required libsocket and libnsl

Code:
cc -lsocket -lnsl -o ifprint ifprint.c


c code:
  1. /*
  2.     ifprint.c - prints active network interfaces
  3. */
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/dlpi.h>
  8. #include <net/if.h>
  9. #include <sys/sockio.h>
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <strings.h>
  14.  
  15. #define SOCKET_AF(af)   (((af) == AF_UNSPEC) ? AF_INET : (af))
  16.  
  17. int main(int argc, char *argv&#91;])
  18. {
  19.         struct lifreq lifr
  20.         int64_t onflags = 0;
  21.         int64_t offflags = 0;
  22.         int setaddr;
  23.         int n;
  24.         int af = AF_INET;
  25.         char *buf;
  26.         int s = socket(SOCKET_AF(af), SOCK_DGRAM, 0);
  27.         struct lifnum lifn;
  28.         struct lifconf lifc;
  29.         struct lifreq *lifrp;
  30.         struct lifreq lifrl;    /* Local lifreq struct */
  31.         int numifs;
  32.         unsigned bufsize;
  33.         int plumball = 0;
  34.         int save_af = af;
  35.         int64_t lifc_flags = LIFC_NOXMIT | LIFC_TEMPORARY | LIFC_ALLZONES;
  36.  
  37.  
  38.         lifn.lifn_family = AF_UNSPEC;
  39.         lifn.lifn_flags = lifc_flags;
  40.         if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
  41.                 /* Could not determine number of interfaces */
  42.                 exit(2);
  43.         }
  44.         numifs = lifn.lifn_count;
  45.  
  46.         bufsize = numifs * sizeof (struct lifreq);
  47.         if ((buf = malloc(bufsize)) == NULL) {
  48.                 /* out of memory */
  49.                 (void) close(s);
  50.                 exit(1);
  51.         }
  52.  
  53.         lifc.lifc_family = AF_UNSPEC;
  54.         lifc.lifc_flags = lifc_flags;
  55.         lifc.lifc_len = bufsize;
  56.         lifc.lifc_buf = buf;
  57.  
  58.         if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
  59.                 free(buf);
  60.                 exit(1);
  61.         }
  62.         lifrp = lifc.lifc_req;
  63.  
  64.         for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifrp++) {
  65.  
  66.  
  67.                 (void) close(s);
  68.                 af = lifrp->lifr_addr.ss_family;
  69.                 s = socket(SOCKET_AF(af), SOCK_DGRAM, 0);
  70.                 if (s == -1) {
  71.                         exit(1);
  72.                 }
  73.                 if (onflags || offflags) {
  74.                         (void) memset(&lifrl, 0, sizeof (lifrl));
  75.                         (void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
  76.                                 sizeof (lifrl.lifr_name));
  77.                         if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifrl) < 0) {
  78.                                 (void) strncpy(lifr.lifr_name,
  79.                                         lifrp->lifr_name, sizeof (lifr.lifr_name));
  80.                                 printf("foreachinterface: SIOCGLIFFLAGS\n");
  81.                                 exit(1);
  82.                         }
  83.                         if ((lifrl.lifr_flags & onflags) != onflags)
  84.                                 continue;
  85.                         if ((~lifrl.lifr_flags & offflags) != offflags)
  86.                                 continue;
  87.                 }
  88.  
  89.                 (void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
  90.                         sizeof (lifrl.lifr_name));
  91.                 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifrl) < 0) {
  92.                         (void) strncpy(lifr.lifr_name,
  93.                         lifrp->lifr_name, sizeof (lifr.lifr_name));
  94.                         printf("foreachinterface: SIOCGLIFADDR\n");
  95.                         continue;
  96.                 }
  97.                 if (lifrl.lifr_addr.ss_family != af) {
  98.                         af = lifrl.lifr_addr.ss_family;
  99.                         (void) close(s);
  100.                         s = socket(SOCKET_AF(af), SOCK_DGRAM, 0);
  101.                         if (s == -1) {
  102.                                 (void) strncpy(lifr.lifr_name,
  103.                                 lifrp->lifr_name,
  104.                                         sizeof (lifr.lifr_name));
  105.                                         printf("socket\n");
  106.                                         exit(1);
  107.                         }
  108.                 }
  109.                 setaddr = 0;
  110.                 /* print the name of the interface */
  111.                 printf("%s\n",  lifrp->lifr_name);
  112.         }
  113.         if (buf != NULL)
  114.                 free(buf);
  115.  
  116. }
# 10  
Old 07-10-2008
Getting interface names using code

Thanks. This looks like something I can use. I only have access to my linux and windows boxes until Monday. I will try it out on Solaris Monday.
Thanks again,
Herb Miller
Smilie
# 11  
Old 07-10-2008
Mine's smaller Smilie

Code:
cc -lkstat  -o knet knet.c

Code:
#include <kstat.h>
#include <string.h>
#include <stdio.h>
int main(int argc, char **argv) {
  kstat_ctl_t *kc;
  kstat_t *ksp;
  kc=kstat_open();
  for(ksp=kc->kc_chain; ksp != NULL; ksp=ksp->ks_next) {
    if((strcmp(ksp->ks_class, "net")==0) && (strcmp(ksp->ks_module, "link")==0)) {
      printf("%s\n", ksp->ks_name);
    }
  }
  kstat_close(kc);
}

# 12  
Old 07-10-2008
Am I missing something here?

Code:
bash-3.00# cc -lkstat  -o knet knet.c
bash-3.00# ./knet
bash-3.00#

I get nothing on Sparc or x86/64, as I would expect. ksp->ks_module concatenated with ksp->ks_instance and compared with ks->ks_name might give you something, but I wouldn't expect ksp->ks_module to ever return "link" as a value it should return the module name eg. bge, hme or whatever the interface type is.


Code:
bash-3.00# ./ifprint
lo0
elxl0

# 13  
Old 07-11-2008
You are right, my code relies on recent kstat changes.

It works fine with latest Solaris Express releases.
# 14  
Old 07-11-2008
I thought as much, but my nevada machine is broken at the moment.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

2. Solaris

Haed code nxge0 nic interface with full duplex & 1000mbps

Hi All I would need to do hard code of nxge0 nic to full duplex & 1000mbps. So that whenever solaris server come up after reboot. it will show full duplex & 1000mbps setting only. Please also let me know how to do it online through command. regards (0 Replies)
Discussion started by: amity
0 Replies

3. UNIX for Dummies Questions & Answers

Adding a network interface to a bonded interface

I have a RHEL 5 system with a bonded interface configure using only one network port (eth0). So I have config file for ifcfg-bond0 and ifcfg-eth. I'd like to configure eth5 to be the second SLAVE in the bond. My question is, after I modify ifcfg-eth5, can I add eth5 to the bond0 interface without... (1 Reply)
Discussion started by: westmoreland
1 Replies

4. IP Networking

Need a bridge from an ethernet interface to a serial interface

This is my situation DOS pc serial cable (sl0) Linux Pc eth1 192.168.0.10 <-------------------->192.168.0.2 <------------>192.168.0.1 (router) I connected the linux pc and the dos pc with a SLIP (serial line internet protocol), so they can communicate in the sl0 interface. ... (3 Replies)
Discussion started by: mghis
3 Replies

5. SCO

Change SCO - GUI or Desktop interface to DOS based interface

Hi all I have installed a demo version of SCO OpenServer 5.0.2, I finally found it is Desktop Interface, I would like to know how to change its interface to dos based interface? If you have any ideas, please tell me then. Thank you (2 Replies)
Discussion started by: TinhNhi
2 Replies

6. Solaris

Command line Interface or GUI Interface not shown on solaris

Dear all, I am a newbie in solaris and I need your advice. I have a Solaris version 5.9 installed on Sunfire V240. I am able to ssh the machine from putty remotely. My problem is that I cannot see the display from KVM switch I have connected to it. I need also to be able to see the GUI... (2 Replies)
Discussion started by: mbouster
2 Replies

7. Shell Programming and Scripting

Searching for file names in a directory while ignoring certain file names

Sun Solaris Unix Question Haven't been able to find any solution for this situation. Let's just say the file names listed below exist in a directory. I want the find command to find all files in this directory but at the same time I want to eliminate certain file names or files with certain... (2 Replies)
Discussion started by: 2reperry
2 Replies

8. Shell Programming and Scripting

Shell Script to display function names (called & defined) in a C++ Source Code

Hello to all, I am looking for a way to display only the names of function (calls & definition) of a C++ source code.There is already a post related to this, but the script is to find the functions using a specific variable, and the replies are not that convincing since they cannot be used for... (2 Replies)
Discussion started by: frozensmilz
2 Replies

9. Programming

How to modify this c code to get full GUI interface for Unix Linux ?

Hi, the following code comes from Unix Linux dialog utility project. Unfortunately, this project is no more actively maintained. Unix Linux dialog utility is made of widget boxes. One of them is mixedgauge.c In the following example I would like to get rid of flickering when run in... (0 Replies)
Discussion started by: jack2
0 Replies
Login or Register to Ask a Question