CORDET Framework - C2 Implementation
pustests/CrFwClientSocket.c
1 
24 #include <stdlib.h>
25 #include "CrFwClientSocket.h"
26 #include "CrFwConstants.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 "CrFwInStreamUserPar.h"
36 #include "CrFwCmpData.h"
37 /* Include framework files */
38 #include "InStream/CrFwInStream.h"
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 <strings.h>
56 
57 #define h_addr h_addr_list[0] /* for backward compatibility */
58 
60 static unsigned short portno = 0;
61 
63 static char* hostName = NULL;
64 
66 static int sockfd = 0;
67 
69 static unsigned int pcktMaxLength;
70 
72 static unsigned char* readBuffer;
73 
74 /* ---------------------------------------------------------------------------------------------*/
75 void CrFwClientSocketInitAction(FwPrDesc_t prDesc) {
76  CrFwCmpData_t* streamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
77  struct sockaddr_in serv_addr;
78  struct hostent* server;
79  int flags;
80 
81  if (sockfd != 0) { /* Check if socket is already initialized */
82  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
84  else
86  return;
87  }
88 
89  /* Create the read buffer */
91  readBuffer = malloc((FwPrCounterU4_t)(pcktMaxLength*sizeof(char)));
92 
93  sockfd = socket(AF_INET, SOCK_STREAM, 0);
94  if (sockfd < 0) {
95  perror("CrFwClientSocketInitAction, Socket Creation");
96  streamData->outcome = 0;
97  return;
98  }
99 
100  /* Set the socket to non-blocking mode */
101  if ((flags = fcntl(sockfd, F_GETFL, 0)) < 0) {
102  perror("CrFwClientSocketInitAction, Set socket attributes");
103  streamData->outcome = 0;
104  return;
105  }
106  if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) < 0) {
107  perror("CrFwClientSocketInitAction, Set socket attributes");
108  streamData->outcome = 0;
109  return;
110  }
111 
112  server = gethostbyname(hostName);
113  if (server == NULL) {
114  perror("CrFwClientSocketInitAction, Get host name");
115  streamData->outcome = 0;
116  return;
117  }
118 
119  bzero((char*) &serv_addr, sizeof(serv_addr));
120  serv_addr.sin_family = AF_INET;
121  bcopy((char*)server->h_addr,
122  (char*)&serv_addr.sin_addr.s_addr,
123  (long unsigned int)server->h_length);
124  serv_addr.sin_port = (unsigned short int)htons(portno);
125 
126  if (connect(sockfd,(struct sockaddr*) &serv_addr,sizeof(serv_addr)) < 0) {
127  if (errno != EINPROGRESS) {
128  perror("CrFwClientSocketInitAction, Connect Socket");
129  streamData->outcome = 0;
130  return;
131  }
132  }
133 
134  /* Execute default initialization action for OutStream/InStream */
135  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
137  else
139 }
140 
141 /* ---------------------------------------------------------------------------------------------*/
142 void CrFwClientSocketShutdownAction(FwSmDesc_t smDesc) {
143  CrFwCmpData_t* streamData = (CrFwCmpData_t*)FwSmGetData(smDesc);
144 
145  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
147  else if (streamData->typeId == CR_FW_OUTSTREAM_TYPE)
149  else {
150  perror("CrFwClientSocketShutdownAction, Incorrect caller type");
151  return;
152  }
153 
154  if (sockfd == 0) /* Check if socket was already shutdown */
155  return;
156  free(readBuffer);
157  close(sockfd);
158  sockfd = 0;
159 }
160 
161 /* ---------------------------------------------------------------------------------------------*/
162 void CrFwClientSocketInitCheck(FwPrDesc_t prDesc) {
163  CrFwCmpData_t* prData = (CrFwCmpData_t*)FwPrGetData(prDesc);
164 
165  if (pcktMaxLength > 255) {
166  prData->outcome = 0;
167  return;
168  }
169 
170  if (portno == 0) {
171  prData->outcome = 0;
172  return;
173  }
174 
175  if (hostName == NULL) {
176  prData->outcome = 0;
177  return;
178  }
179 
180  prData->outcome = 1;
181  return;
182 }
183 
184 /* ---------------------------------------------------------------------------------------------*/
185 void CrFwClientSocketConfigAction(FwPrDesc_t prDesc) {
186  CrFwCmpData_t* streamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
187 
188  /* Clear Read Buffer */
189  readBuffer[0] = 0;
190 
191  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
193  else if (streamData->typeId == CR_FW_OUTSTREAM_TYPE)
195  else {
196  perror("CrFwClientSocketConfigAction, Incorrect caller type");
197  return;
198  }
199 }
200 
201 /* ---------------------------------------------------------------------------------------------*/
202 void CrFwClientSocketPoll() {
203  long int n;
204  FwSmDesc_t inStream;
205 
206  if (readBuffer[0] != 0) {
207  /* src = CrFwPcktGetSrc((CrFwPckt_t)readBuffer); */
208  /* inStream = CrFwGetInStream(src); */
209  inStream = CrFwInStreamMake(5);
210  CrFwInStreamPcktAvail(inStream);
211  return;
212  }
213 
214  n = read(sockfd, readBuffer, pcktMaxLength);
215  if (n == -1) /* no data are available from the socket */
216  return;
217  if (n == 0) {
218  printf("CrFwClientSocketPoll: ERROR reading from socket\n");
219  return;
220  }
221  if (n == readBuffer[0]) { /* a valid packet has arrived */
222  /* src = CrFwPcktGetSrc((CrFwPckt_t)readBuffer); */
223  /* inStream = CrFwGetInStream(src); */
224  inStream = CrFwInStreamMake(5);
225  CrFwInStreamPcktAvail(inStream);
226  return;
227  }
228  if (n != readBuffer[0]) {
229  printf("CrFwClientSocketPoll: invalid packet received from socket\n");
230  readBuffer[0] = 0;
231  return;
232  }
233 }
234 
235 /* ---------------------------------------------------------------------------------------------*/
237  CrFwPckt_t pckt;
238  CrFwDestSrc_t pcktSrc;
239 
240  if (readBuffer[0] != 0) {
242  if (src == pcktSrc) {
244  memcpy(pckt, readBuffer, readBuffer[0]);
245  readBuffer[0] = 0;
246  return pckt;
247  } else
248  return NULL;
249  } else
250  return NULL;
251 }
252 
253 /* ---------------------------------------------------------------------------------------------*/
255  long int n;
256  CrFwDestSrc_t pcktSrc;
257 
258  if (readBuffer[0] != 0) {
259  return 1;
260  }
261 
262  n = read(sockfd, readBuffer, pcktMaxLength);
263  if (n == -1) /* no data are available from the socket */
264  return 0;
265 
266  if (n == 0) {
267  printf("CrFwClientSocketIsPcktAvail: ERROR reading from socket\n");
268  return 0;
269  }
270  if (n == readBuffer[0]) { /* a valid packet has arrived */
272  if (src == pcktSrc)
273  return 1;
274  else
275  return 0;
276  }
277 
278  if (n != readBuffer[0]) {
279  printf("CrFwClientSocketIsPcktAvail: invalid packet received from socket\n");
280  readBuffer[0] = 0;
281  return 0;
282  }
283 
284  return 0;
285 }
286 
287 /* ---------------------------------------------------------------------------------------------*/
289  unsigned int len = (unsigned int)CrFwPcktGetLength(pckt);
290  long int n;
291 
292  n = write(sockfd, pckt, len);
293 
294  if (n < 0)
295  return 0;
296 
297  if (n != (int)CrFwPcktGetLength(pckt)) {
298  printf("CrFwClientSocketPcktHandover: error writing to socket\n");
299  return 0;
300  }
301 
302  return 1;
303 }
304 
305 /* ---------------------------------------------------------------------------------------------*/
306 void CrFwClientSocketSetPort(unsigned short n) {
307  portno = n;
308 }
309 
310 /* ---------------------------------------------------------------------------------------------*/
311 void CrFwClientSocketSetHost(char* name) {
312  hostName = name;
313 }
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.
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.
CrFwDestSrc_t CrFwPcktGetSrc(CrFwPckt_t pckt)
Return the source of the command or report encapsulated in a packet.
static int portno
The port number.
void CrFwClientSocketPoll()
Poll the client socket to check whether a new packet has arrived.
static unsigned char * readBuffer
The Read Buffer.
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.
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...
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.
static int pcktMaxLength
The maximum size of an incoming packet.
void CrFwClientSocketInitCheck(FwPrDesc_t prDesc)
Initialization check for the client socket.
unsigned short int CrFwPcktLength_t
Type for the packet length.
CrFwPcktLength_t CrFwPcktGetMaxLength()
Return the maximum length of a packet in number of bytes.
static char * hostName
The host name.
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.
static int sockfd
The file descriptor for the socket.
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