libtools
Classes | Macros | Typedefs | Enumerations | Functions
Network routines

HTTP server, HTTP client, socket functionality. More...

Collaboration diagram for Network routines:

Classes

struct  http_header
 HTTP header type. More...
 
struct  iface
 network interface More...
 

Macros

#define HTTP_HEADER_STRING(n, v)    { .type = HTTP_HEADER_STRING, .name = (n), .val.s = (v), }
 define HTTP header name with string value More...
 
#define HTTP_HEADER_INT(n, v)    { .type = HTTP_HEADER_INT, .name = (n), .val.i = (v), }
 define HTTP header name with integer value More...
 
#define HTTP_ERROR(x)   ((x) != HTTP_SUCCESS)
 return true if HTTP error occurred More...
 
#define IN_LOOPBACK(a)   ((((in_addr_t)(a)) & 0x7f000000) == 0x7f000000)
 expression is true, if address from loopback networks More...
 

Typedefs

typedef enum http_method http_method_t
 HTTP methods, see rfc2616. More...
 
typedef enum http_status http_status_t
 HTTP status code. More...
 
typedef enum http_header_type http_header_type_t
 HTTP header values type. More...
 
typedef struct http_header http_header_t
 HTTP header type. More...
 
typedef int(* http_request_cb_t) (http_method_t m, const char *path, const char *ver, void *priv)
 pointer to a callback function to process request More...
 
typedef int(* http_reply_cb_t) (const char *ver, int code, const char *msg, void *priv)
 pointer to a callback function to process reply More...
 
typedef int(* http_header_cb_t) (const char *name, const char *value, void *priv)
 pointer to a callback function to process header More...
 

Enumerations

enum  http_method {
  HTTP_OPTIONS, HTTP_GET, HTTP_HEAD, HTTP_POST,
  HTTP_PUT, HTTP_DELETE, HTTP_TRACE, HTTP_CONNECT
}
 HTTP methods, see rfc2616. More...
 
enum  http_status {
  HTTP_SUCCESS = 0, HTTP_CONNECTION_CLOSED, HTTP_SOCKET_ERROR, HTTP_LARGE_REQUEST,
  HTTP_INVALID_REQUEST, HTTP_UNKNOWN_REQUEST, HTTP_INVALID_HEADER, HTTP_INVALID_REPLY,
  HTTP_ABORTED, HTTP_OUT_MEMORY
}
 HTTP status code. More...
 
enum  http_header_type { HTTP_HEADER_STRING, HTTP_HEADER_INT }
 HTTP header values type. More...
 

Functions

struct in_addr * gethostaddr (const char *hostname, struct in_addr *addr)
 resolve hostname or ip address into in_addr More...
 
http_status_t http_request_send (int sockfd, http_method_t method, const char *path, const char *ver, http_header_t hdr[], size_t count)
 send HTTP request by socket More...
 
http_status_t http_request_recv (int sockfd, http_request_cb_t request_cb, http_header_cb_t header_cb, void *priv)
 read HTTP request from socket and parse it More...
 
http_status_t http_reply_send (int sockfd, const char *ver, int code, const char *desc, http_header_t hdr[], size_t count)
 send HTTP reply to socket More...
 
http_status_t http_reply_recv (int sockfd, http_reply_cb_t reply_cb, http_header_cb_t header_cb, void *priv)
 read HTTP reply from socket and parse it More...
 
int iface_list (void(*iface_cb)(struct iface *, void *), void *priv)
 list IPv4 network interfaces More...
 
ssize_t recvall (int sockfd, void *buf, size_t len, int flags)
 read buffer from socket descriptor More...
 
int recvfile (int sockfd, const char *path, size_t size)
 receive file from socket More...
 
ssize_t recvline (int fd, char *buf, size_t size, const char *d, size_t dlen)
 receive data from socket until delimiter is not reached More...
 
int sendfile (int sockfd, const char *path, size_t size)
 Send file to the socket. More...
 

Detailed Description

HTTP server, HTTP client, socket functionality.

Macro Definition Documentation

#define HTTP_ERROR (   x)    ((x) != HTTP_SUCCESS)

return true if HTTP error occurred

Parameters
[in]xHTTP response value
Returns
comparison result
#define HTTP_HEADER_INT (   n,
 
)    { .type = HTTP_HEADER_INT, .name = (n), .val.i = (v), }

define HTTP header name with integer value

Parameters
[in]nHTTP header name
[in]vHTTP header value
#define HTTP_HEADER_STRING (   n,
 
)    { .type = HTTP_HEADER_STRING, .name = (n), .val.s = (v), }

define HTTP header name with string value

Parameters
[in]nHTTP header name
[in]vHTTP header value
#define IN_LOOPBACK (   a)    ((((in_addr_t)(a)) & 0x7f000000) == 0x7f000000)

expression is true, if address from loopback networks

Typedef Documentation

typedef int(* http_header_cb_t) (const char *name, const char *value, void *priv)

pointer to a callback function to process header

Parameters
[in]nameHTTP header name
[in]valueHTTP header value
[in]privprivate data for callback
Returns
result of the function
Return values
0if function ran properly, other values will mean an error
typedef struct http_header http_header_t

HTTP header type.

HTTP header values type.

typedef enum http_method http_method_t

HTTP methods, see rfc2616.

typedef int(* http_reply_cb_t) (const char *ver, int code, const char *msg, void *priv)

pointer to a callback function to process reply

