Skip to content

Commit a66444e

Browse files
committed
Add docs
1 parent 83ede0a commit a66444e

File tree

1 file changed

+149
-29
lines changed

1 file changed

+149
-29
lines changed

dtls/server-dtls-demux.c

Lines changed: 149 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
#define CID_SIZE 8
4747

4848
static int intCalled = 0;
49+
50+
/**
51+
* \brief Signal handler for teardown.
52+
*
53+
* \param signum Signal number.
54+
*/
4955
static void teardown(int signum)
5056
{
5157
intCalled = 1;
@@ -57,50 +63,158 @@ static void teardown(int signum)
5763
* important to check because we want to limit the ability for malicious clients
5864
* to stall and use up server resources. */
5965

66+
/**
67+
* \struct ConnList
68+
* \brief Structure to hold connection information.
69+
*/
6070
struct ConnList {
61-
WOLFSSL* ssl;
62-
time_t t_started;
63-
struct ConnList* next;
71+
WOLFSSL* ssl; /**< WOLFSSL object for the connection */
72+
time_t t_started; /**< Time when the connection started */
73+
int id; /**< ID number of the connection */
74+
struct ConnList* next; /**< Pointer to the next connection in the list */
6475
};
65-
/* Timeout is multiplied by two on every timeout. The initial and maximum
66-
* timeouts can be set using:
67-
* int wolfSSL_dtls_set_timeout_init(WOLFSSL* ssl, int timeout)
68-
* int wolfSSL_dtls_set_timeout_max(WOLFSSL* ssl, int timeout) */
76+
77+
/**
78+
* \struct DtlsTimeout
79+
* \brief Structure to hold timeout information.
80+
*/
6981
struct DtlsTimeout {
70-
struct timespec ts;
71-
struct ConnList* conn;
72-
struct DtlsTimeout* next;
82+
struct timespec ts; /**< Time when the timeout should occur */
83+
struct ConnList* conn; /**< Pointer to the connection associated with the timeout */
84+
struct DtlsTimeout* next; /**< Pointer to the next timeout in the list */
7385
};
7486

75-
/* Our context factory */
87+
/**
88+
* \brief Create a new WOLFSSL_CTX object.
89+
*
90+
* \return Pointer to the new WOLFSSL_CTX object, or NULL on error.
91+
*/
7692
WOLFSSL_CTX* newCTX(void);
77-
/* Our ssl factory */
93+
94+
/**
95+
* \brief Create a new WOLFSSL object.
96+
*
97+
* \param ctx Pointer to the WOLFSSL_CTX object.
98+
* \param fd File descriptor for the socket.
99+
* \param rng Pointer to the random number generator.
100+
* \param connList Pointer to the list of connections.
101+
*
102+
* \return Pointer to the new WOLFSSL object, or NULL on error.
103+
*/
78104
WOLFSSL* newSSL(WOLFSSL_CTX* ctx, int fd, WC_RNG* rng, struct ConnList* connList);
79-
/* Our socket factory */
105+
106+
/**
107+
* \brief Create a new socket.
108+
*
109+
* \return File descriptor for the new socket, or INVALID_SOCKET on error.
110+
*/
80111
int newFD(void);
81112

82-
/* Create connection and add it to the connList */
113+
/**
114+
* \brief Create a new connection and add it to the connection list.
115+
*
116+
* \param ssl Pointer to the WOLFSSL object.
117+
* \param connList Pointer to the list of connections.
118+
*
119+
* \return Pointer to the new connection, or NULL on error.
120+
*/
83121
struct ConnList* newConn(WOLFSSL* ssl, struct ConnList** connList);
84-
/* Free connection. Remove it from connList and clear any timeouts in tList */
122+
123+
/**
124+
* \brief Free a connection and remove it from the connection list.
125+
*
126+
* \param connList Pointer to the list of connections.
127+
* \param conn Pointer to the connection to be freed.
128+
* \param tList Pointer to the list of timeouts.
129+
*/
85130
void freeConn(struct ConnList** connList, struct ConnList* conn, struct DtlsTimeout** tList);
86-
/* Try to find the matching connection in connList. We look based on the connection ID or the peer address. */
131+
132+
/**
133+
* \brief Find a connection in the connection list based on the connection ID or peer address
134+
*
135+
* \param connList Pointer to the list of connections.
136+
* \param msg Pointer to the message.
137+
* \param sz Size of the message.
138+
* \param peerAddr Pointer to the peer address.
139+
* \param peerAddrLen Length of the peer address.
140+
*
141+
* \return Pointer to the matching connection, or NULL if not found.
142+
*/
87143
struct ConnList* findConn(struct ConnList* connList, byte* msg, ssize_t sz, struct sockaddr* peerAddr, socklen_t peerAddrLen);
88-
/* Handle existing connection */
89-
int dispatchExistingConnection(struct ConnList* conn, byte* msg, ssize_t msgSz, struct sockaddr* peerAddr,
90-
socklen_t peerAddrLen);
91-
/* Handle new connection */
144+
145+
/**
146+
* \brief Handle an existing connection.
147+
*
148+
* \param conn Pointer to the connection.
149+
* \param msg Pointer to the message.
150+
* \param msgSz Size of the message.
151+
* \param peerAddr Pointer to the peer address.
152+
* \param peerAddrLen Length of the peer address.
153+
*
154+
* \return 1 on success, 0 on error.
155+
*/
156+
int dispatchExistingConnection(struct ConnList* conn, byte* msg, ssize_t msgSz, struct sockaddr* peerAddr, socklen_t peerAddrLen);
157+
158+
/**
159+
* \brief Handle a new connection.
160+
*
161+
* \param ssl Pointer to the WOLFSSL object.
162+
* \param msg Pointer to the message.
163+
* \param msgSz Size of the message.
164+
* \param peerAddr Pointer to the peer address.
165+
* \param peerAddrLen Length of the peer address.
166+
*
167+
* \return WOLFSSL_SUCCESS on success, WOLFSSL_FATAL_ERROR on error.
168+
*/
92169
int dispatchNewConnection(WOLFSSL* ssl, byte* msg, ssize_t msgSz, struct sockaddr* peerAddr, socklen_t peerAddrLen);
93170

94-
/* Return the next timeout in milliseconds. Returns -1 if no timeout set. */
171+
/**
172+
* \brief Return the next timeout in milliseconds.
173+
*
174+
* \param t Pointer to the list of timeouts.
175+
*
176+
* \return Next timeout in milliseconds, or -1 if no timeout set.
177+
*/
95178
int getNextTimeout(struct DtlsTimeout* t);
96-
/* Register the next timeout for conn in the list out */
179+
180+
/**
181+
* \brief Register the next timeout for a connection.
182+
*
183+
* \param out Pointer to the list of timeouts.
184+
* \param conn Pointer to the connection.
185+
*
186+
* \return 1 on success, 0 on error.
187+
*/
97188
int registerTimeout(struct DtlsTimeout** out, struct ConnList* conn);
98-
/* Free any timeouts associated with conn in out */
189+
190+
/**
191+
* \brief Free any timeouts associated with a connection.
192+
*
193+
* \param out Pointer to the list of timeouts.
194+
* \param conn Pointer to the connection.
195+
*/
99196
void freeTimeouts(struct DtlsTimeout** out, struct ConnList* conn);
100-
/* Handle timeout that occured for conn */
197+
198+
/**
199+
* \brief Handle a timeout that occurred for a connection.
200+
*
201+
* \param conn Pointer to the connection.
202+
*
203+
* \return WOLFSSL_SUCCESS on success, -1 on error.
204+
*/
101205
int handleTimeout(struct ConnList* conn);
102206

103-
static int handleApplicationData(WOLFSSL* ssl, byte* appData, int appDataSz)
207+
/**
208+
* \brief Handle application data received from a peer.
209+
*
210+
* \param ssl Pointer to the WOLFSSL object.
211+
* \param appData Pointer to the application data.
212+
* \param appDataSz Size of the application data.
213+
* \param id ID number of the connection.
214+
*
215+
* \return Number of bytes written, or a negative value on error.
216+
*/
217+
static int handleApplicationData(WOLFSSL* ssl, byte* appData, int appDataSz, int id)
104218
{
105219
/* Process app data from peer. For this example just echo it */
106220
const void* peer = NULL;
@@ -111,11 +225,15 @@ static int handleApplicationData(WOLFSSL* ssl, byte* appData, int appDataSz)
111225
peerName = inet_ntoa(((struct sockaddr_in *)peer)->sin_addr);
112226
peerPort = ntohs(((struct sockaddr_in *)peer)->sin_port);
113227
}
114-
printf("%s:%d wrote: %.*s\n", peerName, peerPort, appDataSz, appData);
228+
printf("(#%d) from %s:%d wrote: %.*s\n", id, peerName, peerPort, appDataSz, appData);
115229
return wolfSSL_write(ssl, appData, appDataSz);
116-
117230
}
118231

