aboutsummaryrefslogtreecommitdiffstats
path: root/demos/ATSAMA5D2/RT-SAMA5D2-XPLAINED-NSEC/daemons/tssockskel.c
blob: c3d6d3cacaa72ae0026fb009ce617ab622d11c1b (plain)
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
/*
    ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

/**
 * @file    tssockskel.c
 * @brief   Sockets skeleton daemon for trusted clients.
 *
 */

#include "ch.h"
#include "chobjfifos.h"
#include "tsclient.h"
#include "tsdaemonskels.h"
#include "tssockskel.h"
#include <string.h>

/*===========================================================================*/
/* Module local definitions.                                                 */
/*===========================================================================*/

/*===========================================================================*/
/* Module exported variables.                                                */
/*===========================================================================*/

/*===========================================================================*/
/* Module local types.                                                       */
/*===========================================================================*/

/*===========================================================================*/
/* Module local variables.                                                   */
/*===========================================================================*/
static skel_ctx_t skel_ctx;

/*===========================================================================*/
/* Module local functions.                                                   */
/*===========================================================================*/

/**
 * @name    Sockets API skeletons.
 * @{
 */

/**
 * @brief int socket(int domain, int type, int protocol)
 */
static void l_socket(skel_req_t *skreqp) {
  int result;

  /* call the api exposed by the TCP/IP stack.*/
  result = socket((int)skreqp->stub_op_p[0],
                  (int)skreqp->stub_op_p[1],
                  (int)skreqp->stub_op_p[2]);

  /* report the result and copy the 'out' parameters.*/
  returnToRemote(skreqp, result);
}

/**
 * @brief int connect(int s, const struct sockaddr *name, socklen_t namelen)
 */
static void l_connect(skel_req_t *skreqp) {
  int s, result, socklen;
  struct sockaddr sockaddr;

  s = (int)skreqp->stub_op_p[0];
  skreqp->stub_op_p[1] = (uint32_t)&sockaddr;
  socklen = (int)skreqp->stub_op_p[2];

  /* Call the stub service in order to copy the 'in' parameter
     sockaddr.*/
  paramsInFromRemote(skreqp);

  /* Call the api exposed by the TCP/IP stack.*/
  result = connect(s, &sockaddr, socklen);

  /* Report the result.*/
  returnToRemote(skreqp, result);
}

/**
 * @brief int close(int s)
 */
static void l_close(skel_req_t *skreqp) {
  int result;

  /* Call the api exposed by the TCP/IP stack.*/
  result = close((int)skreqp->stub_op_p[0]);

  /* report the result.*/
  returnToRemote(skreqp, result);
}


/**
 * @brief int recv(int s, void *mem, size_t len, int flags)
 */
static void l_recv(skel_req_t *skreqp) {
  int result;
  void *mem;
  size_t len;

  len = skreqp->stub_op_p[2];

  /* Allocate the space for the receive buffer.*/
  mem = chHeapAlloc(NULL, len);
  if (NULL == mem) {
    result = ENOMEM;
  } else {

    /* call the api exposed by the TCP/IP stack.*/
    result = recv((int)skreqp->stub_op_p[0], mem, len,
        (int)skreqp->stub_op_p[3]);
    skreqp->stub_op_p_sz[1] = result;
    skreqp->stub_op_p[1] = (uint32_t)mem;
  }

  /* report the result and copy 'out' parameter mem.*/
  returnToRemote(skreqp, result);
  if (NULL != mem)
    chHeapFree(mem);
}

/**
 * @brief int send(int s, const void *dataptr, size_t size, int flags)
 */
static void l_send(skel_req_t *skreqp) {
  int result;
  void *dataptr;
  size_t size;

  size = skreqp->stub_op_p[2];

  /* Allocate the space for the send buffer.*/
  dataptr = chHeapAlloc(NULL, size);
  if (NULL == dataptr) {
    result = ENOMEM;
  } else {
    skreqp->stub_op_p[1] = (uint32_t)dataptr;

    /* call the stub service in order to copy the
       'in' parameter dataptr.*/
    paramsInFromRemote(skreqp);

    /* call the api exposed by the TCP/IP stack.*/
    result = send((int)skreqp->stub_op_p[0], dataptr, size,
        (int)skreqp->stub_op_p[3]);
    chHeapFree(dataptr);
  }

  /* report the result.*/
  returnToRemote(skreqp, result);
}


/**
 * @brief int select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
 *                    struct timeval *timeout)
 */
