CORDET Framework - C2 Implementation
tests/CrFwInStreamSocket.c
Go to the documentation of this file.
1 
19 #include <stdlib.h>
20 #include "CrFwRepErrStub.h"
21 #include "CrFwInStreamSocket.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;
62 
64 static unsigned int pcktMaxLength;
65 
67 static unsigned char* readBuffer;
68 
69 /* ---------------------------------------------------------------------------------------------*/
70 void CrFwInStreamSocketInitAction(FwPrDesc_t prDesc) {
71  CrFwCmpData_t* inStreamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
72  struct sockaddr_in serv_addr;
73  struct hostent* server;
74  int flags;
75 
77  readBuffer = malloc((FwPrCounterU4_t)(pcktMaxLength*sizeof(char)));
78 
79  sockfd = socket(AF_INET, SOCK_STREAM, 0);
80  if (sockfd < 0) {
81  perror("CrFwInStreamSocketInitAction, Socket Creation");
82  inStreamData->outcome = 0;
83  return;
84  }
85 
86  /* Set the socket to non-blocking mode */
87  if ((flags = fcntl(sockfd, F_GETFL, 0)) < 0) {
88  perror("CrFwInStreamSocketInitAction, Set socket attributes");
89  inStreamData->outcome = 0;
90  return;
91  }
92  if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) < 0) {
93  perror("CrFwInStreamSocketInitAction, Set socket attributes");
94  inStreamData->outcome = 0;
95  return;
96  }
97 
98  fcntl(sockfd, F_SETFL, O_NONBLOCK);
99 
100  server = gethostbyname(hostName);
101  if (server == NULL) {
102  perror("CrFwInStreamSocketInitAction, Get host name");
103  inStreamData->outcome = 0;
104  return;
105  }
106 
107  bzero((char*) &serv_addr, sizeof(serv_addr));
108  serv_addr.sin_family = AF_INET;
109  bcopy((char*)server->h_addr,
110  (char*)&serv_addr.sin_addr.s_addr,
111  (long unsigned int)server->h_length);
112  serv_addr.sin_port = (unsigned short int)htons(portno);
113 
114  if (connect(sockfd,(struct sockaddr*) &serv_addr,sizeof(serv_addr)) < 0) {
115  if (errno != EINPROGRESS) {
116  perror("CrFwInStreamSocketInitAction, Connect Socket");
117  inStreamData->outcome = 0;
118  return;
119  }
120  }
121 
123 }
124 
125 /* ---------------------------------------------------------------------------------------------*/
126 void CrFwInStreamSocketShutdownAction(FwSmDesc_t smDesc) {
128  free(readBuffer);
129  close(sockfd);
130 }
131 
132 /* ---------------------------------------------------------------------------------------------*/
133 void CrFwInStreamSocketInitCheck(FwPrDesc_t prDesc) {
134  CrFwCmpData_t* prData = (CrFwCmpData_t*)FwPrGetData(prDesc);
135 
136  if (pcktMaxLength > 255) {
137  prData->outcome = 0;
138  return;
139  }
140 
141  if (portno == 0) {
142  prData->outcome = 0;
143  return;
144  }
145 
146  if (hostName == NULL) {
147  prData->outcome = 0;
148  return;
149  }
150 
151  prData->outcome = 1;
152  return;
153 }
154 
155 /* ---------------------------------------------------------------------------------------------*/
156 void CrFwInStreamSocketConfigAction(FwPrDesc_t prDesc) {
157 
158  /* Clear Read Buffer */
159  readBuffer[0] = 0;
160 
162 }
163 
164 /* ---------------------------------------------------------------------------------------------*/
165 void CrFwInStreamSocketPoll(FwSmDesc_t inStream) {
166  long int n;
167 
168  n = read(sockfd, readBuffer, pcktMaxLength);
169  if (n == -1) /* no data are available from the socket */
170  return;
171  if (n == 0) {
172  printf("CrFwInStreamSocketConfigAction: ERROR reading from socket\n");
173  return;
174  }
175  if (n == readBuffer[0]) { /* a valid packet has arrived */
176  CrFwInStreamPcktAvail(inStream);
177  return;
178  }
179  if (n != readBuffer[0]) {
180  printf("CrFwInStreamSocketConfigAction: invalid packet received from socket\n");
181  return;
182  }
183 }
184 
185 /* ---------------------------------------------------------------------------------------------*/
187  CrFwPckt_t pckt;
188  (void)(src);
189 
190  if (readBuffer[0] != 0) {
192  memcpy(pckt, readBuffer, readBuffer[0]);
193  readBuffer[0] = 0;
194  return pckt;
195  } else
196  return NULL;
197 }
198 
199 /* ---------------------------------------------------------------------------------------------*/
201  long int n;
202  (void)(src);
203 
204  if (readBuffer[0] != 0) {
205  return 1;
206  }
207 
208  n = read(sockfd, readBuffer, pcktMaxLength);
209  if (n == -1) /* no data are available from the socket */
210  return 0;
211 
212  if (n == 0) {
213  printf("CrFwInStreamSocketConfigAction: ERROR reading from socket\n");
214  return 0;
215  }
216  if (n == readBuffer[0]) /* a valid packet has arrived */
217  return 1;
218 
219  if (n != readBuffer[0]) {
220  printf("CrFwInStreamSocketConfigAction: invalid packet received from socket\n");
221  return 0;
222  }
223 
224  return 0;
225 }
226 
227 /* ---------------------------------------------------------------------------------------------*/
228 void CrFwInStreamSocketSetPort(unsigned short n) {
229  portno = n;
230 }
231 
232 /* ---------------------------------------------------------------------------------------------*/
233 void CrFwInStreamSocketSetHost(char* name) {
234  hostName = name;
235 }
Type for the Framework Component Data (FCD).
Interface through which framework components access the current time.
void CrFwInStreamSocketPoll(FwSmDesc_t inStream)
Poll the socket to check whether a new packet has arrived.
CrFwOutcome_t outcome
The outcome of an action or check executed by a state machine or by one of its procedures.
static char * hostName
The host name.
static unsigned int pcktMaxLength
The maximum size of an incoming packet.
static CrFwCmpData_t inStreamData[CR_FW_NOF_INSTREAM]
The data structures for the InStream State Machines and their Procedures.
Definition: CrFwInStream.c:82
void CrFwInStreamSocketSetPort(unsigned short n)
Set the port number for the socket.
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.
CrFwPckt_t CrFwInStreamSocketPcktCollect(CrFwDestSrc_t src)
Function implementing the Packet Collect Operation for the socket-based InStream. ...
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
void CrFwInStreamSocketConfigAction(FwPrDesc_t prDesc)
Configuration action for the socket-based InStream.
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
static int sockfd
The file descriptor for the socket.
unsigned short int CrFwPcktLength_t
Type for the packet length.
static unsigned short portno
The port number.
CrFwPcktLength_t CrFwPcktGetMaxLength()
Return the maximum length of a packet in number of bytes.
void CrFwInStreamSocketInitCheck(FwPrDesc_t prDesc)
Initialization check for the socket-based InStream.
Definition of the utility functions for the CORDET Framework.
void CrFwInStreamSocketInitAction(FwPrDesc_t prDesc)
Initialization action for the socket-based InStream.
static unsigned char * readBuffer
The Read Buffer.
void CrFwInStreamDefShutdownAction(FwSmDesc_t smDesc)
Default shutdown action for an InStream.
Definition: CrFwInStream.c:301
Definition of Base Component.
void CrFwInStreamSocketSetHost(char *name)
Set the host name of the server.
char * CrFwPckt_t
Type for packets (see CrFwPckt.h).
Definition: CrFwConstants.h:36
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
CrFwBool_t CrFwInStreamSocketIsPcktAvail(CrFwDestSrc_t src)
Function implementing the Packet Available Check Operation for the InStream.
void CrFwInStreamSocketShutdownAction(FwSmDesc_t smDesc)
Shutdown action for the socket-based InStream.
P&P Software GmbH, Copyright 2012-2013, All Rights Reserved