COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
socketlib.h
Go to the documentation of this file.
1 /********************************************************************
2 * Copyright (C) 2015 by Interstel Technologies, Inc.
3 * and Hawaii Space Flight Laboratory.
4 *
5 * This file is part of the COSMOS/core that is the central
6 * module for COSMOS. For more information on COSMOS go to
7 * <http://cosmos-project.com>
8 *
9 * The COSMOS/core software is licenced under the
10 * GNU Lesser General Public License (LGPL) version 3 licence.
11 *
12 * You should have received a copy of the
13 * GNU Lesser General Public License
14 * If not, go to <http://www.gnu.org/licenses/>
15 *
16 * COSMOS/core is free software: you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public License
18 * as published by the Free Software Foundation, either version 3 of
19 * the License, or (at your option) any later version.
20 *
21 * COSMOS/core is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
25 *
26 * Refer to the "licences" folder for further information on the
27 * condititons and terms to use this software.
28 ********************************************************************/
29 
30 #ifndef SOCKETLIB_H
31 #define SOCKETLIB_H
32 
37 
50 #include "support/configCosmos.h"
51 #ifdef COSMOS_WIN_OS
52 #include <io.h>
53 #else
54 #include <sys/socket.h>
55 #include <sys/ioctl.h>
56 #include <net/if.h>
57 #endif
58 
62 
63 enum class NetworkType : std::uint16_t
64  {
66  MULTICAST=0,
68  BROADCAST=2, // was previously NetworkType::BROADCAST
70  UDP=5, // was previously NetworkType::UDP
72  TCP=3,
74  CSP=4
75  };
76 
78 #define SOCKET_BLOCKING true
79 #define SOCKET_NONBLOCKING false
81 #define SOCKET_TALK 0
83 #define SOCKET_LISTEN 1
85 #define SOCKET_COMMUNICATE 2
87 #define SOCKET_JABBER 3
89 
91 #define SOCKET_RCVTIMEO 100000
92 
94 #define SOCKET_IP_BYTE_VERSION 0
95 #define SOCKET_IP_BYTE_LEN_LOW 3
96 #define SOCKET_IP_BYTE_LEN_HIGH 2
97 #define SOCKET_IP_BYTE_PROTOCOL 9
98 #define SOCKET_IP_BYTE_SRC_ADDR 12
99 #define SOCKET_IP_BYTE_DEST_ADDR 16
100 #define SOCKET_IP_BYTE_UDP_LEN 24
101 #define SOCKET_IP_BYTE_UDP_CS 26
102 
103 #define SOCKET_IP_PROTOCOL_UDP 17
104 
105 #define SOCKET_BUFFER_LENGTH 512 //Max length of buffer
106 
108 
112 
116 {
117  // Channel type
119  // Channel UDP socket handle
120  int32_t cudp=-1;
121  // Channel UDP INET4 address
122  struct sockaddr_in caddr;
123  // Channel UDP INET4 broadcast address
124  struct sockaddr_in baddr;
125  // Channel UDP INET6 address
126  struct sockaddr_in6 caddr6;
127  // Length for chosen address
128  int addrlen;
129  // Channel port
130  uint16_t cport;
131  // Channel's maximum message size
132  uint16_t msgsize;
133  // Channel's protocol address in string form
134  char address[17];
135  // Channel's broadcast address in string form
136  char baddress[17];
137  // Channel's interface name
139 };
140 
141 
143 
147 
148 int32_t socket_open(socket_channel* channel, NetworkType ntype, const char *address, uint16_t port, uint16_t direction, bool blocking=true, uint32_t usectimeo=0, uint32_t rcvbuf=0, uint32_t sndbuf=0);
149 int32_t socket_open(socket_channel& channel, NetworkType ntype, const char *address, uint16_t port, uint16_t direction, bool blocking=true, uint32_t usectimeo=0, uint32_t rcvbuf=0, uint32_t sndbuf=0);
150 int32_t socket_accept(socket_channel server, socket_channel& client);
151 uint16_t socket_calc_udp_checksum(vector<uint8_t> packet);
152 int32_t socket_check_udp_checksum(vector<uint8_t> packet);
153 int32_t socket_set_udp_checksum(vector<uint8_t>& packet);
154 int32_t socket_blocking(socket_channel* channel, bool blocking);
155 int32_t socket_blocking(socket_channel& channel, bool blocking);
156 int32_t socket_close(socket_channel* channel);
157 int32_t socket_close(socket_channel& channel);
158 int32_t socket_recvfrom(socket_channel &channel, vector<uint8_t> &buffer, size_t maxlen, int flags=0);
159 int32_t socket_recvfrom(socket_channel &channel,string &buffer, size_t maxlen, int flags=0);
160 int32_t socket_sendto(socket_channel &channel, const string buffer, int flags=0);
161 int32_t socket_sendto(socket_channel &channel, const vector<uint8_t> buffer, int flags=0);
162 vector <socket_channel> socket_find_addresses(NetworkType ntype);
163 
164 //-------------------------------------------------------------------
165 // Simple UDP class to send data
166 
168 {
169  // set the defaults
170  // Channel type
172  // UDP socket handle
173  int32_t handle = 0;
174  // UDP INET4 address
175  struct sockaddr_in server;
176  struct sockaddr_in s_other;
177  // to use socket connect function
178  bool connect = false;
179 
180  // Channel's protocol address in string form
181  string address = "127.0.0.1";
182  // port
183  uint16_t port = 8888;
184 
185  // Length for chosen address
186  int addrlen = sizeof(server);
187 
188  uint16_t role = SOCKET_TALK;
189  bool blocking = SOCKET_BLOCKING;
190  uint32_t timeout = SOCKET_RCVTIMEO;
191 
192  bool stream = false; // for SOCK_STREAM in simgen
193 
194 public:
195  // constructor
197  //SocketOptions():type(0),handle(0),address(""),port(),addrlen(0),role(0),blocking(false),timeout(0){}
198 
199  // using list initialization is not supported be gcc 4.8 and/or MSVC 2013
200  // so for the moment keep these commented out
201  // SocketOptions(uint16_t p):port{p}{}
202  // SocketOptions(string a, uint16_t p):address{a},port{p}{}
203  // SocketOptions(string a, uint16_t p, uint16_t r):address{a},port{p},role{r}{}
204 
205  SocketOptions(uint16_t p);
206  SocketOptions(string a, uint16_t p);
207  SocketOptions(string a, uint16_t p, uint16_t r);
208 
209 
210 } ;
211 
212 class Udp
213 {
214 
215 private:
216  // agent stuff
217  //socket_channel socket;
219  //beatstruc hbeat;
220 
221  int32_t iretn = 0; // return error
222 
223  //int32_t openClient();
224  int32_t openServer();
225  int32_t errorStatus(string functionName);
226 
227 public:
228  // constructors
229  Udp(); // : sok("127.0.0.1",8888){}
230  //Udp(uint16_t p);
231  //Udp(string a, uint16_t p);
232  //Udp(string a, uint16_t p, uint16_t r);
233 
234  int32_t socketOpen();
235 
236  int32_t setupClient();
237  int32_t setupClient(string a, uint16_t p);
238 
239  int32_t setupClientSimGen(string a, uint16_t p);
240  int32_t setupClientAcstb(string a, uint16_t p);
241 
242  //int32_t setupServer();
243  //int32_t setupServer(uint16_t p);
244  int32_t setupServer(uint16_t port, float timeout_sec);
245 
246 
247  int32_t send(string package2send);
248  int32_t receiveLoop();
249  int32_t receiveOnce();
250 
251  int32_t close();
252 
253  string receivedData; // container for received data
254 
255 };
256 
258 
259 #endif // SOCKETLIB_H
static string port
Definition: add_radio.cpp:16
vector< socket_channel > socket_find_addresses(NetworkType ntype)
Discover interfaces.
Definition: socketlib.cpp:547
Agent socket using Unicast UDP.
int32_t socket_set_udp_checksum(vector< uint8_t > &packet)
Set UDP checksum.
Definition: socketlib.cpp:428
Agent socket using Unicast TCP.
int32_t socket_close(socket_channel *channel)
Close socket.
Definition: socketlib.cpp:509
#define COSMOS_MAX_NAME
Largest JSON name.
Definition: cosmos-defs.h:55
SocketOptions()
Definition: socketlib.h:196
int iretn
Definition: rw_test.cpp:37
int32_t socket_sendto(socket_channel &channel, const string buffer, int flags=0)
Definition: socketlib.cpp:737
int32_t socket_accept(socket_channel server, socket_channel &client)
Definition: socketlib.cpp:311
int32_t socket_recvfrom(socket_channel &channel, vector< uint8_t > &buffer, size_t maxlen, int flags=0)
Definition: socketlib.cpp:720
#define SOCKET_TALK
Talk followed by optional listen (sendto address)
Definition: socketlib.h:82
static double * p
Definition: gauss_jackson_test.cpp:42
uint16_t cport
Definition: socketlib.h:130
char address[]
Definition: netperf_listen.cpp:69
Agent socket using Broadcast CSP.
#define SOCKET_RCVTIMEO
Default SOCKET RCVTIMEO (100 msec)
Definition: socketlib.h:91
static char buffer[255]
Definition: propagator_simple.cpp:60
NetworkType type
Definition: socketlib.h:118
Definition: socketlib.h:115
int32_t socket_check_udp_checksum(vector< uint8_t > packet)
Check UDP checksum.
Definition: socketlib.cpp:407
Headers and definitions common to all COSMOS.
string name
Definition: cubesat2obj.cpp:6
NetworkType
Definition: socketlib.h:63
#define SOCKET_BLOCKING
Blocking Agent.
Definition: socketlib.h:78
gige_handle * handle
Definition: kpc9612p_recv.cpp:33
int addrlen
Definition: socketlib.h:128
Definition: eci2kep_test.cpp:33
Agent socket using Broadcast UDP.
uint16_t msgsize
Definition: socketlib.h:132
NetworkType ntype
Definition: agent_node.cpp:50
Agent socket using Multicast UDP.
uint16_t socket_calc_udp_checksum(vector< uint8_t > packet)
Calculate UDP Checksum.
Definition: socketlib.cpp:335
int32_t socket_open(socket_channel *channel, NetworkType ntype, const char *address, uint16_t port, uint16_t direction, bool blocking=true, uint32_t usectimeo=0, uint32_t rcvbuf=0, uint32_t sndbuf=0)
Open UDP socket.
Definition: socketlib.cpp:51
int32_t socket_blocking(socket_channel *channel, bool blocking)
Definition: socketlib.cpp:448
Definition: socketlib.h:212
Definition: socketlib.h:167
SocketOptions sok
Definition: socketlib.h:218
string receivedData
Definition: socketlib.h:253