Guest User

Untitled

a guest
Feb 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. /* <MYSQL_ROOT>/vio/vio.c */
  2.  
  3. /* Copyright (C) 2000 MySQL AB
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; version 2 of the License.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  17.  
  18. /*
  19. Note that we can't have assertion on file descriptors; The reason for
  20. this is that during mysql shutdown, another thread can close a file
  21. we are working on. In this case we should just return read errors from
  22. the file descriptior.
  23. */
  24.  
  25. #include "vio_priv.h"
  26.  
  27. /*
  28. * Helper to fill most of the Vio* with defaults.
  29. */
  30.  
  31. static void vio_init(Vio* vio, enum enum_vio_type type,
  32. my_socket sd, HANDLE hPipe, uint flags)
  33. {
  34. DBUG_ENTER("vio_init");
  35. DBUG_PRINT("enter", ("type: %d sd: %d flags: %d", type, sd, flags));
  36.  
  37. #ifndef HAVE_VIO_READ_BUFF
  38. flags&= ~VIO_BUFFERED_READ;
  39. #endif
  40. bzero((char*) vio, sizeof(*vio));
  41. vio->type = type;
  42. vio->sd = sd;
  43. vio->hPipe = hPipe;
  44. vio->localhost= flags & VIO_LOCALHOST;
  45. if ((flags & VIO_BUFFERED_READ) &&
  46. !(vio->read_buffer= (char*)my_malloc(VIO_READ_BUFFER_SIZE, MYF(MY_WME))))
  47. flags&= ~VIO_BUFFERED_READ;
  48. #ifdef __WIN__
  49. if (type == VIO_TYPE_NAMEDPIPE)
  50. {
  51. vio->viodelete =vio_delete;
  52. vio->vioerrno =vio_errno;
  53. vio->read =vio_read_pipe;
  54. vio->write =vio_write_pipe;
  55. vio->fastsend =vio_fastsend;
  56. vio->viokeepalive =vio_keepalive;
  57. vio->should_retry =vio_should_retry;
  58. vio->was_interrupted=vio_was_interrupted;
  59. vio->vioclose =vio_close_pipe;
  60. vio->peer_addr =vio_peer_addr;
  61. vio->in_addr =vio_in_addr;
  62. vio->vioblocking =vio_blocking;
  63. vio->is_blocking =vio_is_blocking;
  64. vio->timeout =vio_ignore_timeout;
  65. }
  66. else /* default is VIO_TYPE_TCPIP */
  67. #endif
  68. #ifdef HAVE_SMEM
  69. if (type == VIO_TYPE_SHARED_MEMORY)
  70. {
  71. vio->viodelete =vio_delete;
  72. vio->vioerrno =vio_errno;
  73. vio->read =vio_read_shared_memory;
  74. vio->write =vio_write_shared_memory;
  75. vio->fastsend =vio_fastsend;
  76. vio->viokeepalive =vio_keepalive;
  77. vio->should_retry =vio_should_retry;
  78. vio->was_interrupted=vio_was_interrupted;
  79. vio->vioclose =vio_close_shared_memory;
  80. vio->peer_addr =vio_peer_addr;
  81. vio->in_addr =vio_in_addr;
  82. vio->vioblocking =vio_blocking;
  83. vio->is_blocking =vio_is_blocking;
  84. vio->timeout =vio_ignore_timeout;
  85. }
  86. else
  87. #endif
  88. #ifdef HAVE_OPENSSL
  89. if (type == VIO_TYPE_SSL)
  90. {
  91. vio->viodelete =vio_ssl_delete;
  92. vio->vioerrno =vio_errno;
  93. vio->read =vio_ssl_read;
  94. vio->write =vio_ssl_write;
  95. vio->fastsend =vio_fastsend;
  96. vio->viokeepalive =vio_keepalive;
  97. vio->should_retry =vio_should_retry;
  98. vio->was_interrupted=vio_was_interrupted;
  99. vio->vioclose =vio_ssl_close;
  100. vio->peer_addr =vio_peer_addr;
  101. vio->in_addr =vio_in_addr;
  102. vio->vioblocking =vio_ssl_blocking;
  103. vio->is_blocking =vio_is_blocking;
  104. vio->timeout =vio_timeout;
  105. }
  106. else /* default is VIO_TYPE_TCPIP */
  107. #endif /* HAVE_OPENSSL */
  108. {
  109. vio->viodelete =vio_delete;
  110. vio->vioerrno =vio_errno;
  111. vio->read= (flags & VIO_BUFFERED_READ) ? vio_read_buff : vio_read;
  112. vio->write =vio_write;
  113. vio->fastsend =vio_fastsend;
  114. vio->viokeepalive =vio_keepalive;
  115. vio->should_retry =vio_should_retry;
  116. vio->was_interrupted=vio_was_interrupted;
  117. vio->vioclose =vio_close;
  118. vio->peer_addr =vio_peer_addr;
  119. vio->in_addr =vio_in_addr;
  120. vio->vioblocking =vio_blocking;
  121. vio->is_blocking =vio_is_blocking;
  122. vio->timeout =vio_timeout;
  123. }
  124. DBUG_VOID_RETURN;
  125. }
  126.  
  127.  
  128. /* Reset initialized VIO to use with another transport type */
  129.  
  130. void vio_reset(Vio* vio, enum enum_vio_type type,
  131. my_socket sd, HANDLE hPipe, uint flags)
  132. {
  133. my_free(vio->read_buffer, MYF(MY_ALLOW_ZERO_PTR));
  134. vio_init(vio, type, sd, hPipe, flags);
  135. }
  136.  
  137.  
  138. /* Open the socket or TCP/IP connection and read the fnctl() status */
  139.  
  140. Vio *vio_new(my_socket sd, enum enum_vio_type type, uint flags)
  141. {
  142. Vio *vio;
  143. DBUG_ENTER("vio_new");
  144. DBUG_PRINT("enter", ("sd: %d", sd));
  145. if ((vio = (Vio*) my_malloc(sizeof(*vio),MYF(MY_WME))))
  146. {
  147. vio_init(vio, type, sd, 0, flags);
  148. sprintf(vio->desc,
  149. (vio->type == VIO_TYPE_SOCKET ? "socket (%d)" : "TCP/IP (%d)"),
  150. vio->sd);
  151. #if !defined(__WIN__) && !defined(__EMX__) && !defined(OS2)
  152. #if !defined(NO_FCNTL_NONBLOCK)
  153. /*
  154. We call fcntl() to set the flags and then immediately read them back
  155. to make sure that we and the system are in agreement on the state of
  156. things.
  157.  
  158. An example of why we need to do this is FreeBSD (and apparently some
  159. other BSD-derived systems, like Mac OS X), where the system sometimes
  160. reports that the socket is set for non-blocking when it really will
  161. block.
  162. */
  163. fcntl(sd, F_SETFL, 0);
  164. vio->fcntl_mode= fcntl(sd, F_GETFL);
  165. #elif defined(HAVE_SYS_IOCTL_H) /* hpux */
  166. /* Non blocking sockets doesn't work good on HPUX 11.0 */
  167. (void) ioctl(sd,FIOSNBIO,0);
  168. vio->fcntl_mode &= ~O_NONBLOCK;
  169. #endif
  170. #else /* !defined(__WIN__) && !defined(__EMX__) */
  171. {
  172. /* set to blocking mode by default */
  173. ulong arg=0, r;
  174. r = ioctlsocket(sd,FIONBIO,(void*) &arg);
  175. vio->fcntl_mode &= ~O_NONBLOCK;
  176. }
  177. #endif
  178. }
  179. DBUG_RETURN(vio);
  180. }
  181.  
  182.  
  183. #ifdef __WIN__
  184.  
  185. Vio *vio_new_win32pipe(HANDLE hPipe)
  186. {
  187. Vio *vio;
  188. DBUG_ENTER("vio_new_handle");
  189. if ((vio = (Vio*) my_malloc(sizeof(Vio),MYF(MY_WME))))
  190. {
  191. vio_init(vio, VIO_TYPE_NAMEDPIPE, 0, hPipe, VIO_LOCALHOST);
  192. strmov(vio->desc, "named pipe");
  193. }
  194. DBUG_RETURN(vio);
  195. }
  196.  
  197. #ifdef HAVE_SMEM
  198. Vio *vio_new_win32shared_memory(NET *net,HANDLE handle_file_map, HANDLE handle_map,
  199. HANDLE event_server_wrote, HANDLE event_server_read,
  200. HANDLE event_client_wrote, HANDLE event_client_read,
  201. HANDLE event_conn_closed)
  202. {
  203. Vio *vio;
  204. DBUG_ENTER("vio_new_win32shared_memory");
  205. if ((vio = (Vio*) my_malloc(sizeof(Vio),MYF(MY_WME))))
  206. {
  207. vio_init(vio, VIO_TYPE_SHARED_MEMORY, 0, 0, VIO_LOCALHOST);
  208. vio->handle_file_map= handle_file_map;
  209. vio->handle_map= handle_map;
  210. vio->event_server_wrote= event_server_wrote;
  211. vio->event_server_read= event_server_read;
  212. vio->event_client_wrote= event_client_wrote;
  213. vio->event_client_read= event_client_read;
  214. vio->event_conn_closed= event_conn_closed;
  215. vio->shared_memory_remain= 0;
  216. vio->shared_memory_pos= handle_map;
  217. vio->net= net;
  218. strmov(vio->desc, "shared memory");
  219. }
  220. DBUG_RETURN(vio);
  221. }
  222. #endif
  223. #endif
  224.  
  225.  
  226. void vio_delete(Vio* vio)
  227. {
  228. if (!vio)
  229. return; /* It must be safe to delete null pointers. */
  230.  
  231. if (vio->type != VIO_CLOSED)
  232. vio->vioclose(vio);
  233. my_free((gptr) vio->read_buffer, MYF(MY_ALLOW_ZERO_PTR));
  234. my_free((gptr) vio,MYF(0));
  235. }
  236.  
  237.  
  238. /*
  239. Cleanup memory allocated by vio or the
  240. components below it when application finish
  241.  
  242. */
  243. void vio_end(void)
  244. {
  245. #ifdef HAVE_YASSL
  246. yaSSL_CleanUp();
  247. #endif
  248. }
Add Comment
Please, Sign In to add comment