-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredux.c
More file actions
473 lines (376 loc) · 17.6 KB
/
redux.c
File metadata and controls
473 lines (376 loc) · 17.6 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
* Copyright (C) 2025 Peter Lawrence
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <unistd.h>
#include <fcntl.h>
#include "parameters.h"
#include "urandom.h"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x))
static const int tcp_server_port = 51111;
static const uint8_t sdp_request[] =
{
0x01, /* V2GTP Version 1 */
0xfe, /* inverted protocol */
0x90, 0x00, /* SDP REQUEST */
0x00, 0x00, 0x00, 0x02, /* payload length */
0x10, /* TLS: no */
0x00, /* TCP protocol */
};
static uint8_t sdp_response[28] =
{
0x01, /* V2GTP Version 1 */
0xfe, /* inverted protocol */
0x90, 0x01, /* SDP RESPONSE */
0x00, 0x00, 0x00, 0x14, /* payload length */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* TBD IPv6 address */
(uint8_t)(tcp_server_port >> 8), (uint8_t)(tcp_server_port >> 0),
0x10, /* TLS: no */
0x00, /* TCP protocol */
};
static struct in6_addr *localip = NULL;
static struct iso1PhysicalValueType EVTargetVoltage, EVTargetCurrent;
static void get_link_local_addr(const char *ifname)
{
struct ifaddrs *ifaddr, *ifa;
static struct in6_addr foundip;
if (getifaddrs(&ifaddr) == -1) return;
for (ifa = ifaddr; ifa; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr->sa_family != AF_INET6) continue;
if (strncmp(ifa->ifa_name, ifname, sizeof(ifname))) continue;
struct sockaddr_in6 *current_addr = (struct sockaddr_in6 *) ifa->ifa_addr;
if (!IN6_IS_ADDR_LINKLOCAL(&(current_addr->sin6_addr))) continue;
memcpy(&foundip, ¤t_addr->sin6_addr, sizeof(current_addr->sin6_addr));
localip = &foundip;
}
freeifaddrs(ifaddr);
}
/* utility function to configure a socket as non-blocking */
static void set_nonblocking(int sock)
{
int opts = fcntl(sock, F_GETFL);
if (opts <= 0) return;
opts = (opts | O_NONBLOCK);
fcntl(sock, F_SETFL, opts);
}
/* utility function to FD_SET and track highest socket */
static int set_reads(fd_set *reads, int sock, int highest_socket)
{
FD_SET(sock, reads);
if (sock > highest_socket) highest_socket = sock;
return highest_socket;
}
/* helper to translate string into OpenV2G format */
static uint16_t set_chars(exi_string_character_t *chars, uint16_t maxlen, char *string)
{
uint16_t len = 0;
while (*string && (len < maxlen))
{
*chars++ = *string++;
len++;
}
return len;
}
int main(int argc, char *argv[])
{
int rc;
uint8_t buffer[4096];
struct sockaddr_in6 server_addr;
const char *ifname = (argc > 1) ? argv[1] : "seth0";
bool handshake_expected;
get_link_local_addr(ifname);
if (!localip)
{
fprintf(stderr, "ERROR: interface (%s) not found\n", ifname);
return -1;
}
urandom_init();
/*
setup SDP server
*/
int sdp_sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (sdp_sock < 0) return -1;
struct ipv6_mreq mreq;
inet_pton(AF_INET6, "ff02::1", &mreq.ipv6mr_multiaddr);
mreq.ipv6mr_interface = if_nametoindex(ifname);
rc = setsockopt(sdp_sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char *) &mreq, sizeof(mreq));
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin6_family = AF_INET6;
server_addr.sin6_addr = in6addr_any;
server_addr.sin6_port = htons(15118);
server_addr.sin6_scope_id = if_nametoindex(ifname);
memcpy(sdp_response + 8, &localip->s6_addr, sizeof(localip->s6_addr));
rc = bind(sdp_sock, (const struct sockaddr *)&server_addr, sizeof(server_addr));
if (rc < 0)
{
fprintf(stderr, "SDP bind error %d\n", rc);
return -1;
}
set_nonblocking(sdp_sock);
/*
setup ISO server
*/
int peer_sock = -1, listen_sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
if (listen_sock < 0) return -1;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin6_family = AF_INET6;
server_addr.sin6_addr = in6addr_any;
server_addr.sin6_port = htons(tcp_server_port);
server_addr.sin6_scope_id = if_nametoindex(ifname);
rc = bind(listen_sock, (const struct sockaddr *)&server_addr, sizeof(server_addr));
if (rc < 0)
{
fprintf(stderr, "ISO bind error %d\n", rc);
return -1;
}
set_nonblocking(listen_sock);
rc = listen(listen_sock, 1);
for (;;)
{
fd_set reads, writes;
FD_ZERO(&reads);
int highest_socket = -1;
highest_socket = set_reads(&reads, sdp_sock, highest_socket);
highest_socket = set_reads(&reads, listen_sock, highest_socket);
if (peer_sock > 0)
highest_socket = set_reads(&reads, peer_sock, highest_socket);
FD_ZERO(&writes);
struct timeval tv = { .tv_sec = 0, .tv_usec = 100000 };
rc = select(highest_socket + 1, &reads, &writes, NULL, &tv);
if (rc < 0) break;
if (0 == rc) continue;
if (FD_ISSET(sdp_sock, &reads))
{
struct sockaddr_in6 client_addr;
socklen_t client_len = sizeof(client_addr);
ssize_t len = recvfrom(sdp_sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&client_addr, &client_len);
printf("recv %d\n", len);
if (len == sizeof(sdp_request))
if (0 == memcmp(buffer, sdp_request, sizeof(sdp_request)))
{
rc = sendto(sdp_sock, sdp_response, sizeof(sdp_response), 0, (struct sockaddr *)&client_addr, client_len);
printf("xmit %d\n", rc);
}
}
if (FD_ISSET(listen_sock, &reads))
{
peer_sock = accept(listen_sock, NULL, NULL);
handshake_expected = true;
}
if (FD_ISSET(peer_sock, &reads))
{
ssize_t len = recv(peer_sock, buffer, sizeof(buffer), 0);
printf("recv %d\n", len);
if (len <= 0)
{
close(peer_sock);
peer_sock = -1;
rc = listen(listen_sock, 1);
}
else
{
bitstream_t streamIn, streamOut;
size_t posi = 0, poso = 0;
int errn;
uint32_t payloadLength;
streamIn.size = len;
streamIn.data = (uint8_t *)buffer;
streamIn.pos = &posi;
streamOut.size = sizeof(buffer);
streamOut.data = buffer;
streamOut.pos = &poso;
errn = read_v2gtpHeader(streamIn.data, &payloadLength);
printf("read %d\n", errn);
if (errn) continue;
*streamIn.pos += V2GTP_HEADER_LENGTH;
if (handshake_expected)
{
struct appHandEXIDocument exiDoc, appHandResp;
errn = decode_appHandExiDocument(&streamIn, &exiDoc);
printf("hand %d\n", errn);
if (errn) continue;
int selectedSchema = -1;
for(int i=0;i<exiDoc.supportedAppProtocolReq.AppProtocol.arrayLen;i++) {
if (exiDoc.supportedAppProtocolReq.AppProtocol.array[i].ProtocolNamespace.charactersLen == (ARRAY_SIZE(iso1string) - 1))
{
int j;
for (j = 0; j < (ARRAY_SIZE(iso1string) - 1); j++)
if (exiDoc.supportedAppProtocolReq.AppProtocol.array[i].ProtocolNamespace.characters[j] != iso1string[j]) break;
if ((ARRAY_SIZE(iso1string) - 1) == j) selectedSchema = exiDoc.supportedAppProtocolReq.AppProtocol.array[i].SchemaID;
}
}
printf("selectedSchema %d\n", selectedSchema);
if (-1 == selectedSchema) continue;
init_appHandEXIDocument(&appHandResp);
appHandResp.supportedAppProtocolRes_isUsed = 1u;
appHandResp.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_OK_SuccessfulNegotiation;
/* signal the protocol by the provided schema id */
appHandResp.supportedAppProtocolRes.SchemaID = selectedSchema;
appHandResp.supportedAppProtocolRes.SchemaID_isUsed = 1u;
*streamOut.pos = V2GTP_HEADER_LENGTH;
errn = encode_appHandExiDocument(&streamOut, &appHandResp);
printf("handenc %d\n", errn);
if (errn) continue;
errn = write_v2gtpHeader(streamOut.data, *streamOut.pos-V2GTP_HEADER_LENGTH, V2GTP_EXI_TYPE);
printf("handwrite %d %d\n", errn, (int)*streamOut.pos);
handshake_expected = false;
}
else
{
struct iso1EXIDocument exiIn, exiOut;
/* OpenV2G init_ helpers don't clear everything; trust only memset */
memset(&exiOut, 0, sizeof(exiOut));
memset(&exiIn, 0, sizeof(exiIn));
decode_iso1ExiDocument(&streamIn, &exiIn);
printf("iso %d %d %d\n", errn, exiIn.V2G_Message_isUsed, exiIn.V2G_Message.Body.SessionSetupReq_isUsed);
if (!exiIn.V2G_Message_isUsed) continue;
exiOut.V2G_Message_isUsed = 1u;
/*
NOTE: the following V2G message types are deliberately not handled:
ServiceDetailReq (opt VAS)
MeteringReceiptReq (opt Metering)
CertificateUpdateReq (opt Certificate Installation)
CertificateInstallationReq (opt Certificate Update)
ChargingStatusReq (specific to AC charging)
*/
if (exiIn.V2G_Message.Body.SessionSetupReq_isUsed) {
exiOut.V2G_Message.Header.SessionID.bytesLen = 8;
urandom_get(exiOut.V2G_Message.Header.SessionID.bytes, exiOut.V2G_Message.Header.SessionID.bytesLen);
exiOut.V2G_Message.Body.SessionSetupRes_isUsed = 1u;
struct iso1SessionSetupResType *body = &exiOut.V2G_Message.Body.SessionSetupRes;
body->ResponseCode = iso1responseCodeType_OK;
body->EVSEID.charactersLen = set_chars(body->EVSEID.characters, ARRAY_SIZE(body->EVSEID.characters), "ZZ00000");
body->EVSETimeStamp_isUsed = 0u;
} else if (exiIn.V2G_Message.Body.ServiceDiscoveryReq_isUsed) {
exiOut.V2G_Message.Body.ServiceDiscoveryRes_isUsed = 1u;
struct iso1ServiceDiscoveryResType *body = &exiOut.V2G_Message.Body.ServiceDiscoveryRes;
body->ServiceList_isUsed = 0u;
body->ResponseCode = iso1responseCodeType_OK;
body->PaymentOptionList.PaymentOption.array[0] = iso1paymentOptionType_ExternalPayment;
body->PaymentOptionList.PaymentOption.arrayLen = 1;
body->ChargeService.ServiceID = 1;
body->ChargeService.ServiceCategory = iso1serviceCategoryType_EVCharging;
body->ChargeService.FreeService = 1;
body->ChargeService.SupportedEnergyTransferMode.EnergyTransferMode.arrayLen = 1;
body->ChargeService.SupportedEnergyTransferMode.EnergyTransferMode.array[0] = dcmode;
} else if (exiIn.V2G_Message.Body.PaymentServiceSelectionReq_isUsed) {
exiOut.V2G_Message.Body.PaymentServiceSelectionRes_isUsed = 1u;
exiOut.V2G_Message.Body.ServiceDetailRes.ResponseCode = iso1responseCodeType_OK;
} else if (exiIn.V2G_Message.Body.PaymentDetailsReq_isUsed) {
exiOut.V2G_Message.Body.PaymentDetailsRes_isUsed = 1u;
exiOut.V2G_Message.Body.PaymentDetailsRes.ResponseCode = iso1responseCodeType_OK;
} else if (exiIn.V2G_Message.Body.AuthorizationReq_isUsed) {
exiOut.V2G_Message.Body.AuthorizationRes_isUsed = 1u;
exiOut.V2G_Message.Body.AuthorizationRes.ResponseCode = iso1responseCodeType_OK;
exiOut.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished;
} else if (exiIn.V2G_Message.Body.ChargeParameterDiscoveryReq_isUsed) {
exiOut.V2G_Message.Body.ChargeParameterDiscoveryRes_isUsed = 1u;
bool compatible = (dcmode == exiIn.V2G_Message.Body.ChargeParameterDiscoveryReq.RequestedEnergyTransferMode);
struct iso1ChargeParameterDiscoveryResType *body = &exiOut.V2G_Message.Body.ChargeParameterDiscoveryRes;
body->ResponseCode = (compatible) ? iso1responseCodeType_OK : iso1responseCodeType_FAILED_WrongEnergyTransferMode;
body->EVSEProcessing = iso1EVSEProcessingType_Finished;
body->DC_EVSEChargeParameter_isUsed = 1;
body->DC_EVSEChargeParameter = dccharge;
} else if (exiIn.V2G_Message.Body.PowerDeliveryReq_isUsed) {
exiOut.V2G_Message.Body.PowerDeliveryRes_isUsed = 1u;
struct iso1PowerDeliveryResType *body = &exiOut.V2G_Message.Body.PowerDeliveryRes;
body->ResponseCode = iso1responseCodeType_OK;
body->EVSEStatus_isUsed = 1;
body->EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
body->EVSEStatus.NotificationMaxDelay = max_delay;
} else if (exiIn.V2G_Message.Body.SessionStopReq_isUsed) {
exiOut.V2G_Message.Body.SessionStopRes_isUsed = 1u;
exiOut.V2G_Message.Body.SessionStopRes.ResponseCode = iso1responseCodeType_OK;
} else if (exiIn.V2G_Message.Body.CableCheckReq_isUsed) {
exiOut.V2G_Message.Body.CableCheckRes_isUsed = 1u;
struct iso1CableCheckResType *body = &exiOut.V2G_Message.Body.CableCheckRes;
body->ResponseCode = iso1responseCodeType_OK;
body->DC_EVSEStatus.NotificationMaxDelay = max_delay;
body->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
body->EVSEProcessing = iso1EVSEProcessingType_Finished;
} else if (exiIn.V2G_Message.Body.PreChargeReq_isUsed) {
/* jot down the targets */
EVTargetVoltage = exiIn.V2G_Message.Body.PreChargeReq.EVTargetVoltage;
EVTargetCurrent = exiIn.V2G_Message.Body.PreChargeReq.EVTargetCurrent;
exiOut.V2G_Message.Body.PreChargeRes_isUsed = 1u;
struct iso1PreChargeResType *body = &exiOut.V2G_Message.Body.PreChargeRes;
body->ResponseCode = iso1responseCodeType_OK;
body->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
body->DC_EVSEStatus.NotificationMaxDelay = max_delay;
body->EVSEPresentVoltage = EVTargetVoltage;
} else if (exiIn.V2G_Message.Body.PowerDeliveryReq_isUsed) {
exiOut.V2G_Message.Body.PowerDeliveryRes_isUsed = 1u;
struct iso1PowerDeliveryResType *body = &exiOut.V2G_Message.Body.PowerDeliveryRes;
body->ResponseCode = iso1responseCodeType_OK;
body->DC_EVSEStatus_isUsed = 1;
body->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
body->DC_EVSEStatus.NotificationMaxDelay = max_delay;
} else if (exiIn.V2G_Message.Body.WeldingDetectionReq_isUsed) {
exiOut.V2G_Message.Body.WeldingDetectionRes_isUsed = 1u;
struct iso1WeldingDetectionResType *body = &exiOut.V2G_Message.Body.WeldingDetectionRes;
body->ResponseCode = iso1responseCodeType_OK;
body->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
body->DC_EVSEStatus.NotificationMaxDelay = max_delay;
body->EVSEPresentVoltage = EVTargetVoltage;
} else if (exiIn.V2G_Message.Body.CurrentDemandReq_isUsed) {
/* jot down the targets */
EVTargetVoltage = exiIn.V2G_Message.Body.CurrentDemandReq.EVTargetVoltage;
EVTargetCurrent = exiIn.V2G_Message.Body.CurrentDemandReq.EVTargetCurrent;
exiOut.V2G_Message.Body.CurrentDemandRes_isUsed = 1u;
struct iso1CurrentDemandResType *body = &exiOut.V2G_Message.Body.CurrentDemandRes;
body->ResponseCode = iso1responseCodeType_OK;
body->DC_EVSEStatus.NotificationMaxDelay = max_delay;
body->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
body->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
body->EVSEPresentVoltage = EVTargetVoltage;
body->EVSEPresentCurrent = EVTargetCurrent;
} else {
printf("unhandled\n");
continue;
}
/* if the value has not already been written, echo back the provided SessionID (if any) */
if (!exiOut.V2G_Message.Header.SessionID.bytesLen && exiIn.V2G_Message.Header.SessionID.bytesLen)
{
exiOut.V2G_Message.Header.SessionID.bytesLen = exiIn.V2G_Message.Header.SessionID.bytesLen;
memcpy(exiOut.V2G_Message.Header.SessionID.bytes, exiIn.V2G_Message.Header.SessionID.bytes, exiOut.V2G_Message.Header.SessionID.bytesLen);
}
*streamOut.pos = V2GTP_HEADER_LENGTH;
errn = encode_iso1ExiDocument(&streamOut, &exiOut);
printf("isoenc %d\n", errn);
if (errn) continue;
errn = write_v2gtpHeader(streamOut.data, *streamOut.pos-V2GTP_HEADER_LENGTH, V2GTP_EXI_TYPE);
printf("isowrite %d %d\n", errn, (int)*streamOut.pos);
}
send(peer_sock, streamOut.data, *streamOut.pos, 0);
}
}
}
close(sdp_sock);
close(listen_sock);
if (peer_sock > 0) close(peer_sock);
urandom_deinit();
return 0;
}