Пример #1
0
/* *********************************************************************** */
int NPSL_GetProtoByNameBasic(const char *proto_name,
	NPSL_PROTOENT *proto_ent_ptr, void *proto_ent_buffer_ptr,
	unsigned int proto_ent_buffer_length, unsigned int *required_length,
	char *error_text)
{
	int            return_code = NPSL_SUCCESS;
	NPSL_PROTOENT *tmp_ent_ptr;

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

	if (proto_name == NULL) {
		if (error_text != NULL)
			strcpy(error_text, "The 'proto_name' parameter is 'NULL'.");
		return_code = NPSL_FAILURE;
	}
	else if (!(*proto_name)) {
		if (error_text != NULL)
			strcpy(error_text, "The 'proto_name' parameter is an empty string.");
		return_code = NPSL_FAILURE;
	}
	else if ((return_code = NPSL_CheckProtoEntParams(proto_ent_ptr,
		proto_ent_buffer_ptr, proto_ent_buffer_length, error_text)) !=
		NPSL_SUCCESS)
		;
	else {
#if NPSL_HAS_GETPROTOBYNAME_R
		if ((tmp_ent_ptr = getprotobyname_r(proto_name,
			proto_ent_ptr, proto_ent_buffer_ptr,
			((int) proto_ent_buffer_length))) == NULL) {
			if (error_text != NULL) {
				sprintf(error_text,
					"%s '%s()' for protocol name '%-.127s': ",
					"Unable to get protocol entry with", "getprotobyname_r",
					proto_name);
				NPSL_AppendLastErrorString(0, NPSL_MAX_ERROR_TEXT, error_text);
			}
			return_code = NPSL_SUPP_MapLastError();
		}
#else
		if ((tmp_ent_ptr = getprotobyname(proto_name)) == NULL) {
			if (error_text != NULL) {
				sprintf(error_text,
					"%s '%s()' for protocol name '%-.127s': ",
					"Unable to get protocol entry with", "getprotobyname",
					proto_name);
				NPSL_AppendLastErrorString(0, NPSL_MAX_ERROR_TEXT, error_text);
			}
			return_code = NPSL_SUPP_MapLastError();
		}
		else
			return_code = NPSL_CopyProtoEntFlatBasic(tmp_ent_ptr, proto_ent_ptr,
				proto_ent_buffer_ptr, proto_ent_buffer_length, required_length,
				error_text);
#endif // #if NPSL_HAS_GETPROTOBYNAME_R
	}

	return(return_code);
}
Пример #2
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);
}
Пример #3
0
/* *********************************************************************** */
int NPSL_SendTo(NPSL_SOCKET_HANDLE socket_handle, const char *buffer,
	unsigned int buffer_length, int flags, const NPSL_SOCKADDR *addr,
	NPSL_SOCKLEN_T addrlen, unsigned int *send_count, char *error_text)
{
	int                  return_code;
	NPSL_SEND_RECV_LEN_T data_length;

	*send_count = 0;

	if (buffer_length > ((unsigned int) INT_MAX)) {
		if (error_text != NULL)
			sprintf(error_text, "%s (%u) %s (%d).",
				"The specified buffer length", buffer_length,
				"exceeds the maximum permissible signed integer value", INT_MAX);
		return_code = NPSL_FAILURE;
	}
	else {
		if ((data_length = sendto(socket_handle, buffer, ((int) buffer_length),
			flags, addr, addrlen)) != NPSL_SOCKET_ERROR) {
			*send_count  = ((unsigned int) data_length);
			 return_code = NPSL_SUCCESS;
		}
		else {
			if (error_text != NULL)
				NPSL_AppendLastErrorString(0, NPSL_MAX_ERROR_TEXT,
					strcpy(error_text,
"Attempt to send a datagram message on a socket with 'sendto()' failed: "));
			return_code = NPSL_SUPP_MapLastError();
		}
	}

	return(return_code);
}
Пример #4
0
/* *********************************************************************** */
int NPSL_Select(int fd_set_size, NPSL_FD_SET_T *read_fds,
	NPSL_FD_SET_T *write_fds, NPSL_FD_SET_T *error_fds,
	const struct timeval *time_out, unsigned int *ready_count, char *error_text)
{
	int return_code;
	int select_count;

	*ready_count = 0;

	/*
		Cast needed to support benighted declarations of 'select()'...
	*/
	if ((select_count = select(fd_set_size, read_fds, write_fds, error_fds,
		const_cast<struct timeval *>(time_out))) != NPSL_SOCKET_ERROR) {
		*ready_count = ((unsigned int) select_count);
		 return_code = NPSL_SUCCESS;
	}
	else {
		if (error_text != NULL)
			NPSL_AppendLastErrorString(0, NPSL_MAX_ERROR_TEXT,
				strcpy(error_text,
				"Attempt to perform a select with 'select()' failed: "));
		return_code = NPSL_SUPP_MapLastError();
	}

	return(return_code);
}
Пример #5
0
/* *********************************************************************** */
int NPSL_Recv(NPSL_SOCKET_HANDLE socket_handle, char *buffer,
	unsigned int buffer_length, int flags, unsigned int *recv_count,
	char *error_text)
{
	int                  return_code;
	NPSL_SEND_RECV_LEN_T data_length;

	*recv_count = 0;

	if (buffer_length > ((unsigned int) INT_MAX)) {
		if (error_text != NULL)
			sprintf(error_text, "%s (%u) %s (%d).",
				"The specified buffer length", buffer_length,
				"exceeds the maximum permissible signed integer value", INT_MAX);
		return_code = NPSL_FAILURE;
	}
	else {
		if ((data_length = recv(socket_handle, buffer, ((int) buffer_length),
			flags)) != NPSL_SOCKET_ERROR) {
			*recv_count  = ((unsigned int) data_length);
			 return_code = NPSL_SUCCESS;
		}
		else {
			if (error_text != NULL)
				NPSL_AppendLastErrorString(0, NPSL_MAX_ERROR_TEXT,
					strcpy(error_text,
"Attempt to receive a message from a socket with 'recv()' failed: "));
			return_code = NPSL_SUPP_MapLastError();
		}
	}

	return(return_code);
}
Пример #6
0
/* *********************************************************************** */
int NPSL_GetProtoByNumberBasic(int proto_number, NPSL_PROTOENT *proto_ent_ptr,
	void *proto_ent_buffer_ptr, unsigned int proto_ent_buffer_length,
	unsigned int *required_length, char *error_text)
{
	int            return_code = NPSL_SUCCESS;
	NPSL_PROTOENT *tmp_ent_ptr;

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

	if ((return_code = NPSL_CheckProtoEntParams(proto_ent_ptr,
		proto_ent_buffer_ptr, proto_ent_buffer_length, error_text)) !=
		NPSL_SUCCESS)
		;
	else {
#if NPSL_HAS_GETPROTOBYPORT_R
		if ((tmp_ent_ptr = getprotobynumber_r(proto_number, proto_ent_ptr,
			proto_ent_buffer_ptr, ((int) proto_ent_buffer_length))) == NULL) {
			if (error_text != NULL) {
				sprintf(error_text,
					"%s '%s()' for protocol number '%d': ",
					"Unable to get protocol entry with", "getprotobynumber_r",
					proto_number);
				NPSL_AppendLastErrorString(0, NPSL_MAX_ERROR_TEXT, error_text);
			}
			return_code = NPSL_SUPP_MapLastError();
		}
#else
		if ((tmp_ent_ptr = getprotobynumber(proto_number)) == NULL) {
			if (error_text != NULL) {
				sprintf(error_text,
					"%s '%s()' for protocol number '%d': ",
					"Unable to get protocol entry with", "getprotobynumber",
					proto_number);
				NPSL_AppendLastErrorString(0, NPSL_MAX_ERROR_TEXT, error_text);
			}
			return_code = NPSL_SUPP_MapLastError();
		}
		else
			return_code = NPSL_CopyProtoEntFlatBasic(tmp_ent_ptr, proto_ent_ptr,
				proto_ent_buffer_ptr, proto_ent_buffer_length, required_length,
				error_text);
#endif // #if NPSL_HAS_GETPROTOBYPORT_R
	}

	return(return_code);
}
Пример #7
0
/* *********************************************************************** */
int NPSL_Accept(NPSL_SOCKET_HANDLE in_socket_handle,
	NPSL_SOCKET_HANDLE *out_socket_handle, NPSL_SOCKADDR *addr,
	NPSL_SOCKLEN_T *addrlen, char *error_text)
{
	int            return_code;
	NPSL_SOCKADDR  tmp_addr;
	NPSL_SOCKLEN_T tmp_addrlen;

	*out_socket_handle = NPSL_INVALID_SOCKET;

	/*
		Permits the caller to pass NULLs in cases where they're not
		interested in the client socket address --- which is retrievable
		using the NPSL_GetPeerName() interface anyway.

		In general, the 'addr' and 'addrlen' parameters should both be 'NULL'
		or both be non 'NULL'.
	*/
	if ((addr == NULL) && (addrlen != NULL)) {
		if (error_text != NULL)
			strcpy(error_text, "Invalid call to 'NPSL_Accept()': The 'addr' "
				"parameter is 'NULL', but the 'addrlen' parameter is non-"
				"NULL'.");
		return_code = NPSL_SUPP_MapLastError();
	}
	else if ((addr != NULL) && (addrlen == NULL)) {
		if (error_text != NULL)
			strcpy(error_text, "Invalid call to 'NPSL_Accept()': The 'addr' "
				"parameter is not 'NULL', but the 'addrlen' parameter is 'NULL'.");
		return_code = NPSL_SUPP_MapLastError();
	}
	else {
		if ((addr == NULL) && (addrlen == NULL)) {
			addr    = &tmp_addr;
			addrlen = &tmp_addrlen;
		}
		if ((*out_socket_handle = accept(in_socket_handle, addr,
			addrlen)) == NPSL_INVALID_SOCKET) {
			if (error_text != NULL)
				NPSL_AppendLastErrorString(0, NPSL_MAX_ERROR_TEXT,
					strcpy(error_text,
			"Attempt to accept a socket connection with 'accept()' failed: "));
			return_code = NPSL_SUPP_MapLastError();
		}
		else
			return_code = NPSL_SUCCESS;
	}

	return(return_code);
}
Пример #8
0
/* *********************************************************************** */
int NPSL_ShutDown(NPSL_SOCKET_HANDLE socket_handle, int shut_down_flag,
	char *error_text)
{
	int return_code;

	if (shutdown(socket_handle, shut_down_flag) == NPSL_SOCKET_ERROR) {
		if (error_text != NULL)
			NPSL_AppendLastErrorString(0, NPSL_MAX_ERROR_TEXT,
				strcpy(error_text,
				"Attempt to shutdown a socket with 'shutdown()' failed: "));
		return_code = NPSL_SUPP_MapLastError();
	}
	else
		return_code = NPSL_SUCCESS;

	return(return_code);
}
Пример #9
0
/* *********************************************************************** */
int NPSL_SetSockOpt(NPSL_SOCKET_HANDLE socket_handle, int opt_level,
	int opt_name, const void *opt_value, int opt_length, char *error_text)
{
	int return_code;

	if (setsockopt(socket_handle, opt_level, opt_name,
		((const NPSL_NATIVE_SocketOptionValueType) opt_value), opt_length) !=
		NPSL_SOCKET_OK) {
		if (error_text != NULL)
			NPSL_AppendLastErrorString(0, NPSL_MAX_ERROR_TEXT,
				strcpy(error_text,
"Attempt to set the socket options with 'setsockopt()' failed: "));
		return_code = NPSL_SUPP_MapLastError();
	}
	else
		return_code = NPSL_SUCCESS;

	return(return_code);
}
Пример #10
0
/* *********************************************************************** */
int NPSL_GetSockOpt(NPSL_SOCKET_HANDLE socket_handle, int opt_level,
	int opt_name, void *opt_value, int *opt_length, char *error_text)
{
	int return_code;

#ifdef __linux__
	if (getsockopt(socket_handle, opt_level, opt_name,
		((NPSL_NATIVE_SocketOptionValueType) opt_value),
		reinterpret_cast<socklen_t *>(opt_length)) != NPSL_SOCKET_OK) {
#else
	if (getsockopt(socket_handle, opt_level, opt_name,
		((NPSL_NATIVE_SocketOptionValueType) opt_value), opt_length) !=
		NPSL_SOCKET_OK) {
#endif // #ifdef __linux__
		if (error_text != NULL)
			NPSL_AppendLastErrorString(0, NPSL_MAX_ERROR_TEXT,
				strcpy(error_text,
"Attempt to get the socket options with 'getsockopt()' failed: "));
		return_code = NPSL_SUPP_MapLastError();
	}
	else
		return_code = NPSL_SUCCESS;

	return(return_code);
}
/* *********************************************************************** */

/* *********************************************************************** */
int NPSL_GetSockOpt_RcvBuf(NPSL_SOCKET_HANDLE socket_handle,
	unsigned int *buffer_size, char *error_text)
{
	return(NPSL_GetSockOpt_BufferSize(socket_handle, buffer_size,
		SO_RCVBUF, error_text));
}
/* *********************************************************************** */

/* *********************************************************************** */
int NPSL_GetSockOpt_SndBuf(NPSL_SOCKET_HANDLE socket_handle,
	unsigned int *buffer_size, char *error_text)
{
	return(NPSL_GetSockOpt_BufferSize(socket_handle, buffer_size,
		SO_SNDBUF, error_text));
}
/* *********************************************************************** */

/* *********************************************************************** */
static int NPSL_GetSockOpt_BufferSize(NPSL_SOCKET_HANDLE socket_handle,
	unsigned int *buffer_size, int opt_name, char *error_text)
{
	int  return_code;
	int  tmp_buffer_length = sizeof(*buffer_size);
	char tmp_error_text[NPSL_MAX_ERROR_TEXT + 1];

	if ((return_code = NPSL_GetSockOpt(socket_handle, SOL_SOCKET, opt_name,
		buffer_size, &tmp_buffer_length,
		(error_text != NULL) ? tmp_error_text : NULL)) != NPSL_SUCCESS) {
		if (error_text != NULL)
			strcat(strcat(strcat(strcpy(error_text, "Unable to get the "),
				(opt_name == SO_RCVBUF) ? "receive" : "send"), " buffer size: "),
				tmp_error_text);
	}

	return(return_code);
}