view C/nsock.h @ 186:d4b8416e9ab1

Move C
author David Demelier <markand@malikania.fr>
date Sat, 23 Nov 2013 16:14:05 +0100
parents nsock.h@29531c2f8213
children
line wrap: on
line source

/*
 * nsock.h -- portable BSD sockets wrapper
 *
 * Copyright (c) 2013 David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or 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.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef _NSOCK_H_
#define _NSOCK_H_

#if !defined(NSOCK_NOINCLUDES)
#  if defined(_WIN32)
#    include <WinSock2.h>
#    include <WS2tcpip.h>
#  else
#    include <sys/types.h>
#    include <sys/socket.h>
#    include <sys/un.h>

#    include <arpa/inet.h>

#    include <netinet/in.h>

#    include <netdb.h>
#    include <unistd.h>
#  endif
#endif

struct nsock;
struct nsock_address;
struct nsock_listener;

/* --------------------------------------------------------
 * Sockets functions
 * -------------------------------------------------------- */

void
nsock_init(void);

struct nsock *
nsock_new(void);

int
nsock_create(struct nsock *, int, int, int);

const char *
nsock_error(struct nsock *);

int
nsock_bind(struct nsock *, const struct nsock_address *);

int
nsock_listen(struct nsock *, int);

int
nsock_accept(struct nsock *, struct nsock **, struct nsock_address **);

int
nsock_connect(struct nsock *, const struct nsock_address *);

int
nsock_set(struct nsock *, int, int, const void *, unsigned);

long
nsock_recv(struct nsock *, void *, size_t, int);

long
nsock_recvfrom(struct nsock *, struct nsock_address *, void *, size_t, int);

long
nsock_send(struct nsock *, const void *, size_t, int);

long
nsock_sendto(struct nsock *, const struct nsock_address *, const void *, size_t, int);

void
nsock_close(struct nsock *);

void
nsock_free(struct nsock *);

void
nsock_finish(void);

/* --------------------------------------------------------
 * End point functions
 * -------------------------------------------------------- */

struct nsock_address *
nsock_addr_new(void);

const char *
nsock_addr_error(struct nsock_address *);

int
nsock_addr_bind_ip(struct nsock_address *, const char *, unsigned, int);

int
nsock_addr_connect_ip(struct nsock_address *, const char *, unsigned, int);

#if !defined(_WIN32)

void
nsock_addr_unix(struct nsock_address *, const char *);

#endif

struct sockaddr *
nsock_addr_getaddr(struct nsock_address *);

socklen_t
nsock_addr_getaddrlen(const struct nsock_address *);

void
nsock_addr_free(struct nsock_address *);

/* --------------------------------------------------------
 * listener functions
 * -------------------------------------------------------- */

typedef void (*nsock_lst_map_t)(const struct nsock *, void *);

struct nsock_listener *
nsock_lst_new(const struct nsock *);

const char *
nsock_lst_error(struct nsock_listener *);

int
nsock_lst_push(struct nsock_listener *, struct nsock *);

int
nsock_lst_append(struct nsock_listener *, struct nsock *);

size_t
nsock_lst_count(const struct nsock_listener *);

void
nsock_lst_remove(struct nsock_listener *, const struct nsock *);

struct nsock *
nsock_lst_select(struct nsock_listener *, long, long);

void
nsock_lst_map(struct nsock_listener *, nsock_lst_map_t, void *);

void
nsock_lst_free(struct nsock_listener *);

#endif /* !_NSOCK_H_ */