trilogy 2.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of trilogy might be problematic. Click here for more details.

Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +22 -0
  3. data/README.md +74 -0
  4. data/Rakefile +18 -0
  5. data/ext/trilogy-ruby/cast.c +272 -0
  6. data/ext/trilogy-ruby/cext.c +933 -0
  7. data/ext/trilogy-ruby/extconf.rb +16 -0
  8. data/ext/trilogy-ruby/inc/trilogy/blocking.h +163 -0
  9. data/ext/trilogy-ruby/inc/trilogy/buffer.h +64 -0
  10. data/ext/trilogy-ruby/inc/trilogy/builder.h +161 -0
  11. data/ext/trilogy-ruby/inc/trilogy/charset.h +277 -0
  12. data/ext/trilogy-ruby/inc/trilogy/client.h +546 -0
  13. data/ext/trilogy-ruby/inc/trilogy/error.h +43 -0
  14. data/ext/trilogy-ruby/inc/trilogy/packet_parser.h +34 -0
  15. data/ext/trilogy-ruby/inc/trilogy/protocol.h +756 -0
  16. data/ext/trilogy-ruby/inc/trilogy/reader.h +212 -0
  17. data/ext/trilogy-ruby/inc/trilogy/socket.h +111 -0
  18. data/ext/trilogy-ruby/inc/trilogy/vendor/curl_hostcheck.h +29 -0
  19. data/ext/trilogy-ruby/inc/trilogy/vendor/openssl_hostname_validation.h +51 -0
  20. data/ext/trilogy-ruby/inc/trilogy.h +8 -0
  21. data/ext/trilogy-ruby/src/blocking.c +241 -0
  22. data/ext/trilogy-ruby/src/buffer.c +60 -0
  23. data/ext/trilogy-ruby/src/builder.c +198 -0
  24. data/ext/trilogy-ruby/src/charset.c +212 -0
  25. data/ext/trilogy-ruby/src/client.c +728 -0
  26. data/ext/trilogy-ruby/src/error.c +17 -0
  27. data/ext/trilogy-ruby/src/packet_parser.c +140 -0
  28. data/ext/trilogy-ruby/src/protocol.c +676 -0
  29. data/ext/trilogy-ruby/src/reader.c +244 -0
  30. data/ext/trilogy-ruby/src/socket.c +623 -0
  31. data/ext/trilogy-ruby/src/vendor/curl_hostcheck.c +206 -0
  32. data/ext/trilogy-ruby/src/vendor/openssl_hostname_validation.c +175 -0
  33. data/ext/trilogy-ruby/trilogy-ruby.h +36 -0
  34. data/lib/trilogy/version.rb +3 -0
  35. data/lib/trilogy.rb +61 -0
  36. data/trilogy.gemspec +27 -0
  37. metadata +106 -0