static void l_select(skel_req_t *skreqp) {
  int result;
  int maxfdpl;
  fd_set readset, writeset, exceptset;
  struct timeval timeout;

  maxfdpl = skreqp->stub_op_p[0];

  skreqp->stub_op_p_sz[1] = sizeof (fd_set);
  skreqp->stub_op_p_sz[2] = sizeof (fd_set);
  skreqp->stub_op_p_sz[3] = sizeof (fd_set);
  skreqp->stub_op_p[1] = (uint32_t)&readset;
  skreqp->stub_op_p[2] = (uint32_t)&writeset;
  skreqp->stub_op_p[3] = (uint32_t)&exceptset;
  skreqp->stub_op_p[4] = (uint32_t)&timeout;

  /* call the stub service in order to copy the
     'in' parameter readset, writeset, exceptset and timeout.*/
  paramsInFromRemote(skreqp);

  /* call the api exposed by the TCP/IP stack.*/
  result = select(maxfdpl, &readset, &writeset, &exceptset, &timeout);

  /* report the result and the parameters readset, writeset and exceptset.*/
  returnToRemote(skreqp, result);
}

/**
 * @brief bind(int s, const struct sockaddr *name, socklen_t namelen);
 */
static void l_bind(skel_req_t *skreqp) {
  int s, result, socklen;
  struct sockaddr sockaddr;

  s = (int)skreqp->stub_op_p[0];
  skreqp->stub_op_p[1] = (uint32_t)&sockaddr;
  socklen = (int)skreqp->stub_op_p[2];

  /* Call the stub service in order to copy the 'in' parameter
     sockaddr.*/
  paramsInFromRemote(skreqp);

  /* Call the api exposed by the TCP/IP stack.*/
  result = bind(s, &sockaddr, socklen);

  /* Report the result.*/
  returnToRemote(skreqp, result);
}

/**
 * @brief listen(int s, int backlog);
 */
static void l_listen(skel_req_t *skreqp) {
  int s, result, backlog;

  s = (int)skreqp->stub_op_p[0];
  backlog = (int)skreqp->stub_op_p[1];

  /* Call the api exposed by the TCP/IP stack.*/
  result = listen(s, backlog);

  /* Report the result.*/
  returnToRemote(skreqp, result);
}

/**
 * @brief   Socket Daemon. Dispatch a request to a local method.
 */
static THD_FUNCTION(TsSockSkelDaemon, arg) {
  objects_fifo_t *ofp = arg;
  skel_req_t *skreqp;

  for (;/* ever */;) {
    chFifoReceiveObjectTimeout(ofp, (void **)&skreqp,
        TIME_INFINITE);
    switch (skreqp->stub_op_code) {
    case SOCK_OP_SOCKET:
      l_socket(skreqp);
      break;
    case SOCK_OP_CONNECT:
      l_connect(skreqp);
      break;
    case SOCK_OP_CLOSE:
      l_close(skreqp);
      break;
    case SOCK_OP_RECV:
      l_recv(skreqp);
      break;
    case SOCK_OP_SEND:
      l_send(skreqp);
      break;
    case SOCK_OP_SELECT:
      l_select(skreqp);
      break;
    case SOCK_OP_BIND:
      l_bind(skreqp);
      break;
    case SOCK_OP_LISTEN:
      l_listen(skreqp);
      break;
    default:
      break;
    }
  }
}

/*===========================================================================*/
/* Module exported functions.                                                */
/*===========================================================================*/

/**
 * @brief   Init the socket skeletons daemon objects and create the
 *          corresponding threads.
 */
void tsSocksSkelInit(void) {
  int i;
  skel_ctx_t *scp;

  scp = &skel_ctx;
  chFifoObjectInit(&scp->skel_req_fifo, sizeof (skel_req_t), N_MAX_SKEL_REQS,
      sizeof (uint8_t), scp->skel_reqs, scp->skel_req_msgs);
  scp->skel_eventflag = EVT_F_SOCK_NEW_OP;
  scp->stub_svc_name = SOCKS_SVC_NAME;

  for (i = 0; i < N_SOCKSKEL_THD; ++i)
    chThdCreateFromHeap(NULL, 2048, "TsSockSkelDaemonWrk", NORMALPRIO,
        TsSockSkelDaemon, &scp->skel_req_fifo);
  chThdCreateFromHeap(NULL, 2048, "TsSocksSkelDaemon", NORMALPRIO,
      TsSkelsDaemon, scp);
}

/** @} */