]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/utils/socket_bundle.h
made the logging module fail gracefully when syslog is not present
[OpenSRF.git] / 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 "log.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 <netinet/tcp.h>
23 #include <sys/un.h>
24
25 #include <signal.h>
26
27 #ifndef SOCKET_BUNDLE_H
28 #define SOCKET_BUNDLE_H
29
30
31 #define SERVER_SOCKET                   1
32 #define CLIENT_SOCKET                   2
33
34 #define INET 10 
35 #define UNIX 11 
36
37
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 */
42         int sock_fd;
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;
46 };
47 typedef struct socket_node_struct socket_node;
48
49
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) */
55         void (*data_received) 
56                 (void* blob, struct socket_manager_struct*, 
57                  int sock_fd, char* data, int parent_id);
58
59         void (*on_socket_closed) (void* blob, int sock_fd);
60
61         socket_node* socket;
62         void* blob;
63 };
64 typedef struct socket_manager_struct socket_manager;
65
66 void socket_manager_free(socket_manager* mgr);
67
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 );
72
73 int socket_open_unix_server(socket_manager* mgr, char* path);
74
75 int socket_open_udp_server( socket_manager* mgr, int port, char* listen_ip );
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 int socket_open_udp_client( socket_manager* mgr, int port, char* dest_addr);
86
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);
89
90 /* waits at most usecs microseconds for the socket buffer to
91  * be available */
92 int socket_send_timeout( int sock_fd, const char* data, int usecs );
93
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);
97
98 /* XXX This only works if 'sock_fd' is a client socket... */
99 int socket_wait(socket_manager* mgr, int timeout, int sock_fd);
100
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);
106
107 /* utility function for displaying the currently attached sockets */
108 void _socket_print_list(socket_manager* mgr);
109
110 int socket_connected(int sock_fd);
111
112 #endif