Bug 14723 - Undocumented behaviour of connect() with SO_SNDTIMEO
Summary: Undocumented behaviour of connect() with SO_SNDTIMEO
Status: RESOLVED CODE_FIX
Alias: None
Product: Documentation
Classification: Unclassified
Component: man-pages (show other bugs)
Hardware: All Linux
: P1 normal
Assignee: documentation_man-pages@kernel-bugs.osdl.org
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-12-03 10:08 UTC by Roman Chebotarev
Modified: 2013-12-10 17:17 UTC (History)
2 users (show)

See Also:
Kernel Version:
Subsystem:
Regression: No
Bisected commit-id:


Attachments

Description Roman Chebotarev 2009-12-03 10:08:52 UTC
Setting option SO_SNDTIMEO to the socket cause connect() return with EINPROGRESS errno after timeout, but this nonstandard feature is not described in man pages of socket(7) and connect(2).

Sample:

int main(int argc, char *argv[])
{
    int sockfd, n;
    struct sockaddr_in serv_addr;

    struct timeval timeout;
    timeout.tv_sec = 4;
    timeout.tv_usec = 0;

    if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        perror("socket");
        return -1;
    }

    if( setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) == -1 )
    {
        perror("setsockopt");
        return -1;
    }

    bzero (&serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(9999);

    if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr) < 1)
    {
        printf("Invalid IP address value. Exit.\n");
        return -1;
    }

    if(connect(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0)
    {
        printf("errno = %d\n", errno);
        perror("connect()");
        return -1;
    }

    close(sockfd);

    return 0;
}

$ time ./client 88.88.88.88
errno = 115
connect(): Operation now in progress

real    0m4.001s
user    0m0.000s
sys     0m0.000s

# tcpdump -i ppp0 -n port 9999 -ttt
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ppp0, link-type LINUX_SLL (Linux cooked), capture size 96 bytes
000000 IP 81.25.50.166.47087 > 88.88.88.88.9999: S 3574453598:3574453598(0) win 5840 <mss 1460,sackOK,timestamp 207409345 0,nop,wscale 6>
2. 997721 IP 81.25.50.166.47087 > 88.88.88.88.9999: S 3574453598:3574453598(0) win 5840 <mss 1460,sackOK,timestamp 207410245 0,nop,wscale 6>
Comment 1 Jorge Nerín 2013-09-10 16:38:16 UTC
From man-pages-3.51 socket(7):

       SO_RCVTIMEO and SO_SNDTIMEO
              Specify the receiving or sending timeouts until reporting an error.  The
              argument is a struct timeval.  If an input or output function blocks for
              this  period  of  time,  and  data has been sent or received, the return
              value of that function will be the amount of  data  transferred;  if  no
              data  has  been  transferred and the timeout has been reached then -1 is
              returned with errno set to EAGAIN or EWOULDBLOCK,  or  EINPROGRESS  (for
              connect(2))  just  as if the socket was specified to be nonblocking.  If
              the timeout is set to zero (the default) then the operation  will  never
              timeout.  Timeouts only have effect for system calls that perform socket
              I/O (e.g., read(2), recvmsg(2), send(2), sendmsg(2)); timeouts  have  no
              effect for select(2), poll(2), epoll_wait(2), and so on.
	

I think this bug can be closed.

Note You need to log in before you can comment on or make changes to this bug.