1 #include <sys/cdefs.h> 2 #include "namespace.h" 3 4 #include <assert.h> 5 #include <errno.h> 6 #include <stdlib.h> 7 #include <stdio.h> 8 #include <string.h> 9 #include <unistd.h> 10 #include <sys/ioctl.h> 11 #include <sys/socket.h> 12 #include <netinet/in.h> 13 14 #include <net/gen/in.h> 15 #include <net/gen/tcp.h> 16 #include <net/gen/tcp_io.h> 17 #include <net/gen/udp.h> 18 #include <net/gen/udp_hdr.h> 19 #include <net/gen/udp_io.h> 20 21 #include <net/gen/ip_hdr.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 { 89 ip_hdr_t *ip_hdr; 90 int rd; 91 struct sockaddr_in sin; 92 93 rd = read(sock, buffer, length); 94 95 if(rd < 0) return rd; 96 97 assert((size_t)rd >= sizeof(*ip_hdr)); 98 99 ip_hdr= buffer; 100 101 if (address != NULL) 102 { 103 int len; 104 memset(&sin, 0, sizeof(sin)); 105 sin.sin_family= AF_INET; 106 sin.sin_addr.s_addr= ip_hdr->ih_src; 107 sin.sin_len= sizeof(sin); 108 len= *address_len; 109 if ((size_t)len > sizeof(sin)) 110 len= (int)sizeof(sin); 111 memcpy(address, &sin, len); 112 *address_len= sizeof(sin); 113 } 114 115 return rd; 116 } 117 118 #if DEBUG 119 fprintf(stderr, "recvfrom: not implemented for fd %d\n", sock); 120 #endif 121 abort(); 122 } 123 124 static ssize_t _tcp_recvfrom(int sock, void *__restrict buffer, size_t length, 125 int flags, struct sockaddr *__restrict address, 126 socklen_t *__restrict address_len, nwio_tcpconf_t *tcpconfp) 127 { 128 int r; 129 size_t len; 130 struct sockaddr_in sin; 131 132 if (flags != 0) 133 { 134 #if DEBUG 135 fprintf(stderr, "recvfrom(tcp): flags not implemented\n"); 136 #endif 137 errno= ENOSYS; 138 return -1; 139 } 140 141 r = read(sock, buffer, length); 142 143 if (r >= 0 && address != NULL) 144 { 145 sin.sin_family= AF_INET; 146 sin.sin_addr.s_addr= tcpconfp->nwtc_remaddr; 147 sin.sin_port= tcpconfp->nwtc_remport; 148 sin.sin_len= sizeof(sin); 149 len= *address_len; 150 if (len > sizeof(sin)) 151 len= sizeof(sin); 152 memcpy(address, &sin, len); 153 *address_len= sizeof(sin); 154 } 155 156 return r; 157 } 158 159 static ssize_t _udp_recvfrom(int sock, void *__restrict buffer, size_t length, 160 int flags, struct sockaddr *__restrict address, 161 socklen_t *__restrict address_len, nwio_udpopt_t *udpoptp) 162 { 163 int r, t_errno; 164 size_t buflen, len; 165 void *buf; 166 udp_io_hdr_t *io_hdrp; 167 struct sockaddr_in sin; 168 169 if (flags) 170 { 171 #if DEBUG 172 fprintf(stderr, "recvfrom(udp): flags not implemented\n"); 173 #endif 174 errno= ENOSYS; 175 return -1; 176 } 177 178 if (udpoptp->nwuo_flags & NWUO_RWDATONLY) 179 { 180 if (address != NULL && 181 (udpoptp->nwuo_flags & (NWUO_RA_SET | NWUO_RP_SET)) != 182 (NWUO_RA_SET | NWUO_RP_SET)) 183 { 184 185 #if DEBUG 186 fprintf(stderr, 187 "recvfrom(udp): RWDATONLY on unconnected socket\n"); 188 #endif 189 errno= ENOTCONN; 190 return -1; 191 } 192 193 r= read(sock, buffer, length); 194 if (r == -1) 195 return r; 196 197 if (address != NULL) 198 { 199 sin.sin_family= AF_INET; 200 sin.sin_addr.s_addr= udpoptp->nwuo_remaddr; 201 sin.sin_port= udpoptp->nwuo_remport; 202 sin.sin_len= sizeof(sin); 203 len= *address_len; 204 if (len > sizeof(sin)) 205 len= sizeof(sin); 206 memcpy(address, &sin, len); 207 *address_len= sizeof(sin); 208 } 209 210 return r; 211 } 212 213 buflen= sizeof(*io_hdrp) + length; 214 if (buflen < length) 215 { 216 /* Overflow */ 217 errno= EMSGSIZE; 218 return -1; 219 } 220 buf= malloc(buflen); 221 if (buf == NULL) 222 return -1; 223 224 r= read(sock, buf, buflen); 225 if (r == -1) 226 { 227 t_errno= errno; 228 #if DEBUG 229 fprintf(stderr, "recvfrom(udp): read failed: %s\n", 230 strerror(errno)); 231 fprintf(stderr, "udp opt flags = 0x%x\n", udpoptp->nwuo_flags); 232 #endif 233 free(buf); 234 errno= t_errno; 235 return -1; 236 } 237 238 assert((size_t)r >= sizeof(*io_hdrp)); 239 length= r-sizeof(*io_hdrp); 240 241 io_hdrp= buf; 242 memcpy(buffer, &io_hdrp[1], length); 243 244 if (address != NULL) 245 { 246 sin.sin_family= AF_INET; 247 sin.sin_addr.s_addr= io_hdrp->uih_src_addr; 248 sin.sin_port= io_hdrp->uih_src_port; 249 sin.sin_len= sizeof(sin); 250 len= *address_len; 251 if (len > sizeof(sin)) 252 len= sizeof(sin); 253 memcpy(address, &sin, len); 254 *address_len= sizeof(sin); 255 } 256 free(buf); 257 return length; 258 } 259 260 static ssize_t _uds_recvfrom_conn(int sock, void *__restrict buffer, 261 size_t length, int flags, struct sockaddr *__restrict address, 262 socklen_t *__restrict address_len, struct sockaddr_un *uds_addr) 263 { 264 int r; 265 size_t len; 266 267 /* for connection oriented unix domain sockets (SOCK_STREAM / 268 * SOCK_SEQPACKET) 269 */ 270 271 if (flags != 0) 272 { 273 #if DEBUG 274 fprintf(stderr, "recvfrom(uds): flags not implemented\n"); 275 #endif 276 errno= ENOSYS; 277 return -1; 278 } 279 280 r = read(sock, buffer, length); 281 282 if (r >= 0 && address != NULL) 283 { 284 285 len= *address_len; 286 if (len > sizeof(struct sockaddr_un)) 287 len= sizeof(struct sockaddr_un); 288 memcpy(address, uds_addr, len); 289 *address_len= sizeof(struct sockaddr_un); 290 } 291 292 return r; 293 } 294 295 static ssize_t _uds_recvfrom_dgram(int sock, void *__restrict buffer, 296 size_t length, int flags, struct sockaddr *__restrict address, 297 socklen_t *__restrict address_len) 298 { 299 int r; 300 size_t len; 301 302 /* for connectionless unix domain sockets (SOCK_DGRAM) */ 303 304 if (flags != 0) 305 { 306 #if DEBUG 307 fprintf(stderr, "recvfrom(uds): flags not implemented\n"); 308 #endif 309 errno= ENOSYS; 310 return -1; 311 } 312 313 r = read(sock, buffer, length); 314 315 if (r >= 0 && address != NULL) 316 { 317 len= *address_len; 318 if (len > sizeof(struct sockaddr_un)) 319 len= sizeof(struct sockaddr_un); 320 ioctl(sock, NWIOGUDSFADDR, address); 321 *address_len= sizeof(struct sockaddr_un); 322 } 323 324 return r; 325 } 326 327