Пример #1
0
/**
 * @brief
 *	Connects to a resource monitor and returns a file descriptor to
 *	talk to it.  If port is zero, use default port.
 *
 * @param[in] host - hostname
 * @param[in] port - port number
 *
 * @return	int
 * @retval	socket stream	success
 * @retval	-1		error
 */
int
openrm(char *host, unsigned int port)
{
	int			stream;
	static	int		first = 1;

	DBPRT(("openrm: host %s port %u\n", host, port))
	pbs_errno = 0;
	if (port == 0)
		port = pbs_conf.manager_service_port;
	DBPRT(("using port %u\n", port))

#if	RPP
	if (first && pbs_conf.pbs_use_tcp == 0) {
		int tryport = IPPORT_RESERVED;

		first = 0;
		while (--tryport > 0) {
			if (rpp_bind(tryport) != -1)
				break;
			if ((errno != EADDRINUSE) && (errno != EADDRNOTAVAIL))
				break;
		}
	}
	stream = rpp_open(host, port);
#else
	if ((stream = socket(AF_INET, SOCK_STREAM, 0)) != -1) {
		int	tryport = IPPORT_RESERVED;
		struct	sockaddr_in	addr;
		struct	hostent		*hp;
		if ((!is_local_host(host))) {
			if ((hp = gethostbyname(host)) == NULL) {
				DBPRT(("host %s not found\n", host))
				pbs_errno = ENOENT;
				return -1;
			}
		}
		memset(&addr, '\0', sizeof(addr));
		addr.sin_family = AF_INET;
		addr.sin_addr.s_addr = htonl(INADDR_ANY);
		while (--tryport > 0) {
			addr.sin_port = htons((u_short)tryport);
			if (bind(stream, (struct sockaddr *)&addr,
				sizeof(addr)) != -1)
				break;
			if ((errno != EADDRINUSE) && (errno != EADDRNOTAVAIL))
				break;
		}
		memset(&addr, '\0', sizeof(addr));
		addr.sin_family = hp->h_addrtype;
		addr.sin_port = htons((unsigned short)port);
		memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);

		if (connect(stream, (struct sockaddr *)&addr,
			sizeof(addr)) == -1) {
			pbs_errno = errno;
#ifdef WIN32
			closesocket(stream);
#else
			close(stream);
#endif
			return -1;
		}
	}
#endif
	pbs_errno = errno;
	if (stream < 0)
		return -1;
	if (addrm(stream) == -1) {
		pbs_errno = errno;
		close_dis(stream);
		return -1;
	}
	return stream;
}
Пример #2
0
int openrm(

  char         *host,  /* I */
  unsigned int  port)  /* I (optional,0=DEFAULT) */

  {
  int                 stream;
  int                 rc;
  int                 retries = 0;

  static unsigned int gotport = 0;

  if (port == 0)
    {
    if (gotport == 0)
      {
      gotport = get_svrport((char *)PBS_MANAGER_SERVICE_NAME, (char *)"tcp",
                            PBS_MANAGER_SERVICE_PORT);
      }  /* END if (gotport == 0) */

    port = gotport;
    }

  if ((stream = socket(AF_INET, SOCK_STREAM, 0)) != -1)
    {

    struct sockaddr_in  addr;
    struct addrinfo    *addr_info;

    if (pbs_getaddrinfo(host, NULL, &addr_info) != 0)
      {
      DBPRT(("host %s not found\n", host))
      close(stream);

      return(ENOENT * -1);
      }

    memset(&addr, '\0', sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);

    while (retries++ < MAX_RETRIES)
      {
      rc = bindresvport(stream, &addr);
      if (rc != 0)
        {
        if (retries >= MAX_RETRIES)
          {
          close(stream);
          return(-1*errno);
          }
        sleep(1);
        }
      else
        break;
      }

    memset(&addr, '\0', sizeof(addr));

    addr.sin_addr = ((struct sockaddr_in *)addr_info->ai_addr)->sin_addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons((unsigned short)port);

    if (connect(stream, (struct sockaddr *)&addr, sizeof(addr)) == -1)
      {
      close(stream);

      return(-1 * errno);
      }
    }    /* END if ((stream = socket(AF_INET,SOCK_STREAM,0)) != -1) */

  if (stream < 0)
    {
    return(-1 * errno);
    }

  if (addrm(stream) == -1)
    {
    close(stream);

    return(-1 * errno);
    }

  return(stream);
  }  /* END openrm() */