232+
/**
233+
* \brief Main function for the DTLS server.
234+
*
235+
* \return 0 on success, non-zero on error.
236+
*/
119237
int main(void)
120238
{
121239
int exitVal = 1;
@@ -137,7 +255,7 @@ int main(void)
137255
listenfd.events = POLLIN;
138256

139257
/* Uncomment if you want debugging. */
140-
// wolfSSL_Debugging_ON();
258+
// wolfSSL_Debugging_ON();
141259

142260
if ((rng = wc_rng_new(NULL, 0, NULL)) == NULL) {
143261
fprintf(stderr, "wc_rng_new error.\n");
@@ -403,11 +521,13 @@ int newFD(void)
403521
struct ConnList* newConn(WOLFSSL* ssl, struct ConnList** connList)
404522
{
405523
struct ConnList* conn = (struct ConnList*)malloc(sizeof(struct ConnList));
524+
static int id = 0;
406525
if (conn == NULL)
407526
return NULL;
408527
conn->ssl = ssl;
409528
conn->t_started = time(NULL);
410529
conn->next = *connList;
530+
conn->id = id++;
411531
*connList = conn;
412532
return conn;
413533
}
@@ -484,7 +604,7 @@ int dispatchExistingConnection(struct ConnList* conn, byte* msg, ssize_t msgSz,
484604
/* re-use msg buffer since output will always be smaller than input */
485605
int readSz = ret = wolfSSL_read(conn->ssl, msg, msgSz);
486606
if (ret > 0) {
487-
ret = handleApplicationData(conn->ssl, msg, readSz);
607+
ret = handleApplicationData(conn->ssl, msg, readSz, conn->id);
488608
if (ret <= 0)
489609
return 0;
490610
}

0 commit comments

Comments
 (0)