forked from antran-ciena/Waveserver-Mini
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.h
More file actions
221 lines (187 loc) · 5.47 KB
/
common.h
File metadata and controls
221 lines (187 loc) · 5.47 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
#pragma once
#include <arpa/inet.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <time.h>
#define PORT_MANAGER_UDP (5001)
#define CONN_MANAGER_UDP (5002)
#define TRAFFIC_MGR_UDP (5003)
#define MAX_UDP_MSG_SIZE (512)
#define MAX_CONN_NAME_CHARACTER (32)
#define MAX_OTN_PAYLOAD_SIZE (128)
#define MAX_CONNS (4)
#define LOG_FILE_PATH "wsmini.log"
////// LOGGING ////
typedef enum
{
LOG_DEBUG = 0,
LOG_INFO,
LOG_WARN,
LOG_ERROR
} log_level_t;
void log_init(const char *service);
void log_write(log_level_t level, const char *file, int line, const char *fmt,
...);
#define LOG(level, fmt, ...) \
log_write((level), __FILE__, __LINE__, (fmt), ##__VA_ARGS__);
/////// Port Info ////
#define MAX_PORT_NUM (6)
#define MAX_LINE_PORTS (2)
#define MAX_CLIENT_PORTS (4)
typedef enum
{
PORT_DOWN = 0,
PORT_UP
} port_state_t;
typedef enum
{
LINE_PORT = 0,
CLIENT_PORT
} port_type_t;
typedef struct
{
uint8_t id;
port_type_t type;
port_state_t operational_state;
bool admin_enabled;
bool fault_active;
uint32_t rx_frames;
uint32_t dropped_frames;
} port_t;
/////// CONN INFO /////
typedef enum
{
CONN_DOWN = 0,
CONN_UP
} conn_state_t;
typedef struct
{
char conn_name[MAX_CONN_NAME_CHARACTER];
uint8_t client_port;
uint8_t line_port;
conn_state_t operational_state;
} conn_t;
/////// OTN ///////
typedef struct {
uint8_t client_port;
uint8_t line_port;
uint32_t frame_id;
} otn_header_t;
typedef struct {
otn_header_t header;
char data[MAX_OTN_PAYLOAD_SIZE];
} otn_frame_t;
typedef struct {
uint32_t next_frame_id;
uint32_t total_dropped;
uint32_t total_forwarded;
bool running;
uint8_t client_port;
uint8_t line_port;
} traffic_stats_t;
/////// Message types /////
typedef enum
{
// Port Manager messages
MSG_GET_PORT_INFO = 0, // Conn Mgr / CLI → Port Mgr : port_info_request_t → port_t
MSG_UPDATE_COUNTERS, // Traffic → Port Mgr : counter_update_t → (no reply)
MSG_PORT_STATE_CHANGE, // Port Mgr → Conn Mgr : port_state_change_t → (no reply)
// CLI → Port Manager
MSG_SET_PORT, // CLI → Port Mgr : enable a port (request → reply)
MSG_DELETE_PORT, // CLI → Port Mgr : disable a port (request → reply)
MSG_INJECT_FAULT, // CLI → Port Mgr : simulate signal loss (request → reply)
MSG_CLEAR_FAULT, // CLI → Port Mgr : clear a fault (request → reply)
// Connection Manager messages
MSG_LOOKUP_CONNECTION, // Traffic → Conn Mgr : route_lookup_request_t → route_lookup_reply_t
MSG_CREATE_CONN, // CLI → Conn Mgr : create a connection (request → reply)
MSG_DELETE_CONN, // CLI → Conn Mgr : delete a connection (request → reply)
MSG_GET_CONNECTIONS, // CLI → Conn Mgr : get all connections (request → reply)
// Traffic Manager messages
MSG_GET_TRAFFIC_STATS, // CLI → Traffic Mgr : get traffic counters and if traffic up/down (request → reply)
MSG_START_TRAFFIC, // CLI → Traffic Mgr : start frame generation (request → reply)
MSG_STOP_TRAFFIC, // CLI → Traffic Mgr : stop frame generation (request → reply)
} msg_type_t;
typedef enum
{
STATUS_REQUEST = 0,
STATUS_SUCCESS,
STATUS_FAILURE
} msg_status_t;
typedef struct
{
uint8_t msg_type; // msg_type_t
uint8_t status;
char payload[MAX_UDP_MSG_SIZE];
} udp_message_t;
// MSG_SET_PORT, MSG_DELETE_PORT, MSG_INJECT_FAULT, MSG_CLEAR_FAULT
typedef struct
{
uint8_t port_id;
} udp_port_cmd_request_t;
//MSG_SET_PORT, MSG_DELETE_PORT, MSG_INJECT_FAULT, MSG_CLEAR_FAULT reply
// MSG_CREATE_CONN and MSG_DELETE_CONN
typedef struct
{
char error_msg[64]; // populated on STATUS_FAILURE, empty on success
} udp_cmd_reply_t;
// MSG_UPDATE_COUNTERS (fire-and-forget)
typedef struct
{
uint8_t port_id;
uint32_t pkts_rx;
uint32_t pkts_dropped;
} udp_counter_update_t;
// MSG_PORT_STATE_CHANGE (fire_and_forget)
typedef struct
{
uint8_t port_id;
uint8_t operational_state; // port_state_t
} udp_port_state_change_t;
// MSG_LOOKUP_CONNECTION request
typedef struct
{
uint8_t client_port;
uint8_t line_port;
} udp_route_lookup_request_t;
// MSG_LOOKUP_CONNECTION reply
typedef struct
{
char conn_name[MAX_CONN_NAME_CHARACTER];
uint8_t operational_state; // conn_state_t
} udp_route_lookup_reply_t;
// MSG_CREATE_CONN request
typedef struct
{
char name[MAX_CONN_NAME_CHARACTER];
uint8_t client_port;
uint8_t line_port;
} udp_create_conn_request_t;
// MSG_DELETE_CONN request
typedef struct
{
char name[MAX_CONN_NAME_CHARACTER];
} udp_delete_conn_request_t;
// MSG_GET_CONNECTIONS request
typedef struct
{
conn_t all_connections[MAX_CONNS];
uint8_t conn_count;
} udp_get_connections_reply_t;
// MSG_START_TRAFFIC request
typedef struct
{
uint8_t client_port; // 0 = random (3-6)
uint8_t line_port; // 0 = random (1-2)
} udp_start_traffic_request_t;
////// Shared functions /////
int create_udp_server(uint16_t);
int create_udp_client();
void set_error_msg(udp_message_t *resp, const char *msg);
// Fire-and-forget: send a message, don't wait for a reply
void send_udp_message_one_way(int sock, udp_message_t *msg, uint16_t dest_port);
// Request-reply: send a message and wait for a response. Returns true on success.
bool send_udp_message_and_receive(int sock, udp_message_t *req, udp_message_t *resp, uint16_t dest_port);