11 //---------------------------------------------------------------
13 //---------------------------------------------------------------
18 #include <sys/socket.h>
19 #include <arpa/inet.h>
21 #include <netinet/in.h>
22 #include <netinet/tcp.h>
27 #ifndef SOCKET_BUNDLE_H
28 #define SOCKET_BUNDLE_H
31 #define SERVER_SOCKET 1
32 #define CLIENT_SOCKET 2
38 /* models a single socket connection */
39 struct socket_node_struct {
40 int endpoint; /* SERVER_SOCKET or CLIENT_SOCKET */
41 int addr_type; /* INET or UNIX */
43 int parent_id; /* if we're a new client for a server socket,
44 this points to the server socket we spawned from */
45 struct socket_node_struct* next;
47 typedef struct socket_node_struct socket_node;
50 /* Maintains the socket set */
51 struct socket_manager_struct {
52 /* callback for passing up any received data. sock_fd is the socket
53 that read the data. parent_id (if > 0) is the socket id of the
54 server that this socket spawned from (i.e. it's a new client connection) */
56 (void* blob, struct socket_manager_struct*,
57 int sock_fd, char* data, int parent_id);
59 void (*on_socket_closed) (void* blob, int sock_fd);
64 typedef struct socket_manager_struct socket_manager;
66 void socket_manager_free(socket_manager* mgr);
68 /* creates a new server socket node and adds it to the socket set.
69 returns socket id on success. -1 on failure.
70 socket_type is one of INET or UNIX */
71 int socket_open_tcp_server(socket_manager*, int port, char* listen_ip );
73 int socket_open_unix_server(socket_manager* mgr, char* path);
75 int socket_open_udp_server( socket_manager* mgr, int port, char* listen_ip );
77 /* creates a client TCP socket and adds it to the socket set.
78 returns 0 on success. -1 on failure. */
79 int socket_open_tcp_client(socket_manager*, int port, char* dest_addr);
81 /* creates a client UNIX socket and adds it to the socket set.
82 returns 0 on success. -1 on failure. */
83 int socket_open_unix_client(socket_manager*, char* sock_path);
85 int socket_open_udp_client( socket_manager* mgr, int port, char* dest_addr);
87 /* sends the given data to the given socket. returns 0 on success, -1 otherwise */
88 int socket_send(int sock_fd, const char* data);
90 /* waits at most usecs microseconds for the socket buffer to
92 int socket_send_timeout( int sock_fd, const char* data, int usecs );
94 /* disconnects the node with the given sock_fd and removes
95 it from the socket set */
96 void socket_disconnect(socket_manager*, int sock_fd);
98 /* XXX This only works if 'sock_fd' is a client socket... */
99 int socket_wait(socket_manager* mgr, int timeout, int sock_fd);
101 /* waits on all sockets for incoming data.
102 timeout == -1 | block indefinitely
103 timeout == 0 | don't block, just read any available data off all sockets
104 timeout == x | block for at most x seconds */
105 int socket_wait_all(socket_manager* mgr, int timeout);
107 /* utility function for displaying the currently attached sockets */
108 void _socket_print_list(socket_manager* mgr);
110 int socket_connected(int sock_fd);