Go Back   The UNIX and Linux Forums > Operating Systems > Solaris
google site



Solaris The Solaris Operating System, usually known simply as Solaris, is a Unix-based operating system introduced by Sun Microsystems. The Solaris OS is now owned by Oracle.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Display Modes
  #8  
Old 07-10-2008
pupp's Avatar
pupp pupp is offline Forum Advisor  
cap_10hdx 1
 

Join Date: Feb 2008
Location: Jersey Shore
Posts: 647
Thanks: 0
Thanked 0 Times in 0 Posts
might be better in the high level programming forum.
Sponsored Links
  #9  
Old 07-10-2008
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,466
Thanks: 0
Thanked 1 Time in 1 Post
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[])
  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
Registered User
 

Join Date: Sep 2007
Location: Huntsville, Alabama
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
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
  #11  
Old 07-10-2008
ɹǝsn sıɹɐlosuǝdo
 

Join Date: Dec 2007
Location: Paris
Posts: 2,312
Thanks: 0
Thanked 24 Times in 22 Posts
Mine's smaller


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
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,466
Thanks: 0
Thanked 1 Time in 1 Post
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
ɹǝsn sıɹɐlosuǝdo
 

Join Date: Dec 2007
Location: Paris
Posts: 2,312
Thanks: 0
Thanked 24 Times in 22 Posts
You are right, my code relies on recent kstat changes.

It works fine with latest Solaris Express releases.
  #14  
Old 07-11-2008
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,466
Thanks: 0
Thanked 1 Time in 1 Post
I thought as much, but my nevada machine is broken at the moment.
Sponsored Links
Closed Thread

Bookmarks

Tags
linux, solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
interface may off fredginting Solaris 7 05-22-2008 09:50 AM
how i prepare a c++ code(c code) for implementing my own protocol format amitpansuria Programming 1 09-06-2007 11:09 PM
Network interface Qfe, ce, bge ... unclefab Solaris 1 10-30-2006 07:37 AM
SSH key code versus server key code Texan Security 1 04-12-2006 11:57 AM
sco GUI interface qphillips SCO 1 09-27-2005 06:16 PM



All times are GMT -4. The time now is 07:31 AM.