db_sockets.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_socket.c
  15. *
  16. * This file contains operands for streams with URIs that start with
  17. * "tcp:", "tcp.srv:", "udp:", "udp.srv:".
  18. */
  19. #include "pal_defs.h"
  20. #include "pal_linux_defs.h"
  21. #include "pal.h"
  22. #include "pal_internal.h"
  23. #include "pal_linux.h"
  24. #include "pal_linux_error.h"
  25. #include "pal_debug.h"
  26. #include "pal_security.h"
  27. #include "pal_error.h"
  28. #include "api.h"
  29. #include <linux/types.h>
  30. #include <linux/poll.h>
  31. #include <linux/in.h>
  32. #include <linux/in6.h>
  33. typedef __kernel_pid_t pid_t;
  34. #include <asm/fcntl.h>
  35. #include <asm-generic/socket.h>
  36. #ifndef SOL_TCP
  37. # define SOL_TCP 6
  38. #endif
  39. #ifndef TCP_NODELAY
  40. # define TCP_NODELAY 1
  41. #endif
  42. #ifndef TCP_CORK
  43. # define TCP_CORK 3
  44. #endif
  45. /* 96 bytes is the minimal size of buffer to store a IPv4/IPv6
  46. address */
  47. #define PAL_SOCKADDR_SIZE 96
  48. static size_t addr_size(const struct sockaddr* addr) {
  49. switch (addr->sa_family) {
  50. case AF_INET:
  51. return sizeof(struct sockaddr_in);
  52. case AF_INET6:
  53. return sizeof(struct sockaddr_in6);
  54. default:
  55. return 0;
  56. }
  57. }
  58. /* parsing the string of uri, and fill in the socket address structure.
  59. the latest pointer of uri, length of socket address are returned. */
  60. static int inet_parse_uri (char ** uri, struct sockaddr * addr, unsigned int * addrlen)
  61. {
  62. char * tmp = *uri, * end;
  63. char * addr_str = NULL, * port_str;
  64. int af;
  65. void * addr_buf;
  66. int addr_len;
  67. __be16 * port_buf;
  68. int slen;
  69. if (tmp[0] == '[') {
  70. /* for IPv6, the address will be in the form of
  71. "[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx]:port". */
  72. struct sockaddr_in6 * addr_in6 = (struct sockaddr_in6 *) addr;
  73. slen = sizeof(struct sockaddr_in6);
  74. memset(addr, 0, slen);
  75. end = strchr(tmp + 1, ']');
  76. if (!end || *(end + 1) != ':')
  77. goto inval;
  78. addr_str = tmp + 1;
  79. addr_len = end - tmp - 1;
  80. port_str = end + 2;
  81. for (end = port_str ; *end >= '0' && *end <= '9' ; end++);
  82. addr_in6->sin6_family = af = AF_INET6;
  83. addr_buf = &addr_in6->sin6_addr.s6_addr;
  84. port_buf = &addr_in6->sin6_port;
  85. } else {
  86. /* for IP, the address will be in the form of "x.x.x.x:port". */
  87. struct sockaddr_in * addr_in = (struct sockaddr_in *) addr;
  88. slen = sizeof(struct sockaddr_in);
  89. memset(addr, 0, slen);
  90. end = strchr(tmp, ':');
  91. if (!end)
  92. goto inval;
  93. addr_str = tmp;
  94. addr_len = end - tmp;
  95. port_str = end + 1;
  96. for (end = port_str ; *end >= '0' && *end <= '9' ; end++);
  97. addr_in->sin_family = af = AF_INET;
  98. addr_buf = &addr_in->sin_addr.s_addr;
  99. port_buf = &addr_in->sin_port;
  100. }
  101. if (af == AF_INET) {
  102. if (!inet_pton4(addr_str, addr_len, addr_buf))
  103. goto inval;
  104. } else {
  105. if (!inet_pton6(addr_str, addr_len, addr_buf))
  106. goto inval;
  107. }
  108. *port_buf = __htons(atoi(port_str));
  109. *uri = *end ? end + 1 : NULL;
  110. if (addrlen)
  111. *addrlen = slen;
  112. return 0;
  113. inval:
  114. return -PAL_ERROR_INVAL;
  115. }
  116. /* create the string of uri from the given socket address */
  117. static int inet_create_uri (char * uri, int count, struct sockaddr * addr,
  118. int addrlen)
  119. {
  120. int len = 0;
  121. if (addr->sa_family == AF_INET) {
  122. if (addrlen != sizeof(struct sockaddr_in))
  123. return -PAL_ERROR_INVAL;
  124. struct sockaddr_in * addr_in = (struct sockaddr_in *) addr;
  125. char * addr = (char *) &addr_in->sin_addr.s_addr;
  126. /* for IP, the address will be in the form of "x.x.x.x:port". */
  127. len = snprintf(uri, count, "%u.%u.%u.%u:%u",
  128. (unsigned char) addr[0],
  129. (unsigned char) addr[1],
  130. (unsigned char) addr[2],
  131. (unsigned char) addr[3],
  132. __ntohs(addr_in->sin_port));
  133. } else if (addr->sa_family == AF_INET6) {
  134. if (addrlen != sizeof(struct sockaddr_in6))
  135. return -PAL_ERROR_INVAL;
  136. struct sockaddr_in6 * addr_in6 = (struct sockaddr_in6 *) addr;
  137. unsigned short * addr = (unsigned short *) &addr_in6->sin6_addr.s6_addr;
  138. /* for IPv6, the address will be in the form of
  139. "[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx]:port". */
  140. len = snprintf(uri, count, "[%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x]:%u",
  141. addr[0], addr[1], addr[2], addr[3],
  142. addr[4], addr[5], addr[6], addr[7],
  143. __ntohs(addr_in6->sin6_port));
  144. } else {
  145. return -PAL_ERROR_INVAL;
  146. }
  147. if (len >= count)
  148. return -PAL_ERROR_TOOLONG;
  149. return len;
  150. }
  151. /* parse the uri for a socket stream. The uri might have both binding
  152. address and connecting address, or connecting address only. The form
  153. of uri will be either "bind-addr:bind-port:connect-addr:connect-port"
  154. or "addr:port". */
  155. static int socket_parse_uri (char * uri,
  156. struct sockaddr ** bind_addr, unsigned int * bind_addrlen,
  157. struct sockaddr ** dest_addr, unsigned int * dest_addrlen)
  158. {
  159. int ret;
  160. if (!bind_addr && !dest_addr)
  161. return 0;
  162. if (!uri || !(*uri)) {
  163. if (bind_addr)
  164. *bind_addr = NULL;
  165. if (bind_addrlen)
  166. *bind_addrlen = 0;
  167. if (dest_addr)
  168. *dest_addr = NULL;
  169. if (dest_addrlen)
  170. *dest_addrlen = 0;
  171. return 0;
  172. }
  173. /* at least parse uri once */
  174. if ((ret = inet_parse_uri(&uri, bind_addr ? *bind_addr : *dest_addr,
  175. bind_addr ? bind_addrlen : dest_addrlen)) < 0)
  176. return ret;
  177. if (!(bind_addr && dest_addr))
  178. return 0;
  179. /* if you reach here, it can only be connection address */
  180. if (!uri || (ret = inet_parse_uri(&uri, *dest_addr, dest_addrlen)) < 0) {
  181. *dest_addr = *bind_addr;
  182. *dest_addrlen = *bind_addrlen;
  183. *bind_addr = NULL;
  184. *bind_addrlen = 0;
  185. }
  186. return 0;
  187. }
  188. /* fill in the PAL handle based on the file descriptors and address given. */
  189. static inline
  190. PAL_HANDLE socket_create_handle (int type, int fd, int options,
  191. struct sockaddr * bind_addr,
  192. unsigned int bind_addrlen,
  193. struct sockaddr * dest_addr,
  194. unsigned int dest_addrlen,
  195. struct sockopt * sock_options)
  196. {
  197. PAL_HANDLE hdl = malloc(HANDLE_SIZE(sock) + (bind_addr ? bind_addrlen : 0) +
  198. (dest_addr ? dest_addrlen : 0));
  199. if (!hdl)
  200. return NULL;
  201. memset(hdl, 0, sizeof(struct pal_handle));
  202. init_handle_hdr(HANDLE_HDR(hdl), type);
  203. HANDLE_HDR(hdl)->flags |= RFD(0)|(type != pal_type_tcpsrv ? WFD(0) : 0);
  204. hdl->sock.fd = fd;
  205. void * addr = (void *) hdl + HANDLE_SIZE(sock);
  206. if (bind_addr) {
  207. hdl->sock.bind = (PAL_PTR) addr;
  208. memcpy(addr, bind_addr, bind_addrlen);
  209. addr += bind_addrlen;
  210. } else {
  211. hdl->sock.bind = (PAL_PTR) NULL;
  212. }
  213. if (dest_addr) {
  214. hdl->sock.conn = (PAL_PTR) addr;
  215. memcpy(addr, dest_addr, dest_addrlen);
  216. addr += dest_addrlen;
  217. } else {
  218. hdl->sock.conn = (PAL_PTR) NULL;
  219. }
  220. hdl->sock.nonblocking = (options & PAL_OPTION_NONBLOCK) ?
  221. PAL_TRUE : PAL_FALSE;
  222. hdl->sock.linger = sock_options->linger;
  223. hdl->sock.receivebuf = sock_options->receivebuf;
  224. hdl->sock.sendbuf = sock_options->sendbuf;
  225. hdl->sock.receivetimeout = sock_options->receivetimeout;
  226. hdl->sock.sendtimeout = sock_options->sendtimeout;
  227. hdl->sock.tcp_cork = sock_options->tcp_cork;
  228. hdl->sock.tcp_keepalive = sock_options->tcp_keepalive;
  229. hdl->sock.tcp_nodelay = sock_options->tcp_nodelay;
  230. return hdl;
  231. }
  232. #if ALLOW_BIND_ANY == 0
  233. static bool check_zero (void * mem, size_t size)
  234. {
  235. void * p = mem, * q = mem + size;
  236. while (p < q) {
  237. if (p <= q - sizeof(long)) {
  238. if (*(long *) p)
  239. return false;
  240. p += sizeof(long);
  241. } else if (p <= q - sizeof(int)) {
  242. if (*(int *) p)
  243. return false;
  244. p += sizeof(int);
  245. } else if (p <= q - sizeof(short)) {
  246. if (*(short *) p)
  247. return false;
  248. p += sizeof(short);
  249. } else {
  250. if (*(char *) p)
  251. return false;
  252. p++;
  253. }
  254. }
  255. return true;
  256. }
  257. /* check if an address is "Any" */
  258. static bool check_any_addr (struct sockaddr * addr)
  259. {
  260. if (addr->sa_family == AF_INET) {
  261. struct sockaddr_in * addr_in =
  262. (struct sockaddr_in *) addr;
  263. return addr_in->sin_port == 0 &&
  264. check_zero(&addr_in->sin_addr,
  265. sizeof(addr_in->sin_addr));
  266. } else if (addr->sa_family == AF_INET6) {
  267. struct sockaddr_in6 * addr_in6 =
  268. (struct sockaddr_in6 *) addr;
  269. return addr_in6->sin6_port == 0 &&
  270. check_zero(&addr_in6->sin6_addr,
  271. sizeof(addr_in6->sin6_addr));
  272. }
  273. return false;
  274. }
  275. #endif
  276. static inline int sock_type (int type, int options)
  277. {
  278. if (options & PAL_OPTION_NONBLOCK)
  279. type |= SOCK_NONBLOCK;
  280. return type;
  281. }
  282. /* listen on a tcp socket */
  283. static int tcp_listen (PAL_HANDLE * handle, char * uri, int options)
  284. {
  285. struct sockaddr buffer, * bind_addr = &buffer;
  286. unsigned int bind_addrlen;
  287. int ret;
  288. if ((ret = socket_parse_uri(uri, &bind_addr, &bind_addrlen,
  289. NULL, NULL)) < 0)
  290. return ret;
  291. #if ALLOW_BIND_ANY == 0
  292. /* the socket need to have a binding address, a null address or an
  293. any address is not allowed */
  294. if (check_any_addr(bind_addr))
  295. return -PAL_ERROR_INVAL;
  296. #endif
  297. struct sockopt sock_options;
  298. ret = ocall_sock_listen(bind_addr->sa_family,
  299. sock_type(SOCK_STREAM, options), 0,
  300. bind_addr, &bind_addrlen,
  301. &sock_options);
  302. if (IS_ERR(ret))
  303. return unix_to_pal_error(ERRNO(ret));
  304. *handle = socket_create_handle(pal_type_tcpsrv, ret, options,
  305. bind_addr, bind_addrlen, NULL, 0,
  306. &sock_options);
  307. if (!(*handle)) {
  308. ocall_close(ret);
  309. return -PAL_ERROR_NOMEM;
  310. }
  311. return 0;
  312. }
  313. /* accept a tcp connection */
  314. static int tcp_accept (PAL_HANDLE handle, PAL_HANDLE * client)
  315. {
  316. if (!IS_HANDLE_TYPE(handle, tcpsrv) ||
  317. !handle->sock.bind || handle->sock.conn)
  318. return -PAL_ERROR_NOTSERVER;
  319. if (handle->sock.fd == PAL_IDX_POISON)
  320. return -PAL_ERROR_BADHANDLE;
  321. struct sockaddr * bind_addr = (struct sockaddr *) handle->sock.bind;
  322. unsigned int bind_addrlen = addr_size(bind_addr);
  323. struct sockaddr dest_addr;
  324. unsigned int dest_addrlen = sizeof(struct sockaddr);
  325. int ret = 0;
  326. struct sockopt sock_options;
  327. ret = ocall_sock_accept(handle->sock.fd, &dest_addr, &dest_addrlen,
  328. &sock_options);
  329. if (IS_ERR(ret))
  330. return unix_to_pal_error(ERRNO(ret));
  331. *client = socket_create_handle(pal_type_tcp, ret, 0, bind_addr,
  332. bind_addrlen,
  333. &dest_addr, dest_addrlen,
  334. &sock_options);
  335. if (!(*client)) {
  336. ocall_close(ret);
  337. return -PAL_ERROR_NOMEM;
  338. }
  339. return 0;
  340. }
  341. /* connect on a tcp socket */
  342. static int tcp_connect (PAL_HANDLE * handle, char * uri, int options)
  343. {
  344. struct sockaddr buffer[2];
  345. struct sockaddr * bind_addr = buffer, * dest_addr = buffer + 1;
  346. unsigned int bind_addrlen, dest_addrlen;
  347. int ret;
  348. /* accepting two kind of different uri:
  349. dest-ip:dest-port or bind-ip:bind-port:dest-ip:dest-port */
  350. if ((ret = socket_parse_uri(uri, &bind_addr, &bind_addrlen,
  351. &dest_addr, &dest_addrlen)) < 0)
  352. return ret;
  353. if (!dest_addr)
  354. return -PAL_ERROR_INVAL;
  355. if (bind_addr && bind_addr->sa_family != dest_addr->sa_family)
  356. return -PAL_ERROR_INVAL;
  357. #if ALLOW_BIND_ANY == 0
  358. /* the socket need to have a binding address, a null address or an
  359. any address is not allowed */
  360. if (bind_addr && addr_check_any(bind_addr))
  361. return -PAL_ERROR_INVAL;
  362. #endif
  363. struct sockopt sock_options;
  364. ret = ocall_sock_connect(dest_addr->sa_family,
  365. sock_type(SOCK_STREAM, options), 0,
  366. dest_addr, dest_addrlen,
  367. bind_addr, &bind_addrlen, &sock_options);
  368. if (IS_ERR(ret))
  369. return unix_to_pal_error(ERRNO(ret));
  370. *handle = socket_create_handle(pal_type_tcp, ret, options,
  371. bind_addr, bind_addrlen,
  372. dest_addr, dest_addrlen,
  373. &sock_options);
  374. if (!(*handle)) {
  375. ocall_close(ret);
  376. return -PAL_ERROR_NOMEM;
  377. }
  378. return 0;
  379. }
  380. /* 'open' operation of tcp stream */
  381. static int tcp_open (PAL_HANDLE *handle, const char * type, const char * uri,
  382. int access, int share, int create, int options)
  383. {
  384. if (!WITHIN_MASK(access, PAL_ACCESS_MASK) ||
  385. !WITHIN_MASK(share, PAL_SHARE_MASK) ||
  386. !WITHIN_MASK(create, PAL_CREATE_MASK) ||
  387. !WITHIN_MASK(options, PAL_OPTION_MASK))
  388. return -PAL_ERROR_INVAL;
  389. int uri_len = strlen(uri) + 1;
  390. if (uri_len > PAL_SOCKADDR_SIZE)
  391. return -PAL_ERROR_TOOLONG;
  392. char uri_buf[PAL_SOCKADDR_SIZE];
  393. memcpy(uri_buf, uri, uri_len);
  394. if (strcmp_static(type, "tcp.srv"))
  395. return tcp_listen(handle, uri_buf, options);
  396. if (strcmp_static(type, "tcp"))
  397. return tcp_connect(handle, uri_buf, options);
  398. return -PAL_ERROR_NOTSUPPORT;
  399. }
  400. /* 'read' operation of tcp stream */
  401. static int64_t tcp_read (PAL_HANDLE handle, uint64_t offset, uint64_t len,
  402. void * buf)
  403. {
  404. if (offset)
  405. return -PAL_ERROR_INVAL;
  406. if (!IS_HANDLE_TYPE(handle, tcp) || !handle->sock.conn)
  407. return -PAL_ERROR_NOTCONNECTION;
  408. if (handle->sock.fd == PAL_IDX_POISON)
  409. return -PAL_ERROR_ENDOFSTREAM;
  410. if (len >= (1ULL << (sizeof(unsigned int) * 8)))
  411. return -PAL_ERROR_INVAL;
  412. int bytes = ocall_sock_recv(handle->sock.fd, buf, len, NULL, NULL);
  413. if (IS_ERR(bytes))
  414. return unix_to_pal_error(ERRNO(bytes));
  415. if (!bytes)
  416. return -PAL_ERROR_ENDOFSTREAM;
  417. return bytes;
  418. }
  419. /* write' operation of tcp stream */
  420. static int64_t tcp_write (PAL_HANDLE handle, uint64_t offset, uint64_t len,
  421. const void * buf)
  422. {
  423. if (offset)
  424. return -PAL_ERROR_INVAL;
  425. if (!IS_HANDLE_TYPE(handle, tcp) || !handle->sock.conn)
  426. return -PAL_ERROR_NOTCONNECTION;
  427. if (handle->sock.fd == PAL_IDX_POISON)
  428. return -PAL_ERROR_CONNFAILED;
  429. if (len >= (1ULL << (sizeof(unsigned int) * 8)))
  430. return -PAL_ERROR_INVAL;
  431. int bytes = ocall_sock_send(handle->sock.fd, buf, len, NULL, 0);
  432. if (IS_ERR(bytes)) {
  433. bytes = unix_to_pal_error(ERRNO(bytes));
  434. if (bytes == -PAL_ERROR_TRYAGAIN)
  435. HANDLE_HDR(handle)->flags &= ~WRITABLE(0);
  436. return bytes;
  437. }
  438. if ((uint64_t)bytes == len)
  439. HANDLE_HDR(handle)->flags |= WRITABLE(0);
  440. else
  441. HANDLE_HDR(handle)->flags &= ~WRITABLE(0);
  442. return bytes;
  443. }
  444. /* used by 'open' operation of tcp stream for bound socket */
  445. static int udp_bind (PAL_HANDLE * handle, char * uri, int options)
  446. {
  447. struct sockaddr buffer, * bind_addr = &buffer;
  448. unsigned int bind_addrlen;
  449. int ret = 0;
  450. if ((ret = socket_parse_uri(uri, &bind_addr, &bind_addrlen,
  451. NULL, NULL)) < 0)
  452. return ret;
  453. assert(bind_addr);
  454. assert(bind_addrlen == addr_size(bind_addr));
  455. #if ALLOW_BIND_ANY == 0
  456. /* the socket need to have a binding address, a null address or an
  457. any address is not allowed */
  458. if (addr_check_any(bind_addr))
  459. return -PAL_ERROR_INVAL;
  460. #endif
  461. struct sockopt sock_options;
  462. ret = ocall_sock_listen(bind_addr->sa_family,
  463. sock_type(SOCK_DGRAM, options), 0,
  464. bind_addr, &bind_addrlen, &sock_options);
  465. if (IS_ERR(ret))
  466. return unix_to_pal_error(ERRNO(ret));
  467. *handle = socket_create_handle(pal_type_udpsrv, ret, options,
  468. bind_addr, bind_addrlen, NULL, 0,
  469. &sock_options);
  470. if (!(*handle)) {
  471. ocall_close(ret);
  472. return -PAL_ERROR_NOMEM;
  473. }
  474. return 0;
  475. }
  476. /* used by 'open' operation of tcp stream for connected socket */
  477. static int udp_connect (PAL_HANDLE * handle, char * uri, int options)
  478. {
  479. struct sockaddr buffer[2];
  480. struct sockaddr * bind_addr = buffer, * dest_addr = buffer + 1;
  481. unsigned int bind_addrlen, dest_addrlen;
  482. int ret;
  483. if ((ret = socket_parse_uri(uri, &bind_addr, &bind_addrlen,
  484. &dest_addr, &dest_addrlen)) < 0)
  485. return ret;
  486. #if ALLOW_BIND_ANY == 0
  487. /* the socket need to have a binding address, a null address or an
  488. any address is not allowed */
  489. if (bind_addr && addr_check_any(bind_addr))
  490. return -PAL_ERROR_INVAL;
  491. #endif
  492. struct sockopt sock_options;
  493. ret = ocall_sock_connect(dest_addr ? dest_addr->sa_family : AF_INET,
  494. sock_type(SOCK_DGRAM, options), 0,
  495. dest_addr, dest_addrlen,
  496. bind_addr, &bind_addrlen, &sock_options);
  497. if (IS_ERR(ret))
  498. return unix_to_pal_error(ERRNO(ret));
  499. *handle = socket_create_handle(dest_addr ? pal_type_udp :
  500. pal_type_udpsrv, ret, options,
  501. bind_addr, bind_addrlen,
  502. dest_addr, dest_addrlen,
  503. &sock_options);
  504. if (!(*handle)) {
  505. ocall_close(ret);
  506. return -PAL_ERROR_NOMEM;
  507. }
  508. return 0;
  509. }
  510. static int udp_open (PAL_HANDLE *hdl, const char * type, const char * uri,
  511. int access, int share, int create, int options)
  512. {
  513. if (!WITHIN_MASK(access, PAL_ACCESS_MASK) ||
  514. !WITHIN_MASK(share, PAL_SHARE_MASK) ||
  515. !WITHIN_MASK(create, PAL_CREATE_MASK) ||
  516. !WITHIN_MASK(options, PAL_OPTION_MASK))
  517. return -PAL_ERROR_INVAL;
  518. char buf[PAL_SOCKADDR_SIZE];
  519. int len = strlen(uri);
  520. if (len >= PAL_SOCKADDR_SIZE)
  521. return -PAL_ERROR_TOOLONG;
  522. memcpy(buf, uri, len + 1);
  523. if (strcmp_static(type, "udp.srv"))
  524. return udp_bind(hdl, buf, options);
  525. if (strcmp_static(type, "udp"))
  526. return udp_connect(hdl, buf, options);
  527. return -PAL_ERROR_NOTSUPPORT;
  528. }
  529. static int64_t udp_receive (PAL_HANDLE handle, uint64_t offset, uint64_t len,
  530. void * buf)
  531. {
  532. if (offset)
  533. return -PAL_ERROR_INVAL;
  534. if (!IS_HANDLE_TYPE(handle, udp))
  535. return -PAL_ERROR_NOTCONNECTION;
  536. if (handle->sock.fd == PAL_IDX_POISON)
  537. return -PAL_ERROR_BADHANDLE;
  538. if (len >= (1ULL << (sizeof(unsigned int) * 8)))
  539. return -PAL_ERROR_INVAL;
  540. int ret = ocall_sock_recv(handle->sock.fd, buf, len, NULL, NULL);
  541. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : ret;
  542. }
  543. static int64_t udp_receivebyaddr (PAL_HANDLE handle, uint64_t offset, uint64_t len,
  544. void * buf, char * addr, size_t addrlen)
  545. {
  546. if (offset)
  547. return -PAL_ERROR_INVAL;
  548. if (!IS_HANDLE_TYPE(handle, udpsrv))
  549. return -PAL_ERROR_NOTCONNECTION;
  550. if (handle->sock.fd == PAL_IDX_POISON)
  551. return -PAL_ERROR_BADHANDLE;
  552. if (len >= (1ULL << (sizeof(unsigned int) * 8)))
  553. return -PAL_ERROR_INVAL;
  554. struct sockaddr conn_addr;
  555. socklen_t conn_addrlen = sizeof(struct sockaddr);
  556. int bytes = ocall_sock_recv(handle->sock.fd, buf, len, &conn_addr,
  557. &conn_addrlen);
  558. if (IS_ERR(bytes))
  559. return unix_to_pal_error(ERRNO(bytes));
  560. char * addr_uri = strcpy_static(addr, "udp:", addrlen);
  561. if (!addr_uri)
  562. return -PAL_ERROR_OVERFLOW;
  563. int ret = inet_create_uri(addr_uri, addr + addrlen - addr_uri, &conn_addr,
  564. conn_addrlen);
  565. if (ret < 0)
  566. return ret;
  567. return bytes;
  568. }
  569. static int64_t udp_send (PAL_HANDLE handle, uint64_t offset, uint64_t len,
  570. const void * buf)
  571. {
  572. if (offset)
  573. return -PAL_ERROR_INVAL;
  574. if (!IS_HANDLE_TYPE(handle, udp))
  575. return -PAL_ERROR_NOTCONNECTION;
  576. if (handle->sock.fd == PAL_IDX_POISON)
  577. return -PAL_ERROR_BADHANDLE;
  578. if (len >= (1ULL << (sizeof(unsigned int) * 8)))
  579. return -PAL_ERROR_INVAL;
  580. int bytes = ocall_sock_send(handle->sock.fd, buf, len, NULL, 0);
  581. if (IS_ERR(bytes)) {
  582. bytes = unix_to_pal_error(ERRNO(bytes));
  583. if (bytes == -PAL_ERROR_TRYAGAIN)
  584. HANDLE_HDR(handle)->flags &= ~WRITABLE(0);
  585. return bytes;
  586. }
  587. if ((uint64_t)bytes == len)
  588. HANDLE_HDR(handle)->flags |= WRITABLE(0);
  589. else
  590. HANDLE_HDR(handle)->flags &= ~WRITABLE(0);
  591. return bytes;
  592. }
  593. static int64_t udp_sendbyaddr (PAL_HANDLE handle, uint64_t offset, uint64_t len,
  594. const void * buf, const char * addr, size_t addrlen)
  595. {
  596. if (offset)
  597. return -PAL_ERROR_INVAL;
  598. if (!IS_HANDLE_TYPE(handle, udpsrv))
  599. return -PAL_ERROR_NOTCONNECTION;
  600. if (handle->sock.fd == PAL_IDX_POISON)
  601. return -PAL_ERROR_BADHANDLE;
  602. if (!strpartcmp_static(addr, "udp:"))
  603. return -PAL_ERROR_INVAL;
  604. if (len >= (1ULL << (sizeof(unsigned int) * 8)))
  605. return -PAL_ERROR_INVAL;
  606. addr += static_strlen("udp:");
  607. addrlen -= static_strlen("udp:");
  608. char * addrbuf = __alloca(addrlen);
  609. memcpy(addrbuf, addr, addrlen);
  610. struct sockaddr conn_addr;
  611. unsigned int conn_addrlen = sizeof(struct sockaddr);
  612. int ret = inet_parse_uri(&addrbuf, &conn_addr, &conn_addrlen);
  613. if (ret < 0)
  614. return ret;
  615. int bytes = ocall_sock_send(handle->sock.fd, buf, len, &conn_addr,
  616. conn_addrlen);
  617. if (IS_ERR(bytes)) {
  618. bytes = unix_to_pal_error(ERRNO(bytes));
  619. if (bytes == -PAL_ERROR_TRYAGAIN)
  620. HANDLE_HDR(handle)->flags &= ~WRITABLE(0);
  621. return bytes;
  622. }
  623. if ((uint64_t)bytes == len)
  624. HANDLE_HDR(handle)->flags |= WRITABLE(0);
  625. else
  626. HANDLE_HDR(handle)->flags &= ~WRITABLE(0);
  627. return bytes;
  628. }
  629. static int socket_delete (PAL_HANDLE handle, int access)
  630. {
  631. if (handle->sock.fd == PAL_IDX_POISON)
  632. return 0;
  633. if (!IS_HANDLE_TYPE(handle, tcp) && access)
  634. return -PAL_ERROR_INVAL;
  635. if (IS_HANDLE_TYPE(handle, tcp) || IS_HANDLE_TYPE(handle, tcpsrv)) {
  636. int shutdown;
  637. switch (access) {
  638. case 0:
  639. shutdown = SHUT_RDWR;
  640. break;
  641. case PAL_DELETE_RD:
  642. shutdown = SHUT_RD;
  643. break;
  644. case PAL_DELETE_WR:
  645. shutdown = SHUT_WR;
  646. break;
  647. default:
  648. return -PAL_ERROR_INVAL;
  649. }
  650. ocall_sock_shutdown(handle->sock.fd, shutdown);
  651. }
  652. return 0;
  653. }
  654. static int socket_close (PAL_HANDLE handle)
  655. {
  656. if (handle->sock.fd != PAL_IDX_POISON) {
  657. ocall_close(handle->sock.fd);
  658. handle->sock.fd = PAL_IDX_POISON;
  659. }
  660. if (handle->sock.bind)
  661. handle->sock.bind = (PAL_PTR) NULL;
  662. if (handle->sock.conn)
  663. handle->sock.conn = (PAL_PTR) NULL;
  664. return 0;
  665. }
  666. static int socket_attrquerybyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  667. {
  668. if (handle->sock.fd == PAL_IDX_POISON)
  669. return -PAL_ERROR_BADHANDLE;
  670. attr->handle_type = HANDLE_HDR(handle)->type;
  671. attr->disconnected = HANDLE_HDR(handle)->flags & ERROR(0);
  672. attr->nonblocking = handle->sock.nonblocking;
  673. attr->writable = HANDLE_HDR(handle)->flags & WRITABLE(0);
  674. attr->pending_size = 0; /* fill in later */
  675. attr->socket.linger = handle->sock.linger;
  676. attr->socket.receivebuf = handle->sock.receivebuf;
  677. attr->socket.sendbuf = handle->sock.sendbuf;
  678. attr->socket.receivetimeout = handle->sock.receivetimeout;
  679. attr->socket.sendtimeout = handle->sock.sendtimeout;
  680. attr->socket.tcp_cork = handle->sock.tcp_cork;
  681. attr->socket.tcp_keepalive = handle->sock.tcp_keepalive;
  682. attr->socket.tcp_nodelay = handle->sock.tcp_nodelay;
  683. int fd = handle->sock.fd, ret;
  684. if (!IS_HANDLE_TYPE(handle, tcpsrv)) {
  685. /* try use ioctl FIONEAD to get the size of socket */
  686. ret = ocall_fionread(fd);
  687. if (IS_ERR(ret))
  688. return unix_to_pal_error(ERRNO(ret));
  689. attr->pending_size = ret;
  690. }
  691. struct pollfd pfd = { .fd = fd, .events = POLLIN, .revents = 0 };
  692. int64_t waittime = 0;
  693. ret = ocall_poll(&pfd, 1, &waittime);
  694. if (IS_ERR(ret))
  695. return unix_to_pal_error(ERRNO(ret));
  696. attr->readable = (ret == 1 && pfd.revents == POLLIN);
  697. return 0;
  698. }
  699. static int socket_attrsetbyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  700. {
  701. if (handle->sock.fd == PAL_IDX_POISON)
  702. return -PAL_ERROR_BADHANDLE;
  703. int fd = handle->sock.fd, ret, val;
  704. if (attr->nonblocking != handle->sock.nonblocking) {
  705. ret = ocall_fsetnonblock(fd, attr->nonblocking);
  706. if (IS_ERR(ret))
  707. return unix_to_pal_error(ERRNO(ret));
  708. handle->sock.nonblocking = attr->nonblocking;
  709. }
  710. if (HANDLE_TYPE(handle) != pal_type_tcpsrv) {
  711. struct __kernel_linger {
  712. int l_onoff;
  713. int l_linger;
  714. };
  715. if (attr->socket.linger != handle->sock.linger) {
  716. struct __kernel_linger l;
  717. l.l_onoff = attr->socket.linger ? 1 : 0;
  718. l.l_linger = attr->socket.linger;
  719. ret = ocall_sock_setopt(fd, SOL_SOCKET, SO_LINGER, &l,
  720. sizeof(struct __kernel_linger));
  721. if (IS_ERR(ret))
  722. return unix_to_pal_error(ERRNO(ret));
  723. handle->sock.linger = attr->socket.linger;
  724. }
  725. if (attr->socket.receivebuf != handle->sock.receivebuf) {
  726. val = attr->socket.receivebuf;
  727. ret = ocall_sock_setopt(fd, SOL_SOCKET, SO_RCVBUF, &val,
  728. sizeof(int));
  729. if (IS_ERR(ret))
  730. return unix_to_pal_error(ERRNO(ret));
  731. handle->sock.receivebuf = attr->socket.receivebuf;
  732. }
  733. if (attr->socket.sendbuf != handle->sock.sendbuf) {
  734. val = attr->socket.sendbuf;
  735. ret = ocall_sock_setopt(fd, SOL_SOCKET, SO_SNDBUF,
  736. &val, sizeof(int));
  737. if (IS_ERR(ret))
  738. return unix_to_pal_error(ERRNO(ret));
  739. handle->sock.sendbuf = attr->socket.sendbuf;
  740. }
  741. if (attr->socket.receivetimeout != handle->sock.receivetimeout) {
  742. val = attr->socket.receivetimeout;
  743. ret = ocall_sock_setopt(fd, SOL_SOCKET, SO_RCVTIMEO,
  744. &val, sizeof(int));
  745. if (IS_ERR(ret))
  746. return unix_to_pal_error(ERRNO(ret));
  747. handle->sock.receivetimeout = attr->socket.receivetimeout;
  748. }
  749. if (attr->socket.sendtimeout != handle->sock.sendtimeout) {
  750. val = attr->socket.sendtimeout;
  751. ret = ocall_sock_setopt(fd, SOL_SOCKET, SO_SNDTIMEO,
  752. &val, sizeof(int));
  753. if (IS_ERR(ret))
  754. return unix_to_pal_error(ERRNO(ret));
  755. handle->sock.sendtimeout = attr->socket.sendtimeout;
  756. }
  757. }
  758. if (HANDLE_TYPE(handle) == pal_type_tcp ||
  759. HANDLE_TYPE(handle) == pal_type_tcpsrv) {
  760. if (attr->socket.tcp_cork != handle->sock.tcp_cork) {
  761. val = attr->socket.tcp_cork ? 1 : 0;
  762. ret = ocall_sock_setopt(fd, SOL_TCP, TCP_CORK,
  763. &val, sizeof(int));
  764. if (IS_ERR(ret))
  765. return unix_to_pal_error(ERRNO(ret));
  766. handle->sock.tcp_cork = attr->socket.tcp_cork;
  767. }
  768. if (attr->socket.tcp_keepalive != handle->sock.tcp_keepalive) {
  769. val = attr->socket.tcp_keepalive ? 1 : 0;
  770. ret = ocall_sock_setopt(fd, SOL_SOCKET, SO_KEEPALIVE,
  771. &val, sizeof(int));
  772. if (IS_ERR(ret))
  773. return unix_to_pal_error(ERRNO(ret));
  774. handle->sock.tcp_keepalive = attr->socket.tcp_keepalive;
  775. }
  776. if (attr->socket.tcp_nodelay != handle->sock.tcp_nodelay) {
  777. val = attr->socket.tcp_nodelay ? 1 : 0;
  778. ret = ocall_sock_setopt(fd, SOL_TCP, TCP_NODELAY,
  779. &val, sizeof(int));
  780. if (IS_ERR(ret))
  781. return unix_to_pal_error(ERRNO(ret));
  782. handle->sock.tcp_nodelay = attr->socket.tcp_nodelay;
  783. }
  784. }
  785. return 0;
  786. }
  787. static int socket_getname (PAL_HANDLE handle, char * buffer, size_t count)
  788. {
  789. int old_count = count;
  790. int ret;
  791. const char * prefix = NULL;
  792. size_t prefix_len = 0;
  793. struct sockaddr * bind_addr = NULL, * dest_addr = NULL;
  794. switch (HANDLE_TYPE(handle)) {
  795. case pal_type_tcpsrv:
  796. prefix_len = static_strlen("tcp.srv");
  797. prefix = "tcp.srv";
  798. bind_addr = (struct sockaddr *) handle->sock.bind;
  799. break;
  800. case pal_type_tcp:
  801. prefix_len = static_strlen("tcp");
  802. prefix = "tcp";
  803. bind_addr = (struct sockaddr *) handle->sock.bind;
  804. dest_addr = (struct sockaddr *) handle->sock.conn;
  805. break;
  806. case pal_type_udpsrv:
  807. prefix_len = static_strlen("udp.srv");
  808. prefix = "udp.srv";
  809. bind_addr = (struct sockaddr *) handle->sock.bind;
  810. break;
  811. case pal_type_udp:
  812. prefix_len = static_strlen("udp");
  813. prefix = "udp";
  814. bind_addr = (struct sockaddr *) handle->sock.bind;
  815. dest_addr = (struct sockaddr *) handle->sock.conn;
  816. break;
  817. default:
  818. return -PAL_ERROR_INVAL;
  819. }
  820. if (prefix_len >= count)
  821. return -PAL_ERROR_OVERFLOW;
  822. memcpy(buffer, prefix, prefix_len + 1);
  823. buffer += prefix_len;
  824. count -= prefix_len;
  825. for (int i = 0 ; i < 2 ; i++) {
  826. struct sockaddr * addr = i ? dest_addr : bind_addr;
  827. if (addr) {
  828. if (count <= 1)
  829. return -PAL_ERROR_OVERFLOW;
  830. buffer[0] = ':';
  831. buffer[1] = 0;
  832. buffer++;
  833. count--;
  834. if ((ret = inet_create_uri(buffer, count, addr,
  835. addr_size(addr))) < 0)
  836. return ret;
  837. buffer += ret;
  838. count -= ret;
  839. }
  840. }
  841. return old_count - count;
  842. }
  843. struct handle_ops tcp_ops = {
  844. .getname = &socket_getname,
  845. .open = &tcp_open,
  846. .waitforclient = &tcp_accept,
  847. .read = &tcp_read,
  848. .write = &tcp_write,
  849. .delete = &socket_delete,
  850. .close = &socket_close,
  851. .attrquerybyhdl = &socket_attrquerybyhdl,
  852. .attrsetbyhdl = &socket_attrsetbyhdl,
  853. };
  854. struct handle_ops udp_ops = {
  855. .getname = &socket_getname,
  856. .open = &udp_open,
  857. .read = &udp_receive,
  858. .write = &udp_send,
  859. .delete = &socket_delete,
  860. .close = &socket_close,
  861. .attrquerybyhdl = &socket_attrquerybyhdl,
  862. .attrsetbyhdl = &socket_attrsetbyhdl,
  863. };
  864. struct handle_ops udpsrv_ops = {
  865. .getname = &socket_getname,
  866. .open = &udp_open,
  867. .readbyaddr = &udp_receivebyaddr,
  868. .writebyaddr = &udp_sendbyaddr,
  869. .delete = &socket_delete,
  870. .close = &socket_close,
  871. .attrquerybyhdl = &socket_attrquerybyhdl,
  872. .attrsetbyhdl = &socket_attrsetbyhdl,
  873. };
  874. PAL_HANDLE _DkBroadcastStreamOpen (void)
  875. {
  876. PAL_HANDLE hdl = malloc(HANDLE_SIZE(file));
  877. SET_HANDLE_TYPE(hdl, mcast);
  878. HANDLE_HDR(hdl)->flags |= RFD(0)|WFD(1)|WRITABLE(1);
  879. hdl->mcast.port = pal_sec.mcast_port;
  880. hdl->mcast.srv = pal_sec.mcast_srv;
  881. hdl->mcast.cli = pal_sec.mcast_cli;
  882. return hdl;
  883. }
  884. static int64_t mcast_send (PAL_HANDLE handle, uint64_t offset, uint64_t size,
  885. const void * buf)
  886. {
  887. if (offset)
  888. return -PAL_ERROR_INVAL;
  889. if (handle->mcast.srv == PAL_IDX_POISON)
  890. return -PAL_ERROR_BADHANDLE;
  891. if (size >= (1ULL << (sizeof(unsigned int) * 8)))
  892. return -PAL_ERROR_INVAL;
  893. int bytes = ocall_sock_send(handle->mcast.srv, buf, size,
  894. NULL, 0);
  895. if (IS_ERR(bytes)) {
  896. bytes = unix_to_pal_error(ERRNO(bytes));
  897. if (bytes == -PAL_ERROR_TRYAGAIN)
  898. HANDLE_HDR(handle)->flags &= ~WRITABLE(1);
  899. return bytes;
  900. }
  901. if ((uint64_t)bytes == size)
  902. HANDLE_HDR(handle)->flags |= WRITABLE(1);
  903. else
  904. HANDLE_HDR(handle)->flags &= ~WRITABLE(1);
  905. return bytes;
  906. }
  907. static int64_t mcast_receive (PAL_HANDLE handle, uint64_t offset, uint64_t size,
  908. void * buf)
  909. {
  910. if (offset)
  911. return -PAL_ERROR_INVAL;
  912. if (handle->mcast.cli == PAL_IDX_POISON)
  913. return -PAL_ERROR_BADHANDLE;
  914. if (size >= (1ULL << (sizeof(unsigned int) * 8)))
  915. return -PAL_ERROR_INVAL;
  916. int bytes = ocall_sock_recv(handle->mcast.cli, buf, size, NULL,
  917. NULL);
  918. if (IS_ERR(bytes))
  919. bytes = unix_to_pal_error(ERRNO(bytes));
  920. if (bytes == -PAL_ERROR_TRYAGAIN)
  921. HANDLE_HDR(handle)->flags &= ~WRITABLE(1);
  922. return bytes;
  923. }
  924. static int mcast_close (PAL_HANDLE handle)
  925. {
  926. if (handle->mcast.srv != PAL_IDX_POISON) {
  927. ocall_close(handle->mcast.srv);
  928. handle->mcast.srv = PAL_IDX_POISON;
  929. }
  930. if (handle->mcast.cli != PAL_IDX_POISON) {
  931. ocall_close(handle->mcast.cli);
  932. handle->mcast.cli = PAL_IDX_POISON;
  933. }
  934. return 0;
  935. }
  936. static int mcast_attrquerybyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  937. {
  938. if (handle->mcast.cli == PAL_IDX_POISON)
  939. return -PAL_ERROR_BADHANDLE;
  940. int ret = ocall_fionread(handle->mcast.cli);
  941. if (IS_ERR(ret))
  942. return unix_to_pal_error(ERRNO(ret));
  943. memset(attr, 0, sizeof(PAL_STREAM_ATTR));
  944. attr->pending_size = ret;
  945. attr->disconnected = HANDLE_HDR(handle)->flags & (ERROR(0)|ERROR(1));
  946. attr->readable = (attr->pending_size > 0);
  947. attr->writable = HANDLE_HDR(handle)->flags & WRITABLE(1);
  948. attr->nonblocking = handle->mcast.nonblocking;
  949. return 0;
  950. }
  951. static int mcast_attrsetbyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  952. {
  953. if (handle->mcast.cli == PAL_IDX_POISON)
  954. return -PAL_ERROR_BADHANDLE;
  955. PAL_BOL * nonblocking = &handle->mcast.nonblocking;
  956. if (attr->nonblocking != *nonblocking) {
  957. int ret = ocall_fsetnonblock(handle->mcast.cli, *nonblocking);
  958. if (IS_ERR(ret))
  959. return unix_to_pal_error(ERRNO(ret));
  960. *nonblocking = attr->nonblocking;
  961. }
  962. return 0;
  963. }
  964. struct handle_ops mcast_ops = {
  965. .write = &mcast_send,
  966. .read = &mcast_receive,
  967. .close = &mcast_close,
  968. .attrquerybyhdl = &mcast_attrquerybyhdl,
  969. .attrsetbyhdl = &mcast_attrsetbyhdl,
  970. };