ossp-pkg/sa/sa.h.in
/*
** SA - OSSP Socket Abstraction Library
** Copyright (c) 2001 Ralf S. Engelschall <rse@engelschall.com>
** Copyright (c) 2001 The OSSP Project <http://www.ossp.org/>
** Copyright (c) 2001 Cable & Wireless Deutschland <http://www.cw.com/de/>
**
** This file is part of OSSP SA, a socket abstraction library which
** can be found at http://www.ossp.org/pkg/sa/.
**
** Permission to use, copy, modify, and distribute this software for
** any purpose with or without fee is hereby granted, provided that
** the above copyright notice and this permission notice appear in all
** copies.
**
** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
** USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
** OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE.
**
** sa.h: socket abstraction API
*/
#ifndef __SA_H__
#define __SA_H__
/* system definitions of "size_t", "socklen_t", "struct sockaddr *" */
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
/* embedding support */
#ifdef SA_PREFIX
#if defined(__STDC__) || defined(__cplusplus)
#define __SA_CONCAT(x,y) x ## y
#define SA_CONCAT(x,y) __SA_CONCAT(x,y)
#else
#define __SA_CONCAT(x) x
#define SA_CONCAT(x,y) __SA_CONCAT(x)y
#endif
#define sa_addr_st SA_CONCAT(SA_PREFIX,sa_addr_st)
#define sa_addr_t SA_CONCAT(SA_PREFIX,sa_addr_t)
#define sa_rc_t SA_CONCAT(SA_PREFIX,sa_rc_t)
#define sa_st SA_CONCAT(SA_PREFIX,sa_st)
#define sa_t SA_CONCAT(SA_PREFIX,sa_t)
#define sa_addr_create SA_CONCAT(SA_PREFIX,sa_addr_create)
#define sa_addr_destroy SA_CONCAT(SA_PREFIX,sa_addr_destroy)
#define sa_addr_u2a SA_CONCAT(SA_PREFIX,sa_addr_u2a)
#define sa_addr_s2a SA_CONCAT(SA_PREFIX,sa_addr_s2a)
#define sa_addr_a2u SA_CONCAT(SA_PREFIX,sa_addr_a2u)
#define sa_addr_a2s SA_CONCAT(SA_PREFIX,sa_addr_a2s)
#define sa_create SA_CONCAT(SA_PREFIX,sa_create)
#define sa_destroy SA_CONCAT(SA_PREFIX,sa_destroy)
#define sa_type SA_CONCAT(SA_PREFIX,sa_type)
#define sa_timeout SA_CONCAT(SA_PREFIX,sa_timeout)
#define sa_buffers SA_CONCAT(SA_PREFIX,sa_buffers)
#define sa_bind SA_CONCAT(SA_PREFIX,sa_bind)
#define sa_connect SA_CONCAT(SA_PREFIX,sa_connect)
#define sa_listen SA_CONCAT(SA_PREFIX,sa_listen)
#define sa_accept SA_CONCAT(SA_PREFIX,sa_accept)
#define sa_getremote SA_CONCAT(SA_PREFIX,sa_getremote)
#define sa_getlocal SA_CONCAT(SA_PREFIX,sa_getlocal)
#define sa_getfd SA_CONCAT(SA_PREFIX,sa_getfd)
#define sa_shutdown SA_CONCAT(SA_PREFIX,sa_shutdown)
#define sa_read SA_CONCAT(SA_PREFIX,sa_read)
#define sa_readln SA_CONCAT(SA_PREFIX,sa_readln)
#define sa_write SA_CONCAT(SA_PREFIX,sa_write)
#define sa_writef SA_CONCAT(SA_PREFIX,sa_writef)
#define sa_flush SA_CONCAT(SA_PREFIX,sa_flush)
#define sa_recv SA_CONCAT(SA_PREFIX,sa_recv)
#define sa_send SA_CONCAT(SA_PREFIX,sa_send)
#endif
/* fallback for POSIX socklen_t */
@SA_FALLBACK_SOCKLEN_T@
/* socket address abstraction object type */
struct sa_addr_st;
typedef struct sa_addr_st sa_addr_t;
/* socket abstraction object type */
struct sa_st;
typedef struct sa_st sa_t;
/* socket connection types */
typedef enum {
SA_TYPE_STREAM,
SA_TYPE_DATAGRAM
} sa_type_t;
/* return codes */
typedef enum {
SA_OK, /* everything ok */
SA_ERR_ARG, /* invalid argument (wrong parameter) */
SA_ERR_USE, /* invalid use (wrong context) */
SA_ERR_MEM, /* out of memory */
SA_ERR_SYS, /* system error (see errno) */
SA_ERR_EOF, /* end of file/socket communication */
SA_ERR_INT /* internal error */
} sa_rc_t;
/* list of timeouts */
typedef enum {
SA_TIMEOUT_ALL = -1,
SA_TIMEOUT_ACCEPT = 0,
SA_TIMEOUT_CONNECT = 1,
SA_TIMEOUT_READ = 2,
SA_TIMEOUT_WRITE = 3,
SA_TIMEOUT_RECV = 4,
SA_TIMEOUT_SEND = 5
} sa_timeout_t;
/* list of system calls */
typedef enum {
SA_SYSCALL_CONNECT,
SA_SYSCALL_ACCEPT,
SA_SYSCALL_SELECT,
SA_SYSCALL_READ,
SA_SYSCALL_WRITE,
SA_SYSCALL_RECVFROM,
SA_SYSCALL_SENDTO
} sa_syscall_t;
/* error handling operations */
sa_rc_t sa_error (sa_t *sa, sa_rc_t rv, char **str);
/* address object operations */
sa_rc_t sa_addr_create (sa_addr_t **saa);
sa_rc_t sa_addr_destroy (sa_addr_t *saa);
/* address operations */
sa_rc_t sa_addr_u2a (sa_addr_t *saa, const char *uri, ...);
sa_rc_t sa_addr_s2a (sa_addr_t *saa, const struct sockaddr *sabuf, socklen_t salen);
sa_rc_t sa_addr_a2u (sa_addr_t *saa, char **uri);
sa_rc_t sa_addr_a2s (sa_addr_t *saa, struct sockaddr **sabuf, socklen_t *salen);
/* socket object operations */
sa_rc_t sa_create (sa_t **sa);
sa_rc_t sa_destroy (sa_t *sa);
/* socket parameter operations */
sa_rc_t sa_type (sa_t *sa, sa_type_t type);
sa_rc_t sa_timeout (sa_t *sa, sa_timeout_t id, long sec, long usec);
sa_rc_t sa_buffers (sa_t *sa, size_t rsize, size_t wsize);
sa_rc_t sa_syscall (sa_t *sa, sa_syscall_t id, void (*fptr)(), void *fctx);
/* socket connection operations */
sa_rc_t sa_bind (sa_t *sa, sa_addr_t *laddr);
sa_rc_t sa_connect (sa_t *sa, sa_addr_t *raddr);
sa_rc_t sa_listen (sa_t *sa, int backlog);
sa_rc_t sa_accept (sa_t *sa, sa_addr_t **caddr, sa_t **csa);
sa_rc_t sa_getremote (sa_t *sa, sa_addr_t **raddr);
sa_rc_t sa_getlocal (sa_t *sa, sa_addr_t **laddr);
sa_rc_t sa_getfd (sa_t *sa, int *fd);
sa_rc_t sa_shutdown (sa_t *sa, char *flags);
/* socket input/output operations (stream communication) */
sa_rc_t sa_read (sa_t *sa, char *buf, size_t buflen, size_t *bufdone);
sa_rc_t sa_readln (sa_t *sa, char *buf, size_t buflen, size_t *bufdone);
sa_rc_t sa_write (sa_t *sa, const char *buf, size_t buflen, size_t *bufdone);
sa_rc_t sa_writef (sa_t *sa, const char *fmt, ...);
sa_rc_t sa_flush (sa_t *sa);
/* socket input/output operations (datagram communication) */
sa_rc_t sa_recv (sa_t *sa, char *buf, size_t buflen, size_t *bufdone, sa_addr_t **raddr);
sa_rc_t sa_send (sa_t *sa, const char *buf, size_t buflen, size_t *bufdone, sa_addr_t *raddr);
#endif /* __SA_H__ */