p2p: Set connection timeout
authorSamuel Ortiz <sameo@linux.intel.com>
Tue, 21 May 2013 17:02:05 +0000 (19:02 +0200)
committerSamuel Ortiz <sameo@linux.intel.com>
Tue, 21 May 2013 17:02:05 +0000 (19:02 +0200)
The connect() call should not block forever. The 8 seconds timeout makes
it for approximately 3 times the maximum LTO and that seems to be plenty
of time for a peer to reply to an LLCP connection.

plugins/p2p.c

index 88e0ff8..bf90ee8 100644 (file)
@@ -344,6 +344,7 @@ static int p2p_connect(uint32_t adapter_idx, uint32_t target_idx,
                        near_device_io_cb cb,  struct near_p2p_driver *driver)
 {
        int fd, err = 0;
+       struct timeval timeout;
        struct sockaddr_nfc_llcp addr;
 
        DBG("");
@@ -360,6 +361,17 @@ static int p2p_connect(uint32_t adapter_idx, uint32_t target_idx,
        addr.service_name_len = strlen(driver->service_name);
        strcpy(addr.service_name, driver->service_name);
 
+       timeout.tv_sec = 8;
+       timeout.tv_usec = 0;
+
+       if (setsockopt (fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
+                       sizeof(timeout)) < 0)
+               near_error("Could not set the receive timeout\n");
+
+       if (setsockopt (fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
+                       sizeof(timeout)) < 0)
+               near_error("Could not set the send timeout\n");
+
        err = connect(fd, (struct sockaddr *) &addr,
                        sizeof(struct sockaddr_nfc_llcp));
        if (err < 0) {