-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
228 lines (195 loc) · 6.84 KB
/
main.c
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/tcp.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#define TCP_MAX_BUF_LEN 10000
//static char tcpSndBuf[TCP_MAX_BUF_LEN];
int main(int argc, char *argv[])
{
const char *locAddr;
const char *remAddr;
uint16_t locPort;
uint16_t remPort;
const char *name;
const char *gender;
const char *age;
const char *ride;
struct in_addr addr4;
struct in6_addr addr6;
struct sockaddr_storage locSock = {0};
struct sockaddr_storage remSock = {0};
int af, sd;
socklen_t sockLen;
int enabled = 1;
int sndBufSize = 512 * 1024;
char msgBuf[1024];
int bibNum;
time_t startTime;
if (argc != 9) {
printf("SYNTAX: grc <locAddr> <locPort> <remAddr> <remPort> <name> <gender> <age> <ride>\n");
return -1;
}
locAddr = argv[1];
locPort = atoi(argv[2]);
remAddr = argv[3];
remPort = atoi(argv[4]);
name = argv[5];
gender = argv[6];
age = argv[7];
ride = argv[8];
if ((remPort < 49152) || (remPort > 65535)) {
printf("ERROR: illegal local port %u. Valid range is 49152-65535\n", locPort);
return -1;
}
if (inet_pton(AF_INET, locAddr, &addr4) == 1) {
struct sockaddr_in *pSock = (struct sockaddr_in *) &locSock;
af = AF_INET;
pSock->sin_family = AF_INET;
pSock->sin_port = htons(locPort);
memcpy(&pSock->sin_addr, &addr4, sizeof (addr4));
sockLen = sizeof (struct sockaddr_in);
} else if (inet_pton(AF_INET6, locAddr, &addr6) == 1) {
struct sockaddr_in6 *pSock = (struct sockaddr_in6 *) &locSock;
af = AF_INET6;
pSock->sin6_family = AF_INET6;
pSock->sin6_port = htons(locPort);
memcpy(&pSock->sin6_addr, &addr6, sizeof (addr6));
sockLen = sizeof (struct sockaddr_in6);
} else {
printf("ERROR: illegal local address %s\n", locAddr);
return -1;
}
if (inet_pton(AF_INET, remAddr, &addr4) == 1) {
struct sockaddr_in *pSock = (struct sockaddr_in *) &remSock;
pSock->sin_family = AF_INET;
pSock->sin_port = htons(remPort);
memcpy(&pSock->sin_addr, &addr4, sizeof (addr4));
} else if (inet_pton(AF_INET6, remAddr, &addr6) == 1) {
struct sockaddr_in6 *pSock = (struct sockaddr_in6 *) &remSock;
pSock->sin6_family = AF_INET6;
pSock->sin6_port = htons(remPort);
memcpy(&pSock->sin6_addr, &addr6, sizeof (addr6));
} else {
printf("ERROR: illegal local address %s\n", remAddr);
return -1;
}
if (locSock.ss_family != remSock.ss_family) {
printf("ERROR: incompatible local/remote IP addresses\n");
return -1;
}
printf("grc: local=%s:%u remote=%s:%u name=%s gender=%s age=%s ride=%s\n",
locAddr, locPort, remAddr, remPort, name, gender, age, ride);
if ((sd = socket(af, SOCK_STREAM, 0)) < 0) {
printf("ERROR: failed to alloc socket: %s\n", strerror(errno));
return -1;
}
if (setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *) &sndBufSize, sizeof (sndBufSize)) < 0) {
printf("ERROR: failed to set SO_SNDBUF option: %s\n", strerror(errno));
return -1;
}
if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *) &enabled, sizeof (enabled)) < 0) {
printf("ERROR: failed to set SO_REUSEADDR option: %s\n", strerror(errno));
close(sd);
return -1;
}
if (bind(sd, (struct sockaddr *) &locSock, sockLen) < 0) {
printf("ERROR: bind failed: %s\n", strerror(errno));
return -1;
}
if (connect(sd, (struct sockaddr *) &remSock, sockLen) < 0) {
printf("ERROR: failed to connect socket: %s\n", strerror(errno));
close(sd);
return -1;
}
printf("Connected to GRS server!\n");
// Send the "regReq" message
{
size_t msgLen;
snprintf(msgBuf, sizeof (msgBuf), "{\"msgType\": \"regReq\", \"name\": \"%s\", \"gender\": \"%s\", \"age\": \"%s\", \"ride\": \"%s\"}", name, gender, age, ride);
msgLen = strlen(msgBuf) + 1;
if (send(sd, msgBuf, msgLen, 0) != msgLen) {
printf("ERROR: send(regReq) failed: %s\n", strerror(errno));
close(sd);
return -1;
}
}
// Wait for the "regResp" message
{
size_t msgLen;
const char *p;
if ((msgLen = recv(sd, msgBuf, sizeof (msgBuf), 0)) < 0) {
printf("ERROR: recv(regResp) failed: %s\n", strerror(errno));
close(sd);
return -1;
}
printf("Received: %s\n", msgBuf);
if (strstr(msgBuf, "\"msgType\": \"regResp\"") == NULL) {
printf("ERROR: unexpected message type!\n");
close(sd);
return -1;
}
if (strstr(msgBuf, "\"status\": \"success\"") == NULL) {
printf("ERROR: regReq failed!\n");
close(sd);
return -1;
}
if ((p = strstr(msgBuf, "\"bibNum\"")) == NULL) {
printf("ERROR: no bib number! msgBuf=%s\n", msgBuf);
close(sd);
return -1;
} else if (sscanf(p, "\"bibNum\": \"%d\"", &bibNum) != 1) {
printf("ERROR: invalid bib number!\n");
close(sd);
return -1;
}
if ((p = strstr(msgBuf, "\"startTime\"")) == NULL) {
printf("ERROR: no startTime! msgBuf=%s\n", msgBuf);
close(sd);
return -1;
} else if (sscanf(p, " \"startTime\": \"%ld\"", &startTime) != 1) {
printf("ERROR: invalid startTime!\n");
close(sd);
return -1;
}
printf("regResp: binNum=%d startTime=%ld\n", bibNum, startTime);
}
// Wait for the "go" message
{
size_t msgLen;
if ((msgLen = recv(sd, msgBuf, sizeof (msgBuf), 0)) < 0) {
printf("ERROR: recv(go) failed: %s\n", strerror(errno));
close(sd);
return -1;
}
printf("Received: %s\n", msgBuf);
if (strstr(msgBuf, "\"msgType\": \"go\"") == NULL) {
printf("ERROR: unexpected message type!\n");
close(sd);
return -1;
}
}
// Send the "progUpd" messages
{
size_t msgLen;
for (int n = 0; n < 10; n++) {
time_t now = time(NULL);
snprintf(msgBuf, sizeof (msgBuf), "{\"msgType\": \"progUpd\", \"time\": \"%ld\", \"distance\": \"%d\", \"speed\": \"%d\", \"power\": \"%d\"}", now, n, n*2, n*2);
msgLen = strlen(msgBuf) + 1;
if (send(sd, msgBuf, msgLen, 0) != msgLen) {
printf("ERROR: send(progRep) failed: %s\n", strerror(errno));
close(sd);
return -1;
}
sleep(1);
}
}
close(sd);
return 0;
}