| 604 | } |
| 605 | |
| 606 | void xs_socket_write(xsMachine *the) |
| 607 | { |
| 608 | xsSocket xss = xsmcGetHostDataValidate(xsThis, xs_socket_destructor); |
| 609 | int argc = xsmcArgc; |
| 610 | char *msg; |
| 611 | xsUnsignedValue msgLen; |
| 612 | u16_t available, needed = 0; |
| 613 | err_t err; |
| 614 | unsigned char pass, arg; |
| 615 | char addr[64]; |
| 616 | |
| 617 | if (xss->suspended) |
| 618 | xsUnknownError("suspended"); |
| 619 | |
| 620 | if ((NULL == xss) || !(xss->skt || xss->udp || xss->raw) || (xss->pending & (kPendingError | kPendingDisconnect))) { |
| 621 | if (0 == argc) { |
| 622 | xsResult = xsInteger(0); |
| 623 | return; |
| 624 | } |
| 625 | xsTrace("write on closed socket\n"); |
| 626 | return; |
| 627 | } |
| 628 | |
| 629 | if (xss->udp) { |
| 630 | unsigned char *data; |
| 631 | xsUnsignedValue needed; |
| 632 | uint16 port = xsmcToInteger(xsArg(1)); |
| 633 | ip_addr_t dst; |
| 634 | |
| 635 | if (!ipaddr_aton(xsmcToStringBuffer(xsArg(0), addr, sizeof(addr)), &dst)) |
| 636 | xsUnknownError("invalid IP address"); |
| 637 | |
| 638 | xsmcGetBufferReadable(xsArg(2), (void **)&data, &needed); |
| 639 | udp_sendto_safe(xss->udp, data, (uint16_t)needed, &dst, port, &err); |
| 640 | if (ERR_OK != err) { |
| 641 | xsLog("UDP send error %d\n", err); |
| 642 | xsUnknownError("UDP send failed"); |
| 643 | } |
| 644 | |
| 645 | modInstrumentationAdjust(NetworkBytesWritten, needed); |
| 646 | return; |
| 647 | } |
| 648 | |
| 649 | if (xss->raw) { |
| 650 | unsigned char *data; |
| 651 | xsUnsignedValue needed; |
| 652 | ip_addr_t dst = {0}; |
| 653 | struct pbuf *p; |
| 654 | |
| 655 | if (!ipaddr_aton(xsmcToStringBuffer(xsArg(0), addr, sizeof(addr)), &dst)) |
| 656 | xsUnknownError("invalid IP address"); |
| 657 | |
| 658 | xsmcGetBufferReadable(xsArg(1), (void **)&data, &needed); |
| 659 | p = pbuf_alloc(PBUF_TRANSPORT, (u16_t)needed, PBUF_RAM); |
| 660 | if (!p) |
| 661 | xsUnknownError("no buffer"); |
| 662 | c_memcpy(p->payload, data, needed); |
| 663 | err = raw_sendto(xss->raw, p, &dst); //@@ safe version? |
nothing calls this directly
no test coverage detected