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