Safe Communication Framework 0.1.0 build feea4f9
ERTMS/ETCS safe communication (Subset-037 EuroRadio, Subset-098 RBC/RBC), CENELEC EN 50128 SIL 4 oriented
Loading...
Searching...
No Matches
ale.c
Go to the documentation of this file.
1
9#include "safecomm/ale/ale.h"
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14
16static bool parse_peer_line(const char *line, safecomm_ale_peer_t *out)
17{
18 unsigned long peer_id;
19 unsigned long port;
20 char a_host[64];
21 char b_host[64];
22 int consumed;
23
24 consumed = sscanf(line, " %lu %63s %63s %lu", &peer_id, a_host, b_host, &port);
25 if (consumed != 4)
26 {
27 return false;
28 }
29 if ((peer_id > 0xFFFFFFFFUL) || (port == 0UL) || (port > 65535UL))
30 {
31 return false;
32 }
33 (void)memset(out, 0, sizeof(*out));
34 out->peer_id = (uint32_t)peer_id;
35 (void)snprintf(out->ab_a_host, sizeof(out->ab_a_host), "%s", a_host);
36 (void)snprintf(out->ab_b_host, sizeof(out->ab_b_host), "%s", b_host);
37 out->ab_port = (uint16_t)port;
38 out->in_use = true;
39 return true;
40}
41
42static safecomm_status_t load_peer_map(const char *path, safecomm_ale_t *ale)
43{
44 FILE *fp = fopen(path, "re");
45 char line[256];
46
47 if (fp == NULL)
48 {
50 }
51 while ((fgets(line, (int)sizeof(line), fp) != NULL) && (ale->peer_count < SAFECOMM_ALE_MAX_PEERS))
52 {
53 size_t i = 0U;
54
55 while ((line[i] == ' ') || (line[i] == '\t'))
56 {
57 i++;
58 }
59 if ((line[i] == '#') || (line[i] == '\n') || (line[i] == '\0') || (line[i] == '\r'))
60 {
61 continue;
62 }
63 if (parse_peer_line(&line[i], &ale->peers[ale->peer_count]))
64 {
65 ale->peer_count++;
66 }
67 }
68 (void)fclose(fp);
69 return SAFECOMM_STATUS_OK;
70}
71
73{
74 const char *host;
75 const char *peer_map;
76 uint32_t u;
78
79 if ((cfg == NULL) || (out == NULL))
80 {
82 }
83 (void)memset(out, 0, sizeof(*out));
84
85 host = safecomm_config_get_str(cfg, "ale.listen_host");
86 if ((host != NULL) && (strlen(host) < sizeof(out->listen_host)))
87 {
88 (void)strcpy(out->listen_host, host);
89 }
90 st = safecomm_config_get_u32(cfg, "ale.listen_port", &u);
91 if ((st == SAFECOMM_STATUS_OK) && (u <= 0xFFFFU))
92 {
93 out->listen_port = (uint16_t)u;
94 }
95
96 peer_map = safecomm_config_get_str(cfg, "ale.peer_map_file");
97 if (peer_map != NULL)
98 {
99 return load_peer_map(peer_map, out);
100 }
101 return SAFECOMM_STATUS_OK; /* no peer map configured — an empty table is valid */
102}
103
104const safecomm_ale_peer_t *safecomm_ale_lookup(const safecomm_ale_t *ale, uint32_t peer_id)
105{
106 uint32_t i;
107
108 if (ale == NULL)
109 {
110 return NULL;
111 }
112 for (i = 0U; i < ale->peer_count; i++)
113 {
114 if (ale->peers[i].in_use && (ale->peers[i].peer_id == peer_id))
115 {
116 return &ale->peers[i];
117 }
118 }
119 return NULL;
120}
static bool parse_peer_line(const char *line, safecomm_ale_peer_t *out)
Definition ale.c:16
Application Layer Entity (ALE) — NON-VITAL, runs in the Gateway.
safecomm_status_t safecomm_ale_from_config(const safecomm_config_t *cfg, safecomm_ale_t *out)
Build an ALE from a loaded config (ale.* keys) + its peer map.
Definition ale.c:72
#define SAFECOMM_ALE_MAX_PEERS
Max concurrent peer connections one ALE instance tracks.
Definition ale.h:38
const safecomm_ale_peer_t * safecomm_ale_lookup(const safecomm_ale_t *ale, uint32_t peer_id)
Resolve a peer id to its A/B endpoint pair.
Definition ale.c:104
const char * safecomm_config_get_str(const safecomm_config_t *cfg, const char *key)
Raw string value for key.
Definition config.c:141
safecomm_status_t safecomm_config_get_u32(const safecomm_config_t *cfg, const char *key, uint32_t *out)
Unsigned-integer value for key.
Definition config.c:159
safecomm_status_t
Result of a Safe Communication Framework operation.
Definition status.h:17
@ SAFECOMM_STATUS_INVALID_PARAM
Definition status.h:19
@ SAFECOMM_STATUS_OK
Definition status.h:18
@ SAFECOMM_STATUS_TRANSPORT
Definition status.h:23
One routed peer: external bearer <-> the A/B endpoint pair.
Definition ale.h:42
uint32_t peer_id
Definition ale.h:43
An ALE instance. Caller-allocated, opaque.
Definition ale.h:52
A loaded configuration file. Caller-allocated, opaque in use.
Definition config.h:66