CORDET Framework - C2 Implementation
pustests/CrFwServerSocket.c
1 
24 #include <stdlib.h>
25 #include "CrFwRepErrStub.h"
26 #include "CrFwServerSocket.h"
27 /* Include FW Profile files */
28 #include "FwSmConstants.h"
29 #include "FwSmConfig.h"
30 #include "FwSmCore.h"
31 #include "FwPrConfig.h"
32 #include "FwPrCore.h"
33 #include "FwPrConstants.h"
34 /* Include configuration files */
35 #include "CrFwOutStreamUserPar.h"
36 #include "CrFwCmpData.h"
37 /* Include framework files */
39 #include "BaseCmp/CrFwBaseCmp.h"
40 #include "Pckt/CrFwPckt.h"
41 #include "CrFwTime.h"
42 #include "CrFwRepErr.h"
44 /* Include file for socket implementation */
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <string.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <netdb.h>
53 #include <fcntl.h>
54 #include <errno.h>
55 #include <pthread.h>
56 #include <strings.h>
57 
59 static unsigned short portno = 0;
60 
62 static int sockfd = 0;
63 
65 static int newsockfd;
66 
68 static struct sockaddr_in cli_addr;
69 
71 static socklen_t clilen;
72 
74 static unsigned int pcktMaxLength;
75 
77 static unsigned char* readBuffer;
78 
83 static void* acceptThreadEntry(void* ptr);
84 
85 /* ---------------------------------------------------------------------------------------------*/
86 void CrFwServerSocketInitAction(FwPrDesc_t prDesc) {
87  CrFwCmpData_t* streamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
88  struct sockaddr_in serv_addr, cli_addr;
89  pthread_t acceptThread;
90  pthread_attr_t attr;
91 
92  /* Check if server socket has already been initialized */
93  if (sockfd != 0) {
94  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
96  else
98  return;
99  }
100 
101  /* Create the read buffer */
103  readBuffer = malloc((FwPrCounterU4_t)(pcktMaxLength*sizeof(char)));
104 
105  /* Create the socket */
106  sockfd = socket(AF_INET, SOCK_STREAM, 0);
107  if (sockfd < 0) {
108  perror("CrFwServerSocketInitAction, Socket creation");
109  streamData->outcome = 0;
110  return;
111  }
112 
113  bzero((char*) &serv_addr, sizeof(serv_addr));
114  serv_addr.sin_family = AF_INET;
115  serv_addr.sin_addr.s_addr = INADDR_ANY;
116  serv_addr.sin_port = htons(portno);
117  if (bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0) {
118  perror("CrFwServerSocketInitAction, Bind Socket");
119  streamData->outcome = 0;
120  return;
121  }
122  listen(sockfd,5);
123  clilen = sizeof(cli_addr);
124 
125  /* Create thread which will accept the connection call from the client socket */
126  pthread_attr_init(&attr);
127  if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
128  perror("CrFwServerSocketInitAction, set detached state");
129  streamData->outcome = 0;
130  return;
131  }
132  if (pthread_create(&acceptThread, NULL, acceptThreadEntry, NULL) < 0) {
133  streamData->outcome = 0;
134  return;
135  };
136 
137  /* Execute default initialization action for OutStream/InStream */
138  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
140  else
142 }
143 
144 /* ---------------------------------------------------------------------------------------------*/
145 void CrFwServerSocketShutdownAction(FwSmDesc_t smDesc) {
146  CrFwCmpData_t* streamData = (CrFwCmpData_t*)FwSmGetData(smDesc);
147 
148  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
150  else if (streamData->typeId == CR_FW_OUTSTREAM_TYPE)
152  else {
153  perror("CrFwServerSocketShutdownAction, Incorrect caller type");
154  return;
155  }
156  if (sockfd != 0) {
157  free(readBuffer);
158  close(newsockfd);
159  close(sockfd);
160  sockfd = 0;
161  }
162 }
163 
164 /* ---------------------------------------------------------------------------------------------*/
165 void CrFwServerSocketConfigAction(FwPrDesc_t prDesc) {
166  CrFwCmpData_t* streamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
167 
168  /* Clear Read Buffer */
169  readBuffer[0] = 0;
170 
171  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
173  else
175 }
176 
177 /* ---------------------------------------------------------------------------------------------*/
178 void CrFwServerSocketPoll() {
179  long int n;
180  FwSmDesc_t inStream;
181 
182  if (readBuffer[0] != 0) {
183  /* src = CrFwPcktGetSrc((CrFwPckt_t)readBuffer); */
184  /* inStream = CrFwGetInStream(src); */
185  inStream = CrFwInStreamMake(6);
186  CrFwInStreamPcktAvail(inStream);
187  return;
188  }
189 
190  n = read(newsockfd, readBuffer, pcktMaxLength);
191  if (n == -1) /* no data are available from the socket */
192  return;
193  if (n == 0) {
194  printf("CrFwServerSocketPoll: ERROR reading from socket\n");
195  return;
196  }
197  if (n == readBuffer[0]) { /* a valid packet has arrived */
198  /* src = CrFwPcktGetSrc((CrFwPckt_t)readBuffer); */
199  /* inStream = CrFwGetInStream(src); */
200  inStream = CrFwInStreamMake(6);
201  CrFwInStreamPcktAvail(inStream);
202  return;
203  }
204  if (n != readBuffer[0]) {
205  printf("CrFwServerSocketPoll: invalid packet received from socket\n");
206  readBuffer[0] = 0;
207  return;
208  }
209 }
210 
211 /* ---------------------------------------------------------------------------------------------*/
213  CrFwPckt_t pckt;
214  CrFwDestSrc_t pcktSrc;
215 
216  if (readBuffer[0] != 0) {
218  if (src == pcktSrc) {
220  memcpy(pckt, readBuffer, readBuffer[0]);
221  readBuffer[0] = 0;
222  return pckt;
223  } else
224  return NULL;
225  } else
226  return NULL;
227 }
228 
229 /* ---------------------------------------------------------------------------------------------*/
231  long int n;
232  CrFwDestSrc_t pcktSrc;
233 
234  if (readBuffer[0] != 0) {
235  return 1;
236  }
237 
238  n = read(newsockfd, readBuffer, pcktMaxLength);
239  if (n == -1) /* no data are available from the socket */
240  return 0;
241 
242  if (n == 0) {
243  printf("CrFwServerSocketIsPcktAvail: ERROR reading from socket\n");
244  return 0;
245  }
246  if (n == readBuffer[0]) { /* a valid packet has arrived */
248  if (src == pcktSrc)
249  return 1;
250  else
251  return 0;
252  }
253 
254  if (n != readBuffer[0]) {
255  printf("CrFwServerSocketIsPcktAvail: invalid packet received from socket\n");
256  readBuffer[0] = 0;
257  return 0;
258  }
259 
260  return 0;
261 }
262 
263 /* ---------------------------------------------------------------------------------------------*/
265  unsigned int len = CrFwPcktGetLength(pckt);
266  long int n;
267 
268  n = write(newsockfd, pckt, len);
269 
270  if (n < 0)
271  return 0;
272 
273  if (n != (int)CrFwPcktGetLength(pckt)) {
274  printf("CrFwServerSocketPcktHandover: error writing to socket\n");
275  return 0;
276  }
277 
278  return 1;
279 }
280 
281 /* ---------------------------------------------------------------------------------------------*/
282 static void* acceptThreadEntry(void* ptr) {
283  int flags;
284  (void)(ptr);
285 
286  /* The call to accept blocks until a matching connect is done by the client socket */
287  newsockfd = accept(sockfd, (struct sockaddr*) &cli_addr, &clilen);
288  if (newsockfd < 0) {
289  perror("CrFwServerSocketInitAction, Socket Accept");
290  }
291 
292  /* Set the socket to non-blocking mode */
293  if ((flags = fcntl(newsockfd, F_GETFL, 0)) < 0) {
294  perror("CrFwServerSocketInitAction, Set socket attributes");
295  return NULL;
296  }
297  if (fcntl(newsockfd, F_SETFL, flags | O_NONBLOCK) < 0) {
298  perror("CrFwServerSocketInitAction, Set socket attributes");
299  return NULL;
300  }
301 
302  return NULL;
303 }
304 
305 /* ---------------------------------------------------------------------------------------------*/
306 void CrFwServerSocketInitCheck(FwPrDesc_t prDesc) {
307  CrFwCmpData_t* outStreamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
308 
309  if (portno < 2000)
310  outStreamData->outcome = 0;
311  else
312  outStreamData->outcome = 1;
313 
314  return;
315 }
316 
317 /* ---------------------------------------------------------------------------------------------*/
318 void CrFwServerSocketConfigCheck(FwPrDesc_t prDesc) {
319  CrFwCmpData_t* outStreamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
320 
321  if (newsockfd > 0)
322  outStreamData->outcome = 1;
323  else
324  outStreamData->outcome = 0;
325 
326  return;
327 }
328 
329 /* ---------------------------------------------------------------------------------------------*/
330 void CrFwServerSocketSetPort(unsigned short n) {
331  portno = n;
332 }
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.
static int portno
The port number.
static unsigned char * readBuffer
The Read Buffer.
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. ...
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 pcktMaxLength
The maximum size of an incoming packet.
void CrFwServerSocketSetPort(unsigned short n)
Set the port number for the socket.
static int newsockfd[2]
The file descriptors for the client sockets.
unsigned short int CrFwPcktLength_t
Type for the packet length.
void CrFwServerSocketPoll()
Poll the server socket to check whether a new packet has arrived.
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.
static int sockfd
The file descriptor for the socket.
void CrFwInStreamDefShutdownAction(FwSmDesc_t smDesc)
Default shutdown action for an InStream.
Definition: CrFwInStream.c:301
static socklen_t clilen
Socket variable.
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 void * acceptThreadEntry(void *ptr)
Entry point for the thread which waits for the incoming connection from the client socket...
static struct sockaddr_in cli_addr
Socket variable.
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.
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.
P&P Software GmbH, Copyright 2012-2013, All Rights Reserved