]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - utils/statd/rmtcall.c
Define and use HIAVE_IFADDRS_H
[nfs-utils.git] / utils / statd / rmtcall.c
index f01faba06e1a7be80a5c0c3c344cccc1fd58e776..7684b3b51eb142b3c7f18e760868cd0575521729 100644 (file)
  * it won't if it's worth its money).
  */
 
-#include "config.h"
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
 
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/time.h>
 #include <netinet/in.h>
+#include <net/if.h>
 #include <arpa/inet.h>
 #include <rpc/rpc.h>
 #include <rpc/pmap_prot.h>
 #include <netdb.h>
 #include <string.h>
 #include <unistd.h>
+#ifdef HAVE_IFADDRS_H
+#include <ifaddrs.h>
+#endif /* HAVE_IFADDRS_H */
 #include "sm_inter.h"
 #include "statd.h"
 #include "notlist.h"
 #include "log.h"
+#include "ha-callout.h"
 
 #define MAXMSGSIZE     (2048 / sizeof(unsigned int))
 
@@ -56,7 +63,7 @@ statd_get_socket(int port)
                return sockfd;
 
        if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
-               log(L_CRIT, "Can't create socket: %m");
+               note(N_CRIT, "Can't create socket: %m");
                return -1;
        }
 
@@ -64,15 +71,78 @@ statd_get_socket(int port)
 
        memset(&sin, 0, sizeof(sin));
        sin.sin_family = AF_INET;
-       sin.sin_port = port;
+       sin.sin_addr.s_addr = INADDR_ANY;
+       /*
+        * If a local hostname is given (-n option to statd), bind to the address
+        * specified. This is required to support clients that ignore the mon_name in
+        * the statd protocol but use the source address from the request packet.
+        */
+       if (MY_NAME) {
+               struct hostent *hp = gethostbyname(MY_NAME);
+               if (hp)
+                       sin.sin_addr = *(struct in_addr *) hp->h_addr;
+       }
+       if (port != 0) {
+               sin.sin_port = htons(port);
+               if (bind(sockfd, &sin, sizeof(sin)) == 0)
+                       goto out_success;
+               note(N_CRIT, "statd: failed to bind to outgoing port, %d\n"
+                               "       falling back on randomly chosen port\n", port);
+       }
        if (bindresvport(sockfd, &sin) < 0) {
-               dprintf(L_WARNING,
+               dprintf(N_WARNING,
                        "process_hosts: can't bind to reserved port\n");
        }
-
+out_success:
        return sockfd;
 }
 
+#ifdef HAVE_IFADDRS_H
+/*
+ * Using the NL_ADDR(lp), reset (if needed) the hostname
+ * that will be put in the SM_NOTIFY to the hostname
+ * that is associated with the network interface 
+ * that was monitored
+ */
+static void
+reset_my_name(notify_list *lp)
+{
+       struct ifaddrs *ifa = NULL, *ifap;
+       struct in_addr netaddr, tmp;
+       struct sockaddr_in *sin, *nsin;
+       struct hostent *hp;
+
+       netaddr.s_addr = inet_netof(NL_ADDR(lp));
+       if (getifaddrs(&ifa) >= 0) {
+               for (ifap = ifa; ifap != NULL; ifap = ifap->ifa_next) {
+                       if (!(ifap->ifa_flags & IFF_UP))
+                               continue;
+
+                       note(N_DEBUG, "ifa_name %s\n", ifap->ifa_name);
+                       if (ifap->ifa_addr == NULL)
+                               continue;
+                       if (ifap->ifa_addr->sa_family != AF_INET)
+                               continue;
+
+                       sin = (struct sockaddr_in *)ifap->ifa_addr;
+                       nsin = (struct sockaddr_in *)ifap->ifa_netmask;
+                       tmp.s_addr = sin->sin_addr.s_addr & nsin->sin_addr.s_addr;
+                       if (memcmp(&tmp.s_addr, &netaddr.s_addr, sizeof(netaddr.s_addr)))
+                               continue;
+                       hp = gethostbyaddr((char *)&sin->sin_addr, 
+                               sizeof(sin->sin_addr), AF_INET);
+                       if (hp == NULL)
+                               continue;
+                       if (strcmp(NL_MY_NAME(lp), hp->h_name)) {
+                               free(NL_MY_NAME(lp));
+                               NL_MY_NAME(lp)= strdup(hp->h_name);
+                               note(N_DEBUG, "NL_MY_NAME %s\n", NL_MY_NAME(lp));
+                       }
+               }
+       }
+       return;
+}
+#endif /* HAVE_IFADDRS_H */
 /*
  * Try to resolve host name for notify/callback request
  *
@@ -90,7 +160,7 @@ try_to_resolve(notify_list *lp)
        else
                hname = NL_MY_NAME(lp);
        if (!inet_aton(hname, &(NL_ADDR(lp)))) {
-               log(L_ERROR, "%s is not an dotted-quad address", hname);
+               note(N_ERROR, "%s is not an dotted-quad address", hname);
                NL_TIMES(lp) = 0;
                return 0;
        }
@@ -113,7 +183,7 @@ try_to_resolve(notify_list *lp)
        else
                hname = NL_MY_NAME(lp);
 
-       dprintf(L_DEBUG, "Trying to resolve %s.", hname);
+       dprintf(N_DEBUG, "Trying to resolve %s.", hname);
        if (!(hp = gethostbyname(hname))) {
                herror("gethostbyname");
                NL_TIMES(lp) -= 1;
@@ -121,7 +191,7 @@ try_to_resolve(notify_list *lp)
        }
 
        if (hp->h_addrtype != AF_INET) {
-               log(L_ERROR, "%s is not an AF_INET address", hname);
+               note(N_ERROR, "%s is not an AF_INET address", hname);
                NL_TIMES(lp) = 0;
                return 0;
        }
@@ -130,7 +200,7 @@ try_to_resolve(notify_list *lp)
         * alternation because one interface might be down/unreachable. */
        NL_ADDR(lp) = *(struct in_addr *) hp->h_addr;
 
