1 /*
2 * SysDB - src/include/client/sock.h
3 * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #ifndef SDB_CLIENT_SOCK_H
29 #define SDB_CLIENT_SOCK_H 1
31 #include "core/object.h"
32 #include "core/data.h"
33 #include "frontend/proto.h"
34 #include "utils/ssl.h"
35 #include "utils/strbuf.h"
37 #include <sys/socket.h>
39 #include <stdbool.h>
40 #include <stddef.h>
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
46 struct sdb_client;
47 typedef struct sdb_client sdb_client_t;
49 /*
50 * sdb_client_create:
51 * Allocates and initializes a client object to connect to the specified
52 * address.
53 *
54 * Returns:
55 * - a new client object on success
56 * - NULL in case of an error
57 */
58 sdb_client_t *
59 sdb_client_create(const char *address);
61 /*
62 * sdb_client_destroy:
63 * Destroys the client connection and deallocates the client object.
64 */
65 void
66 sdb_client_destroy(sdb_client_t *client);
68 /*
69 * sdb_client_set_ssl_options:
70 * Use the specified options for any SSL connections.
71 *
72 * Returns:
73 * - 0 on success
74 * - a negative value else
75 */
76 int
77 sdb_client_set_ssl_options(sdb_client_t *client, const sdb_ssl_options_t *opts);
79 /*
80 * sdb_client_connect:
81 * Connect to the client's address using the specified username.
82 *
83 * Returns:
84 * - 0 on success
85 * - a negative value else
86 */
87 int
88 sdb_client_connect(sdb_client_t *client, const char *username);
90 /*
91 * sdb_client_sockfd:
92 * Return the client socket's file descriptor.
93 */
94 int
95 sdb_client_sockfd(sdb_client_t *client);
97 /*
98 * sdb_client_shutdown:
99 * Shut down the client's send and/or receive operations.
100 * See shutdown(3) for details.
101 */
102 int
103 sdb_client_shutdown(sdb_client_t *client, int how);
105 /*
106 * sdb_client_close:
107 * Close the client connection.
108 */
109 void
110 sdb_client_close(sdb_client_t *client);
112 /*
113 * sdb_client_rpc:
114 * Send the specified message to the server and wait for the reply. All
115 * received data is written to the specified buffer. If specified, the
116 * returned status code is written to the memory location pointed to by
117 * 'code'. In case of an error or an incomplete command, the status code is
118 * set to UINT32_MAX. The returned data does not include the status code and
119 * message len as received from the remote side but only the data associated
120 * with the message. The function handles all asynchronous log messages by
121 * logging them at the right log level.
122 *
123 * Returns:
124 * - the number of bytes read
125 * (may be zero if the message did not include any data)
126 * - a negative value on error
127 */
128 ssize_t
129 sdb_client_rpc(sdb_client_t *client,
130 uint32_t cmd, uint32_t msg_len, const char *msg,
131 uint32_t *code, sdb_strbuf_t *buf);
133 /*
134 * sdb_client_send:
135 * Send the specified command and accompanying data to the server.
136 *
137 * Returns:
138 * - the number of bytes send
139 * - a negative value else.
140 */
141 ssize_t
142 sdb_client_send(sdb_client_t *client,
143 uint32_t cmd, uint32_t data_len, const char *data);
145 /*
146 * sdb_client_recv:
147 * Receive data from the connection. All data is written to the specified
148 * buffer. If specified, the returned status code is written to the memory
149 * location pointed to by 'code'. In case of an error or an incomplete
150 * command, the status code is set to UINT32_MAX. The returned data does not
151 * include the status code and message len as received from the remote side
152 * but only the data associated with the message.
153 *
154 * Returns:
155 * - the number of bytes read
156 * (may be zero if the message did not include any data)
157 * - a negative value on error
158 */
159 ssize_t
160 sdb_client_recv(sdb_client_t *client,
161 uint32_t *code, sdb_strbuf_t *buf);
163 /*
164 * sdb_client_eof:
165 * Returns true if end of file on the client connection was reached, that is,
166 * if the remote side closed the connection.
167 */
168 bool
169 sdb_client_eof(sdb_client_t *client);
171 #ifdef __cplusplus
172 } /* extern "C" */
173 #endif
175 #endif /* ! SDB_CLIENT_SOCK_H */
177 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */