/*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
 *
 * n4: A fast userspace NFSv4 server.
 * $Id: bounce.c,v 1.6 2001/07/19 03:56:00 mbp Exp $
 * 
 * Copyright (C) 2001 by Martin Pool <mbp@samba.org>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <includes.h>

                    /*
                     | Have you bounced a friend lately? 
                     |           -- Tigger
                     */

#include <sys/socket.h>

/**
 * Decide whether an error is so severe that the socket should be
 * dropped.
 **/
int n4rpc_err_causes_close(n4err err)
{
    switch (err) {
    case N4ERR_NET:
    case N4ERR_RPC_NOHEADER:
    case N4ERR_RPC_UNEXPECTED:
    case N4ERR_RPC_RPCVERS:
    case N4ERR_RPC_GARBAGE:
    case N4ERR_RPC_SHORT:
        return TRUE;
    default:
        return FALSE;        
    }
}


/**
 * Force a socket to close.  Called when the other party seems really
 * screwed up: either we can't send any useful message, or we failed
 * to send a reply.
 **/
n4err n4rpc_drop_socket(int fd)
{
    n4log_info("communication on fd%d is hopelessly screwed; "
                "dropping the socket",
                fd);

    /* XXX: This will be no good for unconnected UDP sockets.  I'm not
     * sure if we'll ever use that situation. */
    if (shutdown(fd, SHUT_RDWR) == -1) {
        n4log_error("shutdown(fd%d, both) failed: %s",
                     fd, strerror(errno));
        return N4ERR_NET;
    }
    return N4OK;
}


/**
 * Pass a high-level error back as an XDR RPC failure, or close the
 * socket if we can't do that.  We never do both, because then the
 * response might get lost if it's not delivered immediately.
 **/
n4err n4rpc_bounce_n4err(TALLOC_CTX *pool, int fd,
                         guint32 xid, n4err err)
{
    n4err double_err;
    int major = RPC_MSG_ACCEPTED, minor = RPC_SYSTEM_ERR;
    
    switch (err) {
    case N4ERR_RPC_NOHEADER:
    case N4ERR_RPC_UNEXPECTED:
    case N4ERR_NET:
        return n4rpc_drop_socket(fd);
    case N4ERR_RPC_PROG_UNAVAIL:
        minor = RPC_PROG_UNAVAIL;
        break;
    case N4ERR_RPC_PROCEDURE_UNAVAIL:
        minor = RPC_PROC_UNAVAIL;
        break;
    case N4ERR_RPC_PROG_VERS:
        minor = RPC_PROG_MISMATCH;
        break;
    case N4ERR_RPC_GARBAGE:
    case N4ERR_RPC_SHORT:
        minor = RPC_GARBAGE_ARGS;
        break;
    case N4ERR_RPC_RPCVERS:
        major = RPC_MSG_DENIED;
        minor = RPC_MISMATCH;
        break;
    default:
        n4log_warning("don't know how to transport n4err %d, "
                       "using RPC_SYSTEM_ERR", err);
    }

    n4log_info("bounce(n4err=%d, xid=%#x, %s, rpcstat=%d)",
                err, xid, major == RPC_MSG_ACCEPTED ? "accepted" : "denied",
                minor);

    if ((double_err = n4rpc_send_error_reply(pool, fd, xid, major, minor))) {
        n4log_error("double error sending error reply, dropping the socket");
        n4rpc_drop_socket(fd);
    }
    
    return N4OK;
}


/**
 * Send an error message back.  The particular message result is
 * determined by \p repstat and \p astat.
 */
n4err n4rpc_send_error_reply(TALLOC_CTX *pool,
                             int fd,
                             guint32 xid,
                             enum rpc_reply_stat repstat,
                             int substat)
{
    n4outbuf  b;

    CHECK_ERR(n4buf_init_outbuf(&b, pool));
    CHECK_ERR(n4pack_rpc_reply_hdr(&b, xid, repstat, substat));
    CHECK_ERR(n4xdr_send_outbuf(fd, &b));

    /* If we fail while trying to send a reply there is not much else
       we can do from this client's point of view.  Probably the best
       thing to do is to abort the connection.  We need to make sure
       that any higher-level code is not going to try to send a
       failure notification. */

    return N4OK;
}


