Beispiel #1
0
int
__gnat_safe_getservbyport (int port, const char *proto,
  struct servent *ret, char *buf, size_t buflen)
{
  struct servent *rh;
  int ri;

#if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
  (void) getservbyport_r (port, proto, ret, buf, buflen, &rh);
#else
  rh = getservbyport_r (port, proto, ret, buf, buflen);
#endif
  ri = (rh == NULL) ? -1 : 0;
  return ri;
}
Beispiel #2
0
int getservbyport_r(int port, const char *prots,
	struct servent *se, char *buf, size_t buflen, struct servent **res)
{
	int i;
	struct sockaddr_in sin = {
		.sin_family = AF_INET,
		.sin_port = port,
	};

	if (!prots) {
		int r = getservbyport_r(port, "tcp", se, buf, buflen, res);
		if (r) r = getservbyport_r(port, "udp", se, buf, buflen, res);
		return r;
	}
	*res = 0;

	/* Align buffer */
	i = (uintptr_t)buf & sizeof(char *)-1;
	if (!i) i = sizeof(char *);
	if (buflen < 3*sizeof(char *)-i)
		return ERANGE;
	buf += sizeof(char *)-i;
	buflen -= sizeof(char *)-i;

	if (strcmp(prots, "tcp") && strcmp(prots, "udp")) return EINVAL;

	se->s_port = port;
	se->s_proto = (char *)prots;
	se->s_aliases = (void *)buf;
	buf += 2*sizeof(char *);
	buflen -= 2*sizeof(char *);
	se->s_aliases[1] = 0;
	se->s_aliases[0] = se->s_name = buf;

	switch (getnameinfo((void *)&sin, sizeof sin, 0, 0, buf, buflen,
		strcmp(prots, "udp") ? 0 : NI_DGRAM)) {
	case EAI_MEMORY:
	case EAI_SYSTEM:
		return ENOMEM;
	default:
		return ENOENT;
	case 0:
		break;
	}

	*res = se;
	return 0;
}
Beispiel #3
0
static char *tcp_service_name(int port)
{
#ifdef __linux__
    int r;
    struct servent result_buf;
    struct servent *result;
    char buf[2048];

    r = getservbyport_r(htons(port), "tcp", &result_buf,buf, sizeof(buf), &result);

    /* ignore ERANGE - if the result can't fit in 2k, just return unknown */
    if (r != 0 || result == NULL)
        return "unknown";

    return strdup(result_buf.s_name);
#else
    struct servent *result;

    result = getservbyport(htons((unsigned short)port), "tcp");

    if (result == NULL)
        return "unknown";

    return strdup(result->s_name);
#endif
}
Beispiel #4
0
/* libc_hidden_proto(getservbyport) */
struct servent * getservbyport(int port, const char *proto)
{
    struct servent *result;

    __initbuf();
    getservbyport_r(port, proto, &serv, servbuf, SBUFSIZE, &result);
    return result;
}
Beispiel #5
0
/* *********************************************************************** */
int NPSL_GetServByPortBasic(int serv_port, const char *serv_proto,
	NPSL_SERVENT *serv_ent_ptr, void *serv_ent_buffer_ptr,
	unsigned int serv_ent_buffer_length, unsigned int *required_length,
	char *error_text)
{
	int           return_code = NPSL_SUCCESS;
	NPSL_SERVENT *tmp_ent_ptr;

	if (required_length != NULL)
		*required_length = 0;

	if ((serv_proto != NULL) && (!(*serv_proto))) {
		if (error_text != NULL)
			strcpy(error_text,
		"The 'serv_proto' parameter is not 'NULL', but is an empty string.");
		return_code = NPSL_FAILURE;
	}
	else if ((return_code = NPSL_CheckServEntParams(serv_ent_ptr,
		serv_ent_buffer_ptr, serv_ent_buffer_length, error_text)) !=
		NPSL_SUCCESS)
		;
	else {
		int tmp_port = COMPAT_CAST_static(int,
			ntohs(COMPAT_CAST_static(unsigned short, serv_port)));
#if NPSL_HAS_GETSERVBYPORT_R
		if ((tmp_ent_ptr = getservbyport_r(tmp_port, serv_proto,
			serv_ent_ptr, serv_ent_buffer_ptr,
			((int) serv_ent_buffer_length))) == NULL) {
			if (error_text != NULL) {
				sprintf(error_text,
					"%s '%s()' for service port '%d', protocol '%-.127s': ",
					"Unable to get service entry with", "getservbyport_r",
					serv_port, (serv_proto != NULL) ? serv_proto : "*NULL*");
				NPSL_AppendLastErrorString(0, NPSL_MAX_ERROR_TEXT, error_text);
			}
			return_code = NPSL_SUPP_MapLastError();
		}
#else
		if ((tmp_ent_ptr = getservbyport(tmp_port, serv_proto)) == NULL) {
			if (error_text != NULL) {
				sprintf(error_text,
					"%s '%s()' for service port '%d', protocol '%-.127s': ",
					"Unable to get service entry with", "getservbyport",
					serv_port, (serv_proto != NULL) ? serv_proto : "*NULL*");
				NPSL_AppendLastErrorString(0, NPSL_MAX_ERROR_TEXT, error_text);
			}
			return_code = NPSL_SUPP_MapLastError();
		}
		else
			return_code = NPSL_CopyServEntFlatBasic(tmp_ent_ptr, serv_ent_ptr,
				serv_ent_buffer_ptr, serv_ent_buffer_length, required_length,
				error_text);
#endif // #if NPSL_HAS_GETSERVBYPORT_R
	}

	return(return_code);
}
Beispiel #6
0
struct servent *
getservbyport(int port, const char *proto)
{
	struct servent *s;

	mutex_lock(&_servent_mutex);
	s = getservbyport_r(port, proto, &_servent_data.serv, &_servent_data);
	mutex_unlock(&_servent_mutex);
	return (s);
}
Beispiel #7
0
struct servent *
getservbyport(int port, const char *proto)
{
	extern struct servent_data _servent_data;
	static struct servent serv;

	if (getservbyport_r(port, proto, &serv, &_servent_data) != 0)
		return (NULL);
	return (&serv);
}
Beispiel #8
0
/*
 * Set the service name on the result buffer is not NULL.
 * return (-1) if the buffer is too small.
 */
