Repository navigation
Expand file tree
/
Copy pathrecvfrom.c
More file actions
176 lines (153 loc) · 3.69 KB
/
Copy pathrecvfrom.c
File metadata and controls
176 lines (153 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#undef NDEBUG
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/gen/in.h>
#include <net/gen/tcp.h>
#include <net/gen/tcp_io.h>
#include <net/gen/udp.h>
#include <net/gen/udp_hdr.h>
#include <net/gen/udp_io.h>
#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);
ssize_t recvfrom(int socket, void *_RESTRICT buffer, size_t length,
int flags, struct sockaddr *_RESTRICT address,
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))
{
if (r == -1)
return r;
return _udp_recvfrom(socket, buffer, length, flags,
address, address_len, &udpopt);
}
#if DEBUG
fprintf(stderr, "recvfrom: not implemented for fd %d\n", socket);
#endif
errno= ENOSYS;
assert(0);
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)
{
int r, t_errno;
size_t buflen, len;
void *buf;
struct sockaddr_in *sinp;
udp_io_hdr_t *io_hdrp;
struct sockaddr_in sin;
if (flags)
{
#if DEBUG
fprintf(stderr, "recvfrom(udp): flags not implemented\n");
#endif
errno= ENOSYS;
return -1;
}
if (udpoptp->nwuo_flags & NWUO_RWDATONLY)
{
#if DEBUG
fprintf(stderr,
"recvfrom(udp): NWUO_RWDATONLY not implemented\n");
#endif
errno= ENOSYS;
return -1;
}
buflen= sizeof(*io_hdrp) + length;
if (buflen < length)
{
/* Overflow */
errno= EMSGSIZE;
return -1;
}
buf= malloc(buflen);
if (buf == NULL)
return -1;
r= read(socket, buf, buflen);
if (r == -1)
{
t_errno= errno;
#if DEBUG
fprintf(stderr, "recvfrom(udp): read failed: %s\n",
strerror(errno));
fprintf(stderr, "udp opt flags = 0x%x\n", udpoptp->nwuo_flags);
#endif
free(buf);
errno= t_errno;
return -1;
}
assert(r >= sizeof(*io_hdrp));
length= r-sizeof(*io_hdrp);
io_hdrp= buf;
memcpy(buffer, &io_hdrp[1], length);
if (address != NULL)
{
sin.sin_family= AF_INET;
sin.sin_addr.s_addr= io_hdrp->uih_src_addr;
sin.sin_port= io_hdrp->uih_src_port;
len= *address_len;
if (len > sizeof(sin))
len= sizeof(sin);
memcpy(address, &sin, len);
*address_len= sizeof(sin);
}
free(buf);
return length;
}