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)(src);
190 
191  if (readBuffer[0] != 0) {
193  memcpy(pckt, readBuffer, readBuffer[0]);
194  readBuffer[0] = 0;
195  return pckt;
196  } else
197  return NULL;
198 }
199 
200 /* ---------------------------------------------------------------------------------------------*/
202  long int n;
203  (void)(src);
204 
205  if (readBuffer[0] != 0) {
206  return 1;
207  }
208 
209  n = read(sockfd, readBuffer, pcktMaxLength);
210  if (n == -1) /* no data are available from the socket */
211  return 0;
212 
213  if (n == 0) {
214  printf("CrFwInStreamSocketConfigAction: ERROR reading from socket\n");
215  return 0;
216  }
217  if (n == readBuffer[0]) /* a valid packet has arrived */
218  return 1;
219 
220  if (n != readBuffer[0]) {
221  printf("CrFwInStreamSocketConfigAction: invalid packet received from socket\n");
222  return 0;
223  }
224 
225  return 0;
226 }
227 
228 /* ---------------------------------------------------------------------------------------------*/
229 void CrFwInStreamSocketSetPort(unsigned short n) {
230  portno = n;
231 }
232 
233 /* ---------------------------------------------------------------------------------------------*/
234 void CrFwInStreamSocketSetHost(char* name) {
235  hostName = name;
236 }
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
Definition of the Framework Component Data (FCD) Type.
CrFwBool_t CrFwInStreamSocketIsPcktAvail(CrFwDestSrc_t src)
Function implementing the Packet Available Check Operation for the InStream.
static CrFwCmpData_t inStreamData[CR_FW_NOF_INSTREAM]
The data structures for the InStream State Machines and their Procedures.
Definition: CrFwInStream.c:82
unsigned char * CrFwPckt_t
Type for packets (see CrFwPckt.h).
Definition: CrFwConstants.h:38
CrFwPckt_t CrFwPcktMake(CrFwPcktLength_t pcktLength)
Make function for command or report packets.
Definition: CrFwPckt.c:129
static unsigned short portno
The port number.
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:32
static unsigned char * readBuffer
The Read Buffer.
unsigned char CrFwDestSrc_t
Type used for the command or report destination and source.
void CrFwInStreamSocketSetPort(unsigned short n)
Set the port number for the socket.
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
void CrFwInStreamSocketConfigAction(FwPrDesc_t prDesc)
Configuration action for the socket-based InStream.
void CrFwInStreamSocketInitCheck(FwPrDesc_t prDesc)
Initialization check for the socket-based InStream.
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
Interface for the Socket-Based InStream.
static char * hostName
The host name.
void CrFwInStreamDefShutdownAction(FwSmDesc_t smDesc)
Default shutdown action for an InStream.
Definition: CrFwInStream.c:301
void CrFwInStreamSocketPoll(FwSmDesc_t inStream)
Poll the socket to check whether a new packet has arrived.
Definition of Base Component.
The CORDET Framework defines an interface for generating error reports (see CrFwRepErr.h).
unsigned short int CrFwPcktLength_t
Type for the packet length.
void CrFwInStreamSocketSetHost(char *name)
Set the host name of the server.
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
User-modifiable parameters for the InStream components (see CrFwInStream.h).
CrFwPckt_t CrFwInStreamSocketPcktCollect(CrFwDestSrc_t src)
Function implementing the Packet Collect Operation for the socket-based InStream. ...
void CrFwInStreamSocketShutdownAction(FwSmDesc_t smDesc)
Shutdown action for the socket-based InStream.
static int sockfd
The file descriptor for the socket.
void CrFwInStreamSocketInitAction(FwPrDesc_t prDesc)
Initialization action for the socket-based InStream.
P&P Software GmbH, Copyright 2012-2013, All Rights Reserved