]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/utils/socket_bundle.h
05c2863741c3d32f4e3495898be4b57d1cdc2912
[Evergreen.git] / OpenSRF / src / utils / socket_bundle.h
1 #include "utils.h"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <sys/types.h>
6 #include <errno.h>
7
8 #include "utils.h"
9 #include "logging.h"
10
11 //---------------------------------------------------------------
12 // Unix headers
13 //---------------------------------------------------------------
14 #include <unistd.h>
15 #include <sys/time.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <sys/socket.h>
19 #include <arpa/inet.h>
20 #include <netdb.h>
21 #include <netinet/in.h>
22 #include <sys/un.h>
23
24 #include <signal.h>
25
26 #ifndef SOCKET_BUNDLE_H
27 #define SOCKET_BUNDLE_H
28
29
30 #define SERVER_SOCKET                   1
31 #define CLIENT_SOCKET                   2
32
33 #define INET 10 
34 #define UNIX 11 
35
36 /* buffer used to read from the sockets */
37 #define BUFSIZE 1024 
38
39
40 /* models a single socket connection */
41 struct socket_node_struct {
42         int endpoint;           /* SERVER_SOCKET or CLIENT_SOCKET */
43         int addr_type;          /* INET or UNIX */
44         int sock_fd;
45         int parent_id;          /* if we're a new client for a server socket, 
46                                                                 this points to the server socket we spawned from */
47         struct socket_node_struct* next;
48 };
49 typedef struct socket_node_struct socket_node;
50
51
52 /* Maintains the socket set */
53 struct socket_manager_struct {
54         /* callback for passing up any received data.  sock_fd is the socket
55                 that read the data.  parent_id (if > 0) is the socket id of the 
56                 server that this socket spawned from (i.e. it's a new client connection) */
57         void (*data_received) 
58                 (void* blob, struct socket_manager_struct*, 
59                  int sock_fd, char* data, int parent_id);
60
61         void (*on_socket_closed) (void* blob, int sock_fd);
62
63         socket_node* socket;
64         void* blob;
65 };
66 typedef struct socket_manager_struct socket_manager;
67
68 void socket_manager_free(socket_manager* mgr);
69
70 /* creates a new server socket node and adds it to the socket set.
71         returns socket id on success.  -1 on failure.
72         socket_type is one of INET or UNIX  */
73 int socket_open_tcp_server(socket_manager*, int port);
74
75 int socket_open_unix_server(socket_manager* mgr, char* path);
76
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);
80
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);
84
85 /* returns the socket_node with the given sock_fd */
86 socket_node* socket_find_node(socket_manager*, int sock_fd);
87
88 /* removes the node with the given sock_fd from the list and frees it */
89 void socket_remove_node(socket_manager*, int sock_fd);
90
91
92 /* sends the given data to the given socket. returns 0 on success, -1 otherwise */
93 int socket_send(int sock_fd, const char* data);
94
95 /* disconnects the node with the given sock_fd and removes
96         it from the socket set */
97 void socket_disconnect(socket_manager*, int sock_fd);
98
99 /* allocates and inserts a new socket node into the nodeset.
100         if parent_id is positive and non-zero, it will be set */
101 socket_node*  _socket_add_node(socket_manager* mgr, 
102                 int endpoint, int addr_type, int sock_fd, int parent_id );
103
104 int socket_wait(socket_manager* mgr, int timeout, int sock_fd);
105
106 /* waits on all sockets for incoming data.  
107         timeout == -1   | block indefinitely
108         timeout == 0    | don't block, just read any available data off all sockets
109         timeout == x    | block for at most x seconds */
110 int socket_wait_all(socket_manager* mgr, int timeout);
111
112 /* iterates over the sockets in the set and handles active sockets.
113         new sockets connecting to server sockets cause the creation
114         of a new socket node.
115         Any new data read is is passed off to the data_received callback
116         as it arrives */
117 int _socket_route_data(socket_manager* mgr, int num_active, fd_set* read_set);
118
119 /* utility function for displaying the currently attached sockets */
120 void _socket_print_list(socket_manager* mgr);
121
122 int socket_connected(int sock_fd);
123
124
125 int _socket_handle_new_client(socket_manager* mgr, socket_node* node);
126 int _socket_handle_client_data(socket_manager* mgr, socket_node* node);
127
128
129 #endif