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  CrFwDestSrc_t src;
202 
203  if (readBuffer[0] != 0) {
205  inStream = CrFwInStreamGet(src);
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 */
219  inStream = CrFwInStreamGet(src);
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, i;
234 
235  if (readBuffer[0] != 0) {
237  for (i=0; i<nofPcktSrc; i++) {
238  if (pcktSrc == pcktSrcs[i]) {
240  memcpy(pckt, readBuffer, readBuffer[0]);
241  readBuffer[0] = 0;
242  return pckt;
243  }
244  }
245  return NULL;
246  } else
247  return NULL;
248 }
249 
250 /* ---------------------------------------------------------------------------------------------*/
252  long int n;
253  CrFwDestSrc_t pcktSrc, i;
254 
255  if (readBuffer[0] != 0) {
256  return 1;
257  }
258 
259  n = read(sockfd, readBuffer, pcktMaxLength);
260  if (n == -1) /* no data are available from the socket */
261  return 0;
262 
263  if (n == 0) {
264  printf("CrFwClientSocketIsPcktAvail: ERROR reading from socket\n");
265  return 0;
266  }
267  if (n == readBuffer[0]) { /* a valid packet has arrived */
269  for (i=0; i<nofPcktSrc; i++) {
270  if (pcktSrc == pcktSrcs[i])
271  return 1;
272  }
273  return 0;
274  }
275 
276  if (n != readBuffer[0]) {
277  printf("CrFwClientSocketIsPcktAvail: invalid packet received from socket\n");
278  readBuffer[0] = 0;
279  return 0;
280  }
281 
282  return 0;
283 }
284 
285 /* ---------------------------------------------------------------------------------------------*/
287  unsigned int len = (unsigned int)CrFwPcktGetLength(pckt);
288  long int n;
289 
290  n = write(sockfd, pckt, len);
291 
292  if (n < 0)
293  return 0;
294 
295  if (n != (int)CrFwPcktGetLength(pckt)) {
296  printf("CrFwClientSocketPcktHandover: error writing to socket\n");
297  return 0;
298  }
299 
300  return 1;
301 }
302 
303 /* ---------------------------------------------------------------------------------------------*/
304 void CrFwClientSocketSetPort(unsigned short n) {
305  portno = n;
306 }
307 
308 /* ---------------------------------------------------------------------------------------------*/
309 void CrFwClientSocketSetHost(char* name) {
310  hostName = name;
311 }
Definition of Base Component.
CrFwPckt_t CrFwClientSocketPcktCollect(CrFwDestSrc_t nofPcktSrc, CrFwDestSrc_t *pcktSrcs)
Function implementing the Packet Collect Operation for the client socket.
void CrFwClientSocketSetPort(unsigned short n)
Set the port number for the socket.
void CrFwClientSocketSetHost(char *name)
Set the host name of the server.
static unsigned int pcktMaxLength
The maximum size of an incoming packet.
void CrFwClientSocketInitCheck(FwPrDesc_t prDesc)
Initialization check for the client socket.
void CrFwClientSocketPoll()
Poll the client socket to check whether a new packet has arrived.
static unsigned char * readBuffer
The Read Buffer.
static char * hostName
The host name.
void CrFwClientSocketConfigAction(FwPrDesc_t prDesc)
Configuration action for the client socket.
void CrFwClientSocketInitAction(FwPrDesc_t prDesc)
Initialization action for the client socket.
CrFwBool_t CrFwClientSocketIsPcktAvail(CrFwDestSrc_t nofPcktSrc, CrFwDestSrc_t *pcktSrcs)
Function implementing the Packet Available Check Operation for the client socket.
CrFwBool_t CrFwClientSocketPcktHandover(CrFwPckt_t pckt)
Function implementing the hand-over operation for the client socket.
static int sockfd
The file descriptor for the socket.
void CrFwClientSocketShutdownAction(FwSmDesc_t smDesc)
Shutdown action for the client socket.
static unsigned short portno
The port number.
Interface for a client socket to be used by InStreams and OutStreams.
Definition of the Framework Component Data (FCD) Type.
Header file to define all invariant publicly available constants and types for the CORDET Framework.
#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.
User-modifiable parameters for the InStream components (see CrFwInStream.h).
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.
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.
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