CORDET Framework - C2 Implementation
CrFwOutStreamSocket.c
Go to the documentation of this file.
1 
19 #include <stdlib.h>
20 #include "CrFwRepErrStub.h"
21 #include "CrFwOutStreamSocket.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 "CrFwOutStreamUserPar.h"
31 #include "CrFwCmpData.h"
32 /* Include framework files */
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 <pthread.h>
51 #include <strings.h>
52 
54 static unsigned short portno = 0;
55 
57 static int sockfd;
58 
60 static int newsockfd;
61 
63 static struct sockaddr_in cli_addr;
64 
66 static socklen_t clilen;
67 
72 static void* acceptThreadEntry(void* ptr);
73 
74 /* ---------------------------------------------------------------------------------------------*/
75 void CrFwOutStreamSocketInitAction(FwPrDesc_t prDesc) {
76  CrFwCmpData_t* outStreamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
77  struct sockaddr_in serv_addr, cli_addr;
78  pthread_t acceptThread;
79  pthread_attr_t attr;
80 
81  /* Create the socket */
82  sockfd = socket(AF_INET, SOCK_STREAM, 0);
83  if (sockfd < 0) {
84  perror("CrFwInStreamSocketInitAction, Socket creation");
86  return;
87  }
88 
89  bzero((char*) &serv_addr, sizeof(serv_addr));
90  serv_addr.sin_family = AF_INET;
91  serv_addr.sin_addr.s_addr = INADDR_ANY;
92  serv_addr.sin_port = htons(portno);
93  if (bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0) {
94  perror("CrFwOutStreamSocketInitAction, Bind Socket");
96  return;
97  }
98  listen(sockfd,5);
99  clilen = sizeof(cli_addr);
100 
101  /* Create thread which will accept the connection call from the InStream */
102  pthread_attr_init(&attr);
103  if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
104  perror("CrFwOutStreamSocketInitAction, set detached state");
105  outStreamData->outcome = 0;
106  return;
107  }
108  if (pthread_create(&acceptThread, &attr, acceptThreadEntry, NULL) < 0) {
109  outStreamData->outcome = 0;
110  return;
111  };
112 
113  /* Execute default initialization action for OutStream */
115 }
116 
117 /* ---------------------------------------------------------------------------------------------*/
118 void CrFwOutStreamSocketShutdownAction(FwSmDesc_t smDesc) {
120  close(newsockfd);
121  close(sockfd);
122 }
123 
124 /* ---------------------------------------------------------------------------------------------*/
126  unsigned int len = (int)CrFwPcktGetLength(pckt);
127  long int n;
128 
129  n = write(newsockfd, pckt, len);
130 
131  if (n < 0)
132  return 0;
133 
134  if (n != (int)CrFwPcktGetLength(pckt)) {
135  printf("CrFwOutStreamSocketInitAction: error writing to socket\n");
136  return 0;
137  }
138 
139  return 1;
140 }
141 
142 /* ---------------------------------------------------------------------------------------------*/
143 static void* acceptThreadEntry(void* ptr) {
144  (void)(ptr);
145 
146  /* The call to accept blocks until a matching connect is done by the InStream */
147  newsockfd = accept(sockfd, (struct sockaddr*) &cli_addr, &clilen);
148  if (newsockfd < 0) {
149  perror("CrFwInStreamSocketInitAction, Socket Accept");
150  }
151  return NULL;
152 }
153 
154 /* ---------------------------------------------------------------------------------------------*/
155 void CrFwOutStreamSocketInitCheck(FwPrDesc_t prDesc) {
156  CrFwCmpData_t* outStreamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
157 
158  if (portno < 2000)
159  outStreamData->outcome = 0;
160  else
161  outStreamData->outcome = 1;
162 
163  return;
164 }
165 
166 /* ---------------------------------------------------------------------------------------------*/
167 void CrFwOutStreamSocketConfigCheck(FwPrDesc_t prDesc) {
168  CrFwCmpData_t* outStreamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
169 
170  if (newsockfd > 0)
171  outStreamData->outcome = 1;
172  else
173  outStreamData->outcome = 0;
174 
175  return;
176 }
177 
178 /* ---------------------------------------------------------------------------------------------*/
179 void CrFwOutStreamSocketSetPort(unsigned short n) {
180  portno = n;
181 }
182 
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 CrFwOutStreamDefInitAction(FwPrDesc_t prDesc)
Default initialization action for an OutStream.
void CrFwOutStreamDefShutdownAction(FwSmDesc_t smDesc)
Default shutdown action for an OutStream.
static CrFwCmpData_t outStreamData[CR_FW_NOF_OUTSTREAM]
The base data structures for the OutStream State Machines and their Procedures.
Definition of the OutStream component.
void CrFwOutStreamSocketInitAction(FwPrDesc_t prDesc)
Initialization action for the socket-based OutStream.
static socklen_t clilen
Socket variable.
static struct sockaddr_in cli_addr
Socket variable.
CrFwBool_t CrFwOutStreamSocketPcktHandover(CrFwPckt_t pckt)
Function implementing the hand-over operation for the socket-based OutStream.
static void * acceptThreadEntry(void *ptr)
Entry point for the thread which waits for the incoming connection from the InStream.
void CrFwOutStreamSocketConfigCheck(FwPrDesc_t prDesc)
Configuration check for the socket-based OutStream.
void CrFwOutStreamSocketShutdownAction(FwSmDesc_t smDesc)
Shutdown action for the socket-based OutStream.
void CrFwOutStreamSocketInitCheck(FwPrDesc_t prDesc)
Initialization check for the socket-based OutStream.
static int sockfd
The file descriptors for the socket.
void CrFwOutStreamSocketSetPort(unsigned short n)
Set the port number for the socket.
static int newsockfd
The file descriptors for the socket.
static unsigned short portno
Set the port number (must be same as the port number specified in CrFwOutStreamSocket....
Interface for the Socket-Based OutStream.
User-modifiable parameters for the OutStream components (see CrFwOutStream.h).
Interface for creating and accessing a report or command packet.
CrFwPcktLength_t CrFwPcktGetLength(CrFwPckt_t pckt)
Return the length (in number of bytes) of a packet.
Definition: CrFwPckt.c:224
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.
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