From: David van Moolenbroek Date: Fri, 21 Aug 2009 09:59:09 +0000 (+0000) Subject: support for TCP sockets in send/sendto/recv/recvfrom X-Git-Tag: v3.1.5~176 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zpipe.c?a=commitdiff_plain;h=47e6506d7bdeb1292e515f1f986b5078832013b5;p=minix.git support for TCP sockets in send/sendto/recv/recvfrom --- diff --git a/lib/ip/recvfrom.c b/lib/ip/recvfrom.c index 23ffd285a..9018c817a 100644 --- a/lib/ip/recvfrom.c +++ b/lib/ip/recvfrom.c @@ -11,12 +11,17 @@ #include #include +#include +#include #include #include #include #define DEBUG 0 +static ssize_t _tcp_recvfrom(int socket, void *_RESTRICT buffer, size_t length, + int flags, struct sockaddr *_RESTRICT address, + socklen_t *_RESTRICT address_len, nwio_tcpconf_t *tcpconfp); static ssize_t _udp_recvfrom(int socket, void *_RESTRICT buffer, size_t length, int flags, struct sockaddr *_RESTRICT address, socklen_t *_RESTRICT address_len, nwio_udpopt_t *udpoptp); @@ -26,12 +31,22 @@ ssize_t recvfrom(int socket, void *_RESTRICT buffer, size_t length, socklen_t *_RESTRICT address_len) { int r; + nwio_tcpconf_t tcpconf; nwio_udpopt_t udpopt; #if DEBUG fprintf(stderr, "recvfrom: for fd %d\n", socket); #endif + r= ioctl(socket, NWIOGTCPCONF, &tcpconf); + if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL)) + { + if (r == -1) + return r; + return _tcp_recvfrom(socket, buffer, length, flags, + address, address_len, &tcpconf); + } + r= ioctl(socket, NWIOGUDPOPT, &udpopt); if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL)) { @@ -49,6 +64,40 @@ ssize_t recvfrom(int socket, void *_RESTRICT buffer, size_t length, return -1; } +static ssize_t _tcp_recvfrom(int socket, void *_RESTRICT buffer, size_t length, + int flags, struct sockaddr *_RESTRICT address, + socklen_t *_RESTRICT address_len, nwio_tcpconf_t *tcpconfp) +{ + int r; + size_t len; + struct sockaddr_in sin; + + if (flags != 0) + { +#if DEBUG + fprintf(stderr, "recvfrom(tcp): flags not implemented\n"); +#endif + errno= ENOSYS; + return -1; + } + + r = read(socket, buffer, length); + + if (r >= 0 && address != NULL) + { + sin.sin_family= AF_INET; + sin.sin_addr.s_addr= tcpconfp->nwtc_remaddr; + sin.sin_port= tcpconfp->nwtc_remport; + len= *address_len; + if (len > sizeof(sin)) + len= sizeof(sin); + memcpy(address, &sin, len); + *address_len= sizeof(sin); + } + + return r; +} + static ssize_t _udp_recvfrom(int socket, void *_RESTRICT buffer, size_t length, int flags, struct sockaddr *_RESTRICT address, socklen_t *_RESTRICT address_len, nwio_udpopt_t *udpoptp) diff --git a/lib/ip/sendto.c b/lib/ip/sendto.c index 6c805ffce..193bf61a7 100644 --- a/lib/ip/sendto.c +++ b/lib/ip/sendto.c @@ -11,12 +11,16 @@ #include #include +#include +#include #include #include #include #define DEBUG 0 +static ssize_t _tcp_sendto(int socket, const void *message, size_t length, + int flags, const struct sockaddr *dest_addr, socklen_t dest_len); static ssize_t _udp_sendto(int socket, const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len, nwio_udpopt_t *udpoptp); @@ -25,8 +29,18 @@ ssize_t sendto(int socket, const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len) { int r; + nwio_tcpopt_t tcpopt; nwio_udpopt_t udpopt; + r= ioctl(socket, NWIOGTCPOPT, &tcpopt); + if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL)) + { + if (r == -1) + return r; + return _tcp_sendto(socket, message, length, flags, + dest_addr, dest_len); + } + r= ioctl(socket, NWIOGUDPOPT, &udpopt); if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL)) { @@ -43,6 +57,23 @@ ssize_t sendto(int socket, const void *message, size_t length, int flags, return -1; } +static ssize_t _tcp_sendto(int socket, const void *message, size_t length, + int flags, const struct sockaddr *dest_addr, socklen_t dest_len) +{ + + if (flags != 0) { +#if DEBUG + fprintf(stderr, "sendto(tcp): flags not implemented\n"); +#endif + errno= ENOSYS; + return -1; + } + + /* Silently ignore destination, if given. */ + + return write(socket, message, length); +} + static ssize_t _udp_sendto(int socket, const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len, nwio_udpopt_t *udpoptp)