@@ -0,0 +1,16 @@
1
+ require "mkmf"
2
+
3
+ # concatenate trilogy library sources to allow the compiler to optimise across
4
+ # source files
5
+ File.binwrite("trilogy.c",
6
+ Dir["#{__dir__}/src/**/*.c"].map { |src| File.binread(src) }.join)
7
+
8
+ $objs = %w[trilogy.o cast.o cext.o]
9
+ $CFLAGS << " -I #{__dir__}/inc -std=gnu99"
10
+
11
+ dir_config("openssl")
12
+
13
+ have_library("crypto", "CRYPTO_malloc")
14
+ have_library("ssl", "SSL_new")
15
+
16
+ create_makefile "trilogy/cext"
@@ -0,0 +1,163 @@
1
+ #ifndef TRILOGY_BLOCKING_H
2
+ #define TRILOGY_BLOCKING_H
3
+
4
+ #include "client.h"
5
+
6
+ /* Trilogy Blocking Client API
7
+ *
8
+ * This API is a set of high level functions for issuing commands to the MySQL-compatible
9
+ * server. Being just a simple wrapper around the non-blocking API - each call
10
+ * will block until a full request and response cycle has completed, or an error
11
+ * occurs.
12
+ *
13
+ * The trilogy_init function from client.h should be used to initialize a
14
+ * trilogy_conn_t struct. While trilogy_free should be used to ensure any internal
15
+ * buffers are freed.
16
+ *
17
+ * Applications requiring finer-grained control over I/O should use the
18
+ * non-blocking API in client.h
19
+ */
20
+
21
+ /* trilogy_connect - Establish a connection to a MySQL-compatible server
22
+ *
23
+ * conn - A pre-initialized trilogy_conn_t pointer.
24
+ * opts - Connection options for where to connect to.
25
+ *
26
+ * Return values
27
+ * TRILOGY_OK - Connected and authenticated successfully.
28
+ * TRILOGY_SYSERR - A system error occurred, check errno.
29
+ * TRILOGY_CLOSED_CONNECTION - The connection was severed during
30
+ * authentication. TRILOGY_PROTOCOL_VIOLATION - An error occurred while processing
31
+ * a network packet.
32
+ */
33
+ int trilogy_connect(trilogy_conn_t *conn, const trilogy_sockopt_t *opts);
34
+
35
+ /* trilogy_connect_sock - Establish a connection to a MySQL-compatible server with an
36
+ * - existing socket.
37
+ *
38
+ * conn - A pre-initialized trilogy_conn_t pointer.
39
+ * sock - A connected socket.
40
+ *
41
+ * Return values
42
+ * TRILOGY_OK - Connected and authenticated successfully.
43
+ * TRILOGY_SYSERR - A system error occurred, check errno.
44
+ * TRILOGY_CLOSED_CONNECTION - The connection was severed during
45
+ * authentication. TRILOGY_PROTOCOL_VIOLATION - An error occurred while processing
46
+ * a network packet.
47
+ */
48
+ int trilogy_connect_sock(trilogy_conn_t *conn, trilogy_sock_t *sock);
49
+
50
+ /* trilogy_change_db - Change the default database for a connection.
51
+ *
52
+ * conn - A connected trilogy_conn_t pointer. Using a disconnected
53
+ * trilogy_conn_t is undefined. name - Name of the database to set as default.
54
+ * name_len - Length of the database name string in bytes.
55
+ *
56
+ * Return values
57
+ * TRILOGY_OK - The change db command completed successfully.
58
+ * TRILOGY_ERR - The server returned an error.
59
+ * TRILOGY_SYSERR - A system error occurred, check errno.
60
+ * TRILOGY_CLOSED_CONNECTION - The connection is closed.
61
+ * TRILOGY_PROTOCOL_VIOLATION - An error occurred while processing a network
62
+ * packet.
63
+ */
64
+ int trilogy_change_db(trilogy_conn_t *conn, const char *name, size_t name_len);
65
+
66
+ /* trilogy_query - Send and execute a query.
67
+ *
68
+ * conn - A connected trilogy_conn_t pointer. Using a disconnected
69
+ * trilogy_conn_t is undefined.
70
+ * query - The query string to be sent to the server.
71
+ * query_len - Length of the query string in bytes.
72
+ * column_count_out - Out parameter; The number of columns in the result set.
73
+ *
74
+ * Return values
75
+ * TRILOGY_OK - The query completed successfully and there are
76
+ * no results to be read. TRILOGY_HAVE_RESULTS - The query completed
77
+ * successfully and there are results to be read. The caller must then call
78
+ * trilogy_read_full_column to read all column info
79
+ * packets, then trilogy_read_full_row until all rows
80
+ * have been read.
81
+ * TRILOGY_ERR - The server returned an error.
82
+ * TRILOGY_SYSERR - A system error occurred, check errno.
83
+ * TRILOGY_CLOSED_CONNECTION - The connection is closed.
84
+ * TRILOGY_PROTOCOL_VIOLATION - An error occurred while processing a network
85
+ * packet.
86
+ */
87
+ int trilogy_query(trilogy_conn_t *conn, const char *query, size_t query_len, uint64_t *column_count_out);
88
+
89
+ /* trilogy_read_full_column - Read a column from the result set.
90
+ *
91
+ * This should be called after issuing a query that has results. For example:
92
+ * an INSERT query won't have a result set.
93
+ *
94
+ * Calling this function at any other time during the connection lifecycle is
95
+ * undefined.
96
+ *
97
+ * conn - A connected trilogy_conn_t pointer. Using a disconnected
98
+ * trilogy_conn_t is undefined.
99
+ * column_out - Out parameter; A pointer to a pre-allocated trilogy_column_t,
100
+ * which will be filled out by this function.
101
+ *
102
+ * Return values
103
+ * TRILOGY_OK - The column was successfully read.
104
+ * TRILOGY_SYSERR - A system error occurred, check errno.
105
+ * TRILOGY_CLOSED_CONNECTION - The connection is closed.
106
+ * TRILOGY_PROTOCOL_VIOLATION - An error occurred while processing a network
107
+ * packet.
108
+ */
109
+ int trilogy_read_full_column(trilogy_conn_t *conn, trilogy_column_t *column_out);
110
+
111
+ /* trilogy_read_full_row - Read a row from the result set.
112
+ *
113
+ * This should only be called after reading all of the columns from a result
114
+ * set.
115
+ *
116
+ * Calling this function at any other time during the connection lifecycle is
117
+ * undefined.
118
+ *
119
+ * conn - A connected trilogy_conn_t pointer. Using a disconnected
120
+ * trilogy_conn_t is undefined.
121
+ * values_out - Out parameter; A pointer to a pre-allocated trilogy_value_t, which
122
+ * will be filled out by this function. It should be allocated with
123
+ * enough space to hold a trilogy_value_t pointer for each column.
124
+ * Something like: `(sizeof(trilogy_value_t) * column_count)`.
125
+ *
126
+ * Return values
127
+ * TRILOGY_OK - The row was successfully read.
128
+ * TRILOGY_SYSERR - A system error occurred, check errno.
129
+ * TRILOGY_CLOSED_CONNECTION - The connection is closed.
130
+ * TRILOGY_PROTOCOL_VIOLATION - An error occurred while processing a network
131
+ * packet.
132
+ */
133
+ int trilogy_read_full_row(trilogy_conn_t *conn, trilogy_value_t *values_out);
134
+
135
+ /* trilogy_ping - Send a ping command to the server.
136
+ *
137
+ * conn - A connected trilogy_conn_t pointer. Using a disconnected trilogy_conn_t
138
+ * is undefined.
139
+ *
140
+ * Return values
141
+ * TRILOGY_OK - The ping command completed successfully.
142
+ * TRILOGY_SYSERR - A system error occurred, check errno.
143
+ * TRILOGY_CLOSED_CONNECTION - The connection is closed.
144
+ * TRILOGY_PROTOCOL_VIOLATION - An error occurred while processing a network
145
+ * packet.
146
+ */
147
+ int trilogy_ping(trilogy_conn_t *conn);
148
+
149
+ /* trilogy_close - Send a quit command to the server.
150
+ *
151
+ * conn - A connected trilogy_conn_t pointer. Using a disconnected trilogy_conn_t
152
+ * is undefined.
153
+ *
154
+ * Return values
155
+ * TRILOGY_OK - The quit command was completed successfully.
156
+ * TRILOGY_SYSERR - A system error occurred, check errno.
157
+ * TRILOGY_CLOSED_CONNECTION - The connection is closed.
158
+ * TRILOGY_PROTOCOL_VIOLATION - An error occurred while processing a network
159
+ * packet.
160
+ */
161
+ int trilogy_close(trilogy_conn_t *conn);
162
+
163
+ #endif
@@ -0,0 +1,64 @@
1
+ #ifndef TRILOGY_BUFFER_H
2
+ #define TRILOGY_BUFFER_H
3
+
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+
7
+ /* trilogy_buffer_t - A convenience type used for wrapping a reusable chunk of
8
+ * memory.
9
+ */
10
+ typedef struct {
11
+ size_t len;
12
+ size_t cap;
13
+ uint8_t *buff;
14
+ } trilogy_buffer_t;
15
+
16
+ /* trilogy_buffer_init - Initialize an trilogy_buffer_t and pre-allocate
17
+ * `initial_capacity` bytes.
18
+ *
19
+ * buffer - A pointer to an allocated, uninitialized trilogy_buffer_t.
20
+ * initial_capacity - The initial capacity for the buffer.
21
+ *
22
+ * Return values:
23
+ * TRILOGY_OK - The buffer was initialized.
24
+ * TRILOGY_SYSERR - A system error occurred, check errno.
25
+ */
26
+ int trilogy_buffer_init(trilogy_buffer_t *buffer, size_t initial_capacity);
27
+
28
+ /* trilogy_buffer_expand - Make sure there is at least `needed` bytes available in
29
+ * the underlying buffer, resizing it to be larger if necessary.
30
+ *
31
+ * buffer - A pre-initialized trilogy_buffer_t pointer.
32
+ * needed - The amount of space requested to be available in the buffer after
33
+ * after this call returns.
34
+ *
35
+ * Return values:
36
+ * TRILOGY_OK - The buffer is guaranteed to have at least `needed` bytes of
37
+ * space available.
38
+ * TRILOGY_SYSERR - A system error occurred, check errno.
39
+ * TRILOGY_TYPE_OVERFLOW - The amount of buffer spaced needed is larger than the
40
+ * what can be represented by `size_t`.
41
+ */
42
+ int trilogy_buffer_expand(trilogy_buffer_t *buffer, size_t needed);
43
+
44
+ /* trilogy_buffer_putc - Appends a byte to the buffer, resizing the underlying
45
+ * allocation if necessary.
46
+ *
47
+ * buffer - A pointer to a pre-initialized trilogy_buffer_t.
48
+ * c - The byte to append to the buffer.
49
+ *
50
+ * Return values:
51
+ * TRILOGY_OK - The character was appended to the buffer
52
+ * TRILOGY_SYSERR - A system error occurred, check errno.
53
+ */
54
+ int trilogy_buffer_putc(trilogy_buffer_t *buffer, uint8_t c);
55
+
56
+ /* trilogy_buffer_free - Free an trilogy_buffer_t's underlying storage. The buffer
57
+ * must be re-initialized with trilogy_buffer_init if it is to be reused. Any
58
+ * operations performed on an unintialized or freed buffer are undefined.
59
+ *
60
+ * buffer - An initialized trilogy_buffer_t.
61
+ */
62
+ void trilogy_buffer_free(trilogy_buffer_t *buffer);
63
+
64
+ #endif
@@ -0,0 +1,161 @@
1
+ #ifndef TRILOGY_BUILDER_H
2
+ #define TRILOGY_BUILDER_H
3
+
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+
7
+ #include "trilogy/buffer.h"
8
+
9
+ /* Trilogy Packet Builder API
10
+ *
11
+ * The builder API is used for building protocol packet buffers.
12
+ */
13
+
14
+ /* trilogy_builder_t - The builder API's instance type.
15
+ */
16
+ typedef struct {
17
+ trilogy_buffer_t *buffer;
18
+ size_t header_offset;
19
+ uint32_t fragment_length;
20
+ uint8_t seq;
21
+ } trilogy_builder_t;
22
+
23
+ /* trilogy_builder_init - Initialize a pre-allocated trilogy_builder_t
24
+ *
25
+ * builder - A pre-allocated trilogy_builder_t pointer
26
+ * buffer - A pre-initialized trilogy_buffer_t pointer
27
+ * seq - The initial sequence number for the packet to be built. This is
28
+ * the initial number because the builder API will automatically
29
+ * split buffers that are larger than TRILOGY_MAX_PROTO_PACKET_LEN into
30
+ * multiple packets and increment the sequence number in each packet
31
+ * following the initial.
32
+ *
33
+ * Return values:
34
+ * TRILOGY_OK - The builder was successfully initialized.
35
+ * TRILOGY_SYSERR - A system error occurred, check errno.
36
+ */
37
+ int trilogy_builder_init(trilogy_builder_t *builder, trilogy_buffer_t *buffer, uint8_t seq);
38
+
39
+ /* trilogy_builder_finalize - Finalize internal buffer state, ensuring all of the
40
+ * packets inside are valid and ready for use over the wire.
41
+ *
42
+ * builder - A pre-initialized trilogy_builder_t pointer.
43
+ *
44
+ * Returns nothing.
45
+ */
46
+ void trilogy_builder_finalize(trilogy_builder_t *builder);
47
+
48
+ /* trilogy_builder_write_uint8 - Append an unsigned 8-bit integer to the packet
49
+ * buffer.
50
+ *
51
+ * builder - A pre-initialized trilogy_builder_t pointer.
52
+ * val - The value to append to the buffer.
53
+ *
54
+ * Return values:
55
+ * TRILOGY_OK - The value was appended to the packet buffer.
56
+ * TRILOGY_SYSERR - A system error occurred, check errno.
57
+ */
58
+ int trilogy_builder_write_uint8(trilogy_builder_t *builder, uint8_t val);
59
+
60
+ /* trilogy_builder_write_uint16 - Append an unsigned 16-bit integer to the packet
61
+ * buffer.
62
+ *
63
+ * builder - A pre-initialized trilogy_builder_t pointer.
64
+ * val - The value to append to the buffer.
65
+ *
66
+ * Return values:
67
+ * TRILOGY_OK - The value was appended to the packet buffer.
68
+ * TRILOGY_SYSERR - A system error occurred, check errno.
69
+ */
70
+ int trilogy_builder_write_uint16(trilogy_builder_t *builder, uint16_t val);
71
+
72
+ /* trilogy_builder_write_uint24 - Append an unsigned 24-bit integer to the packet
73
+ * buffer.
74
+ *
75
+ * builder - A pre-initialized trilogy_builder_t pointer.
76
+ * val - The value to append to the buffer.
77
+ *
78
+ * Return values:
79
+ * TRILOGY_OK - The value was appended to the packet buffer.
80
+ * TRILOGY_SYSERR - A system error occurred, check errno.
81
+ */
82
+ int trilogy_builder_write_uint24(trilogy_builder_t *builder, uint32_t val);
83
+
84
+ /* trilogy_builder_write_uint32 - Append an unsigned 32-bit integer to the packet
85
+ * buffer.
86
+ *
87
+ * builder - A pre-initialized trilogy_builder_t pointer
88
+ * val - The value to append to the buffer
89
+ *
90
+ * Return values:
91
+ * TRILOGY_OK - The value was appended to the packet buffer.
92
+ * TRILOGY_SYSERR - A system error occurred, check errno.
93
+ */
94
+ int trilogy_builder_write_uint32(trilogy_builder_t *builder, uint32_t val);
95
+
96
+ /* trilogy_builder_write_uint64 - Append an unsigned 64-bit integer to the packet
97
+ * buffer.
98
+ *
99
+ * builder - A pre-initialized trilogy_builder_t pointer
100
+ * val - The value to append to the buffer
101
+ *
102
+ * Return values:
103
+ * TRILOGY_OK - The value was appended to the packet buffer.
104
+ * TRILOGY_SYSERR - A system error occurred, check errno.
105
+ */
106
+ int trilogy_builder_write_uint64(trilogy_builder_t *builder, uint64_t val);
107
+
108
+ /* trilogy_builder_write_lenenc - Append a length-encoded integer to the packet
109
+ * buffer.
110
+ *
111
+ * The actual number of bytes appended to the buffer depends on the value passed
112
+ * in.
113
+ *
114
+ * builder - A pre-initialized trilogy_builder_t pointer
115
+ * val - The value to append to the buffer
116
+ *
117
+ * Return values:
118
+ * TRILOGY_OK - The value was appended to the packet buffer.
119
+ * TRILOGY_SYSERR - A system error occurred, check errno.
120
+ */
121
+ int trilogy_builder_write_lenenc(trilogy_builder_t *builder, uint64_t val);
122
+
123
+ /* trilogy_builder_write_buffer - Append opaque bytes to the packet buffer.
124
+ *
125
+ * builder - A pre-initialized trilogy_builder_t pointer
126
+ * data - A pointer to the opaque data to be appended
127
+ * len - The number of bytes to append from the location in memory the
128
+ * `data` parameter points to.
129
+ *
130
+ * Return values:
131
+ * TRILOGY_OK - The value was appended to the packet buffer.
132
+ * TRILOGY_SYSERR - A system error occurred, check errno.
133
+ */
134
+ int trilogy_builder_write_buffer(trilogy_builder_t *builder, const void *data, size_t len);
135
+
136
+ /* trilogy_builder_write_lenenc_buffer - Append opaque bytes to the packet buffer,
137
+ * prepended by its length as a length-encoded integer.
138
+ *
139
+ * builder - A pre-initialized trilogy_builder_t pointer
140
+ * data - A pointer to the opaque data to be appended
141
+ * len - The number of bytes to append from the location in memory the
142
+ * `data` parameter points to.
143
+ *
144
+ * Return values:
145
+ * TRILOGY_OK - The value was appended to the packet buffer.
146
+ * TRILOGY_SYSERR - A system error occurred, check errno.
147
+ */
148
+ int trilogy_builder_write_lenenc_buffer(trilogy_builder_t *builder, const void *data, size_t len);
149
+
150
+ /* trilogy_builder_write_string - Append a C-string to the packet buffer.
151
+ *
152
+ * builder - A pre-initialized trilogy_builder_t pointer
153
+ * data - A pointer to the C-string to append
154
+ *
155
+ * Return values:
156
+ * TRILOGY_OK - The value was appended to the packet buffer.
157
+ * TRILOGY_SYSERR - A system error occurred, check errno.
158
+ */
159
+ int trilogy_builder_write_string(trilogy_builder_t *builder, const char *data);
160
+
161
+ #endif