static int
_servname(struct asr_query *as)
{
	struct servent		 s;
#ifdef HAVE_STRUCT_SERVENT_DATA
	struct servent_data	 sd;
#endif
	int			 port, r;
	char			*buf = as->as.ni.servname;
	size_t			 buflen = as->as.ni.servnamelen;

	if (as->as.ni.servname == NULL || as->as.ni.servnamelen == 0)
		return (0);

	if (as->as.ni.sa.sa.sa_family == AF_INET)
		port = as->as.ni.sa.sain.sin_port;
	else
		port = as->as.ni.sa.sain6.sin6_port;

	if (!(as->as.ni.flags & NI_NUMERICSERV)) {
#ifdef HAVE_STRUCT_SERVENT_DATA
		memset(&sd, 0, sizeof (sd));
#endif
#ifdef HAVE_GETSERVBYPORT_R_4_ARGS
		r = getservbyport_r(port,
		    (as->as.ni.flags & NI_DGRAM) ? "udp" : "tcp",
		    &s, &sd);
#else
		r = -1;
#endif
		if (r != -1) {
			r = strlcpy(buf, s.s_name, buflen) >= buflen;
#ifdef HAVE_ENDSERVENT_R
			endservent_r(&sd);
#endif
			return (r ? -1 : 0);
		}
	}

	r = snprintf(buf, buflen, "%u", ntohs(port));
	if (r == -1 || r >= (int)buflen)
		return (-1);

	return (0);
}
Beispiel #9
0
static int
do_ipv46(char *host, size_t hostlen, char *serv, size_t servlen,
		 void *aptr, size_t alen, int family, int port)
{
	struct hostent		*hptr, hent;
	struct servent		*sptr, sent;
	char				hentbuf[HENTBUFSIZ];

	if (hostlen > 0) {
#ifdef	REENTRANT
		hptr = gethostbyaddr_r(aptr, alen, family,
							   &hent, hentbuf, HENTBUFSIZ, &h_errno);
#else
		hptr = gethostbyaddr(aptr, alen, family);
#endif
		if (hptr != NULL && hptr->h_name != NULL)
			strncpy(host, hptr->h_name, hostlen);
		else
			return(1);
	}

	if (servlen > 0) {
		/*
		 * Notice that we do not have enough information to pass a
		 * "protocol" argument to getservbyport(), so the assumption
		 * is that the protocol (TCP or UDP) does not matter.
		 */
#ifdef	REENTRANT
		sptr = getservbyport_r(port, NULL,
							   &sent, hentbuf, HENTBUFSIZ);
#else
		sptr = getservbyport(port, NULL);
#endif
		if (sptr != NULL && sptr->s_name != NULL)
			strncpy(serv, sptr->s_name, servlen);
		else
			return(1);
	}
	return(0);
}
Beispiel #10
0
static gboolean
tf_getent_services(gchar *key, gchar *member_name, GString *result)
{
  struct servent serv, *res;
  gint64 d;
  gboolean is_num;
  char buf[4096];

  if ((is_num = parse_dec_number(key, &d)) == TRUE)
    getservbyport_r((int)ntohs(d), NULL, &serv, buf, sizeof(buf), &res);
  else
    getservbyname_r(key, NULL, &serv, buf, sizeof(buf), &res);

  if (res == NULL)
    return TRUE;

  if (is_num)
    g_string_append(result, res->s_name);
  else
    g_string_append_printf(result, "%i", htons(res->s_port));

  return TRUE;
}
Beispiel #11
0
/* 
 * Print the textual representation of the port set on "sa".
 *
 * If proto is not NULL, it is used as parameter to "getservbyport_r(3)" to
 * return a service name. If it's not set, or if no matching service is found,
 * it prints the portno.
 *
 * Return the total length of the string it tried to create or 0 if an error
 * occured, in which case errno is set.  On success, the constructed string
 * is guaranteed to be NUL-terminated.  Overflow must be detected by checking
 * the returned size against buflen.
 */
