1874
_PUBLIC_ int swrap_ioctl(int s, int r, void *p)
1877
struct socket_info *si = find_socket_info(s);
1881
return real_ioctl(s, r, p);
1884
ret = real_ioctl(s, r, p);
1888
value = *((int *)p);
1889
if (ret == -1 && errno != EAGAIN && errno != ENOBUFS) {
1890
swrap_dump_packet(si, NULL, SWRAP_PENDING_RST, NULL, 0);
1891
} else if (value == 0) { /* END OF FILE */
1892
swrap_dump_packet(si, NULL, SWRAP_PENDING_RST, NULL, 0);
1900
static ssize_t swrap_sendmsg_before(struct socket_info *si,
1902
struct iovec *tmp_iov,
1903
struct sockaddr_un *tmp_un,
1904
const struct sockaddr_un **to_un,
1905
const struct sockaddr **to,
1923
if (!si->connected) {
1928
if (msg->msg_iovlen == 0) {
1933
* cut down to 1500 byte packets for stream sockets,
1934
* which makes it easier to format PCAP capture files
1935
* (as the caller will simply continue from here)
1938
for (i=0; i < msg->msg_iovlen; i++) {
1940
nlen = len + msg->msg_iov[i].iov_len;
1945
msg->msg_iovlen = i;
1946
if (msg->msg_iovlen == 0) {
1947
*tmp_iov = msg->msg_iov[0];
1948
tmp_iov->iov_len = MIN(tmp_iov->iov_len, 1500);
1949
msg->msg_iov = tmp_iov;
1950
msg->msg_iovlen = 1;
1955
if (si->connected) {
1956
if (msg->msg_name) {
1961
const struct sockaddr *msg_name;
1962
msg_name = (const struct sockaddr *)msg->msg_name;
1964
if (msg_name == NULL) {
1970
ret = sockaddr_convert_to_un(si, msg_name, msg->msg_namelen,
1972
if (ret == -1) return -1;
1980
msg->msg_name = tmp_un;
1981
msg->msg_namelen = sizeof(*tmp_un);
1984
if (si->bound == 0) {
1985
ret = swrap_auto_bind(si, si->family);
1986
if (ret == -1) return -1;
1989
if (!si->defer_connect) {
1993
ret = sockaddr_convert_to_un(si, si->peername, si->peername_len,
1995
if (ret == -1) return -1;
1997
ret = real_connect(si->fd, (struct sockaddr *)(void *)tmp_un,
2000
/* to give better errors */
2001
if (ret == -1 && errno == ENOENT) {
2002
errno = EHOSTUNREACH;
2009
si->defer_connect = 0;
2012
errno = EHOSTUNREACH;
2019
static void swrap_sendmsg_after(struct socket_info *si,
2021
const struct sockaddr *to,
2024
int saved_errno = errno;
2031
/* to give better errors */
2032
if (ret == -1 && saved_errno == ENOENT) {
2033
saved_errno = EHOSTUNREACH;
2036
for (i=0; i < msg->msg_iovlen; i++) {
2037
avail += msg->msg_iov[i].iov_len;
2041
remain = MIN(80, avail);
2046
/* we capture it as one single packet */
2047
buf = (uint8_t *)malloc(remain);
2049
/* we just not capture the packet */
2050
errno = saved_errno;
2054
for (i=0; i < msg->msg_iovlen; i++) {
2055
size_t this_time = MIN(remain, msg->msg_iov[i].iov_len);
2057
msg->msg_iov[i].iov_base,
2060
remain -= this_time;
2067
swrap_dump_packet(si, NULL, SWRAP_SEND, buf, len);
2068
swrap_dump_packet(si, NULL, SWRAP_SEND_RST, NULL, 0);
2070
swrap_dump_packet(si, NULL, SWRAP_SEND, buf, len);
2075
if (si->connected) {
2079
swrap_dump_packet(si, to, SWRAP_SENDTO, buf, len);
2080
swrap_dump_packet(si, to, SWRAP_SENDTO_UNREACH, buf, len);
2082
swrap_dump_packet(si, to, SWRAP_SENDTO, buf, len);
2088
errno = saved_errno;
1852
2091
_PUBLIC_ ssize_t swrap_recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
1854
2093
struct sockaddr_un un_addr;
1902
2145
return real_sendto(s, buf, len, flags, to, tolen);
1905
if (si->connected) {
1912
tolen = si->peername_len;
1917
/* cut down to 1500 byte packets for stream sockets,
1918
* which makes it easier to format PCAP capture files
1919
* (as the caller will simply continue from here) */
1920
len = MIN(len, 1500);
1922
ret = real_send(s, buf, len, flags);
1925
if (si->bound == 0) {
1926
ret = swrap_auto_bind(si, si->family);
1927
if (ret == -1) return -1;
1930
ret = sockaddr_convert_to_un(si, to, tolen, &un_addr, 0, &bcast);
1931
if (ret == -1) return -1;
1936
unsigned int prt = ntohs(((const struct sockaddr_in *)to)->sin_port);
1939
type = SOCKET_TYPE_CHAR_UDP;
1941
for(iface=0; iface <= MAX_WRAPPED_INTERFACES; iface++) {
1942
snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s/"SOCKET_FORMAT,
1943
socket_wrapper_dir(), type, iface, prt);
1944
if (stat(un_addr.sun_path, &st) != 0) continue;
1946
/* ignore the any errors in broadcast sends */
1947
real_sendto(s, buf, len, flags, (struct sockaddr *)&un_addr, sizeof(un_addr));
1950
swrap_dump_packet(si, to, SWRAP_SENDTO, buf, len);
1955
if (si->defer_connect) {
1956
ret = real_connect(s, (struct sockaddr *)&un_addr,
1959
/* to give better errors */
1960
if (ret == -1 && errno == ENOENT) {
1961
errno = EHOSTUNREACH;
1967
si->defer_connect = 0;
1970
ret = real_sendto(s, buf, len, flags, (struct sockaddr *)&un_addr, sizeof(un_addr));
1974
errno = EHOSTUNREACH;
1978
/* to give better errors */
1979
if (ret == -1 && errno == ENOENT) {
1980
errno = EHOSTUNREACH;
2148
tmp.iov_base = discard_const_p(char, buf);
2152
msg.msg_name = discard_const_p(struct sockaddr, to); /* optional address */
2153
msg.msg_namelen = tolen; /* size of address */
2154
msg.msg_iov = &tmp; /* scatter/gather array */
2155
msg.msg_iovlen = 1; /* # elements in msg_iov */
2156
#if 0 /* not available on solaris */
2157
msg.msg_control = NULL; /* ancillary data, see below */
2158
msg.msg_controllen = 0; /* ancillary data buffer len */
2159
msg.msg_flags = 0; /* flags on received message */
2162
ret = swrap_sendmsg_before(si, &msg, &tmp, &un_addr, &to_un, &to, &bcast);
2163
if (ret == -1) return -1;
2165
buf = msg.msg_iov[0].iov_base;
2166
len = msg.msg_iov[0].iov_len;
2171
unsigned int prt = ntohs(((const struct sockaddr_in *)to)->sin_port);
2174
type = SOCKET_TYPE_CHAR_UDP;
2176
for(iface=0; iface <= MAX_WRAPPED_INTERFACES; iface++) {
2177
snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s/"SOCKET_FORMAT,
2178
socket_wrapper_dir(), type, iface, prt);
2179
if (stat(un_addr.sun_path, &st) != 0) continue;
2181
/* ignore the any errors in broadcast sends */
2182
real_sendto(s, buf, len, flags,
2183
(struct sockaddr *)(void *)&un_addr,
1984
2187
swrap_dump_packet(si, to, SWRAP_SENDTO, buf, len);
1985
swrap_dump_packet(si, to, SWRAP_SENDTO_UNREACH, buf, len);
1987
swrap_dump_packet(si, to, SWRAP_SENDTO, buf, ret);
1993
_PUBLIC_ int swrap_ioctl(int s, int r, void *p)
1996
struct socket_info *si = find_socket_info(s);
2000
return real_ioctl(s, r, p);
2003
ret = real_ioctl(s, r, p);
2007
value = *((int *)p);
2008
if (ret == -1 && errno != EAGAIN && errno != ENOBUFS) {
2009
swrap_dump_packet(si, NULL, SWRAP_PENDING_RST, NULL, 0);
2010
} else if (value == 0) { /* END OF FILE */
2011
swrap_dump_packet(si, NULL, SWRAP_PENDING_RST, NULL, 0);
2192
ret = real_sendto(s, buf, len, flags, msg.msg_name, msg.msg_namelen);
2194
swrap_sendmsg_after(si, &msg, to, ret);
2076
2256
_PUBLIC_ ssize_t swrap_send(int s, const void *buf, size_t len, int flags)
2260
struct sockaddr_un un_addr;
2079
2262
struct socket_info *si = find_socket_info(s);
2082
2265
return real_send(s, buf, len, flags);
2085
if (si->type == SOCK_STREAM) {
2086
/* cut down to 1500 byte packets for stream sockets,
2087
* which makes it easier to format PCAP capture files
2088
* (as the caller will simply continue from here) */
2089
len = MIN(len, 1500);
2092
if (si->defer_connect) {
2093
struct sockaddr_un un_addr;
2096
if (si->bound == 0) {
2097
ret = swrap_auto_bind(si, si->family);
2098
if (ret == -1) return -1;
2101
ret = sockaddr_convert_to_un(si, si->peername, si->peername_len,
2102
&un_addr, 0, &bcast);
2103
if (ret == -1) return -1;
2105
ret = real_connect(s, (struct sockaddr *)&un_addr,
2108
/* to give better errors */
2109
if (ret == -1 && errno == ENOENT) {
2110
errno = EHOSTUNREACH;
2116
si->defer_connect = 0;
2268
tmp.iov_base = discard_const_p(char, buf);
2272
msg.msg_name = NULL; /* optional address */
2273
msg.msg_namelen = 0; /* size of address */
2274
msg.msg_iov = &tmp; /* scatter/gather array */
2275
msg.msg_iovlen = 1; /* # elements in msg_iov */
2276
#if 0 /* not available on solaris */
2277
msg.msg_control = NULL; /* ancillary data, see below */
2278
msg.msg_controllen = 0; /* ancillary data buffer len */
2279
msg.msg_flags = 0; /* flags on received message */
2282
ret = swrap_sendmsg_before(si, &msg, &tmp, &un_addr, NULL, NULL, NULL);
2283
if (ret == -1) return -1;
2285
buf = msg.msg_iov[0].iov_base;
2286
len = msg.msg_iov[0].iov_len;
2119
2288
ret = real_send(s, buf, len, flags);
2122
swrap_dump_packet(si, NULL, SWRAP_SEND, buf, len);
2123
swrap_dump_packet(si, NULL, SWRAP_SEND_RST, NULL, 0);
2125
swrap_dump_packet(si, NULL, SWRAP_SEND, buf, ret);
2290
swrap_sendmsg_after(si, &msg, NULL, ret);
2131
_PUBLIC_ ssize_t swrap_sendmsg(int s, const struct msghdr *msg, int flags)
2295
_PUBLIC_ ssize_t swrap_sendmsg(int s, const struct msghdr *omsg, int flags)
2299
struct sockaddr_un un_addr;
2300
const struct sockaddr_un *to_un = NULL;
2301
const struct sockaddr *to = NULL;
2139
2303
struct socket_info *si = find_socket_info(s);
2142
return real_sendmsg(s, msg, flags);
2145
if (si->defer_connect) {
2146
struct sockaddr_un un_addr;
2149
if (si->bound == 0) {
2150
ret = swrap_auto_bind(si, si->family);
2151
if (ret == -1) return -1;
2154
ret = sockaddr_convert_to_un(si, si->peername, si->peername_len,
2155
&un_addr, 0, &bcast);
2156
if (ret == -1) return -1;
2158
ret = real_connect(s, (struct sockaddr *)&un_addr,
2161
/* to give better errors */
2162
if (ret == -1 && errno == ENOENT) {
2163
errno = EHOSTUNREACH;
2169
si->defer_connect = 0;
2172
ret = real_sendmsg(s, msg, flags);
2175
/* we capture it as one single packet */
2176
buf = (uint8_t *)malloc(ret);
2178
/* we just not capture the packet */
2183
for (i=0; i < msg->msg_iovlen; i++) {
2184
size_t this_time = MIN(remain, msg->msg_iov[i].iov_len);
2186
msg->msg_iov[i].iov_base,
2189
remain -= this_time;
2192
swrap_dump_packet(si, NULL, SWRAP_SEND, buf, ret);
2195
swrap_dump_packet(si, NULL, SWRAP_SEND_RST, NULL, 0);
2307
return real_sendmsg(s, omsg, flags);
2310
tmp.iov_base = NULL;
2315
msg.msg_name = omsg->msg_name; /* optional address */
2316
msg.msg_namelen = omsg->msg_namelen; /* size of address */
2317
msg.msg_iov = omsg->msg_iov; /* scatter/gather array */
2318
msg.msg_iovlen = omsg->msg_iovlen; /* # elements in msg_iov */
2319
/* the following is not available on solaris */
2320
msg.msg_control = omsg->msg_control; /* ancillary data, see below */
2321
msg.msg_controllen = omsg->msg_controllen; /* ancillary data buffer len */
2322
msg.msg_flags = omsg->msg_flags; /* flags on received message */
2325
ret = swrap_sendmsg_before(si, &msg, &tmp, &un_addr, &to_un, &to, &bcast);
2326
if (ret == -1) return -1;
2331
unsigned int prt = ntohs(((const struct sockaddr_in *)to)->sin_port);
2339
for (i=0; i < msg.msg_iovlen; i++) {
2340
avail += msg.msg_iov[i].iov_len;
2346
/* we capture it as one single packet */
2347
buf = (uint8_t *)malloc(remain);
2352
for (i=0; i < msg.msg_iovlen; i++) {
2353
size_t this_time = MIN(remain, msg.msg_iov[i].iov_len);
2355
msg.msg_iov[i].iov_base,
2358
remain -= this_time;
2361
type = SOCKET_TYPE_CHAR_UDP;
2363
for(iface=0; iface <= MAX_WRAPPED_INTERFACES; iface++) {
2364
snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s/"SOCKET_FORMAT,
2365
socket_wrapper_dir(), type, iface, prt);
2366
if (stat(un_addr.sun_path, &st) != 0) continue;
2368
msg.msg_name = &un_addr; /* optional address */
2369
msg.msg_namelen = sizeof(un_addr); /* size of address */
2371
/* ignore the any errors in broadcast sends */
2372
real_sendmsg(s, &msg, flags);
2375
swrap_dump_packet(si, to, SWRAP_SENDTO, buf, len);
2381
ret = real_sendmsg(s, &msg, flags);
2383
swrap_sendmsg_after(si, &msg, to, ret);
2268
2460
int swrap_writev(int s, const struct iovec *vector, size_t count)
2464
struct sockaddr_un un_addr;
2271
2466
struct socket_info *si = find_socket_info(s);
2275
2469
return real_writev(s, vector, count);
2278
if (si->type == SOCK_STREAM && count > 0) {
2279
/* cut down to 1500 byte packets for stream sockets,
2280
* which makes it easier to format PCAP capture files
2281
* (as the caller will simply continue from here) */
2284
for (i=0; i < count; i++) {
2286
nlen = len + vector[i].iov_len;
2294
v.iov_len = MIN(v.iov_len, 1500);
2300
ret = real_writev(s, vector, count);
2302
swrap_dump_packet(si, NULL, SWRAP_SEND_RST, NULL, 0);
2307
size_t remain = ret;
2309
/* we capture it as one single packet */
2310
buf = (uint8_t *)malloc(ret);
2312
/* we just not capture the packet */
2317
for (i=0; i < count; i++) {
2318
size_t this_time = MIN(remain, vector[i].iov_len);
2323
remain -= this_time;
2326
swrap_dump_packet(si, NULL, SWRAP_SEND, buf, ret);
2472
tmp.iov_base = NULL;
2476
msg.msg_name = NULL; /* optional address */
2477
msg.msg_namelen = 0; /* size of address */
2478
msg.msg_iov = discard_const_p(struct iovec, vector); /* scatter/gather array */
2479
msg.msg_iovlen = count; /* # elements in msg_iov */
2480
#if 0 /* not available on solaris */
2481
msg.msg_control = NULL; /* ancillary data, see below */
2482
msg.msg_controllen = 0; /* ancillary data buffer len */
2483
msg.msg_flags = 0; /* flags on received message */
2486
ret = swrap_sendmsg_before(si, &msg, &tmp, &un_addr, NULL, NULL, NULL);
2487
if (ret == -1) return -1;
2489
ret = real_writev(s, msg.msg_iov, msg.msg_iovlen);
2491
swrap_sendmsg_after(si, &msg, NULL, ret);