1 #include <sys/cdefs.h> 2 #include "namespace.h" 3 4 #undef NDEBUG 5 6 #include <assert.h> 7 #include <errno.h> 8 #include <stdlib.h> 9 #include <stdio.h> 10 #include <string.h> 11 #include <unistd.h> 12 #include <sys/ioctl.h> 13 #include <sys/socket.h> 14 #include <netinet/in.h> 15 16 #include <net/gen/in.h> 17 #include <net/gen/tcp.h> 18 #include <net/gen/tcp_io.h> 19 #include <net/gen/udp.h> 20 #include <net/gen/udp_hdr.h> 21 #include <net/gen/udp_io.h> 22 23 #define DEBUG 0 24 25 static ssize_t _tcp_recvfrom(int sock, void *__restrict buffer, size_t length, 26 int flags, struct sockaddr *__restrict address, 27 socklen_t *__restrict address_len, nwio_tcpconf_t *tcpconfp); 28 static ssize_t _udp_recvfrom(int sock, void *__restrict buffer, size_t length, 29 int flags, struct sockaddr *__restrict address, 30 socklen_t *__restrict address_len, nwio_udpopt_t *udpoptp); 31 static ssize_t _uds_recvfrom_conn(int sock, void *__restrict buffer, 32 size_t length, int flags, struct sockaddr *__restrict address, 33 socklen_t *__restrict address_len, struct sockaddr_un *uds_addr); 34 static ssize_t _uds_recvfrom_dgram(int sock, void *__restrict buffer, 35 size_t length, int flags, struct sockaddr *__restrict address, 36 socklen_t *__restrict address_len); 37 38 ssize_t recvfrom(int sock, void *__restrict buffer, size_t length, 39 int flags, struct sockaddr *__restrict address, 40 socklen_t *__restrict address_len) 41 { 42 int r; 43 nwio_tcpconf_t tcpconf; 44 nwio_udpopt_t udpopt; 45 struct sockaddr_un uds_addr; 46 int uds_sotype = -1; 47 48 #if DEBUG 49 fprintf(stderr, "recvfrom: for fd %d\n", sock); 50 #endif 51 52 r= ioctl(sock, NWIOGTCPCONF, &tcpconf); 53 if (r != -1 || errno != ENOTTY) 54 { 55 if (r == -1) 56 return r; 57 return _tcp_recvfrom(sock, buffer, length, flags, 58 address, address_len, &tcpconf); 59 } 60 61 r= ioctl(sock, NWIOGUDPOPT, &udpopt); 62 if (r != -1 || errno != ENOTTY) 63 { 64 if (r == -1) 65 return r; 66 return _udp_recvfrom(sock, buffer, length, flags, 67 address, address_len, &udpopt); 68 } 69 70 r= ioctl(sock, NWIOGUDSSOTYPE, &uds_sotype); 71 if (r != -1 || errno != ENOTTY) 72 { 73 74 if (r == -1) { 75 return r; 76 } 77 78 if (uds_sotype == SOCK_DGRAM) { 79 return _uds_recvfrom_dgram(sock, buffer, 80 length, flags, address, address_len); 81 } else { 82 return _uds_recvfrom_conn(sock, buffer, 83 length, flags, address, address_len, 84 &uds_addr); 85 } 86 } 87 88 #if DEBUG 89 fprintf(stderr, "recvfrom: not implemented for fd %d\n", sock); 90 #endif 91 errno= ENOSYS; 92 assert(0); 93 return -1; 94 } 95 96 static ssize_t _tcp_recvfrom(int sock, void *__restrict buffer, size_t length, 97 int flags, struct sockaddr *__restrict address, 98 socklen_t *__restrict address_len, nwio_tcpconf_t *tcpconfp) 99 { 100 int r; 101 size_t len; 102 struct sockaddr_in sin; 103 104 if (flags != 0) 105 { 106 #if DEBUG 107 fprintf(stderr, "recvfrom(tcp): flags not implemented\n"); 108 #endif 109 errno= ENOSYS; 110 return -1; 111 } 112 113 r = read(sock, buffer, length); 114 115 if (r >= 0 && address != NULL) 116 { 117 sin.sin_family= AF_INET; 118 sin.sin_addr.s_addr= tcpconfp->nwtc_remaddr; 119 sin.sin_port= tcpconfp->nwtc_remport; 120 sin.sin_len= sizeof(sin); 121 len= *address_len; 122 if (len > sizeof(sin)) 123 len= sizeof(sin); 124 memcpy(address, &sin, len); 125 *address_len= sizeof(sin); 126 } 127 128 return r; 129 } 130 131 static ssize_t _udp_recvfrom(int sock, void *__restrict buffer, size_t length, 132 int flags, struct sockaddr *__restrict address, 133 socklen_t *__restrict address_len, nwio_udpopt_t *udpoptp) 134 { 135 int r, t_errno; 136 size_t buflen, len; 137 void *buf; 138 udp_io_hdr_t *io_hdrp; 139 struct sockaddr_in sin; 140 141 if (flags) 142 { 143 #if DEBUG 144 fprintf(stderr, "recvfrom(udp): flags not implemented\n"); 145 #endif 146 errno= ENOSYS; 147 return -1; 148 } 149 150 if (udpoptp->nwuo_flags & NWUO_RWDATONLY) 151 { 152 if (address != NULL && 153 (udpoptp->nwuo_flags & (NWUO_RA_SET | NWUO_RP_SET)) != 154 (NWUO_RA_SET | NWUO_RP_SET)) 155 { 156 157 #if DEBUG 158 fprintf(stderr, 159 "recvfrom(udp): RWDATONLY on unconnected socket\n"); 160 #endif 161 errno= ENOTCONN; 162 return -1; 163 } 164 165 r= read(sock, buffer, length); 166 if (r == -1) 167 return r; 168 169 if (address != NULL) 170 { 171 sin.sin_family= AF_INET; 172 sin.sin_addr.s_addr= udpoptp->nwuo_remaddr; 173 sin.sin_port= udpoptp->nwuo_remport; 174 sin.sin_len= sizeof(sin); 175 len= *address_len; 176 if (len > sizeof(sin)) 177 len= sizeof(sin); 178 memcpy(address, &sin, len); 179 *address_len= sizeof(sin); 180 } 181 182 return r; 183 } 184 185 buflen= sizeof(*io_hdrp) + length; 186 if (buflen < length) 187 { 188 /* Overflow */ 189 errno= EMSGSIZE; 190 return -1; 191 } 192 buf= malloc(buflen); 193 if (buf == NULL) 194 return -1; 195 196 r= read(sock, buf, buflen); 197 if (r == -1) 198 { 199 t_errno= errno; 200 #if DEBUG 201 fprintf(stderr, "recvfrom(udp): read failed: %s\n", 202 strerror(errno)); 203 fprintf(stderr, "udp opt flags = 0x%x\n", udpoptp->nwuo_flags); 204 #endif 205 free(buf); 206 errno= t_errno; 207 return -1; 208 } 209 210 assert(r >= sizeof(*io_hdrp)); 211 length= r-sizeof(*io_hdrp); 212 213 io_hdrp= buf; 214 memcpy(buffer, &io_hdrp[1], length); 215 216 if (address != NULL) 217 { 218 sin.sin_family= AF_INET; 219 sin.sin_addr.s_addr= io_hdrp->uih_src_addr; 220 sin.sin_port= io_hdrp->uih_src_port; 221 sin.sin_len= sizeof(sin); 222 len= *address_len; 223 if (len > sizeof(sin)) 224 len= sizeof(sin); 225 memcpy(address, &sin, len); 226 *address_len= sizeof(sin); 227 } 228 free(buf); 229 return length; 230 } 231 232 static ssize_t _uds_recvfrom_conn(int sock, void *__restrict buffer, 233 size_t length, int flags, struct sockaddr *__restrict address, 234 socklen_t *__restrict address_len, struct sockaddr_un *uds_addr) 235 { 236 int r; 237 size_t len; 238 239 /* for connection oriented unix domain sockets (SOCK_STREAM / 240 * SOCK_SEQPACKET) 241 */ 242 243 if (flags != 0) 244 { 245 #if DEBUG 246 fprintf(stderr, "recvfrom(uds): flags not implemented\n"); 247 #endif 248 errno= ENOSYS; 249 return -1; 250 } 251 252 r = read(sock, buffer, length); 253 254 if (r >= 0 && address != NULL) 255 { 256 257 len= *address_len; 258 if (len > sizeof(struct sockaddr_un)) 259 len= sizeof(struct sockaddr_un); 260 memcpy(address, uds_addr, len); 261 *address_len= sizeof(struct sockaddr_un); 262 } 263 264 return r; 265 } 266 267 static ssize_t _uds_recvfrom_dgram(int sock, void *__restrict buffer, 268 size_t length, int flags, struct sockaddr *__restrict address, 269 socklen_t *__restrict address_len) 270 { 271 int r; 272 size_t len; 273 274 /* for connectionless unix domain sockets (SOCK_DGRAM) */ 275 276 if (flags != 0) 277 { 278 #if DEBUG 279 fprintf(stderr, "recvfrom(uds): flags not implemented\n"); 280 #endif 281 errno= ENOSYS; 282 return -1; 283 } 284 285 r = read(sock, buffer, length); 286 287 if (r >= 0 && address != NULL) 288 { 289 len= *address_len; 290 if (len > sizeof(struct sockaddr_un)) 291 len= sizeof(struct sockaddr_un); 292 ioctl(sock, NWIOGUDSFADDR, address); 293 *address_len= sizeof(struct sockaddr_un); 294 } 295 296 return r; 297 } 298 299