Parameters
[in]verversion of HTTP protocol
[in]codeHTTP reply code
[in]msgHTTP message
[in]privprivate data for callback
Returns
result of the function
Return values
0if function ran properly, other values will mean an error
typedef int(* http_request_cb_t) (http_method_t m, const char *path, const char *ver, void *priv)

pointer to a callback function to process request

Parameters
[in]methodHTTP method
[in]pathresource path
[in]verversion of HTTP protocol
[in]privprivate data for callback
Returns
result of the function
Return values
0if function ran properly, other values will mean an error
typedef enum http_status http_status_t

HTTP status code.

Enumeration Type Documentation

HTTP header values type.

Enumerator
HTTP_HEADER_STRING 
HTTP_HEADER_INT 

HTTP methods, see rfc2616.

Enumerator
HTTP_OPTIONS 
HTTP_GET 
HTTP_HEAD 
HTTP_POST 
HTTP_PUT 
HTTP_DELETE 
HTTP_TRACE 
HTTP_CONNECT 

HTTP status code.

Enumerator
HTTP_SUCCESS 
HTTP_CONNECTION_CLOSED 
HTTP_SOCKET_ERROR 
HTTP_LARGE_REQUEST 
HTTP_INVALID_REQUEST 
HTTP_UNKNOWN_REQUEST 
HTTP_INVALID_HEADER 
HTTP_INVALID_REPLY 
HTTP_ABORTED 
HTTP_OUT_MEMORY 

Function Documentation

struct in_addr* gethostaddr ( const char *  hostname,
struct in_addr *  addr 
)

resolve hostname or ip address into in_addr

Parameters
[in]hostnamehostname or ip address string
[out]addrstruct in_addr
Returns
pointer to addr, if successful
Return values
NULLerror occurred
http_status_t http_reply_recv ( int  sockfd,
http_reply_cb_t  reply_cb,
http_header_cb_t  header_cb,
void *  priv 
)

read HTTP reply from socket and parse it

Parameters
[in]sockfdsocket descriptor
reply_cbreply callback
header_cbheader callback
privprivate data for callbacks
Returns
HTTP status code
Return values
HTTP_ABORTEDaborted by callback
HTTP_INVALID_REPLYinvalid HTTP reply
http_status_t http_reply_send ( int  sockfd,
const char *  ver,
int  code,
const char *  desc,
http_header_t  hdr[],
size_t  count 
)

send HTTP reply to socket

Parameters
[in]sockfdsocket descriptor
[in]verHTTP version
[in]codeHTTP code
[in]descHTTP reply description
[in]hdrheaders
[in]countheaders count
Returns
HTTP status code
Return values
HTTP_INVALID_REQUESTinvalid version
HTTP_OUT_MEMORYout of memory to reply
http_status_t http_request_recv ( int  sockfd,
http_request_cb_t  request_cb,
http_header_cb_t  header_cb,
void *  priv 
)

read HTTP request from socket and parse it

Parameters
[in]sockfdsocket descriptor
[in]request_cbrequest callback
[in]header_cbheader callback
[in]privprivate data for callbacks
Returns
HTTP status code
Return values
HTTP_ABORTEDaborted by callback
http_status_t http_request_send ( int  sockfd,
http_method_t  method,
const char *  path,
const char *  ver,
http_header_t  hdr[],
size_t  count 
)

send HTTP request by socket

Parameters
[in]sockfdsocket descriptor
[in]methodHTTP method
[in]pathresource path
[in]verversion of HTTP protocol
[in]hdrarray of headers
[in]countcount of headers in hdr
Returns
HTTP status code
Return values
HTTP_UNKNOWN_REQUESTunknown HTTP request
HTTP_INVALID_REQUESTcan't create valid HTTP request
HTTP_OUT_MEMORYcan't complete HTTP request
int iface_list ( void(*)(struct iface *, void *)  iface_cb,
void *  priv 
)

list IPv4 network interfaces

Parameters
[in]iface_cbcallback handler for interface
[in]privpointer to private data for callback routine
Returns
on success, zero is returned
Return values
-1error occurred

If error occurred function update errno - see errors specified for the routine getifaddrs().

ssize_t recvall ( int  sockfd,
void *  buf,
size_t  len,
int  flags 
)

read buffer from socket descriptor

Parameters
[in]sockfdsocket descriptor
[in]bufbuffer for read data
[in]lenlength of buf in bytes
[in]flagsplease read "man recv" for flags description
Returns
amount of received bytes, should be equal len
Return values
0connection closed
-1error occurred
int recvfile ( int  sockfd,
const char *  path,
size_t  size 
)

receive file from socket

Parameters
[in]sockfdsocket descriptor
[in]pathfilename for receive file
[in]sizesize of file to receive
Returns
on success, zero is returned

If error occurred, function can create file with name path and it will be incomplete.

ssize_t recvline ( int  fd,
char *  buf,
size_t  size,
const char *  d,
size_t  dlen 
)

receive data from socket until delimiter is not reached

Parameters
[in]fdsocket descriptor
[out]bufbuffer for received data
[in]sizelength of buffer buf in bytes
[in]dpointer to delimiter data
[in]dlenlength of delimiter data in bytes
Returns
amount of received bytes
Return values
0connection closed
-1error occurred

Buffer buf should contain data with terminal delimiter d if not, than buffer buf is too small to store whole string.

int sendfile ( int  sockfd,
const char *  path,
size_t  size 
)

Send file to the socket.

Parameters
[in]sockfdsocket descriptor
[in]pathname of file to send
[in]sizesize of file to send
Returns
on success, zero is returned
Return values
-1error occurred
Remarks
If file size is less than size, function will fail with undefined value in errno.

Function updates errno, if error occurred: