Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../proc/self/root/usr/include
File: fstrm.h
/*
[0] Fix | Delete
* Copyright (c) 2013-2014 by Farsight Security, Inc.
[1] Fix | Delete
*
[2] Fix | Delete
* Permission is hereby granted, free of charge, to any person obtaining
[3] Fix | Delete
* a copy of this software and associated documentation files (the
[4] Fix | Delete
* "Software"), to deal in the Software without restriction, including
[5] Fix | Delete
* without limitation the rights to use, copy, modify, merge, publish,
[6] Fix | Delete
* distribute, sublicense, and/or sell copies of the Software, and to
[7] Fix | Delete
* permit persons to whom the Software is furnished to do so, subject to
[8] Fix | Delete
* the following conditions:
[9] Fix | Delete
*
[10] Fix | Delete
* The above copyright notice and this permission notice shall be included
[11] Fix | Delete
* in all copies or substantial portions of the Software.
[12] Fix | Delete
*
[13] Fix | Delete
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
[14] Fix | Delete
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
[15] Fix | Delete
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
[16] Fix | Delete
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
[17] Fix | Delete
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
[18] Fix | Delete
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
[19] Fix | Delete
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
[20] Fix | Delete
*
[21] Fix | Delete
*/
[22] Fix | Delete
[23] Fix | Delete
/*! \file
[24] Fix | Delete
* \mainpage Introduction
[25] Fix | Delete
*
[26] Fix | Delete
* This is `fstrm`, a C implementation of the Frame Streams data transport
[27] Fix | Delete
* protocol.
[28] Fix | Delete
*
[29] Fix | Delete
* Frame Streams is a light weight, binary clean protocol that allows for the
[30] Fix | Delete
* transport of arbitrarily encoded data payload sequences with minimal framing
[31] Fix | Delete
* overhead -- just four bytes per data frame. Frame Streams does not specify an
[32] Fix | Delete
* encoding format for data frames and can be used with any data serialization
[33] Fix | Delete
* format that produces byte sequences, such as [Protocol Buffers], [XML],
[34] Fix | Delete
* [JSON], [MessagePack], [YAML], etc. Frame Streams can be used as both a
[35] Fix | Delete
* streaming transport over a reliable byte stream socket (TCP sockets, TLS
[36] Fix | Delete
* connections, `AF_UNIX` sockets, etc.) for data in motion as well as a file
[37] Fix | Delete
* format for data at rest. A "Content Type" header identifies the type of
[38] Fix | Delete
* payload being carried over an individual Frame Stream and allows cooperating
[39] Fix | Delete
* programs to determine how to interpret a given sequence of data payloads.
[40] Fix | Delete
*
[41] Fix | Delete
* `fstrm` is an optimized C implementation of Frame Streams that includes a
[42] Fix | Delete
* fast, lockless circular queue implementation and exposes library interfaces
[43] Fix | Delete
* for setting up a dedicated Frame Streams I/O thread and asynchronously
[44] Fix | Delete
* submitting data frames for transport from worker threads. It was originally
[45] Fix | Delete
* written to facilitate the addition of high speed binary logging to DNS
[46] Fix | Delete
* servers written in C using the [dnstap] log format.
[47] Fix | Delete
*
[48] Fix | Delete
* This is the API documentation for the `fstrm` library. For the project
[49] Fix | Delete
* hosting site, see <https://github.com/farsightsec/fstrm>.
[50] Fix | Delete
*
[51] Fix | Delete
* \authors Farsight Security, Inc. and the `fstrm` authors.
[52] Fix | Delete
*
[53] Fix | Delete
* \copyright 2013-2018. Licensed under the terms of the [MIT] license.
[54] Fix | Delete
*
[55] Fix | Delete
* [Protocol Buffers]: https://developers.google.com/protocol-buffers/
[56] Fix | Delete
* [XML]: http://www.w3.org/TR/xml11/
[57] Fix | Delete
* [JSON]: http://www.json.org/
[58] Fix | Delete
* [MessagePack]: http://msgpack.org/
[59] Fix | Delete
* [YAML]: http://www.yaml.org/
[60] Fix | Delete
* [dnstap]: http://dnstap.info/
[61] Fix | Delete
* [MIT]: https://opensource.org/licenses/MIT
[62] Fix | Delete
*
[63] Fix | Delete
* \page overview Library overview
[64] Fix | Delete
*
[65] Fix | Delete
* \section init Initializing the library
[66] Fix | Delete
*
[67] Fix | Delete
* `fstrm` has no global library state. In most cases, only a single
[68] Fix | Delete
* \ref fstrm_iothr library context object will be needed for the entire process,
[69] Fix | Delete
* which will implicitly create a background I/O serialization thread. This I/O
[70] Fix | Delete
* thread is bound to a particular output writer (for example, an `AF_UNIX`
[71] Fix | Delete
* socket) and is fully buffered -- submitted data frames will be accumulated in
[72] Fix | Delete
* an output buffer and periodically flushed, minimizing the number of system
[73] Fix | Delete
* calls that need to be performed. This frees worker threads from waiting for a
[74] Fix | Delete
* write() to complete.
[75] Fix | Delete
*
[76] Fix | Delete
* `fstrm` abstracts the actual I/O operations needed to read or write a byte
[77] Fix | Delete
* stream. File and socket I/O implementations are included in the library, but
[78] Fix | Delete
* if necessary `fstrm` can be extended to support new types of byte stream
[79] Fix | Delete
* transports. See the \ref fstrm_reader, \ref fstrm_writer, and \ref fstrm_rdwr
[80] Fix | Delete
* interfaces for details.
[81] Fix | Delete
*
[82] Fix | Delete
* The following code example shows the initialization of an `fstrm_iothr`
[83] Fix | Delete
* library context object connected to an \ref fstrm_file writer.
[84] Fix | Delete
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[85] Fix | Delete
const char *file_path = "/tmp/output.fs";
[86] Fix | Delete
struct fstrm_file_options *fopt;
[87] Fix | Delete
struct fstrm_iothr *iothr;
[88] Fix | Delete
struct fstrm_writer *writer;
[89] Fix | Delete
[90] Fix | Delete
fopt = fstrm_file_options_init();
[91] Fix | Delete
fstrm_file_options_set_file_path(fopt, file_path);
[92] Fix | Delete
[93] Fix | Delete
writer = fstrm_file_writer_init(fopt, NULL);
[94] Fix | Delete
if (!writer) {
[95] Fix | Delete
fprintf(stderr, "Error: fstrm_file_writer_init() failed.\n");
[96] Fix | Delete
exit(EXIT_FAILURE);
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
iothr = fstrm_iothr_init(NULL, &writer);
[100] Fix | Delete
if (!iothr) {
[101] Fix | Delete
fprintf(stderr, "Error: fstrm_iothr_init() failed.\n");
[102] Fix | Delete
exit(EXIT_FAILURE);
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
fstrm_file_options_destroy(&fopt);
[106] Fix | Delete
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[107] Fix | Delete
*
[108] Fix | Delete
* Since the I/O operations are abstracted through the `fstrm_writer` interface,
[109] Fix | Delete
* the `writer` variable in the above example could instead have been
[110] Fix | Delete
* initialized with a completely different implementation. For example,
[111] Fix | Delete
* \ref fstrm_unix_writer objects can be initialized as follows:
[112] Fix | Delete
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[113] Fix | Delete
const char *socket_path = "/tmp/output.sock";
[114] Fix | Delete
struct fstrm_writer *writer;
[115] Fix | Delete
struct fstrm_unix_writer_options *uwopt;
[116] Fix | Delete
[117] Fix | Delete
uwopt = fstrm_unix_writer_options_init();
[118] Fix | Delete
fstrm_unix_writer_options_set_socket_path(uwopt, socket_path);
[119] Fix | Delete
[120] Fix | Delete
writer = fstrm_unix_writer_init(uwopt, NULL);
[121] Fix | Delete
if (!writer) {
[122] Fix | Delete
fprintf(stderr, "Error: fstrm_unix_writer_init() failed.\n");
[123] Fix | Delete
exit(EXIT_FAILURE);
[124] Fix | Delete
}
[125] Fix | Delete
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[126] Fix | Delete
*
[127] Fix | Delete
* \section queue Getting an input queue
[128] Fix | Delete
*
[129] Fix | Delete
* After the `fstrm_iothr` object has been created with fstrm_iothr_init(), an
[130] Fix | Delete
* input queue handle can be obtained with the fstrm_iothr_get_input_queue()
[131] Fix | Delete
* function, which returns an `fstrm_iothr_queue` object. This function is
[132] Fix | Delete
* thread-safe and returns a unique queue each time it is called, up to the
[133] Fix | Delete
* number of queues specified by fstrm_iothr_options_set_num_input_queues().
[134] Fix | Delete
* `fstrm_iothr_queue` objects belong to their parent `fstrm_iothr` object and
[135] Fix | Delete
* will be destroyed when the parent `fstrm_iothr` object is destroyed.
[136] Fix | Delete
*
[137] Fix | Delete
* The following code example shows a single `fstrm_iothr_queue` handle being
[138] Fix | Delete
* obtained from an already initialized `fstrm_iothr` library context object.
[139] Fix | Delete
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[140] Fix | Delete
// 'iothr' is a struct fstrm_iothr *
[141] Fix | Delete
[142] Fix | Delete
struct fstrm_iothr_queue *ioq;
[143] Fix | Delete
ioq = fstrm_iothr_get_input_queue(iothr);
[144] Fix | Delete
if (!ioq) {
[145] Fix | Delete
fprintf(stderr, "Error: fstrm_iothr_get_input_queue() failed.\n");
[146] Fix | Delete
exit(EXIT_FAILURE);
[147] Fix | Delete
}
[148] Fix | Delete
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[149] Fix | Delete
*
[150] Fix | Delete
* \section submit Submitting data frames
[151] Fix | Delete
*
[152] Fix | Delete
* Once the `fstrm_iothr` object has been created and an `fstrm_iothr_queue`
[153] Fix | Delete
* handle is available, data frames can be submitted for asynchronous writing
[154] Fix | Delete
* using the fstrm_iothr_submit() function. A callback is passed to this
[155] Fix | Delete
* function which will be invoked to deallocate the data frame once the I/O
[156] Fix | Delete
* thread has completed processing it. In the common case where the data frame
[157] Fix | Delete
* is dynamically allocated with `malloc()`, the deallocation callback must call
[158] Fix | Delete
* `free()`. fstrm_free_wrapper() is provided as a convenience function which
[159] Fix | Delete
* does this and can be specified as the `free_func` parameter to
[160] Fix | Delete
* fstrm_iothr_submit().
[161] Fix | Delete
*
[162] Fix | Delete
* If space is available in the queue, fstrm_iothr_submit() will return
[163] Fix | Delete
* #fstrm_res_success, indicating that ownership of the memory allocation for the
[164] Fix | Delete
* data frame has passed from the caller to the library. The caller must not
[165] Fix | Delete
* reuse or deallocate the memory for the data frame after a successful call to
[166] Fix | Delete
* fstrm_iothr_submit().
[167] Fix | Delete
*
[168] Fix | Delete
* Callers must check the return value of fstrm_iothr_submit(). If this function
[169] Fix | Delete
* fails, that is, it returns any result code other than #fstrm_res_success, the
[170] Fix | Delete
* caller must deallocate or otherwise dispose of memory allocated for the data
[171] Fix | Delete
* frame, in order to avoid leaking memory. fstrm_iothr_submit() can fail with
[172] Fix | Delete
* #fstrm_res_again if there is currently no space in the circular queue for an
[173] Fix | Delete
* additional frame, in which case a later call to fstrm_iothr_submit() with the
[174] Fix | Delete
* same parameters may succeed. However, if fstrm_iothr_submit() fails with
[175] Fix | Delete
* #fstrm_res_invalid, then there is a problem with the parameters and a later
[176] Fix | Delete
* call will not succeed.
[177] Fix | Delete
*
[178] Fix | Delete
* The following code example shows data frames containing a short sequence of
[179] Fix | Delete
* bytes being created and submitted repeatedly, with appropriate error
[180] Fix | Delete
* handling. Note that the data frames in this example intentionally contain
[181] Fix | Delete
* embedded unprintable characters, showing that Frame Streams is binary clean.
[182] Fix | Delete
* This example follows from the previous examples, where the `iothr` and `ioq`
[183] Fix | Delete
* variables have already been initialized.
[184] Fix | Delete
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[185] Fix | Delete
// 'iothr' is a struct fstrm_iothr *
[186] Fix | Delete
// 'ioq' is a struct fstrm_queue *
[187] Fix | Delete
[188] Fix | Delete
const unsigned num_frames = 100;
[189] Fix | Delete
const uint8_t frame_template[] = {
[190] Fix | Delete
'H', 'e', 'l', 'l', 'o', 0x00, 0x01, 0x02, 0x03,
[191] Fix | Delete
'W', 'o', 'r', 'l', 'd', 0x04, 0x05, 0x06, 0x07,
[192] Fix | Delete
};
[193] Fix | Delete
[194] Fix | Delete
for (unsigned i = 0; i < num_frames; i++) {
[195] Fix | Delete
// Allocate a new frame from the template.
[196] Fix | Delete
uint8_t *frame = malloc(sizeof(frame_template));
[197] Fix | Delete
if (!frame)
[198] Fix | Delete
break;
[199] Fix | Delete
memcpy(frame, frame_template, sizeof(frame_template));
[200] Fix | Delete
[201] Fix | Delete
// Submit the frame for writing.
[202] Fix | Delete
for (;;) {
[203] Fix | Delete
fstrm_res res;
[204] Fix | Delete
res = fstrm_iothr_submit(iothr, ioq, frame,
[205] Fix | Delete
sizeof(frame_template),
[206] Fix | Delete
fstrm_free_wrapper, NULL);
[207] Fix | Delete
if (res == fstrm_res_success) {
[208] Fix | Delete
// Frame successfully queued.
[209] Fix | Delete
break;
[210] Fix | Delete
} else if (res == fstrm_res_again) {
[211] Fix | Delete
// Queue is full. Try again in a busy loop.
[212] Fix | Delete
// Alternatively, if loss can be tolerated we
[213] Fix | Delete
// could free the frame here and break out of
[214] Fix | Delete
// the loop.
[215] Fix | Delete
continue;
[216] Fix | Delete
} else {
[217] Fix | Delete
// Permanent failure.
[218] Fix | Delete
free(frame);
[219] Fix | Delete
fputs("fstrm_iothr_submit() failed.\n", stderr);
[220] Fix | Delete
break;
[221] Fix | Delete
}
[222] Fix | Delete
}
[223] Fix | Delete
}
[224] Fix | Delete
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[225] Fix | Delete
*
[226] Fix | Delete
* \section shutdown Shutting down
[227] Fix | Delete
*
[228] Fix | Delete
* Calling fstrm_iothr_destroy() on the `fstrm_iothr` object will signal the I/O
[229] Fix | Delete
* thread to flush any outstanding data frames being written and will deallocate
[230] Fix | Delete
* all associated resources. This function is synchronous and does not return
[231] Fix | Delete
* until the I/O thread has terminated.
[232] Fix | Delete
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[233] Fix | Delete
// 'iothr' is a struct fstrm_iothr *
[234] Fix | Delete
fstrm_iothr_destroy(&iothr);
[235] Fix | Delete
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[236] Fix | Delete
*/
[237] Fix | Delete
[238] Fix | Delete
#ifndef FSTRM_H
[239] Fix | Delete
#define FSTRM_H
[240] Fix | Delete
[241] Fix | Delete
#ifdef __cplusplus
[242] Fix | Delete
extern "C" {
[243] Fix | Delete
#endif
[244] Fix | Delete
[245] Fix | Delete
#include <sys/uio.h>
[246] Fix | Delete
#include <stddef.h>
[247] Fix | Delete
#include <stdint.h>
[248] Fix | Delete
[249] Fix | Delete
/**
[250] Fix | Delete
* \defgroup fstrm_res fstrm_res
[251] Fix | Delete
*
[252] Fix | Delete
* Library result codes.
[253] Fix | Delete
* @{
[254] Fix | Delete
*/
[255] Fix | Delete
[256] Fix | Delete
/**
[257] Fix | Delete
* Result codes for functions.
[258] Fix | Delete
*/
[259] Fix | Delete
typedef enum {
[260] Fix | Delete
/** Success. */
[261] Fix | Delete
fstrm_res_success,
[262] Fix | Delete
[263] Fix | Delete
/** Failure. */
[264] Fix | Delete
fstrm_res_failure,
[265] Fix | Delete
[266] Fix | Delete
/** Resource temporarily unavailable. */
[267] Fix | Delete
fstrm_res_again,
[268] Fix | Delete
[269] Fix | Delete
/** Parameters were invalid. */
[270] Fix | Delete
fstrm_res_invalid,
[271] Fix | Delete
[272] Fix | Delete
/** The end of a stream has been reached. */
[273] Fix | Delete
fstrm_res_stop,
[274] Fix | Delete
} fstrm_res;
[275] Fix | Delete
[276] Fix | Delete
/**@}*/
[277] Fix | Delete
[278] Fix | Delete
struct fstrm_control;
[279] Fix | Delete
struct fstrm_file_options;
[280] Fix | Delete
struct fstrm_iothr;
[281] Fix | Delete
struct fstrm_iothr_options;
[282] Fix | Delete
struct fstrm_iothr_queue;
[283] Fix | Delete
struct fstrm_rdwr;
[284] Fix | Delete
struct fstrm_reader_options;
[285] Fix | Delete
struct fstrm_unix_writer_options;
[286] Fix | Delete
struct fstrm_writer;
[287] Fix | Delete
struct fstrm_writer_options;
[288] Fix | Delete
[289] Fix | Delete
#include <fstrm/control.h>
[290] Fix | Delete
#include <fstrm/file.h>
[291] Fix | Delete
#include <fstrm/iothr.h>
[292] Fix | Delete
#include <fstrm/rdwr.h>
[293] Fix | Delete
#include <fstrm/reader.h>
[294] Fix | Delete
#include <fstrm/tcp_writer.h>
[295] Fix | Delete
#include <fstrm/unix_writer.h>
[296] Fix | Delete
#include <fstrm/writer.h>
[297] Fix | Delete
[298] Fix | Delete
#ifdef __cplusplus
[299] Fix | Delete
}
[300] Fix | Delete
#endif
[301] Fix | Delete
[302] Fix | Delete
#endif /* FSTRM_H */
[303] Fix | Delete
[304] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function