CUBRID Engine  latest
cas_network.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 
20 /*
21  * cas_network.h -
22  */
23 
24 #ifndef _CAS_NETWORK_H_
25 #define _CAS_NETWORK_H_
26 
27 #ident "$Id$"
28 
29 #include <string.h>
30 
31 #include "cas_net_buf.h"
32 
33 #define NET_MIN_TIMEOUT 2
34 #define NET_DEFAULT_TIMEOUT 60
35 #define MYSQL_CONNECT_TIMEOUT (5*60*60) /* 5 hour. MySQL timeout = 8 hour */
36 
37 #ifndef MIN
38 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
39 #endif
40 
41 #define NET_SIZE_BYTE ((int) sizeof(char))
42 #define NET_SIZE_SHORT ((int) sizeof(short))
43 #define NET_SIZE_INT ((int) sizeof(int))
44 #define NET_SIZE_FLOAT ((int) sizeof(float))
45 #define NET_SIZE_DOUBLE ((int) sizeof(double))
46 #define NET_SIZE_INT64 ((int) sizeof(INT64))
47 #define NET_SIZE_BIGINT NET_SIZE_INT64
48 #define NET_SIZE_DATE (NET_SIZE_SHORT + NET_SIZE_SHORT + NET_SIZE_SHORT)
49 #define NET_SIZE_TIME (NET_SIZE_SHORT + NET_SIZE_SHORT + NET_SIZE_SHORT)
50 #define NET_SIZE_OBJECT (NET_SIZE_INT + NET_SIZE_SHORT + NET_SIZE_SHORT)
51 #define NET_SIZE_TIMESTAMP (NET_SIZE_SHORT * 6)
52 #define NET_SIZE_DATETIME (NET_SIZE_SHORT * 7)
53 
54 #define NET_ARG_GET_SIZE(SIZE, ARG) \
55  do { \
56  int tmp_i; \
57  memcpy(&tmp_i, (char*) (ARG), NET_SIZE_INT); \
58  SIZE = ntohl(tmp_i); \
59  } while (0)
60 
61 #if defined(CAS_FOR_ORACLE) || defined(CAS_FOR_MYSQL)
62 #define NET_ARG_GET_BIGINT(VALUE, ARG) \
63  do { \
64  int64_t tmp_i; \
65  memcpy(&tmp_i, (char*) (ARG) + NET_SIZE_INT, NET_SIZE_BIGINT); \
66  VALUE = ntohi64(tmp_i); \
67  } while (0)
68 
69 #else
70 #define NET_ARG_GET_BIGINT(VALUE, ARG) \
71  do { \
72  DB_BIGINT tmp_i; \
73  memcpy(&tmp_i, (char*) (ARG) + NET_SIZE_INT, NET_SIZE_BIGINT); \
74  VALUE = ntohi64(tmp_i); \
75  } while (0)
76 #endif
77 
78 #define NET_ARG_GET_INT(VALUE, ARG) \
79  do { \
80  int tmp_i; \
81  memcpy(&tmp_i, (char*) (ARG) + NET_SIZE_INT, NET_SIZE_INT); \
82  VALUE = ntohl(tmp_i); \
83  } while (0)
84 
85 #define NET_ARG_GET_SHORT(VALUE, ARG) \
86  do { \
87  short tmp_s; \
88  memcpy(&tmp_s, (char*) (ARG) + NET_SIZE_INT, NET_SIZE_SHORT); \
89  VALUE = ntohs(tmp_s); \
90  } while (0)
91 
92 #define NET_ARG_GET_FLOAT(VALUE, ARG) \
93  do { \
94  float tmp_f; \
95  memcpy(&tmp_f, (char*) (ARG) + NET_SIZE_INT, NET_SIZE_FLOAT); \
96  VALUE = net_ntohf(tmp_f); \
97  } while (0)
98 
99 #define NET_ARG_GET_DOUBLE(VALUE, ARG) \
100  do { \
101  double tmp_d; \
102  memcpy(&tmp_d, (char*) (ARG) + NET_SIZE_INT, NET_SIZE_DOUBLE); \
103  VALUE = net_ntohd(tmp_d); \
104  } while (0)
105 
106 #define NET_ARG_GET_CHAR(VALUE, ARG) \
107  do { \
108  VALUE = (*((char*) (ARG) + NET_SIZE_INT)); \
109  } while (0)
110 
111 #define NET_ARG_GET_STR(VALUE, SIZE, ARG) \
112  do { \
113  int _size; \
114  char *cur_p = (char*) ARG; \
115  memcpy(&_size, cur_p, NET_SIZE_INT); \
116  _size = ntohl(_size); \
117  cur_p += NET_SIZE_INT; \
118  if (_size <= 0) { \
119  VALUE = NULL; \
120  SIZE = 0; \
121  } else { \
122  VALUE = ((char*) cur_p); \
123  SIZE = _size; \
124  } \
125  } while (0)
126 
127 #define NET_ARG_GET_DATE(YEAR, MON, DAY, ARG) \
128  do { \
129  char *cur_p = (char*) ARG; \
130  short tmp_s; \
131  int pos = NET_SIZE_INT; \
132  memcpy(&tmp_s, cur_p + pos, NET_SIZE_SHORT); \
133  YEAR = ntohs(tmp_s); \
134  pos += NET_SIZE_SHORT; \
135  memcpy(&tmp_s, cur_p + pos, NET_SIZE_SHORT); \
136  MON = ntohs(tmp_s); \
137  pos += NET_SIZE_SHORT; \
138  memcpy(&tmp_s, cur_p + pos, NET_SIZE_SHORT); \
139  DAY = ntohs(tmp_s); \
140  } while (0)
141 
142 #define NET_ARG_GET_TIME(HH, MM, SS, ARG) \
143  do { \
144  char *cur_p = (char*) ARG; \
145  short tmp_s; \
146  int pos = NET_SIZE_INT + NET_SIZE_SHORT * 3; \
147  memcpy(&tmp_s, cur_p + pos, NET_SIZE_SHORT); \
148  HH = ntohs(tmp_s); \
149  pos += NET_SIZE_SHORT; \
150  memcpy(&tmp_s, cur_p + pos, NET_SIZE_SHORT); \
151  MM = ntohs(tmp_s); \
152  pos += NET_SIZE_SHORT; \
153  memcpy(&tmp_s, cur_p + pos, NET_SIZE_SHORT); \
154  SS = ntohs(tmp_s); \
155  } while (0)
156 
157 #define NET_ARG_GET_TIMESTAMP(YR, MON, DAY, HH, MM, SS, ARG) \
158  do { \
159  char *cur_p = (char*) ARG; \
160  short tmp_s; \
161  int pos = NET_SIZE_INT; \
162  memcpy(&tmp_s, cur_p + pos, NET_SIZE_SHORT); \
163  YR = ntohs(tmp_s); \
164  pos += NET_SIZE_SHORT; \
165  memcpy(&tmp_s, cur_p + pos, NET_SIZE_SHORT); \
166  MON = ntohs(tmp_s); \
167  pos += NET_SIZE_SHORT; \
168  memcpy(&tmp_s, cur_p + pos, NET_SIZE_SHORT); \
169  DAY = ntohs(tmp_s); \
170  pos += NET_SIZE_SHORT; \
171  memcpy(&tmp_s, cur_p + pos, NET_SIZE_SHORT); \
172  HH = ntohs(tmp_s); \
173  pos += NET_SIZE_SHORT; \
174  memcpy(&tmp_s, cur_p + pos, NET_SIZE_SHORT); \
175  MM = ntohs(tmp_s); \
176  pos += NET_SIZE_SHORT; \
177  memcpy(&tmp_s, cur_p + pos, NET_SIZE_SHORT); \
178  SS = ntohs(tmp_s); \
179  } while (0)
180 
181 #define NET_ARG_GET_DATETIME(YR, MON, DAY, HH, MM, SS, MS, ARG) \
182  do { \
183  char *cur_p = (char*) ARG; \
184  short tmp_s; \
185  int pos = NET_SIZE_INT; \
186  memcpy(&tmp_s, cur_p + pos, NET_SIZE_SHORT); \
187  YR = ntohs(tmp_s); \
188  pos += NET_SIZE_SHORT; \
189  memcpy(&tmp_s, cur_p + pos, NET_SIZE_SHORT); \
190  MON = ntohs(tmp_s); \
191  pos += NET_SIZE_SHORT; \
192  memcpy(&tmp_s, cur_p + pos, NET_SIZE_SHORT); \
193  DAY = ntohs(tmp_s); \
194  pos += NET_SIZE_SHORT; \
195  memcpy(&tmp_s, cur_p + pos, NET_SIZE_SHORT); \
196  HH = ntohs(tmp_s); \
197  pos += NET_SIZE_SHORT; \
198  memcpy(&tmp_s, cur_p + pos, NET_SIZE_SHORT); \
199  MM = ntohs(tmp_s); \
200  pos += NET_SIZE_SHORT; \
201  memcpy(&tmp_s, cur_p + pos, NET_SIZE_SHORT); \
202  SS = ntohs(tmp_s); \
203  pos += NET_SIZE_SHORT; \
204  memcpy(&tmp_s, cur_p + pos, NET_SIZE_SHORT); \
205  MS = ntohs(tmp_s); \
206  } while (0)
207 
208 #define NET_ARG_GET_OBJECT(VALUE, ARG) \
209  do { \
210  char *cur_p = (char*) ARG; \
211  int pageid; \
212  short slotid, volid; \
213  DB_IDENTIFIER tmp_oid; \
214  DB_OBJECT *tmp_obj; \
215  int pos = NET_SIZE_INT; \
216  memcpy(&pageid, cur_p + pos, NET_SIZE_INT);\
217  pageid = ntohl(pageid); \
218  pos += NET_SIZE_INT; \
219  memcpy(&slotid, cur_p + pos, NET_SIZE_SHORT);\
220  slotid = ntohs(slotid); \
221  pos += NET_SIZE_SHORT; \
222  memcpy(&volid, cur_p + pos, NET_SIZE_SHORT);\
223  volid = ntohs(volid); \
224  pos += NET_SIZE_SHORT; \
225  tmp_oid.pageid = pageid; \
226  tmp_oid.slotid = slotid; \
227  tmp_oid.volid = volid; \
228  tmp_obj = db_object(&tmp_oid); \
229  VALUE = tmp_obj; \
230  } while (0)
231 
232 #define NET_ARG_GET_CCI_OBJECT(PAGEID, SLOTID, VOLID, ARG) \
233  do { \
234  char *_macro_tmp_p = (char*) ARG; \
235  int _tmp_pageid; \
236  short _tmp_slotid, _tmp_volid; \
237  int pos = NET_SIZE_INT; \
238  memcpy(&_tmp_pageid, _macro_tmp_p + pos, NET_SIZE_INT); \
239  PAGEID = ntohl(_tmp_pageid); \
240  pos += NET_SIZE_INT; \
241  memcpy(&_tmp_slotid, _macro_tmp_p + pos, NET_SIZE_SHORT); \
242  SLOTID = ntohs(_tmp_slotid); \
243  pos += NET_SIZE_SHORT; \
244  memcpy(&_tmp_volid, _macro_tmp_p + pos, NET_SIZE_SHORT); \
245  VOLID = ntohs(_tmp_volid); \
246  } while (0)
247 
248 #define NET_ARG_GET_CACHE_TIME(CT, ARG) \
249  do { \
250  char *cur_p = (char*) ARG; \
251  int sec, usec; \
252  int pos = NET_SIZE_INT; \
253  memcpy(&sec, cur_p + pos, NET_SIZE_INT); \
254  pos += NET_SIZE_INT; \
255  memcpy(&usec, cur_p + pos, NET_SIZE_INT); \
256  (CT)->sec = ntohl(sec); \
257  (CT)->usec = ntohl(usec); \
258  } while (0)
259 
260 
261 extern SOCKET
262 #if defined(WINDOWS)
263  net_init_env (int *new_port);
264 #else
265  net_init_env (char *port_name);
266 #endif
267 
268 typedef struct
269 {
271  char *info_ptr;
272  char buf[MSG_HEADER_SIZE];
273 } MSG_HEADER;
274 
275 #if defined(WINDOWS)
276 extern SOCKET net_connect_proxy (int proxy_id);
277 #else /* WINDOWS */
278 extern SOCKET net_connect_proxy (void);
279 #endif /* !WINDOWS */
281 
282 extern int net_read_stream (SOCKET sock_fd, char *buf, int size);
283 extern int net_write_stream (SOCKET sock_fd, const char *buf, int size);
284 extern int net_write_int (SOCKET sock_fd, int value);
285 extern int net_read_int (SOCKET sock_fd, int *value);
286 extern int net_decode_str (char *msg, int msg_size, char *func_code, void ***ret_argv);
287 
288 extern int net_read_to_file (SOCKET sock_fd, int file_size, char *filename);
289 extern int net_write_from_file (SOCKET sock_fd, int file_size, char *filename);
290 
291 extern void net_timeout_set (int timeout_sec);
292 extern void init_msg_header (MSG_HEADER * header);
293 extern int net_read_header (SOCKET sock_fd, MSG_HEADER * header);
294 extern int net_write_header (SOCKET sock_fd, MSG_HEADER * header);
295 extern bool is_net_timed_out (void);
296 
297 extern void net_write_error (SOCKET sock, int version, char *driver_info, char *cas_info, int cas_info_size,
298  int indicator, int code, char *msg);
299 #endif /* _CAS_NETWORK_H_ */
#define MSG_HEADER_SIZE
Definition: cas_protocol.h:114
static SOCKET srv_sock_fd
Definition: cas.c:335
SOCKET net_init_env(char *port_name)
Definition: cas_network.c:90
int SOCKET
Definition: porting.h:482
bool is_net_timed_out(void)
Definition: cas_network.c:699
int net_read_int(SOCKET sock_fd, int *value)
Definition: cas_network.c:383
int cas_info_size
Definition: cas.c:183
int proxy_id
Definition: shard_proxy.c:45
int * msg_body_size_ptr
Definition: cas_network.h:270
SOCKET net_connect_client(SOCKET srv_sock_fd)
Definition: cas_network.c:260
SOCKET net_connect_proxy(void)
Definition: cas_network.c:172
int net_write_int(SOCKET sock_fd, int value)
Definition: cas_network.c:375
void init_msg_header(MSG_HEADER *header)
Definition: cas_network.c:358
int net_write_header(SOCKET sock_fd, MSG_HEADER *header)
Definition: cas_network.c:347
int net_read_stream(SOCKET sock_fd, char *buf, int size)
Definition: cas_network.c:305
int net_read_to_file(SOCKET sock_fd, int file_size, char *filename)
Definition: cas_network.c:446
void net_write_error(SOCKET sock, int version, char *driver_info, char *cas_info, int cas_info_size, int indicator, int code, char *msg)
Definition: cas_network.c:717
int net_write_stream(SOCKET sock_fd, const char *buf, int size)
Definition: cas_network.c:283
static SOCKET sock_fd
Definition: broker.c:300
int net_read_header(SOCKET sock_fd, MSG_HEADER *header)
Definition: cas_network.c:329
void net_timeout_set(int timeout_sec)
Definition: cas_network.c:519
int net_write_from_file(SOCKET sock_fd, int file_size, char *filename)
Definition: cas_network.c:482
int net_decode_str(char *msg, int msg_size, char *func_code, void ***ret_argv)
Definition: cas_network.c:393
char * info_ptr
Definition: cas_network.h:271