make sure that socket's send and receive buffers are properly sized

Submitted by:	Iain Hibbert plunky at rya-online dot net
MFC after:	3 weeks
This commit is contained in:
Maksim Yevmenkin 2012-10-18 16:34:00 +00:00
parent 77833fb00d
commit 2dcf7e97da
2 changed files with 45 additions and 1 deletions

View File

@ -47,7 +47,7 @@ client_init(void)
struct sockaddr_l2cap sa;
channel_t *chan;
socklen_t len;
int fd;
int fd, n;
uint16_t mru, mtu;
if (bdaddr_any(&remote_bdaddr))
@ -97,6 +97,17 @@ client_init(void)
exit(EXIT_FAILURE);
}
len = sizeof(n);
if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, &len) == -1) {
log_err("Could not read SO_RCVBUF");
exit(EXIT_FAILURE);
}
if (n < (mru * 10)) {
n = mru * 10;
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) == -1)
log_info("Could not increase SO_RCVBUF (from %d)", n);
}
len = sizeof(mtu);
if (getsockopt(fd, SOL_L2CAP, SO_L2CAP_OMTU, &mtu, &len) == -1) {
log_err("Could not get L2CAP OMTU: %m");
@ -107,6 +118,27 @@ client_init(void)
exit(EXIT_FAILURE);
}
len = sizeof(n);
if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, &len) == -1) {
log_err("Could not get socket send buffer size: %m");
close(fd);
return;
}
if (n < (mtu * 2)) {
n = mtu * 2;
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n)) == -1) {
log_err("Could not set socket send buffer size (%d): %m", n);
close(fd);
return;
}
}
n = mtu;
if (setsockopt(fd, SOL_SOCKET, SO_SNDLOWAT, &n, sizeof(n)) == -1) {
log_err("Could not set socket low water mark (%d): %m", n);
close(fd);
return;
}
chan = channel_alloc();
if (chan == NULL)
exit(EXIT_FAILURE);

View File

@ -177,6 +177,18 @@ server_read(int s, short ev, void *arg)
return;
}
len = sizeof(n);
if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, &len) == -1) {
log_err("Could not read SO_RCVBUF");
close(fd);
return;
}
if (n < (mru * 10)) {
n = mru * 10;
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) == -1)
log_info("Could not increase SO_RCVBUF (from %d)", n);
}
len = sizeof(mtu);
if (getsockopt(fd, SOL_L2CAP, SO_L2CAP_OMTU, &mtu, &len) == -1) {
log_err("Could not get L2CAP OMTU: %m");