aboutsummaryrefslogtreecommitdiffstats
path: root/meta-rcar-gen3-adas/recipes-support/netevent/netevent/0004-Add-TCP-keep-alive-to-handle-peer-death-properly.patch
blob: f84f11386f2ea8c8298af6acd98b5bd91e9de326 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
From 0d797dca82a714688657523a89a14c37ae7767cf Mon Sep 17 00:00:00 2001
From: Grigory Kletsko <grigory.kletsko@cogentembedded.com>
Date: Wed, 5 Oct 2016 22:15:25 +0300
Subject: [PATCH] Add TCP keep alive to handle peer death properly

---
 reader.cpp | 32 +++++++++++++++++++++++++++++++-
 write.cpp  | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/reader.cpp b/reader.cpp
index 0b6de20..e1a26e3 100644
--- a/reader.cpp
+++ b/reader.cpp
@@ -11,6 +11,8 @@
 #include <pthread.h>
 #include <netinet/in.h>
 #include <netdb.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
 
 int64_t htonll(int64_t value){
     int num = 42;
@@ -131,6 +133,8 @@ int socket_open(const char *hostname, int port)
 {
 	int ret;
 	int sockfd;
+	int val;
+	
 	struct sockaddr_in serv_addr;
 	struct hostent *server;
 
@@ -159,6 +163,32 @@ int socket_open(const char *hostname, int port)
 		fprintf(stderr, "ERROR connecting %d, %d\n", ret, errno);
 		return ret;
 	}
+
+	val = 5;
+	ret = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPCNT, &val,
+			 sizeof(val));
+	if (ret < 0) {
+		fprintf(stderr, "ERROR on setsockopt %d", ret);
+		return ret;	
+	}
+
+	val = 1;
+	ret = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, &val,
+			 sizeof(val));
+	if (ret < 0) {
+		fprintf(stderr, "ERROR on setsockopt %d", ret);
+		return ret;	
+	}
+
+	val = 1;
+	ret = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL, &val,
+			 sizeof(val));
+	if (ret < 0) {
+		fprintf(stderr, "ERROR on setsockopt %d", ret);
+		return ret;	
+	}
+
+
 	return sockfd;
 }
 
@@ -344,4 +374,4 @@ int read_device(const char *devfile, const char *hostname, int port)
 		sleep(1);
 	}
 	return 0;
-}
\ No newline at end of file
+}
diff --git a/write.cpp b/write.cpp
index dc6c3bc..67ce412 100644
--- a/write.cpp
+++ b/write.cpp
@@ -8,6 +8,9 @@
 #include <unistd.h>
 #include <netinet/in.h>
 #include <netdb.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
 
 int64_t ntohll(int64_t value){
     int num = 42;
@@ -30,6 +33,8 @@ int socket_start_listen(int port)
 {
 	int ret;
 	int sockfd;
+	int val;
+	
 	struct sockaddr_in serv_addr;
 
 	printf("starting on port %d\n", port);
@@ -48,7 +53,41 @@ int socket_start_listen(int port)
 		fprintf(stderr, "ERROR on binding %d", ret);
 		return ret;
 	}
+
+	val = 1;
+	ret = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &val,
+			 sizeof(val));
+	if (ret < 0) {
+		fprintf(stderr, "ERROR on setsockopt %d", ret);
+		return ret;	
+	}
+
+	val = 5;
+	ret = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPCNT, &val,
+			 sizeof(val));
+	if (ret < 0) {
+		fprintf(stderr, "ERROR on setsockopt %d", ret);
+		return ret;	
+	}
+
+	val = 1;
+	ret = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, &val,
+			 sizeof(val));
+	if (ret < 0) {
+		fprintf(stderr, "ERROR on setsockopt %d", ret);
+		return ret;	
+	}
+
+	val = 1;
+	ret = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL, &val,
+			 sizeof(val));
+	if (ret < 0) {
+		fprintf(stderr, "ERROR on setsockopt %d", ret);
+		return ret;	
+	}
+
 	listen(sockfd, 1);
+	
 	return sockfd;
 }
 
@@ -174,10 +213,13 @@ int spawn_device_new(int sock_con)
 
 	fprintf(stderr, "Transferring input events.\n");
 	while (true) {
+		int ret;
 		input_event_t et;
 		int dummy;
 		waitpid(0, &dummy, WNOHANG);
-		if (!read(sock_con, (char*)&et, sizeof(et))) {
+		ret = read(sock_con, (char*)&et, sizeof(et));
+
+		if (ret <= 0) {
 			fprintf(stderr, "End of data\n");
 			break;
 		}
@@ -233,4 +275,4 @@ int spawn_device(int port)
 err_close:
 	close(sock_listen);
 	return -1;
-}
\ No newline at end of file
+}
-- 
2.7.4