You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1141 lines
31 KiB

26 years ago
26 years ago
26 years ago
25 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
25 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
24 years ago
26 years ago
26 years ago
24 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
23 years ago
23 years ago
26 years ago
23 years ago
23 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
23 years ago
23 years ago
26 years ago
26 years ago
26 years ago
25 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
23 years ago
26 years ago
23 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
  1. /* Copyright (C) 2000 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. /*
  13. This file is the net layer API for the MySQL client/server protocol,
  14. which is a tightly coupled, proprietary protocol owned by MySQL AB.
  15. Any re-implementations of this protocol must also be under GPL
  16. unless one has got an license from MySQL AB stating otherwise.
  17. */
  18. /*
  19. Write and read of logical packets to/from socket
  20. Writes are cached into net_buffer_length big packets.
  21. Read packets are reallocated dynamicly when reading big packets.
  22. Each logical packet has the following pre-info:
  23. 3 byte length & 1 byte package-number.
  24. This file needs to be written in C as it's used by the libmysql client as a
  25. C file.
  26. */
  27. /*
  28. HFTODO this must be hidden if we don't want client capabilities in
  29. embedded library
  30. */
  31. #include <my_global.h>
  32. #include <mysql.h>
  33. #include <mysql_embed.h>
  34. #include <mysql_com.h>
  35. #include <mysqld_error.h>
  36. #include <my_sys.h>
  37. #include <m_string.h>
  38. #include <my_net.h>
  39. #include <violite.h>
  40. #include <signal.h>
  41. #include <errno.h>
  42. #ifdef __NETWARE__
  43. #include <sys/select.h>
  44. #endif
  45. #ifdef EMBEDDED_LIBRARY
  46. #undef MYSQL_SERVER
  47. #undef MYSQL_CLIENT
  48. #define MYSQL_CLIENT
  49. #endif /*EMBEDDED_LIBRARY */
  50. /*
  51. The following handles the differences when this is linked between the
  52. client and the server.
  53. This gives an error if a too big packet is found
  54. The server can change this with the -O switch, but because the client
  55. can't normally do this the client should have a bigger max_allowed_packet.
  56. */
  57. #if defined(__WIN__) || !defined(MYSQL_SERVER)
  58. /* The following is because alarms doesn't work on windows. */
  59. #define NO_ALARM
  60. #endif
  61. #ifndef NO_ALARM
  62. #include "my_pthread.h"
  63. void sql_print_error(const char *format,...);
  64. #else
  65. #define DONT_USE_THR_ALARM
  66. #endif /* NO_ALARM */
  67. #include "thr_alarm.h"
  68. #ifdef MYSQL_SERVER
  69. /*
  70. The following variables/functions should really not be declared
  71. extern, but as it's hard to include mysql_priv.h here, we have to
  72. live with this for a while.
  73. */
  74. extern uint test_flags;
  75. extern ulong bytes_sent, bytes_received, net_big_packet_count;
  76. extern pthread_mutex_t LOCK_bytes_sent , LOCK_bytes_received;
  77. #ifndef MYSQL_INSTANCE_MANAGER
  78. #ifdef HAVE_QUERY_CACHE
  79. #define USE_QUERY_CACHE
  80. extern void query_cache_init_query(NET *net);
  81. extern void query_cache_insert(NET *net, const char *packet, ulong length);
  82. #endif // HAVE_QUERY_CACHE
  83. #define update_statistics(A) A
  84. #endif /* MYSQL_INSTANCE_MANGER */
  85. #endif /* defined(MYSQL_SERVER) && !defined(MYSQL_INSTANCE_MANAGER) */
  86. #if !defined(MYSQL_SERVER) || defined(MYSQL_INSTANCE_MANAGER)
  87. #define update_statistics(A)
  88. #define thd_increment_bytes_sent(N)
  89. #endif
  90. #define TEST_BLOCKING 8
  91. #define MAX_PACKET_LENGTH (256L*256L*256L-1)
  92. static my_bool net_write_buff(NET *net,const char *packet,ulong len);
  93. /* Init with packet info */
  94. my_bool my_net_init(NET *net, Vio* vio)
  95. {
  96. DBUG_ENTER("my_net_init");
  97. my_net_local_init(net); /* Set some limits */
  98. if (!(net->buff=(uchar*) my_malloc((uint32) net->max_packet+
  99. NET_HEADER_SIZE + COMP_HEADER_SIZE,
  100. MYF(MY_WME))))
  101. DBUG_RETURN(1);
  102. net->buff_end=net->buff+net->max_packet;
  103. net->vio = vio;
  104. net->no_send_ok= net->no_send_eof= net->no_send_error= 0;
  105. net->error=0; net->return_errno=0; net->return_status=0;
  106. net->pkt_nr=net->compress_pkt_nr=0;
  107. net->write_pos=net->read_pos = net->buff;
  108. net->last_error[0]=0;
  109. net->compress=0; net->reading_or_writing=0;
  110. net->where_b = net->remain_in_buf=0;
  111. net->last_errno=0;
  112. #ifdef USE_QUERY_CACHE
  113. query_cache_init_query(net);
  114. #else
  115. net->query_cache_query= 0;
  116. #endif
  117. net->report_error= 0;
  118. if (vio != 0) /* If real connection */
  119. {
  120. net->fd = vio_fd(vio); /* For perl DBI/DBD */
  121. #if defined(MYSQL_SERVER) && !defined(__WIN__)
  122. if (!(test_flags & TEST_BLOCKING))
  123. {
  124. my_bool old_mode;
  125. vio_blocking(vio, FALSE, &old_mode);
  126. }
  127. #endif
  128. vio_fastsend(vio);
  129. }
  130. DBUG_RETURN(0);
  131. }
  132. void net_end(NET *net)
  133. {
  134. DBUG_ENTER("net_end");
  135. my_free((gptr) net->buff,MYF(MY_ALLOW_ZERO_PTR));
  136. net->buff=0;
  137. DBUG_VOID_RETURN;
  138. }
  139. /* Realloc the packet buffer */
  140. my_bool net_realloc(NET *net, ulong length)
  141. {
  142. uchar *buff;
  143. ulong pkt_length;
  144. DBUG_ENTER("net_realloc");
  145. DBUG_PRINT("enter",("length: %lu", length));
  146. if (length >= net->max_packet_size)
  147. {
  148. DBUG_PRINT("error", ("Packet too large. Max size: %lu",
  149. net->max_packet_size));
  150. net->error= 1;
  151. net->report_error= 1;
  152. net->last_errno= ER_NET_PACKET_TOO_LARGE;
  153. DBUG_RETURN(1);
  154. }
  155. pkt_length = (length+IO_SIZE-1) & ~(IO_SIZE-1);
  156. /*
  157. We must allocate some extra bytes for the end 0 and to be able to
  158. read big compressed blocks
  159. */
  160. if (!(buff=(uchar*) my_realloc((char*) net->buff, (uint32) pkt_length +
  161. NET_HEADER_SIZE + COMP_HEADER_SIZE,
  162. MYF(MY_WME))))
  163. {
  164. net->error= 1;
  165. net->report_error= 1;
  166. net->last_errno= ER_OUT_OF_RESOURCES;
  167. DBUG_RETURN(1);
  168. }
  169. net->buff=net->write_pos=buff;
  170. net->buff_end=buff+(net->max_packet=pkt_length);
  171. DBUG_RETURN(0);
  172. }
  173. /*
  174. Check if there is any data to be read from the socket
  175. SYNOPSIS
  176. net_data_is_ready()
  177. sd socket descriptor
  178. DESCRIPTION
  179. Check if there is any data to be read from the socket.
  180. RETURN VALUES
  181. 0 No data to read
  182. 1 Data or EOF to read
  183. -1 Don't know if data is ready or not
  184. */
  185. #if !defined(EMBEDDED_LIBRARY)
  186. static int net_data_is_ready(my_socket sd)
  187. {
  188. #ifdef HAVE_POLL
  189. struct pollfd ufds;
  190. int res;
  191. ufds.fd= sd;
  192. ufds.events= POLLIN | POLLPRI;
  193. if (!(res= poll(&ufds, 1, 0)))
  194. return 0;
  195. if (res < 0 || !(ufds.revents & (POLLIN | POLLPRI)))
  196. return 0;
  197. return 1;
  198. #else
  199. fd_set sfds;
  200. struct timeval tv;
  201. int res;
  202. #ifndef __WIN__
  203. /* Windows uses an _array_ of 64 fd's as default, so it's safe */
  204. if (sd >= FD_SETSIZE)
  205. return -1;
  206. #define NET_DATA_IS_READY_CAN_RETURN_MINUS_ONE
  207. #endif
  208. FD_ZERO(&sfds);
  209. FD_SET(sd, &sfds);
  210. tv.tv_sec= tv.tv_usec= 0;
  211. if ((res= select(sd+1, &sfds, NULL, NULL, &tv)) < 0)
  212. return 0;
  213. else
  214. return test(res ? FD_ISSET(sd, &sfds) : 0);
  215. #endif /* HAVE_POLL */
  216. }
  217. #endif /* EMBEDDED_LIBRARY */
  218. /*
  219. Remove unwanted characters from connection
  220. and check if disconnected
  221. SYNOPSIS
  222. net_clear()
  223. net NET handler
  224. clear_buffer If <> 0, then clear all data from communication buffer
  225. DESCRIPTION
  226. Read from socket until there is nothing more to read. Discard
  227. what is read.
  228. If there is anything when to read 'net_clear' is called this
  229. normally indicates an error in the protocol.
  230. When connection is properly closed (for TCP it means with
  231. a FIN packet), then select() considers a socket "ready to read",
  232. in the sense that there's EOF to read, but read() returns 0.
  233. */
  234. void net_clear(NET *net, my_bool clear_buffer)
  235. {
  236. #if !defined(EMBEDDED_LIBRARY)
  237. int count, ready;
  238. #endif
  239. DBUG_ENTER("net_clear");
  240. #if !defined(EMBEDDED_LIBRARY)
  241. if (clear_buffer)
  242. {
  243. while ((ready= net_data_is_ready(net->vio->sd)) > 0)
  244. {
  245. /* The socket is ready */
  246. if ((count= vio_read(net->vio, (char*) (net->buff),
  247. (uint32) net->max_packet)) > 0)
  248. {
  249. DBUG_PRINT("info",("skipped %d bytes from file: %s",
  250. count, vio_description(net->vio)));
  251. #if defined(EXTRA_DEBUG) && (MYSQL_VERSION_ID < 51000)
  252. fprintf(stderr,"Error: net_clear() skipped %d bytes from file: %s\n",
  253. count, vio_description(net->vio));
  254. #endif
  255. }
  256. else
  257. {
  258. DBUG_PRINT("info",("socket ready but only EOF to read - disconnected"));
  259. net->error= 2;
  260. break;
  261. }
  262. }
  263. #ifdef NET_DATA_IS_READY_CAN_RETURN_MINUS_ONE
  264. /* 'net_data_is_ready' returned "don't know" */
  265. if (ready == -1)
  266. {
  267. /* Read unblocking to clear net */
  268. my_bool old_mode;
  269. if (!vio_blocking(net->vio, FALSE, &old_mode))
  270. {
  271. while ((count= vio_read(net->vio, (char*) (net->buff),
  272. (uint32) net->max_packet)) > 0)
  273. DBUG_PRINT("info",("skipped %d bytes from file: %s",
  274. count, vio_description(net->vio)));
  275. vio_blocking(net->vio, TRUE, &old_mode);
  276. }
  277. }
  278. #endif /* NET_DATA_IS_READY_CAN_RETURN_MINUS_ONE */
  279. }
  280. #endif /* EMBEDDED_LIBRARY */
  281. net->pkt_nr=net->compress_pkt_nr=0; /* Ready for new command */
  282. net->write_pos=net->buff;
  283. DBUG_VOID_RETURN;
  284. }
  285. /* Flush write_buffer if not empty. */
  286. my_bool net_flush(NET *net)
  287. {
  288. my_bool error= 0;
  289. DBUG_ENTER("net_flush");
  290. if (net->buff != net->write_pos)
  291. {
  292. error=test(net_real_write(net,(char*) net->buff,
  293. (ulong) (net->write_pos - net->buff)));
  294. net->write_pos=net->buff;
  295. }
  296. /* Sync packet number if using compression */
  297. if (net->compress)
  298. net->pkt_nr=net->compress_pkt_nr;
  299. DBUG_RETURN(error);
  300. }
  301. /*****************************************************************************
  302. ** Write something to server/client buffer
  303. *****************************************************************************/
  304. /*
  305. Write a logical packet with packet header
  306. Format: Packet length (3 bytes), packet number(1 byte)
  307. When compression is used a 3 byte compression length is added
  308. NOTE
  309. If compression is used the original package is modified!
  310. */
  311. my_bool
  312. my_net_write(NET *net,const char *packet,ulong len)
  313. {
  314. uchar buff[NET_HEADER_SIZE];
  315. if (unlikely(!net->vio)) /* nowhere to write */
  316. return 0;
  317. /*
  318. Big packets are handled by splitting them in packets of MAX_PACKET_LENGTH
  319. length. The last packet is always a packet that is < MAX_PACKET_LENGTH.
  320. (The last packet may even have a length of 0)
  321. */
  322. while (len >= MAX_PACKET_LENGTH)
  323. {
  324. const ulong z_size = MAX_PACKET_LENGTH;
  325. int3store(buff, z_size);
  326. buff[3]= (uchar) net->pkt_nr++;
  327. if (net_write_buff(net, (char*) buff, NET_HEADER_SIZE) ||
  328. net_write_buff(net, packet, z_size))
  329. return 1;
  330. packet += z_size;
  331. len-= z_size;
  332. }
  333. /* Write last packet */
  334. int3store(buff,len);
  335. buff[3]= (uchar) net->pkt_nr++;
  336. if (net_write_buff(net,(char*) buff,NET_HEADER_SIZE))
  337. return 1;
  338. #ifndef DEBUG_DATA_PACKETS
  339. DBUG_DUMP("packet_header",(char*) buff,NET_HEADER_SIZE);
  340. #endif
  341. return test(net_write_buff(net,packet,len));
  342. }
  343. /*
  344. Send a command to the server.
  345. SYNOPSIS
  346. net_write_command()
  347. net NET handler
  348. command Command in MySQL server (enum enum_server_command)
  349. header Header to write after command
  350. head_len Length of header
  351. packet Query or parameter to query
  352. len Length of packet
  353. DESCRIPTION
  354. The reason for having both header and packet is so that libmysql
  355. can easy add a header to a special command (like prepared statements)
  356. without having to re-alloc the string.
  357. As the command is part of the first data packet, we have to do some data
  358. juggling to put the command in there, without having to create a new
  359. packet.
  360. This function will split big packets into sub-packets if needed.
  361. (Each sub packet can only be 2^24 bytes)
  362. RETURN VALUES
  363. 0 ok
  364. 1 error
  365. */
  366. my_bool
  367. net_write_command(NET *net,uchar command,
  368. const char *header, ulong head_len,
  369. const char *packet, ulong len)
  370. {
  371. ulong length=len+1+head_len; /* 1 extra byte for command */
  372. uchar buff[NET_HEADER_SIZE+1];
  373. uint header_size=NET_HEADER_SIZE+1;
  374. DBUG_ENTER("net_write_command");
  375. DBUG_PRINT("enter",("length: %lu", len));
  376. buff[4]=command; /* For first packet */
  377. if (length >= MAX_PACKET_LENGTH)
  378. {
  379. /* Take into account that we have the command in the first header */
  380. len= MAX_PACKET_LENGTH - 1 - head_len;
  381. do
  382. {
  383. int3store(buff, MAX_PACKET_LENGTH);
  384. buff[3]= (uchar) net->pkt_nr++;
  385. if (net_write_buff(net,(char*) buff, header_size) ||
  386. net_write_buff(net, header, head_len) ||
  387. net_write_buff(net, packet, len))
  388. DBUG_RETURN(1);
  389. packet+= len;
  390. length-= MAX_PACKET_LENGTH;
  391. len= MAX_PACKET_LENGTH;
  392. head_len= 0;
  393. header_size= NET_HEADER_SIZE;
  394. } while (length >= MAX_PACKET_LENGTH);
  395. len=length; /* Data left to be written */
  396. }
  397. int3store(buff,length);
  398. buff[3]= (uchar) net->pkt_nr++;
  399. DBUG_RETURN(test(net_write_buff(net, (char*) buff, header_size) ||
  400. (head_len && net_write_buff(net, (char*) header, head_len)) ||
  401. net_write_buff(net, packet, len) || net_flush(net)));
  402. }
  403. /*
  404. Caching the data in a local buffer before sending it.
  405. SYNOPSIS
  406. net_write_buff()
  407. net Network handler
  408. packet Packet to send
  409. len Length of packet
  410. DESCRIPTION
  411. Fill up net->buffer and send it to the client when full.
  412. If the rest of the to-be-sent-packet is bigger than buffer,
  413. send it in one big block (to avoid copying to internal buffer).
  414. If not, copy the rest of the data to the buffer and return without
  415. sending data.
  416. NOTES
  417. The cached buffer can be sent as it is with 'net_flush()'.
  418. In this code we have to be careful to not send a packet longer than
  419. MAX_PACKET_LENGTH to net_real_write() if we are using the compressed
  420. protocol as we store the length of the compressed packet in 3 bytes.
  421. RETURN
  422. 0 ok
  423. 1
  424. */
  425. static my_bool
  426. net_write_buff(NET *net,const char *packet,ulong len)
  427. {
  428. ulong left_length;
  429. if (net->compress && net->max_packet > MAX_PACKET_LENGTH)
  430. left_length= MAX_PACKET_LENGTH - (net->write_pos - net->buff);
  431. else
  432. left_length= (ulong) (net->buff_end - net->write_pos);
  433. #ifdef DEBUG_DATA_PACKETS
  434. DBUG_DUMP("data", packet, len);
  435. #endif
  436. if (len > left_length)
  437. {
  438. if (net->write_pos != net->buff)
  439. {
  440. /* Fill up already used packet and write it */
  441. memcpy((char*) net->write_pos,packet,left_length);
  442. if (net_real_write(net,(char*) net->buff,
  443. (ulong) (net->write_pos - net->buff) + left_length))
  444. return 1;
  445. net->write_pos= net->buff;
  446. packet+= left_length;
  447. len-= left_length;
  448. }
  449. if (net->compress)
  450. {
  451. /*
  452. We can't have bigger packets than 16M with compression
  453. Because the uncompressed length is stored in 3 bytes
  454. */
  455. left_length= MAX_PACKET_LENGTH;
  456. while (len > left_length)
  457. {
  458. if (net_real_write(net, packet, left_length))
  459. return 1;
  460. packet+= left_length;
  461. len-= left_length;
  462. }
  463. }
  464. if (len > net->max_packet)
  465. return net_real_write(net, packet, len) ? 1 : 0;
  466. /* Send out rest of the blocks as full sized blocks */
  467. }
  468. memcpy((char*) net->write_pos,packet,len);
  469. net->write_pos+= len;
  470. return 0;
  471. }
  472. /*
  473. Read and write one packet using timeouts.
  474. If needed, the packet is compressed before sending.
  475. */
  476. int
  477. net_real_write(NET *net,const char *packet,ulong len)
  478. {
  479. long int length;
  480. char *pos,*end;
  481. thr_alarm_t alarmed;
  482. #ifndef NO_ALARM
  483. ALARM alarm_buff;
  484. #endif
  485. uint retry_count=0;
  486. my_bool net_blocking = vio_is_blocking(net->vio);
  487. DBUG_ENTER("net_real_write");
  488. #if defined(MYSQL_SERVER) && defined(USE_QUERY_CACHE)
  489. query_cache_insert(net, packet, len);
  490. #endif
  491. if (net->error == 2)
  492. DBUG_RETURN(-1); /* socket can't be used */
  493. net->reading_or_writing=2;
  494. #ifdef HAVE_COMPRESS
  495. if (net->compress)
  496. {
  497. ulong complen;
  498. uchar *b;
  499. uint header_length=NET_HEADER_SIZE+COMP_HEADER_SIZE;
  500. if (!(b=(uchar*) my_malloc((uint32) len + NET_HEADER_SIZE +
  501. COMP_HEADER_SIZE, MYF(MY_WME))))
  502. {
  503. #ifdef MYSQL_SERVER
  504. net->last_errno= ER_OUT_OF_RESOURCES;
  505. net->error= 2;
  506. /* TODO is it needed to set this variable if we have no socket */
  507. net->report_error= 1;
  508. #endif
  509. net->reading_or_writing= 0;
  510. DBUG_RETURN(1);
  511. }
  512. memcpy(b+header_length,packet,len);
  513. if (my_compress((byte*) b+header_length,&len,&complen))
  514. complen=0;
  515. int3store(&b[NET_HEADER_SIZE],complen);
  516. int3store(b,len);
  517. b[3]=(uchar) (net->compress_pkt_nr++);
  518. len+= header_length;
  519. packet= (char*) b;
  520. }
  521. #endif /* HAVE_COMPRESS */
  522. #ifdef DEBUG_DATA_PACKETS
  523. DBUG_DUMP("data",packet,len);
  524. #endif
  525. #ifndef NO_ALARM
  526. thr_alarm_init(&alarmed);
  527. if (net_blocking)
  528. thr_alarm(&alarmed,(uint) net->write_timeout,&alarm_buff);
  529. #else
  530. alarmed=0;
  531. /* Write timeout is set in net_set_write_timeout */
  532. #endif /* NO_ALARM */
  533. pos=(char*) packet; end=pos+len;
  534. while (pos != end)
  535. {
  536. if ((long) (length=vio_write(net->vio,pos,(uint32) (end-pos))) <= 0)
  537. {
  538. my_bool interrupted = vio_should_retry(net->vio);
  539. #if !defined(__WIN__)
  540. if ((interrupted || length==0) && !thr_alarm_in_use(&alarmed))
  541. {
  542. if (!thr_alarm(&alarmed,(uint) net->write_timeout,&alarm_buff))
  543. { /* Always true for client */
  544. my_bool old_mode;
  545. while (vio_blocking(net->vio, TRUE, &old_mode) < 0)
  546. {
  547. if (vio_should_retry(net->vio) && retry_count++ < net->retry_count)
  548. continue;
  549. #ifdef EXTRA_DEBUG
  550. fprintf(stderr,
  551. "%s: my_net_write: fcntl returned error %d, aborting thread\n",
  552. my_progname,vio_errno(net->vio));
  553. #endif /* EXTRA_DEBUG */
  554. #ifdef MYSQL_SERVER
  555. net->last_errno= ER_NET_ERROR_ON_WRITE;
  556. #endif
  557. net->error= 2; /* Close socket */
  558. net->report_error= 1;
  559. goto end;
  560. }
  561. retry_count=0;
  562. continue;
  563. }
  564. }
  565. else
  566. #endif /* !defined(__WIN__) */
  567. if (thr_alarm_in_use(&alarmed) && !thr_got_alarm(&alarmed) &&
  568. interrupted)
  569. {
  570. if (retry_count++ < net->retry_count)
  571. continue;
  572. #ifdef EXTRA_DEBUG
  573. fprintf(stderr, "%s: write looped, aborting thread\n",
  574. my_progname);
  575. #endif /* EXTRA_DEBUG */
  576. }
  577. #if defined(THREAD_SAFE_CLIENT) && !defined(MYSQL_SERVER)
  578. if (vio_errno(net->vio) == SOCKET_EINTR)
  579. {
  580. DBUG_PRINT("warning",("Interrupted write. Retrying..."));
  581. continue;
  582. }
  583. #endif /* defined(THREAD_SAFE_CLIENT) && !defined(MYSQL_SERVER) */
  584. net->error= 2; /* Close socket */
  585. net->report_error= 1;
  586. #ifdef MYSQL_SERVER
  587. net->last_errno= (interrupted ? ER_NET_WRITE_INTERRUPTED :
  588. ER_NET_ERROR_ON_WRITE);
  589. #endif /* MYSQL_SERVER */
  590. break;
  591. }
  592. pos+=length;
  593. update_statistics(thd_increment_bytes_sent(length));
  594. }
  595. #ifndef __WIN__
  596. end:
  597. #endif
  598. #ifdef HAVE_COMPRESS
  599. if (net->compress)
  600. my_free((char*) packet,MYF(0));
  601. #endif
  602. if (thr_alarm_in_use(&alarmed))
  603. {
  604. my_bool old_mode;
  605. thr_end_alarm(&alarmed);
  606. vio_blocking(net->vio, net_blocking, &old_mode);
  607. }
  608. net->reading_or_writing=0;
  609. DBUG_RETURN(((int) (pos != end)));
  610. }
  611. /*****************************************************************************
  612. ** Read something from server/clinet
  613. *****************************************************************************/
  614. #ifndef NO_ALARM
  615. static my_bool net_safe_read(NET *net, char *buff, uint32 length,
  616. thr_alarm_t *alarmed)
  617. {
  618. uint retry_count=0;
  619. while (length > 0)
  620. {
  621. int tmp;
  622. if ((tmp=vio_read(net->vio,(char*) net->buff, length)) <= 0)
  623. {
  624. my_bool interrupted = vio_should_retry(net->vio);
  625. if (!thr_got_alarm(alarmed) && interrupted)
  626. { /* Probably in MIT threads */
  627. if (retry_count++ < net->retry_count)
  628. continue;
  629. }
  630. return 1;
  631. }
  632. length-= tmp;
  633. }
  634. return 0;
  635. }
  636. /*
  637. Help function to clear the commuication buffer when we get a too big packet.
  638. SYNOPSIS
  639. my_net_skip_rest()
  640. net Communication handle
  641. remain Bytes to read
  642. alarmed Parameter for thr_alarm()
  643. alarm_buff Parameter for thr_alarm()
  644. RETURN VALUES
  645. 0 Was able to read the whole packet
  646. 1 Got mailformed packet from client
  647. */
  648. static my_bool my_net_skip_rest(NET *net, uint32 remain, thr_alarm_t *alarmed,
  649. ALARM *alarm_buff)
  650. {
  651. uint32 old=remain;
  652. DBUG_ENTER("my_net_skip_rest");
  653. DBUG_PRINT("enter",("bytes_to_skip: %u", (uint) remain));
  654. /* The following is good for debugging */
  655. update_statistics(thd_increment_net_big_packet_count(1));
  656. if (!thr_alarm_in_use(alarmed))
  657. {
  658. my_bool old_mode;
  659. if (thr_alarm(alarmed,net->read_timeout, alarm_buff) ||
  660. vio_blocking(net->vio, TRUE, &old_mode) < 0)
  661. DBUG_RETURN(1); /* Can't setup, abort */
  662. }
  663. for (;;)
  664. {
  665. while (remain > 0)
  666. {
  667. uint length= min(remain, net->max_packet);
  668. if (net_safe_read(net, (char*) net->buff, length, alarmed))
  669. DBUG_RETURN(1);
  670. update_statistics(thd_increment_bytes_received(length));
  671. remain -= (uint32) length;
  672. }
  673. if (old != MAX_PACKET_LENGTH)
  674. break;
  675. if (net_safe_read(net, (char*) net->buff, NET_HEADER_SIZE, alarmed))
  676. DBUG_RETURN(1);
  677. old=remain= uint3korr(net->buff);
  678. net->pkt_nr++;
  679. }
  680. DBUG_RETURN(0);
  681. }
  682. #endif /* NO_ALARM */
  683. /*
  684. Reads one packet to net->buff + net->where_b
  685. Returns length of packet. Long packets are handled by my_net_read().
  686. This function reallocates the net->buff buffer if necessary.
  687. */
  688. static ulong
  689. my_real_read(NET *net, ulong *complen)
  690. {
  691. uchar *pos;
  692. long length;
  693. uint i,retry_count=0;
  694. ulong len=packet_error;
  695. thr_alarm_t alarmed;
  696. #ifndef NO_ALARM
  697. ALARM alarm_buff;
  698. #endif
  699. my_bool net_blocking=vio_is_blocking(net->vio);
  700. uint32 remain= (net->compress ? NET_HEADER_SIZE+COMP_HEADER_SIZE :
  701. NET_HEADER_SIZE);
  702. *complen = 0;
  703. net->reading_or_writing=1;
  704. thr_alarm_init(&alarmed);
  705. #ifndef NO_ALARM
  706. if (net_blocking)
  707. thr_alarm(&alarmed,net->read_timeout,&alarm_buff);
  708. #else
  709. /* Read timeout is set in net_set_read_timeout */
  710. #endif /* NO_ALARM */
  711. pos = net->buff + net->where_b; /* net->packet -4 */
  712. for (i=0 ; i < 2 ; i++)
  713. {
  714. while (remain > 0)
  715. {
  716. /* First read is done with non blocking mode */
  717. if ((int) (length=vio_read(net->vio,(char*) pos,remain)) <= 0L)
  718. {
  719. my_bool interrupted = vio_should_retry(net->vio);
  720. DBUG_PRINT("info",("vio_read returned %ld, errno: %d",
  721. length, vio_errno(net->vio)));
  722. #if !defined(__WIN__) || defined(MYSQL_SERVER)
  723. /*
  724. We got an error that there was no data on the socket. We now set up
  725. an alarm to not 'read forever', change the socket to non blocking
  726. mode and try again
  727. */
  728. if ((interrupted || length == 0) && !thr_alarm_in_use(&alarmed))
  729. {
  730. if (!thr_alarm(&alarmed,net->read_timeout,&alarm_buff)) /* Don't wait too long */
  731. {
  732. my_bool old_mode;
  733. while (vio_blocking(net->vio, TRUE, &old_mode) < 0)
  734. {
  735. if (vio_should_retry(net->vio) &&
  736. retry_count++ < net->retry_count)
  737. continue;
  738. DBUG_PRINT("error",
  739. ("fcntl returned error %d, aborting thread",
  740. vio_errno(net->vio)));
  741. #ifdef EXTRA_DEBUG
  742. fprintf(stderr,
  743. "%s: read: fcntl returned error %d, aborting thread\n",
  744. my_progname,vio_errno(net->vio));
  745. #endif /* EXTRA_DEBUG */
  746. len= packet_error;
  747. net->error= 2; /* Close socket */
  748. net->report_error= 1;
  749. #ifdef MYSQL_SERVER
  750. net->last_errno= ER_NET_FCNTL_ERROR;
  751. #endif
  752. goto end;
  753. }
  754. retry_count=0;
  755. continue;
  756. }
  757. }
  758. #endif /* (!defined(__WIN__) || defined(MYSQL_SERVER) */
  759. if (thr_alarm_in_use(&alarmed) && !thr_got_alarm(&alarmed) &&
  760. interrupted)
  761. { /* Probably in MIT threads */
  762. if (retry_count++ < net->retry_count)
  763. continue;
  764. #ifdef EXTRA_DEBUG
  765. fprintf(stderr, "%s: read looped with error %d, aborting thread\n",
  766. my_progname,vio_errno(net->vio));
  767. #endif /* EXTRA_DEBUG */
  768. }
  769. #if defined(THREAD_SAFE_CLIENT) && !defined(MYSQL_SERVER)
  770. if (vio_errno(net->vio) == SOCKET_EINTR)
  771. {
  772. DBUG_PRINT("warning",("Interrupted read. Retrying..."));
  773. continue;
  774. }
  775. #endif
  776. DBUG_PRINT("error",("Couldn't read packet: remain: %u errno: %d length: %ld",
  777. remain, vio_errno(net->vio), length));
  778. len= packet_error;
  779. net->error= 2; /* Close socket */
  780. net->report_error= 1;
  781. #ifdef MYSQL_SERVER
  782. net->last_errno= (vio_was_interrupted(net->vio) ? ER_NET_READ_INTERRUPTED :
  783. ER_NET_READ_ERROR);
  784. #endif
  785. goto end;
  786. }
  787. remain -= (uint32) length;
  788. pos+= (ulong) length;
  789. update_statistics(thd_increment_bytes_received(length));
  790. }
  791. if (i == 0)
  792. { /* First parts is packet length */
  793. ulong helping;
  794. DBUG_DUMP("packet_header",(char*) net->buff+net->where_b,
  795. NET_HEADER_SIZE);
  796. if (net->buff[net->where_b + 3] != (uchar) net->pkt_nr)
  797. {
  798. if (net->buff[net->where_b] != (uchar) 255)
  799. {
  800. DBUG_PRINT("error",
  801. ("Packets out of order (Found: %d, expected %u)",
  802. (int) net->buff[net->where_b + 3],
  803. net->pkt_nr));
  804. #ifdef EXTRA_DEBUG
  805. fprintf(stderr,"Error: Packets out of order (Found: %d, expected %d)\n",
  806. (int) net->buff[net->where_b + 3],
  807. (uint) (uchar) net->pkt_nr);
  808. #endif
  809. }
  810. len= packet_error;
  811. net->report_error= 1;
  812. #ifdef MYSQL_SERVER
  813. net->last_errno=ER_NET_PACKETS_OUT_OF_ORDER;
  814. #endif
  815. goto end;
  816. }
  817. net->compress_pkt_nr= ++net->pkt_nr;
  818. #ifdef HAVE_COMPRESS
  819. if (net->compress)
  820. {
  821. /*
  822. If the packet is compressed then complen > 0 and contains the
  823. number of bytes in the uncompressed packet
  824. */
  825. *complen=uint3korr(&(net->buff[net->where_b + NET_HEADER_SIZE]));
  826. }
  827. #endif
  828. len=uint3korr(net->buff+net->where_b);
  829. if (!len) /* End of big multi-packet */
  830. goto end;
  831. helping = max(len,*complen) + net->where_b;
  832. /* The necessary size of net->buff */
  833. if (helping >= net->max_packet)
  834. {
  835. if (net_realloc(net,helping))
  836. {
  837. #if defined(MYSQL_SERVER) && !defined(NO_ALARM)
  838. if (!net->compress &&
  839. !my_net_skip_rest(net, (uint32) len, &alarmed, &alarm_buff))
  840. net->error= 3; /* Successfully skiped packet */
  841. #endif
  842. len= packet_error; /* Return error and close connection */
  843. goto end;
  844. }
  845. }
  846. pos=net->buff + net->where_b;
  847. remain = (uint32) len;
  848. }
  849. }
  850. end:
  851. if (thr_alarm_in_use(&alarmed))
  852. {
  853. my_bool old_mode;
  854. thr_end_alarm(&alarmed);
  855. vio_blocking(net->vio, net_blocking, &old_mode);
  856. }
  857. net->reading_or_writing=0;
  858. #ifdef DEBUG_DATA_PACKETS
  859. if (len != packet_error)
  860. DBUG_DUMP("data",(char*) net->buff+net->where_b, len);
  861. #endif
  862. return(len);
  863. }
  864. /*
  865. Read a packet from the client/server and return it without the internal
  866. package header.
  867. If the packet is the first packet of a multi-packet packet
  868. (which is indicated by the length of the packet = 0xffffff) then
  869. all sub packets are read and concatenated.
  870. If the packet was compressed, its uncompressed and the length of the
  871. uncompressed packet is returned.
  872. The function returns the length of the found packet or packet_error.
  873. net->read_pos points to the read data.
  874. */
  875. ulong
  876. my_net_read(NET *net)
  877. {
  878. ulong len,complen;
  879. #ifdef HAVE_COMPRESS
  880. if (!net->compress)
  881. {
  882. #endif
  883. len = my_real_read(net,&complen);
  884. if (len == MAX_PACKET_LENGTH)
  885. {
  886. /* First packet of a multi-packet. Concatenate the packets */
  887. ulong save_pos = net->where_b;
  888. ulong total_length=0;
  889. do
  890. {
  891. net->where_b += len;
  892. total_length += len;
  893. len = my_real_read(net,&complen);
  894. } while (len == MAX_PACKET_LENGTH);
  895. if (len != packet_error)
  896. len+= total_length;
  897. net->where_b = save_pos;
  898. }
  899. net->read_pos = net->buff + net->where_b;
  900. if (len != packet_error)
  901. net->read_pos[len]=0; /* Safeguard for mysql_use_result */
  902. return len;
  903. #ifdef HAVE_COMPRESS
  904. }
  905. else
  906. {
  907. /* We are using the compressed protocol */
  908. ulong buf_length;
  909. ulong start_of_packet;
  910. ulong first_packet_offset;
  911. uint read_length, multi_byte_packet=0;
  912. if (net->remain_in_buf)
  913. {
  914. buf_length= net->buf_length; /* Data left in old packet */
  915. first_packet_offset= start_of_packet= (net->buf_length -
  916. net->remain_in_buf);
  917. /* Restore the character that was overwritten by the end 0 */
  918. net->buff[start_of_packet]= net->save_char;
  919. }
  920. else
  921. {
  922. /* reuse buffer, as there is nothing in it that we need */
  923. buf_length= start_of_packet= first_packet_offset= 0;
  924. }
  925. for (;;)
  926. {
  927. ulong packet_len;
  928. if (buf_length - start_of_packet >= NET_HEADER_SIZE)
  929. {
  930. read_length = uint3korr(net->buff+start_of_packet);
  931. if (!read_length)
  932. {
  933. /* End of multi-byte packet */
  934. start_of_packet += NET_HEADER_SIZE;
  935. break;
  936. }
  937. if (read_length + NET_HEADER_SIZE <= buf_length - start_of_packet)
  938. {
  939. if (multi_byte_packet)
  940. {
  941. /* Remove packet header for second packet */
  942. memmove(net->buff + first_packet_offset + start_of_packet,
  943. net->buff + first_packet_offset + start_of_packet +
  944. NET_HEADER_SIZE,
  945. buf_length - start_of_packet);
  946. start_of_packet += read_length;
  947. buf_length -= NET_HEADER_SIZE;
  948. }
  949. else
  950. start_of_packet+= read_length + NET_HEADER_SIZE;
  951. if (read_length != MAX_PACKET_LENGTH) /* last package */
  952. {
  953. multi_byte_packet= 0; /* No last zero len packet */
  954. break;
  955. }
  956. multi_byte_packet= NET_HEADER_SIZE;
  957. /* Move data down to read next data packet after current one */
  958. if (first_packet_offset)
  959. {
  960. memmove(net->buff,net->buff+first_packet_offset,
  961. buf_length-first_packet_offset);
  962. buf_length-=first_packet_offset;
  963. start_of_packet -= first_packet_offset;
  964. first_packet_offset=0;
  965. }
  966. continue;
  967. }
  968. }
  969. /* Move data down to read next data packet after current one */
  970. if (first_packet_offset)
  971. {
  972. memmove(net->buff,net->buff+first_packet_offset,
  973. buf_length-first_packet_offset);
  974. buf_length-=first_packet_offset;
  975. start_of_packet -= first_packet_offset;
  976. first_packet_offset=0;
  977. }
  978. net->where_b=buf_length;
  979. if ((packet_len = my_real_read(net,&complen)) == packet_error)
  980. return packet_error;
  981. if (my_uncompress((byte*) net->buff + net->where_b, &packet_len,
  982. &complen))
  983. {
  984. net->error= 2; /* caller will close socket */
  985. net->report_error= 1;
  986. #ifdef MYSQL_SERVER
  987. net->last_errno=ER_NET_UNCOMPRESS_ERROR;
  988. #endif
  989. return packet_error;
  990. }
  991. buf_length+=packet_len;
  992. }
  993. net->read_pos= net->buff+ first_packet_offset + NET_HEADER_SIZE;
  994. net->buf_length= buf_length;
  995. net->remain_in_buf= (ulong) (buf_length - start_of_packet);
  996. len = ((ulong) (start_of_packet - first_packet_offset) - NET_HEADER_SIZE -
  997. multi_byte_packet);
  998. net->save_char= net->read_pos[len]; /* Must be saved */
  999. net->read_pos[len]=0; /* Safeguard for mysql_use_result */
  1000. }
  1001. #endif /* HAVE_COMPRESS */
  1002. return len;
  1003. }
  1004. void net_set_read_timeout(NET *net, uint timeout)
  1005. {
  1006. DBUG_ENTER("net_set_read_timeout");
  1007. DBUG_PRINT("enter", ("timeout: %d", timeout));
  1008. net->read_timeout= timeout;
  1009. #ifdef NO_ALARM
  1010. vio_timeout(net->vio, 0, timeout);
  1011. #endif
  1012. DBUG_VOID_RETURN;
  1013. }
  1014. void net_set_write_timeout(NET *net, uint timeout)
  1015. {
  1016. DBUG_ENTER("net_set_write_timeout");
  1017. DBUG_PRINT("enter", ("timeout: %d", timeout));
  1018. net->write_timeout= timeout;
  1019. #ifdef NO_ALARM
  1020. vio_timeout(net->vio, 1, timeout);
  1021. #endif
  1022. DBUG_VOID_RETURN;
  1023. }