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