nanohttp-socket.c File Reference

#include "nanohttp-error.h"
#include "nanohttp-logging.h"
#include "nanohttp-common.h"
#include "nanohttp-socket.h"

Go to the source code of this file.

Defines

#define HSOCKET_FREE   -1

Functions

herror_t hsocket_module_init (int argc, char **argv)
void hsocket_module_destroy (void)
herror_t hsocket_init (struct hsocket_t *sock)
void hsocket_free (struct hsocket_t *sock)
herror_t hsocket_open (struct hsocket_t *dsock, const char *hostname, int port, int ssl)
herror_t hsocket_bind (struct hsocket_t *dsock, unsigned short port)
herror_t hsocket_accept (struct hsocket_t *sock, struct hsocket_t *dest)
herror_t hsocket_listen (struct hsocket_t *sock)
void hsocket_close (struct hsocket_t *sock)
herror_t hsocket_send (struct hsocket_t *sock, const unsigned char *bytes, int n)
herror_t hsocket_send_string (struct hsocket_t *sock, const char *str)
int hsocket_select_recv (int sock, char *buf, size_t len)
herror_t hsocket_recv (struct hsocket_t *sock, unsigned char *buffer, int total, int force, int *received)
int hsocket_get_timeout (void)
void hsocket_set_timeout (int secs)


Define Documentation

#define HSOCKET_FREE   -1
 

Definition at line 98 of file nanohttp-socket.c.

Referenced by hsocket_init().


Function Documentation

herror_t hsocket_accept struct hsocket_t sock,
struct hsocket_t dest
 

Accepts an incoming socket request. Note that this function will not return until a socket connection is ready.

Parameters:
sock the socket which listents to a port
dest the destination socket which will be created
Returns:
H_OK if success. One of the followings if fails:
  • HSOCKET_ERROR_NOT_INITIALIZED
  • HSOCKET_ERROR_ACCEPT

Definition at line 324 of file nanohttp-socket.c.

References hsocket_t::addr, H_OK, herror_message(), herror_new(), HSOCKET_ERROR_NOT_INITIALIZED, hssl_server_ssl(), log_verbose3, log_warn2, SAVE_STR, and hsocket_t::sock.

Referenced by httpd_run().

herror_t hsocket_bind struct hsocket_t sock,
unsigned short  port
 

Binds a socket to a given port number. After bind you can call hsocket_listen() to listen to the port.

Parameters:
sock socket to use.
port port number to bind to
Returns:
H_OK on success. One of the followings if fails:
  • HSOCKET_ERROR_CREATE
  • HSOCKET_ERROR_BIND
See also:
hsocket_listen

Definition at line 292 of file nanohttp-socket.c.

References H_OK, herror_new(), HSOCKET_ERROR_BIND, HSOCKET_ERROR_CREATE, log_error2, and hsocket_t::sock.

Referenced by httpd_init().

void hsocket_close struct hsocket_t sock  ) 
 

e Close a socket connection.

Parameters:
sock the socket to close

Definition at line 368 of file nanohttp-socket.c.

References hsocket_t::bytes_received, hsocket_t::bytes_transmitted, hssl_cleanup(), log_verbose1, log_verbose3, and hsocket_t::sock.

Referenced by httpc_close_free(), and httpd_run().

void hsocket_free struct hsocket_t sock  ) 
 

Destroys and releases a given socket.

Parameters:
sock the socket to destroy

Definition at line 238 of file nanohttp-socket.c.

Referenced by httpc_free().

int hsocket_get_timeout void   ) 
 

Get the socket read/write timeout.

Returns:
The socket timeout in seconds.
See also:
hsocket_set_timeout

Definition at line 501 of file nanohttp-socket.c.

Referenced by httpd_get_timeout().

herror_t hsocket_init struct hsocket_t sock  ) 
 

Initializes a given socket object. This function (or hsokcet_init_ssl) should be called for every socket before using it.

Parameters:
sock the destination socket to initialize.
Returns:
This function should always return H_OK.
See also:
hssl_module_init

Definition at line 229 of file nanohttp-socket.c.

References H_OK, HSOCKET_FREE, and hsocket_t::sock.

Referenced by httpc_new(), and httpd_init().

herror_t hsocket_listen struct hsocket_t sock  ) 
 

Set the socket to the listen mode. You must bind the socket to a port with hsocket_bind() before you can listen to the port.

Parameters:
sock the socket to use
Returns:
H_OK if success. One of the followings if fails:<
  • HSOCKET_ERROR_NOT_INITIALIZED
  • HSOCKET_ERROR_LISTEN

