Hi all,
I wanted a program to get the broadcast address of an
interface. I wanted for selected interface. I followed the iproute
source code to write it. How ever i find that iam not able to get the
bcast address for that particular interface. If i use NLM_F_ROOT i get
all info.
I have pasted my code. Its roughly done. Could someone point me how to
get broadcast address also for particular interface?
Regards,
Varun
#include <errno.h>
#include <error.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#define NIPQUAD(addr) \
((unsigned char *)&addr)[0], \
((unsigned char *)&addr)[1], \
((unsigned char *)&addr)[2], \
((unsigned char *)&addr)[3]
#define NIPQUAD_FMT "%u.%u.%u.%u"
char buf[INET6_ADDRSTRLEN];
main()
{
struct {
struct nlmsghdr n;
struct ifaddrmsg r;
char buf[1024];
} req;
struct rtattr *rta;
struct sockaddr_in6 *sin6p;
struct sockaddr_in *sinp;
int status;
char buf[16384];
struct nlmsghdr *nlmp;
struct ifaddrmsg *rtmp;
struct rtattr *rtatp;
int rtattrlen;
struct in_addr *inp;
int fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
memset(&req, 0, sizeof(req));
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
req.n.nlmsg_flags = NLM_F_REQUEST;
req.n.nlmsg_type = RTM_GETADDR;
req.r.ifa_family = AF_INET;
req.r.ifa_index = 1; // I tried to set to this index.....not sure if
its correct
rta = (struct rtattr *)(((char *)&req) + NLMSG_ALIGN(req.n.nlmsg_len));
rta->rta_type = IFA_BROADCAST;
rta->rta_len = RTA_LENGTH(4);
status = send(fd, &req, req.n.nlmsg_len, 0);
if (status < 0) {
perror("send");
return 1;
}
status = recv(fd, buf, sizeof(buf), 0);
if (status < 0) {
perror("recv");
return 1;
}
nlmp = (struct nlmsghdr *)buf;
if (!NLMSG_OK(nlmp, status)) {
printf("NLMSG not OK\n");
return 1;
}
rtmp = (struct ifaddrmsg *)NLMSG_DATA(nlmp);
rtatp = (struct rtattr *)IFA_RTA(rtmp);
printf("%d\n",rtmp->ifa_index);
rtattrlen = IFA_PAYLOAD(nlmp);
for (; RTA_OK(rtatp, rtattrlen); rtatp = RTA_NEXT(rtatp,
rtattrlen)) {
if(rtatp->rta_type == IFA_BROADCAST)
{
inp = (struct in_addr *)RTA_DATA(rtatp);
printf("addr: "NIPQUAD_FMT"\n",NIPQUAD(*inp));
}
}
}
-
To unsubscribe from this list: send the line "unsubscribe linux-net" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at http://vger.kernel.org/majordomo-info.html