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