uploaded spice-vdagent
[platform/adaptation/emulator/spice-vdagent.git] / src / udscs.h
1 /*  udscs.h Unix Domain Socket Client Server framework header file
2
3     Copyright 2010 Red Hat, Inc.
4
5     Red Hat Authors:
6     Hans de Goede <hdegoede@redhat.com>
7
8     This program is free software: you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation, either version 3 of the License, or   
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of 
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #ifndef __UDSCS_H
23 #define __UDSCS_H
24
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <sys/select.h>
28 #include <sys/socket.h>
29
30 struct udscs_connection;
31 struct udscs_server;
32 struct udscs_message_header {
33     uint32_t type;
34     uint32_t arg1;
35     uint32_t arg2;
36     uint32_t size;  
37 };
38
39 /* Callbacks with this type will be called when a new connection to a
40    server is accepted. */
41 typedef void (*udscs_connect_callback)(struct udscs_connection *conn);
42 /* Callbacks with this type will be called when a complete message has been
43    received. The callback may call udscs_destroy_connection, in which case
44    *connp must be made NULL (which udscs_destroy_connection takes care of) */
45 typedef void (*udscs_read_callback)(struct udscs_connection **connp,
46     struct udscs_message_header *header, uint8_t *data);
47 /* Callback type for udscs_server_for_all_clients. Clients can be disconnected
48    from this callback just like with a read callback. */
49 typedef int (*udscs_for_all_clients_callback)(struct udscs_connection **connp,
50     void *priv);
51 /* Callbacks with this type will be called when the connection is disconnected.
52    Note:
53    1) udscs will destroy the connection in question itself after
54       this callback has completed!
55    2) This callback is always called, even if the disconnect is initiated
56       by the udscs user through returning -1 from a read callback, or
57       by explictly calling udscs_destroy_connection */
58 typedef void (*udscs_disconnect_callback)(struct udscs_connection *conn);
59
60 /* Create a unix domain socket named name and start listening on it. */
61 struct udscs_server *udscs_create_server(const char *socketname,
62     udscs_connect_callback connect_callback,
63     udscs_read_callback read_callback,
64     udscs_disconnect_callback disconnect_callback,
65     const char * const type_to_string[], int no_types, int debug);
66
67 void udscs_destroy_server(struct udscs_server *server);
68
69 /* Connect to a unix domain socket named name. */
70 struct udscs_connection *udscs_connect(const char *socketname,
71     udscs_read_callback read_callback,
72     udscs_disconnect_callback disconnect_callback,
73     const char * const type_to_string[], int no_types, int debug);
74
75 /* The contents of connp will be made NULL */
76 void udscs_destroy_connection(struct udscs_connection **connp);
77
78
79 /* Given an udscs server or client fill the fd_sets pointed to by readfds and
80    writefds for select() usage.
81
82    Return value: value of the highest fd + 1 */
83 int udscs_server_fill_fds(struct udscs_server *server, fd_set *readfds,
84         fd_set *writefds);
85
86 int udscs_client_fill_fds(struct udscs_connection *conn, fd_set *readfds,
87         fd_set *writefds);
88
89 /* Handle any events flagged by select for the given udscs server or client. */
90 void udscs_server_handle_fds(struct udscs_server *server, fd_set *readfds,
91         fd_set *writefds);
92
93 /* Note the connection may be destroyed (when disconnected) by this call
94    in this case the disconnect calllback will get called before the
95    destruction and the contents of connp will be made NULL */
96 void udscs_client_handle_fds(struct udscs_connection **connp, fd_set *readfds,
97         fd_set *writefds);
98
99
100 /* Queue a message for delivery to the client connected through conn.
101
102    Returns 0 on success -1 on error (only happens when malloc fails) */
103 int udscs_write(struct udscs_connection *conn, uint32_t type, uint32_t arg1,
104         uint32_t arg2, const uint8_t *data, uint32_t size);
105
106 /* Like udscs_write, but then send the message to all clients connected to
107    the server */
108 int udscs_server_write_all(struct udscs_server *server,
109         uint32_t type, uint32_t arg1, uint32_t arg2,
110         const uint8_t *data, uint32_t size);
111 /* Call func for all clients connected to the server, passing through
112    priv to all func calls. Returns the total of the return values from all
113    calls to func */
114 int udscs_server_for_all_clients(struct udscs_server *server,
115         udscs_for_all_clients_callback func, void *priv);
116
117
118 struct ucred udscs_get_peer_cred(struct udscs_connection *conn);
119
120 /* For server use, to associate per connection data with a connection */
121 void udscs_set_user_data(struct udscs_connection *conn, void *data);
122 void *udscs_get_user_data(struct udscs_connection *conn);
123
124 #endif