CORDET Framework - C2 Implementation
tests/CrFwClientSocket.c
Go to the documentation of this file.
1 
19 #include <stdlib.h>
20 #include "CrFwClientSocket.h"
21 #include "CrFwConstants.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 "CrFwInStreamUserPar.h"
31 #include "CrFwCmpData.h"
32 /* Include framework files */
33 #include "InStream/CrFwInStream.h"
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 <strings.h>
51 
52 #define h_addr h_addr_list[0] /* for backward compatibility */
53 
55 static unsigned short portno = 0;
56 
58 static char* hostName = NULL;
59 
61 static int sockfd = 0;
62 
64 static unsigned int pcktMaxLength;
65 
67 static unsigned char* readBuffer;
68 
69 /* ---------------------------------------------------------------------------------------------*/
70 void CrFwClientSocketInitAction(FwPrDesc_t prDesc) {
71  CrFwCmpData_t* streamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
72  struct sockaddr_in serv_addr;
73  struct hostent* server;
74  int flags;
75 
76  if (sockfd != 0) { /* Check if socket is already initialized */
77  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
79  else
81  return;
82  }
83 
84  /* Create the read buffer */
86  readBuffer = malloc((FwPrCounterU4_t)(pcktMaxLength*sizeof(char)));
87 
88  sockfd = socket(AF_INET, SOCK_STREAM, 0);
89  if (sockfd < 0) {
90  perror("CrFwClientSocketInitAction, Socket Creation");
91  streamData->outcome = 0;
92  return;
93  }
94 
95  /* Set the socket to non-blocking mode */
96  if ((flags = fcntl(sockfd, F_GETFL, 0)) < 0) {
97  perror("CrFwClientSocketInitAction, Set socket attributes");
98  streamData->outcome = 0;
99  return;
100  }
101  if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) < 0) {
102  perror("CrFwClientSocketInitAction, Set socket attributes");
103  streamData->outcome = 0;
104  return;
105  }
106 
107  server = gethostbyname(hostName);
108  if (server == NULL) {
109  perror("CrFwClientSocketInitAction, Get host name");
110  streamData->outcome = 0;
111  return;
112  }
113 
114  bzero((char*) &serv_addr, sizeof(serv_addr));
115  serv_addr.sin_family = AF_INET;
116  bcopy((char*)server->h_addr,
117  (char*)&serv_addr.sin_addr.s_addr,
118  (long unsigned int)server->h_length);
119  serv_addr.sin_port = (unsigned short int)htons(portno);
120 
121  if (connect(sockfd,(struct sockaddr*) &serv_addr,sizeof(serv_addr)) < 0) {
122  if (errno != EINPROGRESS) {
123  perror("CrFwClientSocketInitAction, Connect Socket");
124  streamData->outcome = 0;
125  return;
126  }
127  }
128 
129  /* Execute default initialization action for OutStream/InStream */
130  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
132  else
134 }
135 
136 /* ---------------------------------------------------------------------------------------------*/
137 void CrFwClientSocketShutdownAction(FwSmDesc_t smDesc) {
138  CrFwCmpData_t* streamData = (CrFwCmpData_t*)FwSmGetData(smDesc);
139 
140  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
142  else if (streamData->typeId == CR_FW_OUTSTREAM_TYPE)
144  else {
145  perror("CrFwClientSocketShutdownAction, Incorrect caller type");
146  return;
147  }
148 
149  if (sockfd == 0) /* Check if socket was already shutdown */
150  return;
151  free(readBuffer);
152  close(sockfd);
153  sockfd = 0;
154 }
155 
156 /* ---------------------------------------------------------------------------------------------*/
157 void CrFwClientSocketInitCheck(FwPrDesc_t prDesc) {
158  CrFwCmpData_t* prData = (CrFwCmpData_t*)FwPrGetData(prDesc);
159 
160  if (pcktMaxLength > 255) {
161  prData->outcome = 0;
162  return;
163  }
164 
165  if (portno == 0) {
166  prData->outcome = 0;
167  return;
168  }
169 
170  if (hostName == NULL) {
171  prData->outcome = 0;
172  return;
173  }
174 
175  prData->outcome = 1;
176  return;
177 }
178 
179 /* ---------------------------------------------------------------------------------------------*/
180 void CrFwClientSocketConfigAction(FwPrDesc_t prDesc) {
181  CrFwCmpData_t* streamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
182 
183  /* Clear Read Buffer */
184  readBuffer[0] = 0;
185 
186  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
188  else if (streamData->typeId == CR_FW_OUTSTREAM_TYPE)
190  else {
191  perror("CrFwClientSocketConfigAction, Incorrect caller type");
192  return;
193  }
194 }
195 
196 /* ---------------------------------------------------------------------------------------------*/
198  long int n;
199  FwSmDesc_t inStream;
200 
201  if (readBuffer[0] != 0) {
202  /* src = CrFwPcktGetSrc((CrFwPckt_t)readBuffer); */
203  /* inStream = CrFwGetInStream(src); */
204  inStream = CrFwInStreamMake(5);
205  CrFwInStreamPcktAvail(inStream);
206  return;
207  }
208 
209  n = read(sockfd, readBuffer, pcktMaxLength);
210  if (n == -1) /* no data are available from the socket */
211  return;
212  if (n == 0) {
213  printf("CrFwClientSocketPoll: ERROR reading from socket\n");
214  return;
215  }
216  if (n == readBuffer[0]) { /* a valid packet has arrived */
217  /* src = CrFwPcktGetSrc((CrFwPckt_t)readBuffer); */
218  /* inStream = CrFwGetInStream(src); */
219  inStream = CrFwInStreamMake(5);
220  CrFwInStreamPcktAvail(inStream);
221  return;
222  }
223  if (n != readBuffer[0]) {
224  printf("CrFwClientSocketPoll: invalid packet received from socket\n");
225  readBuffer[0] = 0;
226  return;
227  }
228 }
229 
230 /* ---------------------------------------------------------------------------------------------*/
232  CrFwPckt_t pckt;
233  CrFwDestSrc_t pcktSrc;
234 
235  if (readBuffer[0] != 0) {
237  if (src == pcktSrc) {
238  pckt = CrFwPcktMake((CrFwPcktLength_t)readBuffer[0]);
239  memcpy(pckt, readBuffer, readBuffer[0]);
240  readBuffer[0] = 0;
241  return pckt;
242  } else
243  return NULL;
244  } else
245  return NULL;
246 }
247 
248 /* ---------------------------------------------------------------------------------------------*/
250  long int n;
251  CrFwDestSrc_t pcktSrc;
252 
253  if (readBuffer[0] != 0) {
254  return 1;
255  }
256 
257  n = read(sockfd, readBuffer, pcktMaxLength);
258  if (n == -1) /* no data are available from the socket */
259  return 0;
260 
261  if (n == 0) {
262  printf("CrFwClientSocketIsPcktAvail: ERROR reading from socket\n");
263  return 0;
264  }
265  if (n == readBuffer[0]) { /* a valid packet has arrived */
267  if (src == pcktSrc)
268  return 1;
269  else
270  return 0;
271  }
272 
273  if (n != readBuffer[0]) {
274  printf("CrFwClientSocketIsPcktAvail: invalid packet received from socket\n");
275  readBuffer[0] = 0;
276  return 0;
277  }
278 
279  return 0;
280 }
281 
282 /* ---------------------------------------------------------------------------------------------*/
284  unsigned int len = (unsigned int)CrFwPcktGetLength(pckt);
285  long int n;
286 
287  n = write(sockfd, pckt, len);
288 
289  if (n < 0)
290  return 0;
291 
292  if (n != (int)CrFwPcktGetLength(pckt)) {
293  printf("CrFwClientSocketPcktHandover: error writing to socket\n");
294  return 0;
295  }
296 
297  return 1;
298 }
299 
300 /* ---------------------------------------------------------------------------------------------*/
301 void CrFwClientSocketSetPort(unsigned short n) {
302  portno = n;
303 }
304 
305 /* ---------------------------------------------------------------------------------------------*/
306 void CrFwClientSocketSetHost(char* name) {
307  hostName = name;
308 }
void CrFwOutStreamDefConfigAction(FwPrDesc_t prDesc)
Default configuration action for an OutStream.
Type for the Framework Component Data (FCD).
void CrFwClientSocketConfigAction(FwPrDesc_t prDesc)
Configuration action for the client socket.
Interface through which framework components access the current time.
static unsigned char * readBuffer
The Read Buffer.
CrFwOutcome_t outcome
The outcome of an action or check executed by a state machine or by one of its procedures.
CrFwPckt_t CrFwClientSocketPcktCollect(CrFwDestSrc_t src)
Function implementing the Packet Collect Operation for the client socket.
static char * hostName
The host name.
CrFwDestSrc_t CrFwPcktGetSrc(CrFwPckt_t pckt)
Return the source of the command or report encapsulated in a packet.
void CrFwClientSocketPoll()
Poll the client socket to check whether a new packet has arrived.
unsigned char CrFwDestSrc_t
Type used for the command or report destination and source.
static int sockfd
The file descriptor for the socket.
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.
Definition of the InStream component.
int CrFwBool_t
Type used for boolean values (1 represent "true" and 0 represents "false").
Definition: CrFwConstants.h:30
Header file to define all invariant publicly available constants and types for the CORDET Framework...
static unsigned int pcktMaxLength
The maximum size of an incoming packet.
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
CrFwPcktLength_t CrFwPcktGetLength(CrFwPckt_t pckt)
Return the length (in number of bytes) of a packet.
CrFwBool_t CrFwClientSocketPcktHandover(CrFwPckt_t pckt)
Function implementing the hand-over operation for the client socket.
void CrFwClientSocketInitCheck(FwPrDesc_t prDesc)
Initialization check for the client socket.
static unsigned short portno
The port number.
unsigned short int CrFwPcktLength_t
Type for the packet length.
CrFwPcktLength_t CrFwPcktGetMaxLength()
Return the maximum length of a packet in number of bytes.
void CrFwClientSocketSetHost(char *name)
Set the host name of the server.
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
void CrFwClientSocketInitAction(FwPrDesc_t prDesc)
Initialization action for the client socket.
Definition of Base Component.
char * CrFwPckt_t
Type for packets (see CrFwPckt.h).
Definition: CrFwConstants.h:36
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.
void CrFwClientSocketShutdownAction(FwSmDesc_t smDesc)
Shutdown action for the client socket.
void CrFwClientSocketSetPort(unsigned short n)
Set the port number for the socket.
CrFwBool_t CrFwClientSocketIsPcktAvail(CrFwDestSrc_t src)
Function implementing the Packet Available Check Operation for the client socket. ...
P&P Software GmbH, Copyright 2012-2013, All Rights Reserved