On Wed, Apr 12, 2017 at 07:41:43PM +0200, Olivier Houchard wrote:
> +     if (default_tcp_maxseg == -1) {
> +             default_tcp_maxseg = -2;
> +             fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
> +             if (fd < 0)
> +                     Warning("Failed to create a temporary socket!\n");
> +             else {
> +                     if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, 
> &default_tcp_maxseg,
> +                         &ready_len) == -1)
> +                             Warning("Failed to get the default value of 
> TCP_MAXSEG\n");

Olivier, you're missing a close(fd) here, it'll leak this fd.

> +             }
> +     }
> +     if (default_tcp6_maxseg == -1) {
> +             default_tcp6_maxseg = -2;
> +             fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
> +             if (fd >= 0) {
> +                     if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, 
> &default_tcp6_maxseg,
> +                         &ready_len) == -1)
> +                             Warning("Failed ot get the default value of 
> TCP_MAXSEG for IPv6\n");
> +                     close(fd);
> +             }
> +     }
> +#endif
> +
>  
(...)

> +             getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
> +             if (tmpmaxseg != defaultmss &&
> +                 setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
> +                 &defaultmss, sizeof(defaultmss)) == -1) {

Please fix the alignment for the argument here, it's a bit confusing :-)

Otherwise looks good. I think it was a good idea to create temporary
sockets to retrieve some default settings. That may be something we
could generalize to guess other parameters or capabilities if needed
in the future. For example we could use this to detect whether or not
IPv6 is supported and emit errors only once instead of every bind line.

Another use case is to always know the MSS applied to a listening socket
in order to automatically adjust the SSL maxrecord instead of assuming
1460 by default. Over time we might find other use cases.

Cheers,
Willy

Reply via email to