socket programming

In the work of porting and modifing some code to retrive extended tags
on
a dhcp server; I have tried to compiled bootpc from the linux sources.
It looks (from the header files) that there is no way to get the
hardware
address to use for broadcasting.

The structure that seems to be missing in inet/if.h is
ifr_hwaddr.

Is there some other location for this information or was this not
included?
Also the ioctl define for SIOCGIFHWADDR is missing.

I know that dhcp.client does do this but the includes that come with the

CD don’t show that I can.

“Igor Kovalenko” <Igor.Kovalenko@motorola.com> wrote in message
news:3A5D1C27.F159C454@motorola.com

How do you know dhcp.client does this? As far as I know, setting up
broadcast address is not supported.

Larry Brigman wrote:

In the work of porting and modifing some code to retrive extended tags
on
a dhcp server; I have tried to compiled bootpc from the linux sources.
It looks (from the header files) that there is no way to get the
hardware
address to use for broadcasting.

Hardware address to use for broadcasting???
Why would you need a “hardware address” to broadcast.
Unless I’m missing something.

The structure that seems to be missing in inet/if.h is
ifr_hwaddr.

Is there some other location for this information or was this not
included?
Also the ioctl define for SIOCGIFHWADDR is missing.

I know that dhcp.client does do this but the includes that come with the

CD don’t show that I can.

How do you know dhcp.client does this? As far as I know, setting up
broadcast address is not supported.

Larry Brigman wrote:

In the work of porting and modifing some code to retrive extended tags
on
a dhcp server; I have tried to compiled bootpc from the linux sources.
It looks (from the header files) that there is no way to get the
hardware
address to use for broadcasting.

The structure that seems to be missing in inet/if.h is
ifr_hwaddr.

Is there some other location for this information or was this not
included?
Also the ioctl define for SIOCGIFHWADDR is missing.

I know that dhcp.client does do this but the includes that come with the

CD don’t show that I can.

Mario Charest wrote:

“Igor Kovalenko” <> Igor.Kovalenko@motorola.com> > wrote in message
news:> 3A5D1C27.F159C454@motorola.com> …
How do you know dhcp.client does this? As far as I know, setting up
broadcast address is not supported.

Larry Brigman wrote:

In the work of porting and modifing some code to retrive extended tags
on
a dhcp server; I have tried to compiled bootpc from the linux sources.
It looks (from the header files) that there is no way to get the
hardware
address to use for broadcasting.

Hardware address to use for broadcasting???
Why would you need a “hardware address” to broadcast.
Unless I’m missing something.

The dhcp protocol requires that the hardware (MAC) address is in the data
section of the packet.

The structure that seems to be missing in inet/if.h is
ifr_hwaddr.

Is there some other location for this information or was this not
included?
Also the ioctl define for SIOCGIFHWADDR is missing.

I know that dhcp.client does do this but the includes that come with the

CD don’t show that I can.

Igor Kovalenko wrote:

How do you know dhcp.client does this? As far as I know, setting up
broadcast address is not supported.

Because I captured the dhcp packet request from my machine
with a network analyzer and it had the hardware (MAC) address
in the data section of the packet.

Larry Brigman wrote:

In the work of porting and modifing some code to retrive extended tags
on
a dhcp server; I have tried to compiled bootpc from the linux sources.
It looks (from the header files) that there is no way to get the
hardware
address to use for broadcasting.

The structure that seems to be missing in inet/if.h is
ifr_hwaddr.

Is there some other location for this information or was this not
included?
Also the ioctl define for SIOCGIFHWADDR is missing.

I know that dhcp.client does do this but the includes that come with the

CD don’t show that I can.

“Larry Brigman” <lbrigman@fcpa.fujitsu.com> wrote in message
news:3A5DD962.EC1ADFCA@fcpa.fujitsu.com

Igor Kovalenko wrote:

How do you know dhcp.client does this? As far as I know, setting up
broadcast address is not supported.

Because I captured the dhcp packet request from my machine
with a network analyzer and it had the hardware (MAC) address
in the data section of the packet.

And that tells you what? Clients send their MAC address because server may
use it to assign a certain IP address (which diskless client can’t store
away).

I’m not sure what you mean by ‘hardware address to use for broadcasting’.
The only broadcast address I know which might be set by DHCP is IP broadcast
address. According to docs NTO dhcp client does not support setting that.

  • igor

