On Fri, Mar 09, 2007 at 11:46:09AM +0530, Varun Chandramohan wrote: > 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 This is wrong, you need to specify the interface index number assigned by the kernel for the interface that you want to query. To do that use the SIOCGIFINDEX ioctl as specified in the netdevice(7) man page Neil - To unsubscribe from this list: send the line "unsubscribe linux-net" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html