A cleanup effort: libssh2_ prefixes only on external APIs. Use _libssh2_ prefix

for library-wide internal functions. Don't use any of those on static functions.
I also did some comments and whitespace changes.
This commit is contained in:
Daniel Stenberg
2009-03-17 13:48:35 +00:00
parent f2fa02c575
commit cc5e952fa0
16 changed files with 1063 additions and 1093 deletions

View File

@@ -50,7 +50,7 @@
#include <alloca.h>
#endif
/* {{{ libssh2_default_alloc
/* libssh2_default_alloc
*/
static
LIBSSH2_ALLOC_FUNC(libssh2_default_alloc)
@@ -59,9 +59,7 @@ LIBSSH2_ALLOC_FUNC(libssh2_default_alloc)
return malloc(count);
}
/* }}} */
/* {{{ libssh2_default_free
/* libssh2_default_free
*/
static
LIBSSH2_FREE_FUNC(libssh2_default_free)
@@ -70,9 +68,7 @@ LIBSSH2_FREE_FUNC(libssh2_default_free)
free(ptr);
}
/* }}} */
/* {{{ libssh2_default_realloc
/* libssh2_default_realloc
*/
static
LIBSSH2_REALLOC_FUNC(libssh2_default_realloc)
@@ -81,15 +77,15 @@ LIBSSH2_REALLOC_FUNC(libssh2_default_realloc)
return realloc(ptr, count);
}
/* }}} */
/* {{{ libssh2_banner_receive
/*
* banner_receive
*
* Wait for a hello from the remote host
* Allocate a buffer and store the banner in session->remote.banner
* Returns: 0 on success, PACKET_EAGAIN if read would block, 1 on failure
*/
static int
libssh2_banner_receive(LIBSSH2_SESSION * session)
banner_receive(LIBSSH2_SESSION * session)
{
int ret;
int banner_len;
@@ -186,9 +182,9 @@ libssh2_banner_receive(LIBSSH2_SESSION * session)
return 0;
}
/* }}} */
/* {{{ libssh2_banner_send
/*
* banner_send
*
* Send the default banner, or the one set via libssh2_setopt_string
*
* Returns PACKET_EAGAIN if it would block - and if it does so, you should
@@ -197,7 +193,7 @@ libssh2_banner_receive(LIBSSH2_SESSION * session)
* (same data pointer and same data_len) until zero or failure is returned.
*/
static int
libssh2_banner_send(LIBSSH2_SESSION * session)
banner_send(LIBSSH2_SESSION * session)
{
char *banner = (char *) LIBSSH2_SSH_DEFAULT_BANNER_WITH_CRLF;
int banner_len = sizeof(LIBSSH2_SSH_DEFAULT_BANNER_WITH_CRLF) - 1;
@@ -254,16 +250,14 @@ libssh2_banner_send(LIBSSH2_SESSION * session)
return 0;
}
/* }}} */
/*
* _libssh2_nonblock() sets the given socket to either blocking or
* session_nonblock() sets the given socket to either blocking or
* non-blocking mode based on the 'nonblock' boolean argument. This function
* is copied from the libcurl sources with permission.
*/
static int
_libssh2_nonblock(int sockfd, /* operate on this */
int nonblock /* TRUE or FALSE */ )
session_nonblock(int sockfd, /* operate on this */
int nonblock /* TRUE or FALSE */ )
{
#undef SETBLOCK
#define SETBLOCK 0
@@ -327,11 +321,12 @@ _libssh2_nonblock(int sockfd, /* operate on this */
}
/*
* _libssh2_get_socket_nonblocking() gets the given blocking or non-blocking
* state of the socket.
* get_socket_nonblocking()
*
* gets the given blocking or non-blocking state of the socket.
*/
static int
_libssh2_get_socket_nonblocking(int sockfd)
get_socket_nonblocking(int sockfd)
{ /* operate on this */
#undef GETBLOCK
#define GETBLOCK 0
@@ -386,7 +381,7 @@ _libssh2_get_socket_nonblocking(int sockfd)
#endif
}
/* {{{ libssh2_banner_set
/* libssh2_banner_set
* Set the local banner
*/
LIBSSH2_API int
@@ -421,9 +416,9 @@ libssh2_banner_set(LIBSSH2_SESSION * session, const char *banner)
return 0;
}
/* }}} */
/* {{{ proto libssh2_session_init
/*
* proto libssh2_session_init
*
* Allocate and initialize a libssh2 session structure
* Allows for malloc callbacks in case the calling program has its own memory manager
* It's allowable (but unadvisable) to define some but not all of the malloc callbacks
@@ -463,9 +458,9 @@ libssh2_session_init_ex(LIBSSH2_ALLOC_FUNC((*my_alloc)),
return session;
}
/* }}} */
/* {{{ libssh2_session_callback_set
/*
* libssh2_session_callback_set
*
* Set (or reset) a callback function
* Returns the prior address
*
@@ -510,9 +505,9 @@ libssh2_session_callback_set(LIBSSH2_SESSION * session,
return NULL;
}
/* }}} */
/* {{{ proto libssh2_session_startup
/*
* proto libssh2_session_startup
*
* session: LIBSSH2_SESSION struct allocated and owned by the calling program
* Returns: 0 on success, or non-zero on failure
* Any memory allocated by libssh2 will use alloc/realloc/free
@@ -537,14 +532,14 @@ libssh2_session_startup(LIBSSH2_SESSION * session, int sock)
session->socket_fd = sock;
session->socket_block =
!_libssh2_get_socket_nonblocking(session->socket_fd);
!get_socket_nonblocking(session->socket_fd);
if (session->socket_block) {
/*
* Since we can't be sure that we are in blocking or there
* was an error detecting the state, so set to blocking to
* be sure
*/
_libssh2_nonblock(session->socket_fd, 0);
session_nonblock(session->socket_fd, 0);
}
session->startup_state = libssh2_NB_state_created;
@@ -553,7 +548,7 @@ libssh2_session_startup(LIBSSH2_SESSION * session, int sock)
/* TODO: Liveness check */
if (session->startup_state == libssh2_NB_state_created) {
rc = libssh2_banner_send(session);
rc = banner_send(session);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending banner to remote host", 0);
@@ -569,7 +564,7 @@ libssh2_session_startup(LIBSSH2_SESSION * session, int sock)
}
if (session->startup_state == libssh2_NB_state_sent) {
rc = libssh2_banner_receive(session);
rc = banner_receive(session);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for banner", 0);
@@ -605,7 +600,7 @@ libssh2_session_startup(LIBSSH2_SESSION * session, int sock)
/* Request the userauth service */
session->startup_service[0] = SSH_MSG_SERVICE_REQUEST;
libssh2_htonu32(session->startup_service + 1,
_libssh2_htonu32(session->startup_service + 1,
sizeof("ssh-userauth") - 1);
memcpy(session->startup_service + 5, "ssh-userauth",
sizeof("ssh-userauth") - 1);
@@ -614,13 +609,14 @@ libssh2_session_startup(LIBSSH2_SESSION * session, int sock)
}
if (session->startup_state == libssh2_NB_state_sent3) {
rc = libssh2_packet_write(session, session->startup_service,
sizeof("ssh-userauth") + 5 - 1);
rc = _libssh2_packet_write(session, session->startup_service,
sizeof("ssh-userauth") + 5 - 1);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block asking for ssh-userauth service", 0);
return LIBSSH2_ERROR_EAGAIN;
} else if (rc) {
}
else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to ask for ssh-userauth service", 0);
return LIBSSH2_ERROR_SOCKET_SEND;
@@ -630,17 +626,17 @@ libssh2_session_startup(LIBSSH2_SESSION * session, int sock)
}
if (session->startup_state == libssh2_NB_state_sent4) {
rc = libssh2_packet_require(session, SSH_MSG_SERVICE_ACCEPT,
&session->startup_data,
&session->startup_data_len, 0, NULL, 0,
&session->startup_req_state);
rc = _libssh2_packet_require(session, SSH_MSG_SERVICE_ACCEPT,
&session->startup_data,
&session->startup_data_len, 0, NULL, 0,
&session->startup_req_state);
if (rc == PACKET_EAGAIN) {
return LIBSSH2_ERROR_EAGAIN;
} else if (rc) {
return LIBSSH2_ERROR_SOCKET_DISCONNECT;
}
session->startup_service_length =
libssh2_ntohu32(session->startup_data + 1);
_libssh2_ntohu32(session->startup_data + 1);
if ((session->startup_service_length != (sizeof("ssh-userauth") - 1))
|| strncmp("ssh-userauth", (char *) session->startup_data + 5,
@@ -663,9 +659,9 @@ libssh2_session_startup(LIBSSH2_SESSION * session, int sock)
return LIBSSH2_ERROR_INVAL;
}
/* }}} */
/* {{{ proto libssh2_session_free
/*
* libssh2_session_free
*
* Frees the memory allocated to the session
* Also closes and frees any channels attached to this session
*/
@@ -699,7 +695,8 @@ libssh2_session_free(LIBSSH2_SESSION * session)
/* free */
LIBSSH2_FREE(session, tmp);
/* reverse linking isn't important here, we're killing the structure */
/* reverse linking isn't important here, we're killing the
* structure */
}
}
@@ -898,9 +895,7 @@ libssh2_session_free(LIBSSH2_SESSION * session)
return 0;
}
/* }}} */
/* {{{ libssh2_session_disconnect_ex
/* libssh2_session_disconnect_ex
*/
LIBSSH2_API int
libssh2_session_disconnect_ex(LIBSSH2_SESSION * session, int reason,
@@ -934,17 +929,17 @@ libssh2_session_disconnect_ex(LIBSSH2_SESSION * session, int reason,
}
*(s++) = SSH_MSG_DISCONNECT;
libssh2_htonu32(s, reason);
_libssh2_htonu32(s, reason);
s += 4;
libssh2_htonu32(s, descr_len);
_libssh2_htonu32(s, descr_len);
s += 4;
if (description) {
memcpy(s, description, descr_len);
s += descr_len;
}
libssh2_htonu32(s, lang_len);
_libssh2_htonu32(s, lang_len);
s += 4;
if (lang) {
memcpy(s, lang, lang_len);
@@ -954,8 +949,8 @@ libssh2_session_disconnect_ex(LIBSSH2_SESSION * session, int reason,
session->disconnect_state = libssh2_NB_state_created;
}
rc = libssh2_packet_write(session, session->disconnect_data,
session->disconnect_data_len);
rc = _libssh2_packet_write(session, session->disconnect_data,
session->disconnect_data_len);
if (rc == PACKET_EAGAIN) {
return PACKET_EAGAIN;
}
@@ -967,9 +962,7 @@ libssh2_session_disconnect_ex(LIBSSH2_SESSION * session, int reason,
return 0;
}
/* }}} */
/* {{{ libssh2_session_methods
/* libssh2_session_methods
* Return the currently active methods for method_type
* NOTE: Currently lang_cs and lang_sc are ALWAYS set to empty string regardless of actual negotiation
* Strings should NOT be freed
@@ -1037,9 +1030,7 @@ libssh2_session_methods(LIBSSH2_SESSION * session, int method_type)
return method->name;
}
/* }}} */
/* {{{ libssh2_session_abstract
/* libssh2_session_abstract
* Retrieve a pointer to the abstract property
*/
LIBSSH2_API void **
@@ -1048,9 +1039,7 @@ libssh2_session_abstract(LIBSSH2_SESSION * session)
return &session->abstract;
}
/* }}} */
/* {{{ libssh2_session_last_error
/* libssh2_session_last_error
* Returns error code and populates an error string into errmsg
* If want_buf is non-zero then the string placed into errmsg must be freed by the calling program
* Otherwise it is assumed to be owned by libssh2
@@ -1106,9 +1095,7 @@ libssh2_session_last_error(LIBSSH2_SESSION * session, char **errmsg,
return session->err_code;
}
/* }}} */
/* {{{ libssh2_session_last_error
/* libssh2_session_last_error
* Returns error code
*/
LIBSSH2_API int
@@ -1117,9 +1104,7 @@ libssh2_session_last_errno(LIBSSH2_SESSION * session)
return session->err_code;
}
/* }}} */
/* {{{ libssh2_session_flag
/* libssh2_session_flag
* Set/Get session flags
* Passing flag==0 will avoid changing session->flags while still returning its current value
*/
@@ -1135,9 +1120,8 @@ libssh2_session_flag(LIBSSH2_SESSION * session, int flag, int value)
return session->flags;
}
/* }}} */
/* {{{ _libssh2_session_set_blocking
/* _libssh2_session_set_blocking
*
* Set a session's blocking mode on or off, return the previous status
* when this function is called.
*/
@@ -1153,14 +1137,13 @@ _libssh2_session_set_blocking(LIBSSH2_SESSION * session, int blocking)
}
session->socket_block = blocking;
_libssh2_nonblock(session->socket_fd, !blocking);
session_nonblock(session->socket_fd, !blocking);
return bl;
}
/* }}} */
/* {{{ libssh2_session_set_blocking
/* libssh2_session_set_blocking
*
* Set a channel's blocking mode on or off, similar to a socket's
* fcntl(fd, F_SETFL, O_NONBLOCK); type command
*/
@@ -1170,20 +1153,19 @@ libssh2_session_set_blocking(LIBSSH2_SESSION * session, int blocking)
(void) _libssh2_session_set_blocking(session, blocking);
}
/* }}} */
/* {{{ libssh2_session_get_blocking
* Returns a session's blocking mode on or off
*/
/* libssh2_session_get_blocking
*
* Returns a session's blocking mode on or off
*/
LIBSSH2_API int
libssh2_session_get_blocking(LIBSSH2_SESSION * session)
{
return session->socket_block;
}
/* }}} */
/* {{{ libssh2_poll_channel_read
/*
* libssh2_poll_channel_read
*
* Returns 0 if no data is waiting on channel,
* non-0 if data is available
*/
@@ -1193,18 +1175,17 @@ libssh2_poll_channel_read(LIBSSH2_CHANNEL * channel, int extended)
LIBSSH2_SESSION *session = channel->session;
LIBSSH2_PACKET *packet = session->packets.head;
while (packet)
{
if ( channel->local.id == libssh2_ntohu32(packet->data + 1)) {
if ( extended == 1 &&
(packet->data[0] == SSH_MSG_CHANNEL_EXTENDED_DATA
|| packet->data[0] == SSH_MSG_CHANNEL_DATA )) {
return 1;
} else if ( extended == 0 &&
packet->data[0] == SSH_MSG_CHANNEL_DATA) {
return 1;
}
/* else - no data of any type is ready to be read */
while (packet) {
if ( channel->local.id == _libssh2_ntohu32(packet->data + 1)) {
if ( extended == 1 &&
(packet->data[0] == SSH_MSG_CHANNEL_EXTENDED_DATA
|| packet->data[0] == SSH_MSG_CHANNEL_DATA )) {
return 1;
} else if ( extended == 0 &&
packet->data[0] == SSH_MSG_CHANNEL_DATA) {
return 1;
}
/* else - no data of any type is ready to be read */
}
packet = packet->next;
}
@@ -1212,33 +1193,32 @@ libssh2_poll_channel_read(LIBSSH2_CHANNEL * channel, int extended)
return 0;
}
/* }}} */
/* {{{ libssh2_poll_channel_write
/*
* poll_channel_write
*
* Returns 0 if writing to channel would block,
* non-0 if data can be written without blocking
*/
static inline int
libssh2_poll_channel_write(LIBSSH2_CHANNEL * channel)
poll_channel_write(LIBSSH2_CHANNEL * channel)
{
return channel->local.window_size ? 1 : 0;
}
/* }}} */
/* {{{ libssh2_poll_listener_queued
/* poll_listener_queued
*
* Returns 0 if no connections are waiting to be accepted
* non-0 if one or more connections are available
*/
static inline int
libssh2_poll_listener_queued(LIBSSH2_LISTENER * listener)
poll_listener_queued(LIBSSH2_LISTENER * listener)
{
return listener->queue ? 1 : 0;
}
/* }}} */
/* {{{ libssh2_poll
/*
* libssh2_poll
*
* Poll sockets, channels, and listeners for activity
*/
LIBSSH2_API int
@@ -1360,25 +1340,30 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
if (fds[i].events != fds[i].revents) {
switch (fds[i].type) {
case LIBSSH2_POLLFD_CHANNEL:
if ((fds[i].events & LIBSSH2_POLLFD_POLLIN) && /* Want to be ready for read */
((fds[i].revents & LIBSSH2_POLLFD_POLLIN) == 0)) { /* Not yet known to be ready for read */
if ((fds[i].events & LIBSSH2_POLLFD_POLLIN) &&
/* Want to be ready for read */
((fds[i].revents & LIBSSH2_POLLFD_POLLIN) == 0)) {
/* Not yet known to be ready for read */
fds[i].revents |=
libssh2_poll_channel_read(fds[i].fd.channel,
0) ?
LIBSSH2_POLLFD_POLLIN : 0;
}
if ((fds[i].events & LIBSSH2_POLLFD_POLLEXT) && /* Want to be ready for extended read */
((fds[i].revents & LIBSSH2_POLLFD_POLLEXT) == 0)) { /* Not yet known to be ready for extended read */
if ((fds[i].events & LIBSSH2_POLLFD_POLLEXT) &&
/* Want to be ready for extended read */
((fds[i].revents & LIBSSH2_POLLFD_POLLEXT) == 0)) {
/* Not yet known to be ready for extended read */
fds[i].revents |=
libssh2_poll_channel_read(fds[i].fd.channel,
1) ?
LIBSSH2_POLLFD_POLLEXT : 0;
}
if ((fds[i].events & LIBSSH2_POLLFD_POLLOUT) && /* Want to be ready for write */
((fds[i].revents & LIBSSH2_POLLFD_POLLOUT) == 0)) { /* Not yet known to be ready for write */
if ((fds[i].events & LIBSSH2_POLLFD_POLLOUT) &&
/* Want to be ready for write */
((fds[i].revents & LIBSSH2_POLLFD_POLLOUT) == 0)) {
/* Not yet known to be ready for write */
fds[i].revents |=
libssh2_poll_channel_write(fds[i].fd.
channel) ?
poll_channel_write(fds[i].fd. channel) ?
LIBSSH2_POLLFD_POLLOUT : 0;
}
if (fds[i].fd.channel->remote.close
@@ -1394,11 +1379,13 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
break;
case LIBSSH2_POLLFD_LISTENER:
if ((fds[i].events & LIBSSH2_POLLFD_POLLIN) && /* Want a connection */
((fds[i].revents & LIBSSH2_POLLFD_POLLIN) == 0)) { /* No connections known of yet */
if ((fds[i].events & LIBSSH2_POLLFD_POLLIN) &&
/* Want a connection */
((fds[i].revents & LIBSSH2_POLLFD_POLLIN) == 0)) {
/* No connections known of yet */
fds[i].revents |=
libssh2_poll_listener_queued(fds[i].fd.
listener) ?
poll_listener_queued(fds[i].fd.
listener) ?
LIBSSH2_POLLFD_POLLIN : 0;
}
if (fds[i].fd.listener->session->socket_state ==
@@ -1444,7 +1431,7 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
switch (fds[i].type) {
case LIBSSH2_POLLFD_SOCKET:
fds[i].revents = sockets[i].revents;
sockets[i].revents = 0; /* In case we loop again, be nice */
sockets[i].revents = 0; /* In case we loop again, be nice */
if (fds[i].revents) {
active_fds++;
}
@@ -1452,7 +1439,7 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
case LIBSSH2_POLLFD_CHANNEL:
if (sockets[i].events & POLLIN) {
/* Spin session until no data available */
while (libssh2_packet_read(fds[i].fd.channel->session)
while (_libssh2_packet_read(fds[i].fd.channel->session)
> 0);
}
if (sockets[i].revents & POLLHUP) {
@@ -1465,7 +1452,7 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
case LIBSSH2_POLLFD_LISTENER:
if (sockets[i].events & POLLIN) {
/* Spin session until no data available */
while (libssh2_packet_read(fds[i].fd.listener->session)
while (_libssh2_packet_read(fds[i].fd.listener->session)
> 0);
}
if (sockets[i].revents & POLLHUP) {
@@ -1534,13 +1521,16 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
}
}
}
#endif /* else no select() or poll() -- timeout (and by extension timeout_remaining) will be equal to 0 */
#endif /* else no select() or poll() -- timeout (and by extension
* timeout_remaining) will be equal to 0 */
} while ((timeout_remaining > 0) && !active_fds);
return active_fds;
}
/* {{{ libssh2_session_block_direction
/*
* libssh2_session_block_direction
*
* Get blocked direction when a function returns LIBSSH2_ERROR_EAGAIN
* Returns LIBSSH2_SOCKET_BLOCK_INBOUND if recv() blocked
* or LIBSSH2_SOCKET_BLOCK_OUTBOUND if send() blocked
@@ -1551,4 +1541,3 @@ libssh2_session_block_directions(LIBSSH2_SESSION *session)
return session->socket_block_directions;
}
/* }}} */