/* IRCfs - IRC FileServ for *nix. * Copyright (C) 2002 Nick 'Zaf' Clifford * For licensing details, refer to the LICENSE file in the source * code directory. */ #ifndef __include_socket_h__ #define __include_socket_h__ #include "runtime.h" struct socket; #define socket_num_events 6 enum socket_event_type { socket_event_read, socket_event_write, socket_event_connect, socket_event_error, socket_event_close }; typedef int (*socket_callback_t)(struct socket *, enum socket_event_type, void *); enum socket_type { socket_type_tcp }; enum socket_err { socket_err_ok, /* No problems here, move along */ socket_err_invalid, /* invalid param/data */ socket_err_inuse, /* Port/Address inuse */ socket_err_pending, /* Request is not complete yet */ socket_err_notconnected, /* Not connected to remote yet */ socket_err_closed, /* Socket is closed. Do not talk to me again */ socket_err_failed, /* Connection/Send/Recv failed. */ socket_err_readonly, /* Tried to change something that can't be changed anymore */ socket_err_noaddr, /* You haven't specified an address/port needed */ socket_err_denied, /* Access denied */ socket_err_refused, /* Connection refused */ socket_err_timeout, /* Timed out */ socket_err_unreachable, /* Destination unreachable */ socket_err_full, /* Socket buffer is full! */ socket_err_unknown /* Unknown/General error */ }; struct socket *socket_create(enum socket_type type); void socket_destroy(struct socket *s); int socket_is_valid(struct socket *s); int socket_set_callback(struct socket *s, enum socket_event_type type, socket_callback_t func, void *appdata); socket_callback_t socket_get_callback(struct socket *s, enum socket_event_type type); int socket_set_local_port(struct socket *s, unsigned short port); /* Returns 0 if not set, yet. * If you want a random local port, call socket_bind, then * socket_get_local_port */ unsigned short socket_get_local_port(struct socket *s); int socket_set_local_addr(struct socket *s, const char *addr); int socket_get_local_addr(struct socket *s, char *buff, size_t bufflen); int socket_set_remote_port(struct socket *s, unsigned short port); unsigned short socket_get_remote_port(struct socket *s); int socket_set_remote_addr(struct socket *s, const char *addr); int socket_get_remote_addr(struct socket *s, char *buff, size_t bufflen); /* Binds the socket to the local port and address, and/or the * remote port & address. * If the port and/or address is invalid/inuse, then this is when * the error occurs */ int socket_bind(struct socket *s); int socket_set_buffer_size(struct socket *s, size_t bytes); size_t socket_get_buffer_size(struct socket *s); int socket_connect_to(struct socket *s, const char *addr, unsigned short port); int socket_connect(struct socket *s); /* Listen on specified port */ int socket_listen(struct socket *s); #include int socket_write(struct socket *s, const char *buffer, size_t len); int socket_write_str(struct socket *s, const char *str); int socket_printf(struct socket *s, const char *format, ...); int socket_vprintf(struct socket *s, const char *format, va_list va); int socket_read(struct socket *s, char *buffer, size_t len); int socket_readline(struct socket *s, char *buffer, size_t bufflen); enum socket_err socket_get_error(struct socket *s); struct socket *socket_accept(struct socket *s); const char *socket_event_str(enum socket_event_type type); #endif