Operating System - Linux
1832840 Members
3020 Online
110047 Solutions
New Discussion

Re: How to get Mac address via DLPI API

 
SOLVED
Go to solution
hgjsj
Occasional Advisor

How to get Mac address via DLPI API

I developed my project on HPUX ia64 11.v3, how can I get NIC's count and MAC address of each using DLPI API or other method? Thanks!!!!
10 REPLIES 10
Laurent Menase
Honored Contributor

Re: How to get Mac address via DLPI API

system("lanscan");

or putmsg a DL_ATTACH_REQ
then a
DL_INFO_REQ message after attaching to a ppa.
then get the DL_INFO_ACK.
Laurent Menase
Honored Contributor
Solution

Re: How to get Mac address via DLPI API

I took 10min to make an example:
#include
#include
#include
#include

main(c,v)
int c;
char **v;
{
int f;
dl_attach_req_t dar;
dl_info_req_t dir;
struct strbuf ctl;
struct strbuf data;
unsigned char buf[4096];
dl_ok_ack_t *doa=(dl_ok_ack_t*)buf;
dl_info_ack_t *dia=(dl_info_ack_t*)buf;
dl_error_ack_t *dea=(dl_info_ack_t*)buf;
int r;
int fl=0;
int prim;

f=open("/dev/dlpi",O_RDWR);
dar.dl_primitive=DL_ATTACH_REQ;
dar.dl_ppa=atoi(v[1]);;
ctl.buf=(char*)&dar;
ctl.maxlen=ctl.len=sizeof(dar);
putmsg(f,&ctl,0,0);

ctl.buf=(char*)&buf;
ctl.maxlen=ctl.len=sizeof(buf);
data.len=0;
data.maxlen=sizeof(buf);
data.buf=&buf;
fl=RS_HIPRI;
r=getmsg(f,&ctl,&data,&fl);
if(r<0) perror("getmsg");
if((prim=doa->dl_primitive)!=DL_OK_ACK)
{
if (dea->dl_primitive==DL_ERROR_ACK)
printf("erreur: ppa=%d dl_error=%d dl_unix_errno=%d\n",atoi(v[1]),dea->dl_errno,dea->dl_unix_errno);
else
printf("erreur: ppa=%d primitive=%x\n",atoi(v[1]),dea->dl_primitive);
exit(1);
}
dir.dl_primitive=DL_INFO_REQ;
ctl.buf=(char*)&dir;
ctl.maxlen=ctl.len=sizeof(dir);
putmsg(f,&ctl,0,0);
ctl.buf=(char*)&buf;
ctl.maxlen=ctl.len=sizeof(buf);
data.len=data.maxlen=0;
data.buf=0;
fl=0;
r=getmsg(f,&ctl,&data,&fl);
if(r<0) perror("getmsg");
dia=buf;
if(dia->dl_primitive==DL_INFO_ACK)
{
int i;
int l;
int o;
char sep='=';
unsigned char*c;
l=dia->dl_addr_length;
o=dia->dl_addr_offset;
printf("interface %d mac addr",atoi(v[1]));
c=buf+o;
for(i=0;i {
printf("%c%02x",sep,c[i]);
sep=':';
}
printf("\n");
}
else
printf("error! primitive=%d\n",dia->dl_primitive);
}




# cc m.c
# ./a.out 1
interface 1 mac addr=00:17:a4:51:4b:d1
# lanscan |grep lan1
0/1/2/1 0x0017A4514BD1 1 UP lan1 snap1 2 ETHER Yes 119

hgjsj
Occasional Advisor

Re: How to get Mac address via DLPI API

Thanks for your reply! according to the code above, it only get MAC address of lan1, could you tell me how many ethernet interface system installed, I need get all the MAC address of them.
VK2COT
Honored Contributor

Re: How to get Mac address via DLPI API

Hello,

Laurent already provided you with
almost everything.

HP documentation on DLPI programming
discusses various primitives including:

DL_HP_PPA_REQ
This primitive is used to obtain a list of
all the valid PPAs currently installed in
the system.

This message consists of one M_PCPROTO
message block which contains the following
structure.

Format

typedef struct {
u_long dl_primitive;
} dl_hp_ppa_req_t;

...

If your C programming is not efficient,
as a simple workaround you could use
Laurent's program and write a Shell
wrapper for it by using nwmgr(1M) or
lanscan(1m).

An example for lanscan(1m) (I cannot remember the layout for command nwmgr as
I am not at work and do not have HP-UX
11.31 server in front of me:

for PPA in `lanscan -i | awk '{print $1}' | sed -e 's/^lan//g'`
do
./a.out $PPA
done

Cheers,

VK2COT
VK2COT - Dusan Baljevic
hgjsj
Occasional Advisor

Re: How to get Mac address via DLPI API

Thanks both of you, I know Laurent code is right, it is also a good workaroud way using shell scrip to wrapper, but i must use C programming to get PPAs list and get all of the MAC address. Could you give me a sample? many thanks.
hgjsj
Occasional Advisor

Re: How to get Mac address via DLPI API

Thanks, I had fixed this issue.

u_long ctrl_area[LONG_AREA_SIZE];
u_long data_area[LONG_AREA_SIZE];
u_long i ;
int fd;
int ppa;
dl_hp_ppa_req_t *ppa_req = (dl_hp_ppa_req_t *)ctrl_area;
dl_hp_ppa_ack_t *ppa_ack = (dl_hp_ppa_ack_t *)ctrl_area;
dl_hp_ppa_info_t *ppa_info;
dl_error_ack_t *err_ack;
int flags = 0;


struct strbuf ctrl_buf;
struct strbuf data_buf;

ctrl_buf.maxlen = AREA_SIZE;
ctrl_buf.len = 0;
ctrl_buf.buf = (char*)ctrl_area;

data_buf.maxlen = AREA_SIZE;
data_buf.len = 0;
data_buf.buf = (char*)data_area;

/* open the device file */
if((fd = open("/dev/dlpi", O_RDWR)) == -1)
{
bRet = AL_false;
goto _exit;
}

ppa_req->dl_primitive = DL_HP_PPA_REQ;

ctrl_buf.len = sizeof(dl_hp_ppa_req_t);

if(putmsg(fd, &ctrl_buf, 0, 0) < 0)
{
bRet = AL_false;
goto _exit;
}

ctrl_area[0] = 0;

if(getmsg(fd, &ctrl_buf, &data_buf, &flags) < 0)
{
bRet = AL_false;
goto _exit;
}

err_ack = (dl_error_ack_t *)ctrl_area;

if(err_ack->dl_primitive != DL_HP_PPA_ACK)
{
bRet = AL_false;
goto _exit;
}

if(ppa_ack->dl_length == 0)
{
bRet = AL_false;
goto _exit;
}

nicinfoex->count = 0;
for ( i = 0; i < ppa_ack->dl_count; ++i )
{
if ( i == 0 )
{
ppa_info = (dl_hp_ppa_info_t *)((u_char *)ctrl_area + ppa_ack->dl_offset);
}
else
{
ppa_info = (dl_hp_ppa_info_t *)((u_char *)ctrl_area + ppa_ack->dl_offset + ppa_info->dl_next_offset);
}

ppa = ppa_info->dl_ppa;

if (ppa_info->dl_mac_type == DL_ETHER)
{
// Todo:
}
}
Laurent Menase
Honored Contributor

Re: How to get Mac address via DLPI API

Just 1 remark,

You can get the DL_HP_PPA_ACK
with 2 getmsg()
the you call the first one to only get the dl_hppa_ack_t setting max_len to sizeof(dl_hppa_ack_t) on the first call
getmsg will return 1 which means MORECTL
then you can read infos/infos or all the info_acks. mallocing ppa_ack->dl_length bytes before the 2nd getmsg()setting max_len to ppa_ack->dl_length.
it will contains all the rest of the DL_HP_PPA_ACK which contains the ppa infos.
jgobbur
Advisor

Re: How to get Mac address via DLPI API

Is there other way to get by using the ioctl call with proper request?
Laurent Menase
Honored Contributor

Re: How to get Mac address via DLPI API

with ioctl you can only get plumb interfaces - the same as netstat -ni output with a ioctl over a AF_INET/SOCK_RAW
jgobbur
Advisor

Re: How to get Mac address via DLPI API

do you know have any sample code. pls share with me.