Larry Brigman wrote:

In the work of porting and modifing some code to retrive extended tags
on
a dhcp server; I have tried to compiled bootpc from the linux sources.
It looks (from the header files) that there is no way to get the
hardware
address to use for broadcasting.

The structure that seems to be missing in inet/if.h is
ifr_hwaddr.

Is there some other location for this information or was this not
included?
Also the ioctl define for SIOCGIFHWADDR is missing.

I know that dhcp.client does do this but the includes that come with
the

CD don’t show that I can.

Igor Kovalenko wrote:

“Larry Brigman” <> lbrigman@fcpa.fujitsu.com> > wrote in message
news:> 3A5DD962.EC1ADFCA@fcpa.fujitsu.com> …
Igor Kovalenko wrote:

How do you know dhcp.client does this? As far as I know, setting up
broadcast address is not supported.

Because I captured the dhcp packet request from my machine
with a network analyzer and it had the hardware (MAC) address
in the data section of the packet.


And that tells you what? Clients send their MAC address because server may
use it to assign a certain IP address (which diskless client can’t store
away).

I’m not sure what you mean by ‘hardware address to use for broadcasting’.
The only broadcast address I know which might be set by DHCP is IP broadcast
address. According to docs NTO dhcp client does not support setting that.

I guess broadcasting is the wrong term. I need the MAC address of the
card to include in the data section of a dhcp/bootp packet.

In digging through the include files the ifru_hwaddr structure is not defined
in the files, which is where I would get this information.

If this type of coding is different from other version of UNIX that support
the BSD api then I need to know how it is different to support my own
requirements.

Larry Brigman wrote:

In the work of porting and modifing some code to retrive extended tags
on
a dhcp server; I have tried to compiled bootpc from the linux sources.
It looks (from the header files) that there is no way to get the
hardware
address to use for broadcasting.

The structure that seems to be missing in inet/if.h is
ifr_hwaddr.

Is there some other location for this information or was this not
included?
Also the ioctl define for SIOCGIFHWADDR is missing.

I know that dhcp.client does do this but the includes that come with
the

CD don’t show that I can.

You’re saying you need to extract your own MAC address? I guess Sean can
comment on that.

Larry Brigman wrote:

Igor Kovalenko wrote:

“Larry Brigman” <> lbrigman@fcpa.fujitsu.com> > wrote in message
news:> 3A5DD962.EC1ADFCA@fcpa.fujitsu.com> …
Igor Kovalenko wrote:

How do you know dhcp.client does this? As far as I know, setting up
broadcast address is not supported.

Because I captured the dhcp packet request from my machine
with a network analyzer and it had the hardware (MAC) address
in the data section of the packet.


And that tells you what? Clients send their MAC address because server may
use it to assign a certain IP address (which diskless client can’t store
away).

I’m not sure what you mean by ‘hardware address to use for broadcasting’.
The only broadcast address I know which might be set by DHCP is IP broadcast
address. According to docs NTO dhcp client does not support setting that.

I guess broadcasting is the wrong term. I need the MAC address of the
card to include in the data section of a dhcp/bootp packet.

In digging through the include files the ifru_hwaddr structure is not defined
in the files, which is where I would get this information.

If this type of coding is different from other version of UNIX that support
the BSD api then I need to know how it is different to support my own
requirements.



Larry Brigman wrote:

In the work of porting and modifing some code to retrive extended tags
on
a dhcp server; I have tried to compiled bootpc from the linux sources.
It looks (from the header files) that there is no way to get the
hardware
address to use for broadcasting.

The structure that seems to be missing in inet/if.h is
ifr_hwaddr.

Is there some other location for this information or was this not
included?
Also the ioctl define for SIOCGIFHWADDR is missing.

I know that dhcp.client does do this but the includes that come with
the

CD don’t show that I can.

Igor Kovalenko <Igor.Kovalenko@motorola.com> wrote:
: You’re saying you need to extract your own MAC address? I guess Sean can
: comment on that.

Sure:

Here are 2 methods, the first more BSD’ish, the second more NTO’ish:

-seanb


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ioctl.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <netinet/in.h>