static size_t
asr_print_port(const struct sockaddr *sa, const char *proto, char *buf, size_t buflen)
{
	struct servent s;
	struct servent_data sd;
	int port, r, saved_errno;
	size_t n;

	switch(sa->sa_family) {
	case AF_INET:
		port = SA_IN(sa)->sin_port;
		break;
	case AF_INET6:
		port = SA_IN6(sa)->sin6_port;
		break;
	default:
		errno = EINVAL;
		return (0);
	}

	if (proto) {
		memset(&sd, 0, sizeof (sd));
		saved_errno = errno;
		if (getservbyport_r(port, proto, &s, &sd) != -1) {
			n = strlcpy(buf, s.s_name, buflen);
			endservent_r(&sd);
			return (n);
		}
		errno = saved_errno;
	}

	r = snprintf(buf, buflen, "%u", ntohs(port));
	if (r == -1) 	/* Actually, this can not happen */
		return (0);

	return (r);
}
Beispiel #12
0
static char *lookup_service(unsigned short port, int flags,
                            char *buf, size_t buflen)
{
  const char *proto;
  struct servent *sep;
#ifdef HAVE_GETSERVBYPORT_R
  struct servent se;
#endif
  char tmpbuf[4096];

  if (port)
    {
      if (flags & ARES_NI_NUMERICSERV)
        sep = NULL;
      else
        {
          if (flags & ARES_NI_UDP)
            proto = "udp";
          else if (flags & ARES_NI_SCTP)
            proto = "sctp";
          else if (flags & ARES_NI_DCCP)
            proto = "dccp";
          else
            proto = "tcp";
#ifdef HAVE_GETSERVBYPORT_R
          sep = &se;
          memset(tmpbuf, 0, sizeof(tmpbuf));
#if GETSERVBYPORT_R_ARGS == 6
          if (getservbyport_r(port, proto, &se, (void *)tmpbuf, sizeof(tmpbuf), &sep) != 0)
            sep = NULL;
#elif GETSERVBYPORT_R_ARGS == 5
          sep = getservbyport_r(port, proto, &se, (void *)tmpbuf, sizeof(tmpbuf));
#elif GETSERVBYPORT_R_ARGS == 4
          if (getservbyport_r(port, proto, &se, (void *)tmpbuf) != 0)
            sep = NULL;
#else
          /* Lets just hope the OS uses TLS! */
          sep = getservbyport(port, proto);
#endif
#else
          /* Lets just hope the OS uses TLS! */
#if (defined(NETWARE) && !defined(__NOVELL_LIBC__))
          sep = getservbyport(port, (char*)proto);
#else
          sep = getservbyport(port, proto);
#endif
#endif
        }
      if (sep && sep->s_name)
        /* get service name */
        strcpy(tmpbuf, sep->s_name);
      else
        /* get port as a string */
        sprintf(tmpbuf, "%u", (unsigned int)ntohs(port));
      if (strlen(tmpbuf) < buflen)
        /* return it if buffer big enough */
        strcpy(buf, tmpbuf);
      else
        /* avoid reusing previous one */
        buf[0] = '\0';
      return buf;
    }
  buf[0] = '\0';
  return NULL;
}
void TcpSocket::getServicePort( struct servent    * pServicePort, const std::string & pService ) const
{
	//
	// Initialise the service port details.
	//
	memset( pServicePort, 0, sizeof( struct servent ) );

	//
	// Port has been specified as a number so get all the relevant details
	// using the port number.
	//
	if ( 0 != atoi( pService.c_str() ) )
	{

		struct servent *lvServicePtr;

#if defined( SOLARIS )

		char lvServiceBuffer[1024];
		memset( lvServiceBuffer, 0, sizeof( lvServiceBuffer ) );
		lvServicePtr = getservbyport_r( atoi( pService.c_str() ), "tcp",
			pServicePort,
			lvServiceBuffer,
			sizeof( lvServiceBuffer ) );

#else // !defined( SOLARIS )

		lvServicePtr = getservbyport( atoi( pService.c_str() ), "tcp" );

#endif // !defined( SOLARIS )

		if ( 0 != lvServicePtr )
		{
			memcpy( pServicePort, lvServicePtr, sizeof( struct servent ) );
		}
		else
		{
			pServicePort->s_port = htons( atoi( pService.c_str() ) );
			pServicePort->s_proto = "tcp";
		}

	}

	//
	// Port name specified.
	//
	else
	{

		if ( 0 == atoi( pService.c_str() ) )
		{

			struct servent *lvServicePtr;

#if defined( SOLARIS )
			char lvServiceBuffer[1024];
			memset( lvServiceBuffer, 0, sizeof( lvServiceBuffer ) );
			lvServicePtr = getservbyname_r( pService.c_str(), "tcp",
				pServicePort,
				lvServiceBuffer,
				sizeof( lvServiceBuffer ) );
#else // !defined( SOLARIS )

			lvServicePtr = getservbyname( pService.c_str(), "tcp" );

#endif // !defined( SOLARIS )

			if ( 0 == lvServicePtr )
			{

				//throw exception
				std::ostringstream streamErrInfo;
				std::string strErrorInfo;

				streamErrInfo << "Unknown service";
				strErrorInfo = streamErrInfo.str();

				throw std::exception(strErrorInfo.c_str());
			}
			memcpy( pServicePort, lvServicePtr, sizeof( struct servent ) );

		}
	}
}
Beispiel #14
0
int
ServicePortNumberToName(unsigned short port, char *const dst, const size_t dsize, const int proto)
{
#if defined(HAVE_GETSERVBYNAME_R) && (defined(AIX) || defined(TRU64UNIX) || defined(DIGITAL_UNIX))
    struct servent *sp;
#elif defined(HAVE_GETSERVBYNAME_R) && (defined(LINUX) || defined(SOLARIS) || defined(IRIX) || defined(BSDOS))
    struct servent se, *sp;
    char spbuf[256];
#else
    struct servent *sp;
#endif

    sp = NULL;
#if defined(HAVE_GETSERVBYPORT_R) && (defined(SOLARIS) || defined(IRIX) || defined(BSDOS))
    if ((sp == NULL) && ((proto == 0) || (proto == 't')))
    {
        memset(spbuf, 0, sizeof(spbuf));
        sp = getservbyport_r((int) htons(port), "tcp", &se, spbuf, sizeof(spbuf));
    }
    if ((sp == NULL) && ((proto == 0) || (proto == 'u')))
    {
        memset(spbuf, 0, sizeof(spbuf));
        sp = getservbyport_r((int) htons(port), "udp", &se, spbuf, sizeof(spbuf));
    }
#elif defined(HAVE_GETSERVBYPORT_R) && defined(LINUX)
    if ((sp == NULL) && ((proto == 0) || (proto == 't')))
    {
        memset(spbuf, 0, sizeof(spbuf));
        if (getservbyport_r((int) htons(port), "tcp", &se, spbuf, sizeof(spbuf), &sp) != 0)
            sp = NULL;
    }
    if ((sp == NULL) && ((proto == 0) || (proto == 'u')))
    {
        memset(spbuf, 0, sizeof(spbuf));
        if (getservbyport_r((int) htons(port), "udp", &se, spbuf, sizeof(spbuf), &sp) != 0)
            sp = NULL;
    }
#elif defined(HAVE_GETSERVBYPORT_R) && defined(AIX)
    {
        struct servent_data sed;
        if ((sp == NULL) && ((proto == 0) || (proto == 't')))
        {
            memset(&sed, 0, sizeof(sed));
            if (getservbyport_r((int) htons(port), "tcp", sp, &sed) != 0)
                sp = NULL;
        }
        if ((sp == NULL) && ((proto == 0) || (proto == 'u')))
        {
            memset(&sed, 0, sizeof(sed));
            if (getservbyport_r((int) htons(port), "udp", sp, &sed) != 0)
                sp = NULL;
        }
    }
#else
    /* Note: getservbyport is already threadsafe on: HP-UX, Tru64 */
    if ((sp == NULL) && ((proto == 0) || (proto == 't')))
    {
        sp = getservbyport((int) htons(port), "tcp");
    }
    if ((sp == NULL) && ((proto == 0) || (proto == 'u')))
    {
        sp = getservbyport((int) htons(port), "ucp");
    }
#endif

    if (sp != NULL)
    {
        strncpy(dst, sp->s_name, dsize);
        dst[dsize - 1] = '\0';
        return (1);
    }

#ifdef HAVE_SNPRINTF
    snprintf(dst, dsize,
#else
    sprintf(dst,
#endif
             "%u", (unsigned int) port);

    return (0);	/* error */
}	/* ServicePortNumberToName */