db_sockets.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * db_socket.c
  17. *
  18. * This file contains operands for streams with URIs that start with
  19. * "tcp:", "tcp.srv:", "udp:", "udp.srv:".
  20. */
  21. #include "pal_defs.h"
  22. #include "pal.h"
  23. #include "pal_internal.h"
  24. #include "pal_debug.h"
  25. #include "pal_error.h"
  26. #include "api.h"
  27. /* 96 bytes is the minimal size of buffer to store a IPv4/IPv6
  28. address */
  29. #define PAL_SOCKADDR_SIZE 96
  30. /* listen on a tcp socket */
  31. static int tcp_listen (PAL_HANDLE * handle, char * uri, int create)
  32. {
  33. return -PAL_ERROR_NOTIMPLEMENTED;
  34. }
  35. /* accept a tcp connection */
  36. static int tcp_accept (PAL_HANDLE handle, PAL_HANDLE *client)
  37. {
  38. return -PAL_ERROR_NOTIMPLEMENTED;
  39. }
  40. /* connect on a tcp socket */
  41. static int tcp_connect (PAL_HANDLE * handle, char * uri, int create)
  42. {
  43. return -PAL_ERROR_NOTIMPLEMENTED;
  44. }
  45. /* 'open' operation of tcp stream */
  46. static int tcp_open (PAL_HANDLE *handle, const char * type, const char * uri,
  47. int access, int share, int create, int options)
  48. {
  49. int uri_len = strlen(uri) + 1;
  50. if (uri_len > PAL_SOCKADDR_SIZE)
  51. return -PAL_ERROR_TOOLONG;
  52. char uri_buf[PAL_SOCKADDR_SIZE];
  53. memcpy(uri_buf, uri, uri_len);
  54. if (!strpartcmp_static(type, "tcp.srv:"))
  55. return tcp_listen(handle, uri_buf, create);
  56. if (!strpartcmp_static(type, "tcp:"))
  57. return tcp_connect(handle, uri_buf, create);
  58. return -PAL_ERROR_NOTSUPPORT;
  59. }
  60. /* 'read' operation of tcp stream */
  61. static int tcp_read (PAL_HANDLE handle, int offset, int len, void * buf)
  62. {
  63. return -PAL_ERROR_NOTIMPLEMENTED;
  64. }
  65. /* write' operation of tcp stream */
  66. static int tcp_write (PAL_HANDLE handle, int offset, int len, const void * buf)
  67. {
  68. return -PAL_ERROR_NOTIMPLEMENTED;
  69. }
  70. /* used by 'open' operation of tcp stream for bound socket */
  71. static int udp_bind (PAL_HANDLE *handle, char * uri, int create)
  72. {
  73. return -PAL_ERROR_NOTIMPLEMENTED;
  74. }
  75. /* used by 'open' operation of tcp stream for connected socket */
  76. static int udp_connect (PAL_HANDLE * handle, char * uri)
  77. {
  78. return -PAL_ERROR_NOTIMPLEMENTED;
  79. }
  80. static int udp_open (PAL_HANDLE *hdl, const char * type, const char * uri,
  81. int access, int share, int create, int options)
  82. {
  83. char buf[PAL_SOCKADDR_SIZE];
  84. int len = strlen(uri);
  85. if (len >= PAL_SOCKADDR_SIZE)
  86. return -PAL_ERROR_TOOLONG;
  87. memcpy(buf, uri, len + 1);
  88. if (strpartcmp_static(type, "udp.srv:"))
  89. return udp_bind(hdl, buf, create);
  90. if (strpartcmp_static(type, "udp:"))
  91. return udp_connect(hdl, buf);
  92. return -PAL_ERROR_NOTSUPPORT;
  93. }
  94. static int udp_receive (PAL_HANDLE handle, int offset, int len, void * buf)
  95. {
  96. return -PAL_ERROR_NOTIMPLEMENTED;
  97. }
  98. static int udp_receivebyaddr (PAL_HANDLE handle, int offset, int len,
  99. void * buf, char * addr, int addrlen)
  100. {
  101. return -PAL_ERROR_NOTIMPLEMENTED;
  102. }
  103. static int udp_send (PAL_HANDLE handle, int offset, int len, const void * buf)
  104. {
  105. return -PAL_ERROR_NOTIMPLEMENTED;
  106. }
  107. static int udp_sendbyaddr (PAL_HANDLE handle, int offset, int len,
  108. const void * buf, const char * addr, int addrlen)
  109. {
  110. return -PAL_ERROR_NOTIMPLEMENTED;
  111. }
  112. static int socket_delete (PAL_HANDLE handle, int access)
  113. {
  114. return -PAL_ERROR_NOTIMPLEMENTED;
  115. }
  116. static int socket_close (PAL_HANDLE handle)
  117. {
  118. return -PAL_ERROR_NOTIMPLEMENTED;
  119. }
  120. static int socket_attrquerybyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  121. {
  122. return -PAL_ERROR_NOTIMPLEMENTED;
  123. }
  124. static int socket_getname (PAL_HANDLE handle, char * buffer, int count)
  125. {
  126. return -PAL_ERROR_NOTIMPLEMENTED;
  127. }
  128. struct handle_ops tcp_ops = {
  129. .getname = &socket_getname,
  130. .open = &tcp_open,
  131. .waitforclient = &tcp_accept,
  132. .read = &tcp_read,
  133. .write = &tcp_write,
  134. .delete = &socket_delete,
  135. .close = &socket_close,
  136. .attrquerybyhdl = &socket_attrquerybyhdl,
  137. };
  138. struct handle_ops udp_ops = {
  139. .getname = &socket_getname,
  140. .open = &udp_open,
  141. .read = &udp_receive,
  142. .write = &udp_send,
  143. .delete = &socket_delete,
  144. .close = &socket_close,
  145. .attrquerybyhdl = &socket_attrquerybyhdl,
  146. };
  147. struct handle_ops udpsrv_ops = {
  148. .getname = &socket_getname,
  149. .open = &udp_open,
  150. .readbyaddr = &udp_receivebyaddr,
  151. .writebyaddr = &udp_sendbyaddr,
  152. .delete = &socket_delete,
  153. .close = &socket_close,
  154. .attrquerybyhdl = &socket_attrquerybyhdl,
  155. };
  156. PAL_HANDLE _DkBroadcastStreamOpen (void)
  157. {
  158. return NULL;
  159. }
  160. static int mcast_send (PAL_HANDLE handle, int offset, int size,
  161. const void * buf)
  162. {
  163. return -PAL_ERROR_NOTIMPLEMENTED;
  164. }
  165. static int mcast_receive (PAL_HANDLE handle, int offset, int size, void * buf)
  166. {
  167. return -PAL_ERROR_NOTIMPLEMENTED;
  168. }
  169. static int mcast_close (PAL_HANDLE handle)
  170. {
  171. return -PAL_ERROR_NOTIMPLEMENTED;
  172. }
  173. struct handle_ops mcast_ops = {
  174. .write = &mcast_send,
  175. .read = &mcast_receive,
  176. .close = &mcast_close,
  177. };