int main (int argc, char **argv)
{
int s, i;
char buf[4096], *cplim;
struct ifconf ifc;
struct ifreq *ifr;
struct sockaddr_dl *sdl;

if(argc < 2)
{
fprintf (stderr, “Must specify interface.\n”);
exit (1);
}

if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror (“socket”);
exit(1);
}

ifc.ifc_len = sizeof (buf);
ifc.ifc_buf = buf;

if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0)
{
perror (“SIOCGIFCONF”);
exit(1);
}
ifr = ifc.ifc_req;

cplim = (char *)ifr + ifc.ifc_len;

for(; (char )ifr <cplim; ifr= (struct ifreq)((char *)ifr + ifr->ifr_addr.sa_len + IFNAMSIZ))
{
if(strcmp(ifr->ifr_name, argv[1]) || ifr->ifr_addr.sa_family != AF_LINK)
continue;

sdl = (struct sockaddr_dl *)&ifr->ifr_addr;

if(sdl->sdl_type != IFT_ETHER)
continue;

printf("%s: ", argv[1]);

for(i = 0; i < sdl->sdl_alen; i++)
printf("%#x ", (unsigned char)sdl->sdl_data[sdl->sdl_nlen + i]);

printf("\n");
exit(EXIT_SUCCESS);

}

fprintf (stderr, “%s: not found\n”, argv[1]);
exit(1);
}


#include <sys/dcmd_io-net.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>

int
main(void)
{
int fd;
Nic_t nic;

if((fd = open("/dev/io-net/en0", O_RDONLY)) == -1)
{
perror(“open”);
return 1;
}

if(errno = devctl(fd, DCMD_IO_NET_NICINFO, &nic, sizeof( Nic_t ), NULL ))
{
perror(“devctl”);
return 1;
}

printf("%02X%02X%02X %02X%02X%02X\n",
nic.current_address[0], nic.current_address[1],
nic.current_address[2], nic.current_address[3],
nic.current_address[4], nic.current_address[5] );

}


: Larry Brigman wrote:
:>
:> Igor Kovalenko wrote:
:>
:> > “Larry Brigman” <lbrigman@fcpa.fujitsu.com> wrote in message
:> > news:3A5DD962.EC1ADFCA@fcpa.fujitsu.com
:> > > Igor Kovalenko wrote:
:> > >
:> > > > How do you know dhcp.client does this? As far as I know, setting up
:> > > > broadcast address is not supported.
:> > >
:> > > Because I captured the dhcp packet request from my machine
:> > > with a network analyzer and it had the hardware (MAC) address
:> > > in the data section of the packet.
:> > >
:> >
:> > And that tells you what? Clients send their MAC address because server may
:> > use it to assign a certain IP address (which diskless client can’t store
:> > away).
:> >
:> > I’m not sure what you mean by ‘hardware address to use for broadcasting’.
:> > The only broadcast address I know which might be set by DHCP is IP broadcast
:> > address. According to docs NTO dhcp client does not support setting that.
:>
:> I guess broadcasting is the wrong term. I need the MAC address of the
:> card to include in the data section of a dhcp/bootp packet.
:>
:> In digging through the include files the ifru_hwaddr structure is not defined
:> in the files, which is where I would get this information.
:>
:> If this type of coding is different from other version of UNIX that support
:> the BSD api then I need to know how it is different to support my own
:> requirements.
:>
:> > > >
:> > > >
:> > > > Larry Brigman wrote:
:> > > > >
:> > > > > In the work of porting and modifing some code to retrive extended tags
:> > > > > on
:> > > > > a dhcp server; I have tried to compiled bootpc from the linux sources.
:> > > > > It looks (from the header files) that there is no way to get the
:> > > > > hardware
:> > > > > address to use for broadcasting.
:> > > > >
:> > > > > The structure that seems to be missing in inet/if.h is
:> > > > > ifr_hwaddr.
:> > > > >
:> > > > > Is there some other location for this information or was this not
:> > > > > included?
:> > > > > Also the ioctl define for SIOCGIFHWADDR is missing.
:> > > > >
:> > > > > I know that dhcp.client does do this but the includes that come with
:> > the
:> > > > >
:> > > > > CD don’t show that I can.
:> > >

Sean Boudreau wrote:

Igor Kovalenko <> Igor.Kovalenko@motorola.com> > wrote:
: You’re saying you need to extract your own MAC address? I guess Sean can
: comment on that.

