f1f7691a9a0490d6c2107871f8343ac284272806
1 /*
2 * syscollector - src/include/utils/unixsock.h
3 * Copyright (C) 2012 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 SC_UTILS_UNIXSOCK_H
29 #define SC_UTILS_UNIXSOCK_H 1
31 #include "utils/data.h"
33 #include <sys/socket.h>
35 #include <stddef.h>
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
41 struct sc_unixsock_client;
42 typedef struct sc_unixsock_client sc_unixsock_client_t;
44 typedef int (*sc_unixsock_client_data_cb)(sc_unixsock_client_t *,
45 size_t, sc_data_t *);
47 sc_unixsock_client_t *
48 sc_unixsock_client_create(const char *path);
50 int
51 sc_unixsock_client_connect(sc_unixsock_client_t *client);
53 int
54 sc_unixsock_client_send(sc_unixsock_client_t *client, const char *msg);
56 char *
57 sc_unixsock_client_recv(sc_unixsock_client_t *client, char *buffer, size_t buflen);
59 /*
60 * sc_unixsock_client_process_lines:
61 * Reads up to 'max_lines' lines from the socket, splits each line at the
62 * specified 'delim' and passes the data on to the specified 'callback'. If
63 * 'max_lines' is less than zero, the function will read until EOF or an error
64 * is encountered. If 'n_cols' is greater than zero, the function will expect
65 * that number of columns to appear in each line. Also, it will expect that
66 * number of further arguments, specifying the data-type to be returned for
67 * the respective column (see sc_data_t). The content of each column will then
68 * be converted accordingly.
69 *
70 * Returns:
71 * - 0 on success
72 * - a negative value else
73 */
74 int
75 sc_unixsock_client_process_lines(sc_unixsock_client_t *client,
76 sc_unixsock_client_data_cb callback, long int max_lines,
77 const char *delim, int n_cols, ...);
79 /*
80 * sc_unixsock_client_shutdown:
81 * Shut down the client's send and/or receive operations. If appropriate, the
82 * client will automatically re-connect on the next send / receive operation
83 * after that.
84 *
85 * See shutdown(3) for details.
86 */
87 int
88 sc_unixsock_client_shutdown(sc_unixsock_client_t *client, int how);
90 /*
91 * sc_unixsock_client_clearerr, sc_unixsock_client_eof,
92 * sc_unixsock_client_error:
93 * Check and reset the client status. See the clearerr(3), feof(3), and
94 * ferror(3) manpages for details.
95 */
96 void
97 sc_unixsock_client_clearerr(sc_unixsock_client_t *client);
98 int
99 sc_unixsock_client_eof(sc_unixsock_client_t *client);
100 int
101 sc_unixsock_client_error(sc_unixsock_client_t *client);
103 void
104 sc_unixsock_client_destroy(sc_unixsock_client_t *client);
106 const char *
107 sc_unixsock_client_path(sc_unixsock_client_t *client);
109 #ifdef __cplusplus
110 } /* extern "C" */
111 #endif
113 #endif /* ! SC_UTILS_UNIXSOCK_H */
115 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */