/* * OpenPBS (Portable Batch System) v2.3 Software License * * Copyright (c) 1999-2000 Veridian Information Solutions, Inc. * All rights reserved. * * --------------------------------------------------------------------------- * For a license to use or redistribute the OpenPBS software under conditions * other than those described below, or to purchase support for this software, * please contact Veridian Systems, PBS Products Department ("Licensor") at: * * www.OpenPBS.org +1 650 967-4675 sales@OpenPBS.org * 877 902-4PBS (US toll-free) * --------------------------------------------------------------------------- * * This license covers use of the OpenPBS v2.3 software (the "Software") at * your site or location, and, for certain users, redistribution of the * Software to other sites and locations. Use and redistribution of * OpenPBS v2.3 in source and binary forms, with or without modification, * are permitted provided that all of the following conditions are met. * After December 31, 2001, only conditions 3-6 must be met: * * 1. Commercial and/or non-commercial use of the Software is permitted * provided a current software registration is on file at www.OpenPBS.org. * If use of this software contributes to a publication, product, or * service, proper attribution must be given; see www.OpenPBS.org/credit.html * * 2. Redistribution in any form is only permitted for non-commercial, * non-profit purposes. There can be no charge for the Software or any * software incorporating the Software. Further, there can be no * expectation of revenue generated as a consequence of redistributing * the Software. * * 3. Any Redistribution of source code must retain the above copyright notice * and the acknowledgment contained in paragraph 6, this list of conditions * and the disclaimer contained in paragraph 7. * * 4. Any Redistribution in binary form must reproduce the above copyright * notice and the acknowledgment contained in paragraph 6, this list of * conditions and the disclaimer contained in paragraph 7 in the * documentation and/or other materials provided with the distribution. * * 5. Redistributions in any form must be accompanied by information on how to * obtain complete source code for the OpenPBS software and any * modifications and/or additions to the OpenPBS software. The source code * must either be included in the distribution or be available for no more * than the cost of distribution plus a nominal fee, and all modifications * and additions to the Software must be freely redistributable by any party * (including Licensor) without restriction. * * 6. All advertising materials mentioning features or use of the Software must * display the following acknowledgment: * * "This product includes software developed by NASA Ames Research Center, * Lawrence Livermore National Laboratory, and Veridian Information * Solutions, Inc. * Visit www.OpenPBS.org for OpenPBS software support, * products, and information." * * 7. DISCLAIMER OF WARRANTY * * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. ANY EXPRESS * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT * ARE EXPRESSLY DISCLAIMED. * * IN NO EVENT SHALL VERIDIAN CORPORATION, ITS AFFILIATED COMPANIES, OR THE * U.S. GOVERNMENT OR ANY OF ITS AGENCIES BE LIABLE FOR ANY DIRECT OR INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * This license will be governed by the laws of the Commonwealth of Virginia, * without reference to its choice of law rules. */ #include /* the master config generated by configure */ #include #include #include #include #include #include #include #include #include #include #include "portability.h" #include "server_limits.h" #include "net_connect.h" #include "mcom.h" #include #include #include #ifdef _AIX #include #endif #ifdef __APPLE__ /* this is a hack for the missing bindresvport declaration on OS X the function works fine but its use will generate a compiler warning if -Wall is used with gcc */ int bindresvport(int sd, struct sockaddr_in *sin); #endif /** * Returns the max number of possible file descriptors (as * per the OS limits). * */ int get_max_num_descriptors(void) { static int max_num_descriptors = 0; if (max_num_descriptors <= 0) max_num_descriptors = getdtablesize(); return(max_num_descriptors); } /* END get_num_max_descriptors() */ /** * Returns the number of bytes needed to allocate * a fd_set array that can hold all of the possible * socket descriptors. */ int get_fdset_size(void) { unsigned int MaxNumDescriptors = 0; int NumFDSetsNeeded = 0; int NumBytesInFDSet = 0; int Result = 0; MaxNumDescriptors = get_max_num_descriptors(); NumBytesInFDSet = sizeof(fd_set); NumFDSetsNeeded = MaxNumDescriptors / FD_SETSIZE; if (MaxNumDescriptors < FD_SETSIZE) { /* the default size already provides sufficient space */ Result = NumBytesInFDSet; } else if ((MaxNumDescriptors % FD_SETSIZE) > 0) { /* we need to allocate more memory to cover extra * bits--add an extra FDSet worth of memory to the size */ Result = (NumFDSetsNeeded + 1) * NumBytesInFDSet; } else { /* division was exact--we know exactly how many bytes we need */ Result = NumFDSetsNeeded * NumBytesInFDSet; } return(Result); } /* END get_fdset_size() */ /* ** wait for connect to complete. We use non-blocking sockets, ** so have to wait for completion this way. */ static int await_connect( long timeout, /* I */ int sockd) /* I */ { int n, val, rc; fd_set *BigFDSet = NULL; struct timeval tv; torque_socklen_t len; /* * some operating systems (like FreeBSD) cannot have a value for tv.tv_usec * larger than 1,000,000 so we need to split up the timeout duration between * seconds and microseconds */ tv.tv_sec = timeout / 1000000; tv.tv_usec = timeout % 1000000; /* calculate needed size for fd_set in select() */ BigFDSet = (fd_set *)calloc(1,sizeof(char) * get_fdset_size()); FD_SET(sockd, BigFDSet); if ((n = select(sockd+1,0,BigFDSet,0,&tv)) != 1) { /* FAILURE: socket not ready for write */ free(BigFDSet); return(-1); } len = sizeof(val); rc = getsockopt(sockd, SOL_SOCKET, SO_ERROR, &val, &len); if ((rc == 0) && (val == 0)) { /* SUCCESS: no failures detected */ free(BigFDSet); return(0); } errno = val; /* FAILURE: socket error detected */ free(BigFDSet); return(-1); } /* END await_connect() */ /* global */ long MaxConnectTimeout = 5000000; /* in microseconds */ /* * client_to_svr - connect to a server * * Perform socket/tcp/ip stuff to connect to a server. * * Returns: >=0 the socket obtained, or * PBS_NET_RC_FATAL (-1) if fatal error, just quit, or * PBS_NET_RC_RETRY (-2) if temp error, should retry * * NOTE: the server's host address and port were chosen as parameters * rather than their names to possibly save extra look-ups. It seems likely * that the caller "might" make several calls to the same host or different * hosts with the same port. Let the caller keep the addresses around * rather than look it up each time. * * NOTE: will wait up to MaxConnectTimeout microseconds for transient network failures */ /* NOTE: create new connection on reserved port to validate root/trusted authority */ int client_to_svr( pbs_net_t hostaddr, /* I - internet addr of host */ unsigned int port, /* I - port to which to connect */ int local_port, /* I - BOOLEAN: not 0 to use local reserved port */ char *EMsg) /* O (optional,minsize=1024) */ { const char id[] = "client_to_svr"; struct sockaddr_in local; struct sockaddr_in remote; int sock; int rc; int flags; int one = 1; memset(&local, 0, sizeof(local)); local.sin_family = AF_INET; sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { if (EMsg != NULL) sprintf(EMsg, "cannot create socket in %s - errno: %d %s", id, errno, strerror(errno)); return(PBS_NET_RC_FATAL); } /* We use our own connection table to track our sockets. sock is the index. We can't exceed the table size. */ if (sock >= PBS_NET_MAX_CONNECTIONS) { if (EMsg != NULL) sprintf(EMsg, "PBS_NET_MAX_CONNECTIONS exceeded in %s", id); close(sock); /* too many connections */ return(PBS_NET_RC_RETRY); } /* we are going to go non-blocking */ flags = fcntl(sock, F_GETFL); flags |= O_NONBLOCK; fcntl(sock, F_SETFL, flags); /* we want to be able to reuse the socket address */ rc = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); if (rc != 0) { if (EMsg != NULL) sprintf(EMsg, "could not set socket option SO_REUSEADDR in %s -- errno: %d %s", id, errno, strerror(errno)); close(sock); return(PBS_NET_RC_RETRY); } if (local_port == 0) { /* we will just get a dynamic port */ rc = bind(sock, &local, sizeof(local)); if (rc != 0) { if (EMsg != NULL) sprintf(EMsg, "could not bind a port in %s -- errno: %d %s", id, errno, strerror(errno)); close(sock); return(PBS_NET_RC_RETRY); } } else { #ifndef NOPRIVPORTS /* privileged ports are on. */ rc = bindresvport(sock, &local); if (rc != 0) { if (EMsg != NULL) sprintf(EMsg, "could not bind a reserve port in %s -- errno: %d %s", id, errno, strerror(errno)); close(sock); return(PBS_NET_RC_RETRY); } #else /* we will just get a dynamic port */ rc = bind(sock, &local, sizeof(local)); if (rc != 0) { if (EMsg != NULL) sprintf(EMsg, "could not bind a port in %s -- errno: %d %s", id, errno, strerror(errno)); close(sock); return(PBS_NET_RC_RETRY); } #endif } /* time to connect to the remote target */ remote.sin_addr.s_addr = htonl(hostaddr); remote.sin_port = htons((unsigned short)port); remote.sin_family = AF_INET; rc = connect(sock, (struct sockaddr *)&remote, sizeof(remote)); if (rc != 0) { switch(errno) { case EINPROGRESS: case EALREADY: { if (await_connect(MaxConnectTimeout, sock) == 0) { break; } close(sock); sock = PBS_NET_RC_RETRY; } default: { close(sock); sock = PBS_NET_RC_RETRY; } } } return(sock); } /* END client_to_svr() */ /* END net_client.c */