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.

1104 lines
30 KiB

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