123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- #define __FORCE_GLIBC__
- #include <features.h>
- #include <rpc/types.h>
- #include <rpc/pmap_prot.h>
- #include <sys/socket.h>
- #include <stdio.h>
- #include <sys/ioctl.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #ifdef __linux__
- #include <netdb.h>
- get_myaddress(addr)
- struct sockaddr_in *addr;
- {
- char localhost[256 + 1];
- struct hostent *hp;
- gethostname(localhost, 256);
- if ((hp = gethostbyname(localhost)) == NULL) {
- perror("get_myaddress: gethostbyname");
- exit(1);
- }
- addr->sin_family = AF_INET;
- bcopy((char *) hp->h_addr, (char *) &addr->sin_addr, hp->h_length);
- addr->sin_port = htons(PMAPPORT);
- }
- #else
- get_myaddress(addr)
- struct sockaddr_in *addr;
- {
- int s;
- char buf[BUFSIZ];
- struct ifconf ifc;
- struct ifreq ifreq, *ifr;
- int len;
- if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
- perror("get_myaddress: socket");
- exit(1);
- }
- ifc.ifc_len = sizeof(buf);
- ifc.ifc_buf = buf;
- if (ioctl(s, SIOCGIFCONF, (char *) &ifc) < 0) {
- perror("get_myaddress: ioctl (get interface configuration)");
- exit(1);
- }
- ifr = ifc.ifc_req;
- for (len = ifc.ifc_len; len; len -= sizeof ifreq) {
- ifreq = *ifr;
- if (ioctl(s, SIOCGIFFLAGS, (char *) &ifreq) < 0) {
- perror("get_myaddress: ioctl");
- exit(1);
- }
- if ((ifreq.ifr_flags & IFF_UP) &&
- ifr->ifr_addr.sa_family == AF_INET) {
- *addr = *((struct sockaddr_in *) &ifr->ifr_addr);
- addr->sin_port = htons(PMAPPORT);
- break;
- }
- ifr++;
- }
- (void) close(s);
- }
- #endif
|