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