source: rtems-libbsd/services/librpc/src/rpc/get_myaddress.c @ e599318

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since e599318 was e599318, checked in by Sebastian Huber <sebastian.huber@…>, on 10/09/13 at 20:52:54

Update files to match FreeBSD layout

Add compatibility with Newlib header files. Some FreeBSD header files
are mapped by the translation script:

o rtems/bsd/sys/_types.h
o rtems/bsd/sys/errno.h
o rtems/bsd/sys/lock.h
o rtems/bsd/sys/param.h
o rtems/bsd/sys/resource.h
o rtems/bsd/sys/time.h
o rtems/bsd/sys/timespec.h
o rtems/bsd/sys/types.h
o rtems/bsd/sys/unistd.h

It is now possible to include <sys/socket.h> directly for example.

Generate one Makefile which builds everything including tests.

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part.  Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
8 *
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12 *
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
16 *
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
20 *
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
24 *
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California  94043
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31/*static char *sccsid = "from: @(#)get_myaddress.c 1.4 87/08/11 Copyr 1984 Sun Micro";*/
32/*static char *sccsid = "from: @(#)get_myaddress.c      2.1 88/07/29 4.0 RPCSRC";*/
33static char *rcsid = "$FreeBSD: src/lib/libc/rpc/get_myaddress.c,v 1.17 2000/01/27 23:06:37 jasone Exp $";
34#endif
35
36/*
37 * get_myaddress.c
38 *
39 * Get client's IP address via ioctl.  This avoids using the yellowpages.
40 * Copyright (C) 1984, Sun Microsystems, Inc.
41 */
42
43#ifdef HAVE_CONFIG_H
44#include "config.h"
45#endif
46
47#include <rpc/types.h>
48#include <rpc/xdr.h>
49#include <rpc/pmap_prot.h>
50#include <sys/socket.h>
51#include <stdio.h>
52#include <unistd.h>
53#ifndef __rtems__
54        /* XXX old.. not new */
55        #include <sys/mbuf.h>
56#else
57        #include <freebsd/sys/mbuf.h>
58#endif
59#include <net/if.h>
60#include <sys/ioctl.h>
61#include <netinet/in.h>
62#include <arpa/inet.h>
63
64/*
65 * don't use gethostbyname, which would invoke yellow pages
66 *
67 * Avoid loopback interfaces.  We return information from a loopback
68 * interface only if there are no other possible interfaces.
69 */
70int
71get_myaddress(
72        struct sockaddr_in *addr)
73{
74        int s;
75        char buf[BUFSIZ];
76        struct ifconf ifc;
77        struct ifreq ifreq, *ifr, *end;
78        int loopback = 0, gotit = 0;
79
80        if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
81                return(-1);
82        }
83        ifc.ifc_len = sizeof (buf);
84        ifc.ifc_buf = buf;
85        if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
86                _RPC_close(s);
87                return(-1);
88        }
89again:
90        ifr = ifc.ifc_req;
91        end = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
92
93        while (ifr < end) {
94                ifreq = *ifr;
95                if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
96                        _RPC_close(s);
97                        return(-1);
98                }
99                if (((ifreq.ifr_flags & IFF_UP) &&
100                    ifr->ifr_addr.sa_family == AF_INET &&
101                        !(ifreq.ifr_flags & IFF_LOOPBACK)) ||
102                    (loopback == 1 && (ifreq.ifr_flags & IFF_LOOPBACK)
103                        && (ifr->ifr_addr.sa_family == AF_INET)
104                        && (ifreq.ifr_flags &  IFF_UP))) {
105                        *addr = *((struct sockaddr_in *)&ifr->ifr_addr);
106                        addr->sin_port = htons(PMAPPORT);
107                        gotit = 1;
108                        break;
109                }
110                if (ifr->ifr_addr.sa_len)
111                        ifr = (struct ifreq *) ((caddr_t) ifr +
112                              ifr->ifr_addr.sa_len -
113                              sizeof(struct sockaddr));
114                ifr++;
115        }
116        if (gotit == 0 && loopback == 0) {
117                loopback = 1;
118                goto again;
119        }
120        (void)_RPC_close(s);
121        return (gotit ? 0 : -1);
122}
Note: See TracBrowser for help on using the repository browser.