CORDET Framework - C2 Implementation
pustests/CrFwInStreamSocket.c
1 
24 #include <stdlib.h>
25 #include "CrFwRepErrStub.h"
26 #include "CrFwInStreamSocket.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;
67 
69 static unsigned int pcktMaxLength;
70 
72 static unsigned char* readBuffer;
73 
74 /* ---------------------------------------------------------------------------------------------*/
75 void CrFwInStreamSocketInitAction(FwPrDesc_t prDesc) {
76  CrFwCmpData_t* inStreamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
77  struct sockaddr_in serv_addr;
78  struct hostent* server;
79  int flags;
80 
82  readBuffer = malloc((FwPrCounterU4_t)(pcktMaxLength*sizeof(char)));
83 
84  sockfd = socket(AF_INET, SOCK_STREAM, 0);
85  if (sockfd < 0) {
86  perror("CrFwInStreamSocketInitAction, Socket Creation");
87  inStreamData->outcome = 0;
88  return;
89  }
90 
91  /* Set the socket to non-blocking mode */
92  if ((flags = fcntl(sockfd, F_GETFL, 0)) < 0) {
93  perror("CrFwInStreamSocketInitAction, Set socket attributes");
94  inStreamData->outcome = 0;
95  return;
96  }
97  if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) < 0) {
98  perror("CrFwInStreamSocketInitAction, Set socket attributes");
99  inStreamData->outcome = 0;
100  return;
101  }
102 
103  fcntl(sockfd, F_SETFL, O_NONBLOCK);
104 
105  server = gethostbyname(hostName);
106  if (server == NULL) {
107  perror("CrFwInStreamSocketInitAction, Get host name");
108  inStreamData->outcome = 0;
109  return;
110  }
111 
112  bzero((char*) &serv_addr, sizeof(serv_addr));
113  serv_addr.sin_family = AF_INET;
114  bcopy((char*)server->h_addr,
115  (char*)&serv_addr.sin_addr.s_addr,
116  (long unsigned int)server->h_length);
117  serv_addr.sin_port = (unsigned short int)htons(portno);
118 
119  if (connect(sockfd,(struct sockaddr*) &serv_addr,sizeof(serv_addr)) < 0) {
120  if (errno != EINPROGRESS) {
121  perror("CrFwInStreamSocketInitAction, Connect Socket");
122  inStreamData->outcome = 0;
123  return;
124  }
125  }
126 
128 }
129 
130 /* ---------------------------------------------------------------------------------------------*/
131 void CrFwInStreamSocketShutdownAction(FwSmDesc_t smDesc) {
133  free(readBuffer);
134  close(sockfd);
135 }
136 
137 /* ---------------------------------------------------------------------------------------------*/
138 void CrFwInStreamSocketInitCheck(FwPrDesc_t prDesc) {
139  CrFwCmpData_t* prData = (CrFwCmpData_t*)FwPrGetData(prDesc);
140 
141  if (pcktMaxLength > 255) {
142  prData->outcome = 0;
143  return;
144  }
145 
146  if (portno == 0) {
147  prData->outcome = 0;
148  return;
149  }
150 
151  if (hostName == NULL) {
152  prData->outcome = 0;
153  return;
154  }
155 
156  prData->outcome = 1;
157  return;
158 }
159 
160 /* ---------------------------------------------------------------------------------------------*/
161 void CrFwInStreamSocketConfigAction(FwPrDesc_t prDesc) {
162 
163  /* Clear Read Buffer */
164  readBuffer[0] = 0;
165 
167 }
168 
169 /* ---------------------------------------------------------------------------------------------*/
170 void CrFwInStreamSocketPoll(FwSmDesc_t inStream) {
171  long int n;
172 
173  n = read(sockfd, readBuffer, pcktMaxLength);
174  if (n == -1) /* no data are available from the socket */
175  return;
176  if (n == 0) {
177  printf("CrFwInStreamSocketConfigAction: ERROR reading from socket\n");
178  return;
179  }
180  if (n == readBuffer[0]) { /* a valid packet has arrived */
181  CrFwInStreamPcktAvail(inStream);
182  return;
183  }
184  if (n != readBuffer[0]) {
185  printf("CrFwInStreamSocketConfigAction: invalid packet received from socket\n");
186  return;
187  }
188 }
189 
190 /* ---------------------------------------------------------------------------------------------*/
192  CrFwPckt_t pckt;
193  (void)(src);
194 
195  if (readBuffer[0] != 0) {
197  memcpy(pckt, readBuffer, readBuffer[0]);
198  readBuffer[0] = 0;
199  return pckt;
200  } else
201  return NULL;
202 }
203 
204 /* ---------------------------------------------------------------------------------------------*/
206  long int n;
207  (void)(src);
208 
209  if (readBuffer[0] != 0) {
210  return 1;
211  }
212 
213  n = read(sockfd, readBuffer, pcktMaxLength);
214  if (n == -1) /* no data are available from the socket */
215  return 0;
216 
217  if (n == 0) {
218  printf("CrFwInStreamSocketConfigAction: ERROR reading from socket\n");
219  return 0;
220  }
221  if (n == readBuffer[0]) /* a valid packet has arrived */
222  return 1;
223 
224  if (n != readBuffer[0]) {
225  printf("CrFwInStreamSocketConfigAction: invalid packet received from socket\n");
226  return 0;
227  }
228 
229  return 0;
230 }
231 
232 /* ---------------------------------------------------------------------------------------------*/
233 void CrFwInStreamSocketSetPort(unsigned short n) {
234  portno = n;
235 }
236 
237 /* ---------------------------------------------------------------------------------------------*/
238 void CrFwInStreamSocketSetHost(char* name) {
239  hostName = name;
240 }
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 int portno
The port number.
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.
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.
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 pcktMaxLength
The maximum size of an incoming packet.
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 CrFwInStreamSocketInitCheck(FwPrDesc_t prDesc)
Initialization check for the socket-based InStream.
static char * hostName
The host name.
Definition of the utility functions for the CORDET Framework.
void CrFwInStreamSocketInitAction(FwPrDesc_t prDesc)
Initialization action for the socket-based InStream.
static int sockfd
The file descriptor for the socket.
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