From e01ae35ca5c40dd191bb4aa049698b940b54c116 Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Tue, 21 May 2013 19:02:05 +0200 Subject: p2p: Set connection timeout 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 | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'plugins') diff --git a/plugins/p2p.c b/plugins/p2p.c index 88e0ff8..bf90ee8 100644 --- a/plugins/p2p.c +++ b/plugins/p2p.c @@ -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) { -- cgit v1.2.3