9a7947b88f6d5513f51734683f19c134c420699f
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/strbuf.h"
36 #include <sys/socket.h>
38 #include <stdbool.h>
39 #include <stddef.h>
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
45 struct sdb_client;
46 typedef struct sdb_client sdb_client_t;
48 /*
49 * sdb_client_create:
50 * Allocates and initializes a client object to connect to the specified
51 * address.
52 *
53 * Returns:
54 * - a new client object on success
55 * - NULL in case of an error
56 */
57 sdb_client_t *
58 sdb_client_create(const char *address);
60 /*
61 * sdb_client_destroy:
62 * Destroyes the client connection and deallocates the client object.
63 */
64 void
65 sdb_client_destroy(sdb_client_t *client);
67 /*
68 * sdb_client_connect:
69 * Connect to the client's address using the specified username.
70 *
71 * Returns:
72 * - 0 on success
73 * - a negative value else
74 */
75 int
76 sdb_client_connect(sdb_client_t *client, const char *username);
78 /*
79 * sdb_client_sockfd:
80 * Return the client socket's file descriptor.
81 */
82 int
83 sdb_client_sockfd(sdb_client_t *client);
85 /*
86 * sdb_client_shutdown:
87 * Shut down the client's send and/or receive operations.
88 * See shutdown(3) for details.
89 */
90 int
91 sdb_client_shutdown(sdb_client_t *client, int how);
93 /*
94 * sdb_client_close:
95 * Close the client connection.
96 */
97 void
98 sdb_client_close(sdb_client_t *client);
100 /*
101 * sdb_client_rpc:
102 * Send the specified message to the server and wait for the reply. All
103 * received data is written to the specified buffer. If specified, the
104 * returned status code is written to the memory location pointed to by
105 * 'code'. In case of an error or an incomplete command, the status code is
106 * set to UINT32_MAX. The returned data does not include the status code and
107 * message len as received from the remote side but only the data associated
108 * with the message. The function handles all asynchronous log messages by
109 * logging them at the right log level.
110 *
111 * Returns:
112 * - the number of bytes read
113 * (may be zero if the message did not include any data)
114 * - a negative value on error
115 */
116 ssize_t
117 sdb_client_rpc(sdb_client_t *client,
118 uint32_t cmd, uint32_t msg_len, const char *msg,
119 uint32_t *code, sdb_strbuf_t *buf);
121 /*
122 * sdb_client_send:
123 * Send the specified command and accompanying data to the server.
124 *
125 * Returns:
126 * - the number of bytes send
127 * - a negative value else.
128 */
129 ssize_t
130 sdb_client_send(sdb_client_t *client,
131 uint32_t cmd, uint32_t data_len, const char *data);
133 /*
134 * sdb_client_recv:
135 * Receive data from the connection. All data is written to the specified
136 * buffer. If specified, the returned status code is written to the memory
137 * location pointed to by 'code'. In case of an error or an incomplete
138 * command, the status code is set to UINT32_MAX. The returned data does not
139 * include the status code and message len as received from the remote side
140 * but only the data associated with the message.
141 *
142 * Returns:
143 * - the number of bytes read
144 * (may be zero if the message did not include any data)
145 * - a negative value on error
146 */
147 ssize_t
148 sdb_client_recv(sdb_client_t *client,
149 uint32_t *code, sdb_strbuf_t *buf);
151 /*
152 * sdb_client_eof:
153 * Returns true if end of file on the client connection was reached, that is,
154 * if the remote side closed the connection.
155 */
156 bool
157 sdb_client_eof(sdb_client_t *client);
159 #ifdef __cplusplus
160 } /* extern "C" */
161 #endif
163 #endif /* ! SDB_CLIENT_SOCK_H */
165 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */