2007-03-10 03:16:51 +03:00
|
|
|
/* $NetBSD: trygetif.c,v 1.6 2007/03/10 00:16:51 hubertf Exp $ */
|
1998-03-14 07:39:53 +03:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2007-03-10 03:16:51 +03:00
|
|
|
__RCSID("$NetBSD: trygetif.c,v 1.6 2007/03/10 00:16:51 hubertf Exp $");
|
1998-03-14 07:39:53 +03:00
|
|
|
#endif
|
1998-01-09 11:03:16 +03:00
|
|
|
|
1994-06-28 01:25:48 +04:00
|
|
|
/*
|
|
|
|
* trygetif.c - test program for getif.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#if defined(SUNOS) || defined(SVR4)
|
|
|
|
#include <sys/sockio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <net/if.h> /* for struct ifreq */
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h> /* inet_ntoa */
|
|
|
|
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "getif.h"
|
|
|
|
|
2002-07-14 03:56:39 +04:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
1994-06-28 01:25:48 +04:00
|
|
|
{
|
|
|
|
struct hostent *hep;
|
|
|
|
struct sockaddr ea; /* Ethernet address */
|
|
|
|
struct sockaddr_in *sip; /* Interface address */
|
|
|
|
struct ifreq *ifr;
|
|
|
|
struct in_addr dst_addr;
|
|
|
|
struct in_addr *dap;
|
|
|
|
int i, s;
|
|
|
|
|
|
|
|
dap = NULL;
|
|
|
|
if (argc > 1) {
|
|
|
|
dap = &dst_addr;
|
1995-05-20 02:05:08 +04:00
|
|
|
if (inet_aton(argv[1], &dst_addr) == 0) {
|
1994-06-28 01:25:48 +04:00
|
|
|
hep = gethostbyname(argv[1]);
|
|
|
|
if (!hep) {
|
2007-03-10 03:16:51 +03:00
|
|
|
fprintf(stderr, "gethostbyname(%s)\n", argv[1]);
|
1994-06-28 01:25:48 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
memcpy(&dst_addr, hep->h_addr, sizeof(dst_addr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (s < 0) {
|
|
|
|
perror("socket open");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ifr = getif(s, dap);
|
|
|
|
if (!ifr) {
|
2007-03-10 03:16:51 +03:00
|
|
|
fprintf(stderr, "no interface for address\n");
|
1994-06-28 01:25:48 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
printf("Intf-name:%s\n", ifr->ifr_name);
|
|
|
|
sip = (struct sockaddr_in *) &(ifr->ifr_addr);
|
|
|
|
printf("Intf-addr:%s\n", inet_ntoa(sip->sin_addr));
|
|
|
|
|
2007-03-10 03:16:51 +03:00
|
|
|
return 0;
|
1994-06-28 01:25:48 +04:00
|
|
|
}
|