Sure:

Here are 2 methods, the first more BSD’ish, the second more NTO’ish:

Thanks that seem to get the information that I needed.

Now, I think that there is some options that need to be set other than
what I have for setsocketopt() to allow broadcast when the network
address is all zero’s? I get an error from sendto() “no route to host”
Maybe some flag(s) to sendto?

My current setsocketopt() is:
setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&one,sizeof(one))
bind():
cli_addr.sin_family = AF_INET;
cli_addr.sin_addr.s_addr = htonl(INADDR_ANY);
cli_addr.sin_port = htons(IPPORT_BOOTPC);

(bind(sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr))

sendto:
if(sendto(sockfd, bootp_xmit, sizeof(struct bootp), 0,
(struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {

server info:
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_NONE) ;
serv_addr.sin_port = htons(IPPORT_BOOTPS);

Larry Brigman <lbrigman@fcpa.fujitsu.com> wrote:


Sean Boudreau wrote:

Igor Kovalenko <> Igor.Kovalenko@motorola.com> > wrote:
: You’re saying you need to extract your own MAC address? I guess Sean can
: comment on that.

Sure:

Here are 2 methods, the first more BSD’ish, the second more NTO’ish:


Thanks that seem to get the information that I needed.

Now, I think that there is some options that need to be set other than
what I have for setsocketopt() to allow broadcast when the network
address is all zero’s? I get an error from sendto() “no route to host”
Maybe some flag(s) to sendto?

My current setsocketopt() is:
setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&one,sizeof(one))
bind():
cli_addr.sin_family = AF_INET;
cli_addr.sin_addr.s_addr = htonl(INADDR_ANY);
cli_addr.sin_port = htons(IPPORT_BOOTPC);

(bind(sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr))

sendto:
if(sendto(sockfd, bootp_xmit, sizeof(struct bootp), 0,
(struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {

server info:
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_NONE) ;
serv_addr.sin_port = htons(IPPORT_BOOTPS);

You will need to define a route for the limited broadcast address. You probably
want to use INADDR_BROADCAST instead of NONE, but they are the same value in
the header. Keep the setsockopt() for SO_BROADCAST. You will specify this
route as destination 255.255.255.0 with gateway 0.0.0.0 which is the
ipaddress of your interface. You will need to do the same with the full stack.
The way this differs is the way you set the route. The tiny stack uses route
ioctls. The full stack uses routing sockets. The route ioctls were later added
to the full stack, so they should work for both. Once your ipaddress is obtained
you will need to delete this route before applying your new ipaddress, gateway,
etc. later versions of the full stack will make use of the BINDTODEVICE ioctl
to avoid setting the route but this version has not shipped yet.

You will need to fill in the ortentry(net/route.h) structure with the dst, gateway
and netmask. Flags should be set to zero for this purpose. If you were
defining a route to a gateway, you would add the RTF_GATEWAY | RTF_UP flag.
You would pass this structure with either the SIOCADDRT or SIOCDELRT
ioctl. All other entries in the structure should be set to zero.

There will be a dhcp.client in the next patch that dumps all tags returned
from the server along with the ip configuration to a script.

struct ortentry rt;
int sock;
int rc;
struct sockaddr_in *rt_dst;
struct sockaddr_in *rt_gate;
struct sockaddr_in *rt_netmask;

sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock < 0){
return(sock);
}

memset(&rt,0,sizeof(rt));

rt_dst=(struct sockaddr_in *)&rt.rt_dst;
rt_gate=(struct sockaddr_in *)&rt.rt_gateway;
rt_netmask=(struct sockaddr_in *)&rt.rt_netmask;

rt_dst->sin_family=rt_gate->sin_family=rt_netmask->sin_family=AF_INET;
rt_dst->sin_len=sizeof(*rt_dst);
rt_dst->sin_addr.s_addr=htonl(dst);
rt_gate->sin_len=sizeof(*rt_gate);
rt_gate->sin_addr.s_addr=htonl(gate);
rt_netmask->sin_len=sizeof(*rt_netmask);
rt_netmask->sin_addr.s_addr=htonl(netmask);
rt.rt_flags=flags;

rc = ioctl(sock, cmd, &rt); /SIOCADDRT or SIOCDELRT/

close(sock);


Dave