]>
Commit | Line | Data |
---|---|---|
28f540f4 RM |
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. | |
cbd3dceb | 8 | * |
28f540f4 RM |
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. | |
cbd3dceb | 12 | * |
28f540f4 RM |
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. | |
cbd3dceb | 16 | * |
28f540f4 RM |
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. | |
cbd3dceb | 20 | * |
28f540f4 RM |
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. | |
cbd3dceb | 24 | * |
28f540f4 RM |
25 | * Sun Microsystems, Inc. |
26 | * 2550 Garcia Avenue | |
27 | * Mountain View, California 94043 | |
28 | */ | |
925ed47c UD |
29 | /* |
30 | * Copyright (C) 1984, Sun Microsystems, Inc. | |
31 | */ | |
28f540f4 RM |
32 | /* |
33 | * pmap_clnt.c | |
34 | * Client interface to pmap rpc service. | |
28f540f4 RM |
35 | */ |
36 | ||
925ed47c UD |
37 | #include <stdio.h> |
38 | #include <unistd.h> | |
4360eafd | 39 | #include <libintl.h> |
925ed47c UD |
40 | #include <net/if.h> |
41 | #include <sys/ioctl.h> | |
42 | #include <sys/socket.h> | |
43 | #include <netinet/in.h> | |
44 | #include <arpa/inet.h> | |
28f540f4 RM |
45 | #include <rpc/rpc.h> |
46 | #include <rpc/pmap_prot.h> | |
47 | #include <rpc/pmap_clnt.h> | |
48 | ||
925ed47c UD |
49 | /* |
50 | * Same as get_myaddress, but we try to use the loopback | |
51 | * interface. portmap caches interfaces, and on DHCP clients, | |
52 | * it could be that only loopback is started at this time. | |
53 | */ | |
ec28fc7c | 54 | static bool_t |
925ed47c UD |
55 | __get_myaddress (struct sockaddr_in *addr) |
56 | { | |
57 | int s; | |
58 | char buf[BUFSIZ]; | |
59 | struct ifconf ifc; | |
60 | struct ifreq ifreq, *ifr; | |
61 | int len, loopback = 1; | |
62 | ||
63 | if ((s = __socket (AF_INET, SOCK_DGRAM, 0)) < 0) | |
64 | { | |
65 | perror ("__get_myaddress: socket"); | |
66 | exit (1); | |
67 | } | |
68 | ifc.ifc_len = sizeof (buf); | |
69 | ifc.ifc_buf = buf; | |
70 | if (__ioctl (s, SIOCGIFCONF, (char *) &ifc) < 0) | |
71 | { | |
72 | perror (_("__get_myaddress: ioctl (get interface configuration)")); | |
73 | exit (1); | |
74 | } | |
75 | ||
76 | again: | |
77 | ifr = ifc.ifc_req; | |
78 | for (len = ifc.ifc_len; len; len -= sizeof ifreq) | |
79 | { | |
80 | ifreq = *ifr; | |
81 | if (__ioctl (s, SIOCGIFFLAGS, (char *) &ifreq) < 0) | |
82 | { | |
83 | perror ("__get_myaddress: ioctl"); | |
84 | exit (1); | |
85 | } | |
86 | if ((ifreq.ifr_flags & IFF_UP) && (ifr->ifr_addr.sa_family == AF_INET) | |
87 | && ((ifreq.ifr_flags & IFF_LOOPBACK) || (loopback == 0))) | |
88 | { | |
89 | *addr = *((struct sockaddr_in *) &ifr->ifr_addr); | |
90 | addr->sin_port = htons (PMAPPORT); | |
91 | __close (s); | |
ec28fc7c | 92 | return TRUE; |
925ed47c UD |
93 | } |
94 | ifr++; | |
95 | } | |
96 | if (loopback == 1) | |
97 | { | |
98 | loopback = 0; | |
99 | goto again; | |
100 | } | |
101 | __close (s); | |
ec28fc7c | 102 | return FALSE; |
925ed47c UD |
103 | } |
104 | ||
105 | ||
e7fd8a39 UD |
106 | static const struct timeval timeout = {5, 0}; |
107 | static const struct timeval tottimeout = {60, 0}; | |
28f540f4 RM |
108 | |
109 | /* | |
110 | * Set a mapping between program,version and port. | |
111 | * Calls the pmap service remotely to do the mapping. | |
112 | */ | |
113 | bool_t | |
e7fd8a39 | 114 | pmap_set (u_long program, u_long version, int protocol, u_short port) |
28f540f4 | 115 | { |
e7fd8a39 UD |
116 | struct sockaddr_in myaddress; |
117 | int socket = -1; | |
118 | CLIENT *client; | |
119 | struct pmap parms; | |
120 | bool_t rslt; | |
28f540f4 | 121 | |
ec28fc7c UD |
122 | if (!__get_myaddress (&myaddress)) |
123 | return FALSE; | |
e7fd8a39 UD |
124 | client = clntudp_bufcreate (&myaddress, PMAPPROG, PMAPVERS, |
125 | timeout, &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE); | |
126 | if (client == (CLIENT *) NULL) | |
127 | return (FALSE); | |
128 | parms.pm_prog = program; | |
129 | parms.pm_vers = version; | |
130 | parms.pm_prot = protocol; | |
131 | parms.pm_port = port; | |
77fe0b9c UD |
132 | if (CLNT_CALL (client, PMAPPROC_SET, (xdrproc_t)INTUSE(xdr_pmap), |
133 | (caddr_t)&parms, (xdrproc_t)INTUSE(xdr_bool), (caddr_t)&rslt, | |
e7fd8a39 UD |
134 | tottimeout) != RPC_SUCCESS) |
135 | { | |
136 | clnt_perror (client, _("Cannot register service")); | |
137 | return FALSE; | |
138 | } | |
139 | CLNT_DESTROY (client); | |
140 | /* (void)close(socket); CLNT_DESTROY closes it */ | |
141 | return rslt; | |
28f540f4 RM |
142 | } |
143 | ||
144 | /* | |
145 | * Remove the mapping between program,version and port. | |
146 | * Calls the pmap service remotely to do the un-mapping. | |
147 | */ | |
148 | bool_t | |
e7fd8a39 | 149 | pmap_unset (u_long program, u_long version) |
28f540f4 | 150 | { |
e7fd8a39 UD |
151 | struct sockaddr_in myaddress; |
152 | int socket = -1; | |
153 | CLIENT *client; | |
154 | struct pmap parms; | |
155 | bool_t rslt; | |
28f540f4 | 156 | |
ec28fc7c UD |
157 | if (!__get_myaddress (&myaddress)) |
158 | return FALSE; | |
e7fd8a39 UD |
159 | client = clntudp_bufcreate (&myaddress, PMAPPROG, PMAPVERS, |
160 | timeout, &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE); | |
161 | if (client == (CLIENT *) NULL) | |
162 | return FALSE; | |
163 | parms.pm_prog = program; | |
164 | parms.pm_vers = version; | |
165 | parms.pm_port = parms.pm_prot = 0; | |
77fe0b9c UD |
166 | CLNT_CALL (client, PMAPPROC_UNSET, (xdrproc_t)INTUSE(xdr_pmap), |
167 | (caddr_t)&parms, (xdrproc_t)INTUSE(xdr_bool), (caddr_t)&rslt, | |
168 | tottimeout); | |
e7fd8a39 UD |
169 | CLNT_DESTROY (client); |
170 | /* (void)close(socket); CLNT_DESTROY already closed it */ | |
171 | return rslt; | |
28f540f4 | 172 | } |