Commit | Line | Data |
---|---|---|
28f7bd66 RS |
1 | /* |
2 | Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2011 | |
3 | ||
4 | This program is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation; either version 3 of the License, or | |
7 | (at your option) any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; if not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
18 | /* Example program using the lowlevel raw broadcast interface. | |
19 | */ | |
00748f36 | 20 | #ifdef HAVE_CONFIG_H |
9a96dd46 | 21 | #include "config.h" |
00748f36 RS |
22 | #endif |
23 | ||
24 | #ifdef WIN32 | |
25 | #include "win32_compat.h" | |
26 | #endif | |
27 | ||
28 | #ifdef HAVE_POLL_H | |
29 | #include <poll.h> | |
30 | #endif | |
31 | ||
32 | #ifdef HAVE_UNISTD_H | |
28f7bd66 | 33 | #include <unistd.h> |
00748f36 RS |
34 | #endif |
35 | ||
36 | #include <stdio.h> | |
28f7bd66 RS |
37 | #include <stdlib.h> |
38 | #include <string.h> | |
28f7bd66 RS |
39 | #include <errno.h> |
40 | #include <sys/socket.h> | |
cb46c8fb | 41 | #include <sys/time.h> |
92e787fe | 42 | #include <net/if.h> |
28f7bd66 | 43 | #include <netdb.h> |
00748f36 RS |
44 | |
45 | #ifdef HAVE_SYS_IOCTL_H | |
46 | #include <sys/ioctl.h> | |
47 | #endif | |
48 | ||
763cd6e3 | 49 | #include "libnfs-zdr.h" |
28f7bd66 RS |
50 | #include "libnfs.h" |
51 | #include "libnfs-raw.h" | |
763cd6e3 | 52 | #include "libnfs-private.h" |
28f7bd66 RS |
53 | #include "libnfs-raw-mount.h" |
54 | #include "libnfs-raw-portmap.h" | |
28f7bd66 | 55 | |
5e9910f0 RS |
56 | struct nfs_list_data { |
57 | int status; | |
58 | struct nfs_server_list *srvrs; | |
59 | }; | |
60 | ||
61 | void free_nfs_srvr_list(struct nfs_server_list *srv) | |
62 | { | |
63 | while (srv != NULL) { | |
64 | struct nfs_server_list *next = srv->next; | |
65 | ||
66 | free(srv->addr); | |
67 | free(srv); | |
68 | srv = next; | |
69 | } | |
70 | } | |
71 | ||
552c7665 | 72 | void pm_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data) |
28f7bd66 | 73 | { |
5e9910f0 | 74 | struct nfs_list_data *srv_data = private_data; |
28f7bd66 RS |
75 | struct sockaddr *sin; |
76 | char hostdd[16]; | |
5e9910f0 | 77 | struct nfs_server_list *srvr; |
28f7bd66 | 78 | |
cb46c8fb RS |
79 | if (status == RPC_STATUS_CANCEL) { |
80 | return; | |
81 | } | |
28f7bd66 | 82 | if (status != 0) { |
5e9910f0 RS |
83 | srv_data->status = -1; |
84 | return; | |
28f7bd66 RS |
85 | } |
86 | ||
87 | sin = rpc_get_recv_sockaddr(rpc); | |
88 | if (sin == NULL) { | |
5e9910f0 RS |
89 | rpc_set_error(rpc, "failed to get sockaddr in CALLIT callback"); |
90 | srv_data->status = -1; | |
91 | return; | |
28f7bd66 RS |
92 | } |
93 | ||
94 | if (getnameinfo(sin, sizeof(struct sockaddr_in), &hostdd[0], sizeof(hostdd), NULL, 0, NI_NUMERICHOST) < 0) { | |
5e9910f0 RS |
95 | rpc_set_error(rpc, "getnameinfo failed in CALLIT callback"); |
96 | srv_data->status = -1; | |
97 | return; | |
98 | } | |
99 | ||
100 | ||
101 | srvr = malloc(sizeof(struct nfs_server_list)); | |
102 | if (srvr == NULL) { | |
103 | rpc_set_error(rpc, "Malloc failed when allocating server structure"); | |
104 | srv_data->status = -1; | |
105 | return; | |
28f7bd66 RS |
106 | } |
107 | ||
5e9910f0 RS |
108 | srvr->addr = strdup(hostdd); |
109 | if (srvr->addr == NULL) { | |
110 | rpc_set_error(rpc, "Strdup failed when allocating server structure"); | |
111 | free(srvr); | |
112 | srv_data->status = -1; | |
113 | return; | |
114 | } | |
115 | ||
116 | srvr->next = srv_data->srvrs; | |
117 | srv_data->srvrs = srvr; | |
28f7bd66 RS |
118 | } |
119 | ||
120 | int main(int argc _U_, char *argv[] _U_) | |
121 | { | |
122 | struct rpc_context *rpc; | |
123 | struct pollfd pfd; | |
92e787fe | 124 | struct ifconf ifc; |
1be803ce | 125 | int size; |
cb46c8fb | 126 | struct timeval tv_start, tv_current; |
5e9910f0 RS |
127 | struct nfs_list_data data = {0, NULL}; |
128 | struct nfs_server_list *srvr; | |
1be803ce | 129 | char *ptr; |
28f7bd66 RS |
130 | |
131 | rpc = rpc_init_udp_context(); | |
132 | if (rpc == NULL) { | |
133 | printf("failed to init context\n"); | |
134 | exit(10); | |
135 | } | |
136 | ||
137 | if (rpc_bind_udp(rpc, "0.0.0.0", 0) < 0) { | |
138 | printf("failed to bind to udp %s\n", rpc_get_error(rpc)); | |
139 | exit(10); | |
140 | } | |
141 | ||
28f7bd66 | 142 | |
92e787fe RS |
143 | /* get list of all interfaces */ |
144 | size = sizeof(struct ifreq); | |
145 | ifc.ifc_buf = NULL; | |
146 | ifc.ifc_len = size; | |
28f7bd66 | 147 | |
41a6209b | 148 | while(ifc.ifc_len > (size - sizeof(struct ifreq))) { |
92e787fe RS |
149 | size *= 2; |
150 | ||
151 | free(ifc.ifc_buf); | |
152 | ifc.ifc_len = size; | |
153 | ifc.ifc_buf = malloc(size); | |
1be803ce | 154 | memset(ifc.ifc_buf, 0, size); |
92e787fe RS |
155 | if (ioctl(rpc_get_fd(rpc), SIOCGIFCONF, (caddr_t)&ifc) < 0) { |
156 | printf("ioctl SIOCGIFCONF failed\n"); | |
157 | exit(10); | |
158 | } | |
159 | } | |
160 | ||
1be803ce RS |
161 | for (ptr =(char *)ifc.ifc_buf; ptr < ((char *)ifc.ifc_buf) + ifc.ifc_len; ) { |
162 | struct ifreq *ifr; | |
92e787fe RS |
163 | char bcdd[16]; |
164 | ||
1be803ce | 165 | ifr = (struct ifreq *)ptr; |
9a96dd46 | 166 | #ifdef HAVE_SOCKADDR_LEN |
1be803ce RS |
167 | if (ifr->ifr_addr.sa_len > sizeof(struct sockaddr)) { |
168 | ptr += sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len; | |
169 | } else { | |
170 | ptr += sizeof(ifr->ifr_name) + sizeof(struct sockaddr); | |
171 | } | |
172 | #else | |
173 | ptr += sizeof(struct ifreq); | |
174 | #endif | |
175 | ||
176 | if (ifr->ifr_addr.sa_family != AF_INET) { | |
92e787fe RS |
177 | continue; |
178 | } | |
1be803ce | 179 | if (ioctl(rpc_get_fd(rpc), SIOCGIFFLAGS, ifr) < 0) { |
92e787fe RS |
180 | printf("ioctl DRBADDR failed\n"); |
181 | exit(10); | |
182 | } | |
1be803ce | 183 | if (!(ifr->ifr_flags & IFF_UP)) { |
92e787fe RS |
184 | continue; |
185 | } | |
1be803ce | 186 | if (ifr->ifr_flags & IFF_LOOPBACK) { |
92e787fe RS |
187 | continue; |
188 | } | |
1be803ce | 189 | if (!(ifr->ifr_flags & IFF_BROADCAST)) { |
92e787fe RS |
190 | continue; |
191 | } | |
1be803ce | 192 | if (ioctl(rpc_get_fd(rpc), SIOCGIFBRDADDR, ifr) < 0) { |
1ad6f931 | 193 | continue; |
92e787fe | 194 | } |
1be803ce | 195 | if (getnameinfo(&ifr->ifr_broadaddr, sizeof(struct sockaddr_in), &bcdd[0], sizeof(bcdd), NULL, 0, NI_NUMERICHOST) < 0) { |
1ad6f931 | 196 | continue; |
92e787fe RS |
197 | } |
198 | if (rpc_set_udp_destination(rpc, bcdd, 111, 1) < 0) { | |
199 | printf("failed to set udp destination %s\n", rpc_get_error(rpc)); | |
200 | exit(10); | |
201 | } | |
202 | ||
5e9910f0 | 203 | if (rpc_pmap_callit_async(rpc, MOUNT_PROGRAM, 2, 0, NULL, 0, pm_cb, &data) < 0) { |
92e787fe RS |
204 | printf("Failed to set up callit function\n"); |
205 | exit(10); | |
206 | } | |
28f7bd66 | 207 | } |
92e787fe RS |
208 | free(ifc.ifc_buf); |
209 | ||
cb46c8fb | 210 | gettimeofday(&tv_start, NULL); |
28f7bd66 | 211 | for(;;) { |
cb46c8fb RS |
212 | int mpt; |
213 | ||
28f7bd66 RS |
214 | pfd.fd = rpc_get_fd(rpc); |
215 | pfd.events = rpc_which_events(rpc); | |
216 | ||
cb46c8fb RS |
217 | gettimeofday(&tv_current, NULL); |
218 | mpt = 1000 | |
219 | - (tv_current.tv_sec *1000 + tv_current.tv_usec / 1000) | |
220 | + (tv_start.tv_sec *1000 + tv_start.tv_usec / 1000); | |
221 | ||
222 | if (poll(&pfd, 1, mpt) < 0) { | |
28f7bd66 RS |
223 | printf("Poll failed"); |
224 | exit(10); | |
225 | } | |
cb46c8fb RS |
226 | if (pfd.revents == 0) { |
227 | break; | |
228 | } | |
229 | ||
28f7bd66 RS |
230 | if (rpc_service(rpc, pfd.revents) < 0) { |
231 | printf("rpc_service failed with %s\n", rpc_get_error(rpc)); | |
232 | break; | |
233 | } | |
234 | } | |
92e787fe | 235 | |
5e9910f0 RS |
236 | for (srvr=data.srvrs; srvr; srvr = srvr->next) { |
237 | printf("NFS SERVER @ %s\n", srvr->addr); | |
238 | } | |
239 | free_nfs_srvr_list(data.srvrs); | |
240 | ||
28f7bd66 RS |
241 | rpc_destroy_context(rpc); |
242 | rpc=NULL; | |
28f7bd66 RS |
243 | return 0; |
244 | } |