-       dprintf(L_DEBUG, "address of %s is %s", hname, inet_ntoa(NL_ADDR(lp)));
+       dprintf(N_DEBUG, "address of %s is %s", hname, inet_ntoa(NL_ADDR(lp)));
        return 1;
 }
 #endif
@@ -181,7 +251,7 @@ xmit_call(int sockfd, struct sockaddr_in *sin,
 
        /* Encode the RPC header part and payload */
        if (!xdr_callmsg(xdrs, &mesg) || !func(xdrs, obj)) {
-               dprintf(L_WARNING, "xmit_mesg: can't encode RPC message!\n");
+               dprintf(N_WARNING, "xmit_mesg: can't encode RPC message!\n");
                xdr_destroy(xdrs);
                return 0;
        }
@@ -191,9 +261,9 @@ xmit_call(int sockfd, struct sockaddr_in *sin,
 
        if ((err = sendto(sockfd, msgbuf, msglen, 0,
                        (struct sockaddr *) sin, sizeof(*sin))) < 0) {
-               dprintf(L_WARNING, "xmit_mesg: sendto failed: %m");
+               dprintf(N_WARNING, "xmit_mesg: sendto failed: %m");
        } else if (err != msglen) {
-               dprintf(L_WARNING, "xmit_mesg: short write: %m\n");
+               dprintf(N_WARNING, "xmit_mesg: short write: %m\n");
        }
 
        xdr_destroy(xdrs);
@@ -213,7 +283,7 @@ recv_rply(int sockfd, struct sockaddr_in *sin, u_long *portp)
        /* Receive message */
        if ((msglen = recvfrom(sockfd, msgbuf, sizeof(msgbuf), 0,
                        (struct sockaddr *) sin, &alen)) < 0) {
-               dprintf(L_WARNING, "recv_rply: recvfrom failed: %m");
+               dprintf(N_WARNING, "recv_rply: recvfrom failed: %m");
                return NULL;
        }
 
@@ -225,18 +295,18 @@ recv_rply(int sockfd, struct sockaddr_in *sin, u_long *portp)
        mesg.rm_reply.rp_acpt.ar_results.proc = (xdrproc_t) xdr_void;
 
        if (!xdr_replymsg(xdrs, &mesg)) {
-               log(L_WARNING, "recv_rply: can't decode RPC message!\n");
+               note(N_WARNING, "recv_rply: can't decode RPC message!\n");
                goto done;
        }
 
        if (mesg.rm_reply.rp_stat != 0) {
-               log(L_WARNING, "recv_rply: [%s] RPC status %d\n", 
+               note(N_WARNING, "recv_rply: [%s] RPC status %d\n", 
                                inet_ntoa(sin->sin_addr),
                                mesg.rm_reply.rp_stat);
                goto done;
        }
        if (mesg.rm_reply.rp_acpt.ar_stat != 0) {
-               log(L_WARNING, "recv_rply: [%s] RPC status %d\n",
+               note(N_WARNING, "recv_rply: [%s] RPC status %d\n",
                                inet_ntoa(sin->sin_addr),
                                mesg.rm_reply.rp_acpt.ar_stat);
                goto done;
@@ -253,13 +323,13 @@ recv_rply(int sockfd, struct sockaddr_in *sin, u_long *portp)
                        strncpy (addr, inet_ntoa(lp->addr),
                                 sizeof (addr) - 1);
                        addr [sizeof (addr) - 1] = '\0';
-                       dprintf(L_WARNING, "address mismatch: "
+                       dprintf(N_WARNING, "address mismatch: "
                                "expected %s, got %s\n",
                                addr, inet_ntoa(sin->sin_addr));
                }
                if (lp->port == 0) {
                        if (!xdr_u_long(xdrs, portp)) {
-                               log(L_WARNING, "recv_rply: [%s] "
+                               note(N_WARNING, "recv_rply: [%s] "
                                        "can't decode reply body!\n",
                                        inet_ntoa(sin->sin_addr));
                                lp = NULL;
@@ -282,6 +352,7 @@ process_entry(int sockfd, notify_list *lp)
 {
        struct sockaddr_in      sin;
        struct status           new_status;
+       stat_chge               new_stat;
        xdrproc_t               func;
        void                    *objp;
        u_int32_t               proc, vers, prog;
@@ -290,7 +361,7 @@ process_entry(int sockfd, notify_list *lp)
        if (lp->addr.s_addr == INADDR_ANY && !try_to_resolve(lp))
                return NL_TIMES(lp);
        if (NL_TIMES(lp) == 0) {
-               log(L_DEBUG, "Cannot notify %s, giving up.\n",
+               note(N_DEBUG, "Cannot notify %s, giving up.\n",
                                        inet_ntoa(NL_ADDR(lp)));
                return 0;
        }
@@ -308,9 +379,20 @@ process_entry(int sockfd, notify_list *lp)
 
                /* Use source address for notify replies */
                sin.sin_addr   = lp->addr;
-
+               /* 
+                * Unless a static hostname has been defined
+                * set the NL_MY_NAME(lp) hostname to the 
+                * one associated with the network interface
+                */
+#ifdef HAVE_IFADDRS_H
+               if (!(run_mode & STATIC_HOSTNAME))
+                       reset_my_name(lp);
+#endif /* HAVE_IFADDRS_H */
                func = (xdrproc_t) xdr_stat_chge;
-               objp = &SM_stat_chge;
+               new_stat.state = MY_STATE;
+               new_stat.mon_name = NL_MY_NAME(lp);
+
+               objp = &new_stat;
                break;
        case NOTIFY_CALLBACK:
                prog = NL_MY_PROG(lp);
@@ -328,14 +410,14 @@ process_entry(int sockfd, notify_list *lp)
                memcpy(new_status.priv, NL_PRIV(lp), SM_PRIV_SIZE);
                break;
        default:
-               log(L_ERROR, "notify_host: unknown notify type %d",
+               note(N_ERROR, "notify_host: unknown notify type %d",
                                NL_TYPE(lp));
                return 0;
        }
 
        lp->xid = xmit_call(sockfd, &sin, prog, vers, proc, func, objp);
        if (!lp->xid) {
-               log(L_WARNING, "notify_host: failed to notify %s\n",
+               note(N_WARNING, "notify_host: failed to notify %s\n",
                                inet_ntoa(lp->addr));
        }
        NL_TIMES(lp) -= 1;
@@ -368,15 +450,15 @@ process_reply(FD_SET_TYPE *rfds)
                        nlist_insert_timer(&notify, lp);
                        return 1;
                }
-               log(L_WARNING, "recv_rply: [%s] service %d not registered",
+               note(N_WARNING, "recv_rply: [%s] service %d not registered",
                        inet_ntoa(lp->addr),
                        NL_TYPE(lp) == NOTIFY_REBOOT? SM_PROG : NL_MY_PROG(lp));
        } else if (NL_TYPE(lp) == NOTIFY_REBOOT) {
-               dprintf(L_DEBUG, "Notification of %s succeeded.",
+               dprintf(N_DEBUG, "Notification of %s succeeded.",
                        NL_MON_NAME(lp));
                xunlink(SM_BAK_DIR, NL_MON_NAME(lp), 0);
        } else {
-               dprintf(L_DEBUG, "Callback to %s (for %d) succeeded.",
+               dprintf(N_DEBUG, "Callback to %s (for %d) succeeded.",
                        NL_MY_NAME(lp), NL_MON_NAME(lp));
        }
        nlist_free(&notify, lp);
@@ -404,16 +486,18 @@ process_notify_list(void)
                        nlist_remove(&notify, entry);
                        nlist_insert_timer(&notify, entry);
                } else if (NL_TYPE(entry) == NOTIFY_CALLBACK) {
-                       log(L_ERROR,
+                       note(N_ERROR,
                                "Can't callback %s (%d,%d), giving up.",
                                        NL_MY_NAME(entry),
                                        NL_MY_PROG(entry),
                                        NL_MY_VERS(entry));
                        nlist_free(&notify, entry);
                } else {
-                       log(L_ERROR,
+                       note(N_ERROR,
                                "Can't notify %s, giving up.",
                                        NL_MON_NAME(entry));
+                       /* PRC: do the HA callout */
+                       ha_callout("del-client", NL_MON_NAME(entry), NL_MY_NAME(entry), -1);
                        xunlink(SM_BAK_DIR, NL_MON_NAME(entry), 0);
                        nlist_free(&notify, entry);
                }