Skip to content
This repository
Browse code

unix: support sending uv_udp_t handles over ipc pipes

  • Loading branch information...
commit e4d8cbac7875449a6c4cf65d98d621738fbc21e6 1 parent cf1dc61
Bert Belder piscisaureus authored

Showing 1 changed file with 56 additions and 13 deletions. Show diff stats Hide diff stats

  1. +56 13 src/unix/stream.c
69 src/unix/stream.c
@@ -576,12 +576,28 @@ int uv_accept(uv_stream_t* server, uv_stream_t* client) {
576 576 goto out;
577 577 }
578 578
579   - if (uv__stream_open(streamClient, streamServer->accepted_fd,
580   - UV_STREAM_READABLE | UV_STREAM_WRITABLE)) {
581   - /* TODO handle error */
582   - close(streamServer->accepted_fd);
583   - streamServer->accepted_fd = -1;
584   - goto out;
  579 + switch (streamClient->type) {
  580 + case UV_NAMED_PIPE:
  581 + case UV_TCP:
  582 + if (uv__stream_open(streamClient, streamServer->accepted_fd,
  583 + UV_STREAM_READABLE | UV_STREAM_WRITABLE)) {
  584 + /* TODO handle error */
  585 + close(streamServer->accepted_fd);
  586 + streamServer->accepted_fd = -1;
  587 + goto out;
  588 + }
  589 + break;
  590 +
  591 + case UV_UDP:
  592 + if (uv_udp_open((uv_udp_t*) client, streamServer->accepted_fd)) {
  593 + close(streamServer->accepted_fd);
  594 + streamServer->accepted_fd = -1;
  595 + goto out;
  596 + }
  597 + break;
  598 +
  599 + default:
  600 + assert(0);
585 601 }
586 602
587 603 uv__io_start(streamServer->loop, &streamServer->io_watcher, UV__POLLIN);
@@ -682,6 +698,21 @@ static void uv__write_req_finish(uv_write_t* req) {
682 698 }
683 699
684 700
  701 +static int uv__handle_fd(uv_handle_t* handle) {
  702 + switch (handle->type) {
  703 + case UV_NAMED_PIPE:
  704 + case UV_TCP:
  705 + return ((uv_stream_t*) handle)->io_watcher.fd;
  706 +
  707 + case UV_UDP:
  708 + return ((uv_udp_t*) handle)->io_watcher.fd;
  709 +
  710 + default:
  711 + return -1;
  712 + }
  713 +}
  714 +
  715 +
685 716 static void uv__write(uv_stream_t* stream) {
686 717 struct iovec* iov;
687 718 ngx_queue_t* q;
@@ -719,7 +750,7 @@ static void uv__write(uv_stream_t* stream) {
719 750 struct msghdr msg;
720 751 char scratch[64];
721 752 struct cmsghdr *cmsg;
722   - int fd_to_send = req->send_handle->io_watcher.fd;
  753 + int fd_to_send = uv__handle_fd((uv_handle_t*) req->send_handle);
723 754
724 755 assert(fd_to_send >= 0);
725 756
@@ -858,6 +889,7 @@ static void uv__write_callbacks(uv_stream_t* stream) {
858 889 static uv_handle_type uv__handle_type(int fd) {
859 890 struct sockaddr_storage ss;
860 891 socklen_t len;
  892 + int type;
861 893
862 894 memset(&ss, 0, sizeof(ss));
863 895 len = sizeof(ss);
@@ -865,14 +897,25 @@ static uv_handle_type uv__handle_type(int fd) {
865 897 if (getsockname(fd, (struct sockaddr*)&ss, &len))
866 898 return UV_UNKNOWN_HANDLE;
867 899
868   - switch (ss.ss_family) {
869   - case AF_UNIX:
870   - return UV_NAMED_PIPE;
871   - case AF_INET:
872   - case AF_INET6:
873   - return UV_TCP;
  900 + len = sizeof type;
  901 +
  902 + if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &len))
  903 + return UV_UNKNOWN_HANDLE;
  904 +
  905 + if (type == SOCK_STREAM) {
  906 + switch (ss.ss_family) {
  907 + case AF_UNIX:
  908 + return UV_NAMED_PIPE;
  909 + case AF_INET:
  910 + case AF_INET6:
  911 + return UV_TCP;
  912 + }
874 913 }
875 914
  915 + if (type == SOCK_DGRAM &&
  916 + (ss.ss_family == AF_INET || ss.ss_family == AF_INET6))
  917 + return UV_UDP;
  918 +
876 919 return UV_UNKNOWN_HANDLE;
877 920 }
878 921

0 comments on commit e4d8cba

Please sign in to comment.
Something went wrong with that request. Please try again.