socket.h 2.22 KB
#ifndef SCOT_SOCKET_H
#define SCOT_SOCKET_H

#include <scot_common.h>
#include <scot/scot_types.h>

#ifndef WIN32
# include <netdb.h>
# include <sys/socket.h>
# include <sys/un.h>
# include <arpa/inet.h>
# include <netinet/in.h>
# include <stdlib.h>
# include <errno.h>

# define INVALID_SOCKET  -1
# define SCOT_ERRNO      errno
# define SCOT_H_ERRNO    h_errno
# define SOCKET          int
# define SCOT_SOCK_CLOSE close
#else
# include <winsock.h>

# define SCOT_ERRNO      WSAGetLastError()
# define SCOT_H_ERRNO    WSAGetLastError()
# define SCOT_SOCK_CLOSE closesocket
#endif /* WIN32 */

#include <scot/stream.h>

/*
 * prevent direct access to the structure
 * from other methods then the ones that are made for this.
 * Should especially encourage the use of getter and setter
 * functions
 */
#ifndef USE_STRUCT_SCOT_SOCKET
struct scot_socket
{
	const char _ [sizeof (struct {
			struct scot_stream socket;
			struct sockaddr *  sa;
			SIZE_T             addr_len;
			})];
};
#else
struct scot_socket
{
	struct scot_stream socket;
	struct sockaddr *  sa;
	SIZE_T             addr_len;
};
#endif /* USE_STRUCT_SCOT_SOCKET */

/* socket errors */
#define SCOT_SOCKET_NEW_FAIL           0
#define SCOT_SOCKET_LISTEN_FAIL        1
#define SCOT_SOCKET_ACCEPT_FAIL        2
#define SCOT_SOCKET_CONNECT_FAIL       3
#define SCOT_SOCKET_NO_VALID_HOST      4
#define SCOT_SOCKET_AF_NOT_IMPLEMENTED 5
/* socket warnings */
#define SCOT_SOCKET_UN_FILE_EXISTS     0
extern const char * scot_socket_errmsg[];
extern const char * scot_socket_wrnmsg[];

/* 
 * This function initializes scot sockets. This primary means it does
 * nothing under a posix system an WSASartup on a Windows system.
 */
void                 scot_socket_init    (uint16_t, uint16_t);
/*
 * This finalizes the scot sockets system. Again does noting on posix
 * and WSACleanup on Windows.
 */
void                 scot_socket_fini    (void);

void                 scot_socket_listen  (const struct scot_socket*);
struct scot_socket * scot_socket_accept  (const struct scot_socket*);
void                 scot_socket_connect (const struct scot_socket*, 
                                          const char *);

void                 scot_socket_free    (struct scot_socket*);

#endif /* SCOT_SOCKET_H */