Definition at line 351 of file nanohttp-socket.c.

References H_OK, herror_new(), HSOCKET_ERROR_LISTEN, HSOCKET_ERROR_NOT_INITIALIZED, log_error2, and hsocket_t::sock.

Referenced by httpd_run().

void hsocket_module_destroy void   ) 
 

Destroys the socket modul. This should be called after finishing an application.

See also:
hssl_module_destroy

Definition at line 221 of file nanohttp-socket.c.

Referenced by httpc_destroy(), and httpd_destroy().

herror_t hsocket_module_init int  argc,
char **  argv
 

Initializes the socket modul. This should be called only once for an application.

Returns:
This function should always return H_OK.

Definition at line 201 of file nanohttp-socket.c.

References H_OK, herror_message(), hssl_module_init(), and log_error2.

Referenced by httpc_init(), and httpd_init().

herror_t hsocket_open struct hsocket_t sock,
const char *  host,
int  port,
int  ssl
 

Connects to a given host. The hostname can be an IP number or a humen readable hostname.

Parameters:
sock the destonation socket object to use
host hostname
port port number to connect to
ssl whether to open a SSL connection
Returns:
H_OK if success. One of the followings if fails:
  • HSOCKET_ERROR_CREATE
  • HSOCKET_ERROR_GET_HOSTNAME
  • HSOCKET_ERROR_CONNECT

Definition at line 246 of file nanohttp-socket.c.

References H_OK, herror_message(), herror_new(), HSOCKET_ERROR_CONNECT, HSOCKET_ERROR_CREATE, HSOCKET_ERROR_GET_HOSTNAME, hssl_client_ssl(), log_error2, log_verbose4, and hsocket_t::sock.

herror_t hsocket_recv struct hsocket_t sock,
unsigned char *  buffer,
int  size,
int  force,
int *  len
 

Reads data from the socket.

Parameters:
sock the socket to read data from
buffer the buffer to use to save the readed bytes
size the maximum size of bytes to read
force if force is 1 then hsocket_read() will wait until maximum size of bytes (size parameter) was readed. Otherwise this function will not wait and will return with the bytes quequed on the socket.
Returns:
This function will return -1 if an read error was occured. Otherwise the return value is the size of bytes readed from the socket.

Definition at line 453 of file nanohttp-socket.c.

References hsocket_t::bytes_received, H_OK, herror_message(), herror_new(), HSOCKET_ERROR_RECEIVE, hsocket_select_recv(), hssl_read(), log_warn2, and hsocket_t::sock.

Referenced by hrequest_new_from_socket(), and hresponse_new_from_socket().

int hsocket_select_recv int  sock,
char *  buf,
size_t  len
 

Definition at line 431 of file nanohttp-socket.c.

References log_verbose2.

Referenced by hsocket_recv(), and hssl_read().

herror_t hsocket_send struct hsocket_t sock,
const unsigned char *  bytes,
int  size
 

Sends data throught the socket.

Parameters:
sock the socket to use to send the data
bytes bytes to send
size size of memory to sent pointed by bytes.
Returns:
H_OK if success. One of the followings if fails:
  • HSOCKET_ERROR_NOT_INITIALIZED
  • HSOCKET_ERROR_SEND

Definition at line 387 of file nanohttp-socket.c.

References hsocket_t::bytes_received, H_OK, herror_message(), herror_new(), HSOCKET_ERROR_NOT_INITIALIZED, HSOCKET_ERROR_SEND, hssl_write(), log_warn2, and hsocket_t::sock.

Referenced by hsocket_send_string(), http_output_stream_write(), httpc_send_header(), and httpd_send_header().

herror_t hsocket_send_string struct hsocket_t sock,
const char *  str
 

Sends a string throught the socket

Parameters:
sock the socket to use to send the data
str the null terminated string to sent
Returns:
H_OK on success. One of the followings if fails:
  • HSOCKET_ERROR_NOT_INITIALIZED
  • HSOCKET_ERROR_SEND

Definition at line 425 of file nanohttp-socket.c.

References hsocket_send().

Referenced by http_output_stream_flush(), http_output_stream_write(), and httpc_send_header().

void hsocket_set_timeout int  secs  ) 
 

Set the socket read/write timeout.

Parameters:
secs Timeout in seconds.
See also:
hsocket_get_timeout

Definition at line 507 of file nanohttp-socket.c.

Referenced by httpd_set_timeout().


Generated on Thu Jan 25 23:36:03 2007 for csoap by  doxygen 1.4.6