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 | */ | |
20 | ||
21 | #include <stdio.h> | |
22 | #include <unistd.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <poll.h> | |
26 | #include <errno.h> | |
27 | #include <sys/socket.h> | |
92e787fe | 28 | #include <sys/ioctl.h> |
cb46c8fb | 29 | #include <sys/time.h> |
92e787fe | 30 | #include <net/if.h> |
28f7bd66 RS |
31 | #include <netdb.h> |
32 | #include "libnfs.h" | |
33 | #include "libnfs-raw.h" | |
34 | #include "libnfs-raw-mount.h" | |
35 | #include "libnfs-raw-portmap.h" | |
36 | #include "libnfs-private.h" | |
37 | ||
5e9910f0 RS |
38 | struct nfs_list_data { |
39 | int status; | |
40 | struct nfs_server_list *srvrs; | |
41 | }; | |
42 | ||
43 | void free_nfs_srvr_list(struct nfs_server_list *srv) | |
44 | { | |
45 | while (srv != NULL) { | |
46 | struct nfs_server_list *next = srv->next; | |
47 | ||
48 | free(srv->addr); | |
49 | free(srv); | |
50 | srv = next; | |
51 | } | |
52 | } | |
53 | ||
552c7665 | 54 | void pm_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data) |
28f7bd66 | 55 | { |
5e9910f0 | 56 | struct nfs_list_data *srv_data = private_data; |
28f7bd66 RS |
57 | struct sockaddr *sin; |
58 | char hostdd[16]; | |
5e9910f0 | 59 | struct nfs_server_list *srvr; |
28f7bd66 | 60 | |
cb46c8fb RS |
61 | if (status == RPC_STATUS_CANCEL) { |
62 | return; | |
63 | } | |
28f7bd66 | 64 | if (status != 0) { |
5e9910f0 RS |
65 | srv_data->status = -1; |
66 | return; | |
28f7bd66 RS |
67 | } |
68 | ||
69 | sin = rpc_get_recv_sockaddr(rpc); | |
70 | if (sin == NULL) { | |
5e9910f0 RS |
71 | rpc_set_error(rpc, "failed to get sockaddr in CALLIT callback"); |
72 | srv_data->status = -1; | |
73 | return; | |
28f7bd66 RS |
74 | } |
75 | ||
76 | if (getnameinfo(sin, sizeof(struct sockaddr_in), &hostdd[0], sizeof(hostdd), NULL, 0, NI_NUMERICHOST) < 0) { | |
5e9910f0 RS |
77 | rpc_set_error(rpc, "getnameinfo failed in CALLIT callback"); |
78 | srv_data->status = -1; | |
79 | return; | |
80 | } | |
81 | ||
82 | ||
83 | srvr = malloc(sizeof(struct nfs_server_list)); | |
84 | if (srvr == NULL) { | |
85 | rpc_set_error(rpc, "Malloc failed when allocating server structure"); | |
86 | srv_data->status = -1; | |
87 | return; | |
28f7bd66 RS |
88 | } |
89 | ||
5e9910f0 RS |
90 | srvr->addr = strdup(hostdd); |
91 | if (srvr->addr == NULL) { | |
92 | rpc_set_error(rpc, "Strdup failed when allocating server structure"); | |
93 | free(srvr); | |
94 | srv_data->status = -1; | |
95 | return; | |
96 | } | |
97 | ||
98 | srvr->next = srv_data->srvrs; | |
99 | srv_data->srvrs = srvr; | |
28f7bd66 RS |
100 | } |
101 | ||
102 | int main(int argc _U_, char *argv[] _U_) | |
103 | { | |
104 | struct rpc_context *rpc; | |
105 | struct pollfd pfd; | |
92e787fe RS |
106 | struct ifconf ifc; |
107 | int i, size; | |
cb46c8fb | 108 | struct timeval tv_start, tv_current; |
5e9910f0 RS |
109 | struct nfs_list_data data = {0, NULL}; |
110 | struct nfs_server_list *srvr; | |
28f7bd66 RS |
111 | |
112 | rpc = rpc_init_udp_context(); | |
113 | if (rpc == NULL) { | |
114 | printf("failed to init context\n"); | |
115 | exit(10); | |
116 | } | |
117 | ||
118 | if (rpc_bind_udp(rpc, "0.0.0.0", 0) < 0) { | |
119 | printf("failed to bind to udp %s\n", rpc_get_error(rpc)); | |
120 | exit(10); | |
121 | } | |
122 | ||
28f7bd66 | 123 | |
92e787fe RS |
124 | /* get list of all interfaces */ |
125 | size = sizeof(struct ifreq); | |
126 | ifc.ifc_buf = NULL; | |
127 | ifc.ifc_len = size; | |
28f7bd66 | 128 | |
92e787fe RS |
129 | while (ifc.ifc_len == size) { |
130 | size *= 2; | |
131 | ||
132 | free(ifc.ifc_buf); | |
133 | ifc.ifc_len = size; | |
134 | ifc.ifc_buf = malloc(size); | |
135 | if (ioctl(rpc_get_fd(rpc), SIOCGIFCONF, (caddr_t)&ifc) < 0) { | |
136 | printf("ioctl SIOCGIFCONF failed\n"); | |
137 | exit(10); | |
138 | } | |
139 | } | |
140 | ||
552c7665 | 141 | for (i = 0; (unsigned)i < ifc.ifc_len / sizeof(struct ifconf); i++) { |
92e787fe RS |
142 | char bcdd[16]; |
143 | ||
144 | if (ifc.ifc_req[i].ifr_addr.sa_family != AF_INET) { | |
145 | continue; | |
146 | } | |
147 | if (ioctl(rpc_get_fd(rpc), SIOCGIFFLAGS, &ifc.ifc_req[i]) < 0) { | |
148 | printf("ioctl DRBADDR failed\n"); | |
149 | exit(10); | |
150 | } | |
151 | if (!(ifc.ifc_req[i].ifr_flags & IFF_UP)) { | |
152 | continue; | |
153 | } | |
154 | if (ifc.ifc_req[i].ifr_flags & IFF_LOOPBACK) { | |
155 | continue; | |
156 | } | |
157 | if (!(ifc.ifc_req[i].ifr_flags & IFF_BROADCAST)) { | |
158 | continue; | |
159 | } | |
160 | if (ioctl(rpc_get_fd(rpc), SIOCGIFBRDADDR, &ifc.ifc_req[i]) < 0) { | |
161 | printf("ioctl DRBADDR failed\n"); | |
162 | exit(10); | |
163 | } | |
164 | if (getnameinfo(&ifc.ifc_req[i].ifr_broadaddr, sizeof(struct sockaddr_in), &bcdd[0], sizeof(bcdd), NULL, 0, NI_NUMERICHOST) < 0) { | |
165 | printf("getnameinfo failed\n"); | |
166 | exit(10); | |
167 | } | |
168 | if (rpc_set_udp_destination(rpc, bcdd, 111, 1) < 0) { | |
169 | printf("failed to set udp destination %s\n", rpc_get_error(rpc)); | |
170 | exit(10); | |
171 | } | |
172 | ||
5e9910f0 | 173 | if (rpc_pmap_callit_async(rpc, MOUNT_PROGRAM, 2, 0, NULL, 0, pm_cb, &data) < 0) { |
92e787fe RS |
174 | printf("Failed to set up callit function\n"); |
175 | exit(10); | |
176 | } | |
28f7bd66 | 177 | } |
92e787fe RS |
178 | free(ifc.ifc_buf); |
179 | ||
cb46c8fb | 180 | gettimeofday(&tv_start, NULL); |
28f7bd66 | 181 | for(;;) { |
cb46c8fb RS |
182 | int mpt; |
183 | ||
28f7bd66 RS |
184 | pfd.fd = rpc_get_fd(rpc); |
185 | pfd.events = rpc_which_events(rpc); | |
186 | ||
cb46c8fb RS |
187 | gettimeofday(&tv_current, NULL); |
188 | mpt = 1000 | |
189 | - (tv_current.tv_sec *1000 + tv_current.tv_usec / 1000) | |
190 | + (tv_start.tv_sec *1000 + tv_start.tv_usec / 1000); | |
191 | ||
192 | if (poll(&pfd, 1, mpt) < 0) { | |
28f7bd66 RS |
193 | printf("Poll failed"); |
194 | exit(10); | |
195 | } | |
cb46c8fb RS |
196 | if (pfd.revents == 0) { |
197 | break; | |
198 | } | |
199 | ||
28f7bd66 RS |
200 | if (rpc_service(rpc, pfd.revents) < 0) { |
201 | printf("rpc_service failed with %s\n", rpc_get_error(rpc)); | |
202 | break; | |
203 | } | |
204 | } | |
92e787fe | 205 | |
5e9910f0 RS |
206 | for (srvr=data.srvrs; srvr; srvr = srvr->next) { |
207 | printf("NFS SERVER @ %s\n", srvr->addr); | |
208 | } | |
209 | free_nfs_srvr_list(data.srvrs); | |
210 | ||
28f7bd66 RS |
211 | rpc_destroy_context(rpc); |
212 | rpc=NULL; | |
28f7bd66 RS |
213 | return 0; |
214 | } |