CORDET Framework - C2 Implementation
tests/CrFwServerSocket.c
Go to the documentation of this file.
1 
19 #include <stdlib.h>
20 #include "CrFwRepErrStub.h"
21 #include "CrFwServerSocket.h"
22 /* Include FW Profile files */
23 #include "FwSmConstants.h"
24 #include "FwSmConfig.h"
25 #include "FwSmCore.h"
26 #include "FwPrConfig.h"
27 #include "FwPrCore.h"
28 #include "FwPrConstants.h"
29 /* Include configuration files */
30 #include "CrFwOutStreamUserPar.h"
31 #include "CrFwCmpData.h"
32 /* Include framework files */
34 #include "BaseCmp/CrFwBaseCmp.h"
35 #include "Pckt/CrFwPckt.h"
36 #include "CrFwTime.h"
37 #include "CrFwRepErr.h"
39 /* Include file for socket implementation */
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <string.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <netdb.h>
48 #include <fcntl.h>
49 #include <errno.h>
50 #include <pthread.h>
51 #include <strings.h>
52 
54 static unsigned short portno = 0;
55 
57 static int sockfd = 0;
58 
60 static int newsockfd;
61 
63 static struct sockaddr_in cli_addr;
64 
66 static socklen_t clilen;
67 
69 static unsigned int pcktMaxLength;
70 
72 static unsigned char* readBuffer;
73 
78 static void* acceptThreadEntry(void* ptr);
79 
80 /* ---------------------------------------------------------------------------------------------*/
81 void CrFwServerSocketInitAction(FwPrDesc_t prDesc) {
82  CrFwCmpData_t* streamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
83  struct sockaddr_in serv_addr, cli_addr;
84  pthread_t acceptThread;
85  pthread_attr_t attr;
86 
87  /* Check if server socket has already been initialized */
88  if (sockfd != 0) {
89  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
91  else
93  return;
94  }
95 
96  /* Create the read buffer */
98  readBuffer = malloc((FwPrCounterU4_t)(pcktMaxLength*sizeof(char)));
99 
100  /* Create the socket */
101  sockfd = socket(AF_INET, SOCK_STREAM, 0);
102  if (sockfd < 0) {
103  perror("CrFwServerSocketInitAction, Socket creation");
104  streamData->outcome = 0;
105  return;
106  }
107 
108  bzero((char*) &serv_addr, sizeof(serv_addr));
109  serv_addr.sin_family = AF_INET;
110  serv_addr.sin_addr.s_addr = INADDR_ANY;
111  serv_addr.sin_port = htons(portno);
112  if (bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0) {
113  perror("CrFwServerSocketInitAction, Bind Socket");
114  streamData->outcome = 0;
115  return;
116  }
117  listen(sockfd,5);
118  clilen = sizeof(cli_addr);
119 
120  /* Create thread which will accept the connection call from the client socket */
121  pthread_attr_init(&attr);
122  if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
123  perror("CrFwServerSocketInitAction, set detached state");
124  streamData->outcome = 0;
125  return;
126  }
127  if (pthread_create(&acceptThread, NULL, acceptThreadEntry, NULL) < 0) {
128  streamData->outcome = 0;
129  return;
130  };
131 
132  /* Execute default initialization action for OutStream/InStream */
133  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
135  else
137 }
138 
139 /* ---------------------------------------------------------------------------------------------*/
140 void CrFwServerSocketShutdownAction(FwSmDesc_t smDesc) {
141  CrFwCmpData_t* streamData = (CrFwCmpData_t*)FwSmGetData(smDesc);
142 
143  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
145  else if (streamData->typeId == CR_FW_OUTSTREAM_TYPE)
147  else {
148  perror("CrFwServerSocketShutdownAction, Incorrect caller type");
149  return;
150  }
151  if (sockfd != 0) {
152  free(readBuffer);
153  close(newsockfd);
154  close(sockfd);
155  sockfd = 0;
156  }
157 }
158 
159 /* ---------------------------------------------------------------------------------------------*/
160 void CrFwServerSocketConfigAction(FwPrDesc_t prDesc) {
161  CrFwCmpData_t* streamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
162 
163  /* Clear Read Buffer */
164  readBuffer[0] = 0;
165 
166  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
168  else
170 }
171 
172 /* ---------------------------------------------------------------------------------------------*/
174  long int n;
175  FwSmDesc_t inStream;
176 
177  if (readBuffer[0] != 0) {
178  /* src = CrFwPcktGetSrc((CrFwPckt_t)readBuffer); */
179  /* inStream = CrFwGetInStream(src); */
180  inStream = CrFwInStreamMake(6);
181  CrFwInStreamPcktAvail(inStream);
182  return;
183  }
184 
185  n = read(newsockfd, readBuffer, pcktMaxLength);
186  if (n == -1) /* no data are available from the socket */
187  return;
188  if (n == 0) {
189  printf("CrFwServerSocketPoll: ERROR reading from socket\n");
190  return;
191  }
192  if (n == readBuffer[0]) { /* a valid packet has arrived */
193  /* src = CrFwPcktGetSrc((CrFwPckt_t)readBuffer); */
194  /* inStream = CrFwGetInStream(src); */
195  inStream = CrFwInStreamMake(6);
196  CrFwInStreamPcktAvail(inStream);
197  return;
198  }
199  if (n != readBuffer[0]) {
200  printf("CrFwServerSocketPoll: invalid packet received from socket\n");
201  readBuffer[0] = 0;
202  return;
203  }
204 }
205 
206 /* ---------------------------------------------------------------------------------------------*/
208  CrFwPckt_t pckt;
209  CrFwDestSrc_t pcktSrc;
210 
211  if (readBuffer[0] != 0) {
213  if (src == pcktSrc) {
214  pckt = CrFwPcktMake((CrFwPcktLength_t)readBuffer[0]);
215  memcpy(pckt, readBuffer, readBuffer[0]);
216  readBuffer[0] = 0;
217  return pckt;
218  } else
219  return NULL;
220  } else
221  return NULL;
222 }
223 
224 /* ---------------------------------------------------------------------------------------------*/
226  long int n;
227  CrFwDestSrc_t pcktSrc;
228 
229  if (readBuffer[0] != 0) {
230  return 1;
231  }
232 
233  n = read(newsockfd, readBuffer, pcktMaxLength);
234  if (n == -1) /* no data are available from the socket */
235  return 0;
236 
237  if (n == 0) {
238  printf("CrFwServerSocketIsPcktAvail: ERROR reading from socket\n");
239  return 0;
240  }
241  if (n == readBuffer[0]) { /* a valid packet has arrived */
243  if (src == pcktSrc)
244  return 1;
245  else
246  return 0;
247  }
248 
249  if (n != readBuffer[0]) {
250  printf("CrFwServerSocketIsPcktAvail: invalid packet received from socket\n");
251  readBuffer[0] = 0;
252  return 0;
253  }
254 
255  return 0;
256 }
257 
258 /* ---------------------------------------------------------------------------------------------*/
260  unsigned int len = CrFwPcktGetLength(pckt);
261  long int n;
262 
263  n = write(newsockfd, pckt, len);
264 
265  if (n < 0)
266  return 0;
267 
268  if (n != (int)CrFwPcktGetLength(pckt)) {
269  printf("CrFwServerSocketPcktHandover: error writing to socket\n");
270  return 0;
271  }
272 
273  return 1;
274 }
275 
276 /* ---------------------------------------------------------------------------------------------*/
277 static void* acceptThreadEntry(void* ptr) {
278  int flags;
279  (void)(ptr);
280 
281  /* The call to accept blocks until a matching connect is done by the client socket */
282  newsockfd = accept(sockfd, (struct sockaddr*) &cli_addr, &clilen);
283  if (newsockfd < 0) {
284  perror("CrFwServerSocketInitAction, Socket Accept");
285  }
286 
287  /* Set the socket to non-blocking mode */
288  if ((flags = fcntl(newsockfd, F_GETFL, 0)) < 0) {
289  perror("CrFwServerSocketInitAction, Set socket attributes");
290  return NULL;
291  }
292  if (fcntl(newsockfd, F_SETFL, flags | O_NONBLOCK) < 0) {
293  perror("CrFwServerSocketInitAction, Set socket attributes");
294  return NULL;
295  }
296 
297  return NULL;
298 }
299 
300 /* ---------------------------------------------------------------------------------------------*/
301 void CrFwServerSocketInitCheck(FwPrDesc_t prDesc) {
302  CrFwCmpData_t* outStreamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
303 
304  if (portno < 2000)
305  outStreamData->outcome = 0;
306  else
307  outStreamData->outcome = 1;
308 
309  return;
310 }
311 
312 /* ---------------------------------------------------------------------------------------------*/
313 void CrFwServerSocketConfigCheck(FwPrDesc_t prDesc) {
314  CrFwCmpData_t* outStreamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
315 
316  if (newsockfd > 0)
317  outStreamData->outcome = 1;
318  else
319  outStreamData->outcome = 0;
320 
321  return;
322 }
323 
324 /* ---------------------------------------------------------------------------------------------*/
325 void CrFwServerSocketSetPort(unsigned short n) {
326  portno = n;
327 }
void CrFwOutStreamDefConfigAction(FwPrDesc_t prDesc)
Default configuration action for an OutStream.
void CrFwServerSocketConfigAction(FwPrDesc_t prDesc)
Configuration action for the server socket.
Type for the Framework Component Data (FCD).
Interface through which framework components access the current time.
CrFwOutcome_t outcome
The outcome of an action or check executed by a state machine or by one of its procedures.
CrFwDestSrc_t CrFwPcktGetSrc(CrFwPckt_t pckt)
Return the source of the command or report encapsulated in a packet.
Definition of the OutStream component.
unsigned char CrFwDestSrc_t
Type used for the command or report destination and source.
CrFwPckt_t CrFwPcktMake(CrFwPcktLength_t pcktLength)
Make function for command or report packets.
#define CR_FW_INSTREAM_TYPE
Type identifier for the InStream components.
Interface for creating and accessing a report or command packet.
void CrFwServerSocketInitAction(FwPrDesc_t prDesc)
Initialization action for the server socket.
int CrFwBool_t
Type used for boolean values (1 represent "true" and 0 represents "false").
Definition: CrFwConstants.h:30
CrFwBool_t CrFwServerSocketIsPcktAvail(CrFwDestSrc_t src)
Function implementing the Packet Available Check Operation for the server socket. ...
static void * acceptThreadEntry(void *ptr)
Entry point for the thread which waits for the incoming connection from the client socket...
Interface for reporting an error detected by a framework component.
void CrFwInStreamDefInitAction(FwPrDesc_t prDesc)
Default initialization action for an InStream.
Definition: CrFwInStream.c:312
void CrFwServerSocketInitCheck(FwPrDesc_t prDesc)
Initialization check for the server socket.
CrFwPcktLength_t CrFwPcktGetLength(CrFwPckt_t pckt)
Return the length (in number of bytes) of a packet.
static int newsockfd
The file descriptors for the socket.
void CrFwServerSocketSetPort(unsigned short n)
Set the port number for the socket.
unsigned short int CrFwPcktLength_t
Type for the packet length.
void CrFwServerSocketPoll()
Poll the server socket to check whether a new packet has arrived.
static unsigned char * readBuffer
The Read Buffer.
CrFwPcktLength_t CrFwPcktGetMaxLength()
Return the maximum length of a packet in number of bytes.
void CrFwServerSocketConfigCheck(FwPrDesc_t prDesc)
Configuration check for the server socket.
Definition of the utility functions for the CORDET Framework.
void CrFwOutStreamDefShutdownAction(FwSmDesc_t smDesc)
Default shutdown action for an OutStream.
void CrFwOutStreamDefInitAction(FwPrDesc_t prDesc)
Default initialization action for an OutStream.
void CrFwInStreamDefShutdownAction(FwSmDesc_t smDesc)
Default shutdown action for an InStream.
Definition: CrFwInStream.c:301
Definition of Base Component.
void CrFwServerSocketShutdownAction(FwSmDesc_t smDesc)
Shutdown action for the server socket.
static CrFwCmpData_t outStreamData[CR_FW_NOF_OUTSTREAM]
The base data structures for the OutStream State Machines and their Procedures.
Definition: CrFwOutStream.c:80
char * CrFwPckt_t
Type for packets (see CrFwPckt.h).
Definition: CrFwConstants.h:36
static int sockfd
The file descriptors for the socket.
static unsigned short portno
Set the port number (must be same as the port number specified in CrFwServerSocket.c
static unsigned int pcktMaxLength
The maximum size of an incoming packet.
FwSmDesc_t CrFwInStreamMake(CrFwInstanceId_t i)
Factory function to retrieve the i-th InStream State Machine instance.
Definition: CrFwInStream.c:121
CrFwTypeId_t typeId
The type identifier of the framework component.
void CrFwInStreamDefConfigAction(FwPrDesc_t prDesc)
Default configuration action for an InStream.
Definition: CrFwInStream.c:288
void CrFwInStreamPcktAvail(FwSmDesc_t smDesc)
Query the middleware for available packets and collect them if they are available.
Definition: CrFwInStream.c:241
#define CR_FW_OUTSTREAM_TYPE
Type identifier for the OutStream components.
static socklen_t clilen
Socket variable.
CrFwBool_t CrFwServerSocketPcktHandover(CrFwPckt_t pckt)
Function implementing the hand-over operation for the server socket.
CrFwPckt_t CrFwServerSocketPcktCollect(CrFwDestSrc_t src)
Function implementing the Packet Collect Operation for the server socket.
static struct sockaddr_in cli_addr
Socket variable.
P&P Software GmbH, Copyright 2012-2013, All Rights Reserved