1 #ifndef TNT_NET_H_INCLUDED 2 #define TNT_NET_H_INCLUDED 3 4 /* 5 * Redistribution and use in source and binary forms, with or 6 * without modification, are permitted provided that the following 7 * conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above 10 * copyright notice, this list of conditions and the 11 * following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above 14 * copyright notice, this list of conditions and the following 15 * disclaimer in the documentation and/or other materials 16 * provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 26 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 29 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /** 34 * \file tnt_net.h 35 * \brief Basic tarantool client library header for network stream layer 36 */ 37 38 #ifdef __cplusplus 39 extern "C" { 40 #endif 41 42 #include <stdint.h> 43 #include <stdarg.h> 44 45 #include <sys/types.h> 46 #include <sys/time.h> 47 48 #include <tarantool/tnt_opt.h> 49 #include <tarantool/tnt_iob.h> 50 51 /** 52 * \brief Internal error codes 53 */ 54 enum tnt_error { 55 TNT_EOK, /*!< Everything is OK */ 56 TNT_EFAIL, /*!< Fail */ 57 TNT_EMEMORY, /*!< Memory allocation failed */ 58 TNT_ESYSTEM, /*!< System error */ 59 TNT_EBIG, /*!< Buffer is too big */ 60 TNT_ESIZE, /*!< Bad buffer size */ 61 TNT_ERESOLVE, /*!< gethostbyname(2) failed */ 62 TNT_ETMOUT, /*!< Operation timeout */ 63 TNT_EBADVAL, /*!< Bad argument (value) */ 64 TNT_ELOGIN, /*!< Failed to login */ 65 TNT_LAST /*!< Not an error */ 66 }; 67 68 /** 69 * \brief Network stream structure 70 */ 71 struct tnt_stream_net { 72 struct tnt_opt opt; /*!< Options for connection */ 73 int connected; /*!< Connection status. 1 - true, 0 - false */ 74 int fd; /*!< fd of connection */ 75 struct tnt_iob sbuf; /*!< Send buffer */ 76 struct tnt_iob rbuf; /*!< Recv buffer */ 77 enum tnt_error error; /*!< If retval == -1, then error is set. */ 78 int errno_; /*!< If TNT_ESYSTEM then errno_ is set */ 79 char *greeting; /*!< Pointer to greeting, if connected */ 80 struct tnt_schema *schema; /*!< Collation for space/index string<->number */ 81 int inited; /*!< 1 if iob/schema were allocated */ 82 }; 83 84 /*! 85 * \internal 86 * \brief Cast tnt_stream to tnt_net 87 */ 88 #define TNT_SNET_CAST(S) ((struct tnt_stream_net*)(S)->data) 89 90 /** 91 * \brief Create tnt_net stream instance 92 * 93 * \param s stream pointer, maybe NULL 94 * 95 * If stream pointer is NULL, then new stream will be created. 96 * 97 * \returns stream pointer 98 * \retval NULL oom 99 * 100 * \code{.c} 101 * struct tnt_stream *tnt = tnt_net(NULL); 102 * assert(tnt); 103 * assert(tnt_set(s, TNT_OPT_URI, "login:passw@localhost:3302") != -1); 104 * assert(tnt_connect(s) != -1); 105 * ... 106 * tnt_close(s); 107 * \endcode 108 */ 109 struct tnt_stream * 110 tnt_net(struct tnt_stream *s); 111 112 /** 113 * \brief Set options for connection 114 * 115 * \param s stream pointer 116 * \param opt option to set 117 * \param ... option value 118 * 119 * \returns status 120 * \retval -1 error 121 * \retval 0 ok 122 * \sa enum tnt_opt_type 123 * 124 * \code{.c} 125 * assert(tnt_set(s, TNT_OPT_SEND_BUF, 16*1024) != -1); 126 * assert(tnt_set(s, TNT_OPT_RECV_BUF, 16*1024) != -1); 127 * assert(tnt_set(s, TNT_OPT_URI, "login:passw@localhost:3302") != -1); 128 * \endcode 129 * 130 * \note 131 * URI format: 132 * * "[login:password@]host:port" for tcp sockets 133 * * "[login:password@]/tmp/socket_path.sock" for unix sockets 134 * \sa enum tnt_opt_type 135 */ 136 int 137 tnt_set(struct tnt_stream *s, int opt, ...); 138 139 /*! 140 * \internal 141 * \brief Initialize network stream 142 * 143 * It must happened before connection, but after options are set. 144 * 1) creation of tnt_iob's (sbuf,rbuf) 145 * 2) schema creation 146 * 147 * \param s stream for initialization 148 * 149 * \returns status 150 * \retval 0 ok 151 * \retval -1 error (oom/einval) 152 */ 153 int 154 tnt_init(struct tnt_stream *s); 155 156 /** 157 * \brief Connect to tarantool with preconfigured and allocated settings 158 * 159 * \param s stream pointer 160 * 161 * \retval 0 ok 162 * \retval -1 error (network/oom) 163 */ 164 int 165 tnt_connect(struct tnt_stream *s); 166 167 /** 168 * \brief Close connection 169 * \param s stream pointer 170 */ 171 void 172 tnt_close(struct tnt_stream *s); 173 174 /** 175 * \brief Send written to buffer queries 176 * 177 * \param s tnt_stream 178 * 179 * \returns number of bytes written to socket 180 * \retval -1 on network error 181 */ 182 ssize_t 183 tnt_flush(struct tnt_stream *s); 184 185 /** 186 * \brief Get tnt_net stream fd 187 */ 188 int 189 tnt_fd(struct tnt_stream *s); 190 191 /** 192 * \brief Error accessor for tnt_net stream 193 */ 194 enum tnt_error 195 tnt_error(struct tnt_stream *s); 196 197 /** 198 * \brief Format error as string 199 */ 200 char * 201 tnt_strerror(struct tnt_stream *s); 202 203 /** 204 * \brief Get last errno on socket 205 */ 206 int 207 tnt_errno(struct tnt_stream *s); 208 209 /** 210 * \brief Flush space/index schema and get it from server 211 * 212 * \param s stream pointer 213 * 214 * \returns result 215 * \retval -1 error 216 * \retval 0 ok 217 */ 218 int 219 tnt_reload_schema(struct tnt_stream *s); 220 221 /** 222 * \brief Get space number from space name 223 * 224 * \returns space number 225 * \retval -1 error 226 */ 227 int tnt_get_spaceno(struct tnt_stream *s, const char *space, size_t space_len); 228 229 /** 230 * \brief Get index number from index name and spaceid 231 * 232 * \returns index number 233 * \retval -1 error 234 */ 235 int tnt_get_indexno(struct tnt_stream *s, int spaceno, const char *index, 236 size_t index_len); 237 238 #ifdef __cplusplus 239 } 240 #endif 241 242 #endif /* TNT_NET_H_INCLUDED */ 243