libvisiontransfer  8.1.0
networking.cpp
1 /*******************************************************************************
2  * Copyright (c) 2020 Nerian Vision GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #include "visiontransfer/networking.h"
16 #include "visiontransfer/exceptions.h"
17 #include <cstring>
18 #include <fcntl.h>
19 
20 using namespace std;
21 using namespace visiontransfer;
22 using namespace visiontransfer::internal;
23 
24 namespace visiontransfer {
25 namespace internal {
26 
27 void Networking::initNetworking() {
28 #ifdef _WIN32
29  // In windows, we first have to initialize winsock
30  WSADATA wsaData;
31  if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
32  throw TransferException("WSAStartup failed!");
33  }
34 #endif
35 }
36 
37 addrinfo* Networking::resolveAddress(const char* address, const char* service) {
38  addrinfo hints;
39  memset(&hints, 0, sizeof(hints));
40  hints.ai_family = AF_INET; // Use IPv4
41  hints.ai_socktype = SOCK_STREAM;
42  hints.ai_flags = 0;
43  hints.ai_protocol = 0;
44 
45  addrinfo* addressInfo = nullptr;
46 
47  if(getaddrinfo(address, service, &hints, &addressInfo) != 0 || addressInfo == nullptr) {
48  TransferException ex("Error resolving address: " + string(strerror(errno)));
49  throw ex;
50  }
51 
52  if(addressInfo->ai_addrlen != sizeof(sockaddr_in)) {
53  throw TransferException("Illegal address length");
54  }
55 
56  return addressInfo;
57 }
58 
59 SOCKET Networking::connectTcpSocket(const addrinfo* address) {
60  SOCKET sock = ::socket(address->ai_family, address->ai_socktype,
61  address->ai_protocol);
62  if(sock == INVALID_SOCKET) {
63  TransferException ex("Error creating socket: " + string(strerror(errno)));
64  throw ex;
65  }
66 
67  if(connect(sock, address->ai_addr, static_cast<int>(address->ai_addrlen)) < 0) {
68  TransferException ex("Error connection to destination address: " + string(strerror(errno)));
69  throw ex;
70  }
71 
72  return sock;
73 }
74 
75 void Networking::setSocketTimeout(SOCKET socket, int timeoutMillisec) {
76 #ifdef _WIN32
77  unsigned int timeout = timeoutMillisec;
78 #else
79  struct timeval timeout;
80  timeout.tv_sec = timeoutMillisec/1000;
81  timeout.tv_usec = timeoutMillisec*1000;
82 #endif
83 
84  setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<char*>(&timeout), sizeof(timeout));
85  setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<char*>(&timeout), sizeof(timeout));
86 }
87 
88 void Networking::closeSocket(SOCKET& socket) {
89  setSocketBlocking(socket, false);
90  shutdown(socket, SHUT_WR);
91 
92  // Receive remaining data
93  char buffer[1024];
94  for(int i=0; i<3; i++) {
95  int received = recv(socket, buffer, sizeof(buffer), 0);
96  if(received <= 0) {
97  break;
98  }
99  }
100 
101  close(socket);
102  socket = INVALID_SOCKET;
103 }
104 
105 void Networking::setSocketBlocking(SOCKET socket, bool blocking) {
106 #ifdef _WIN32
107  unsigned long on = (blocking ? 0 : 1);
108  ioctlsocket(socket, FIONBIO, &on);
109 #else
110  int flags = fcntl(socket, F_GETFL, 0);
111  if(flags != -1) {
112  if(blocking) {
113  flags &= ~O_NONBLOCK;
114  } else {
115  flags |= O_NONBLOCK;
116  }
117  fcntl(socket, F_SETFL, flags);
118  }
119 #endif
120 }
121 
122 void Networking::enableReuseAddress(SOCKET socket, bool reuse) {
123  int enable = reuse ? 1 : 0;
124  setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char*>(&enable), sizeof(int));
125 }
126 
127 void Networking::bindSocket(SOCKET socket, const addrinfo* addressInfo) {
128  if (::bind(socket, addressInfo->ai_addr, static_cast<int>(addressInfo->ai_addrlen)) < 0) {
129  TransferException ex("Error binding socket: " + string(strerror(errno)));
130  throw ex;
131  }
132 }
133 
134 SOCKET Networking::acceptConnection(SOCKET socket, sockaddr_in& remoteAddress) {
135  socklen_t clientAddressLength = sizeof(sockaddr_in);
136 
137  SOCKET newSocket = accept(socket, reinterpret_cast<sockaddr *>(&remoteAddress),
138  &clientAddressLength);
139 
140  if(clientAddressLength != sizeof(sockaddr_in)) {
141  throw TransferException("Received network address with invalid length");
142  }
143 
144  if(newSocket == INVALID_SOCKET) {
145  if(errno == EWOULDBLOCK || errno == ETIMEDOUT) {
146  // No connection
147  return INVALID_SOCKET;
148  } else {
149  TransferException ex("Error accepting connection: " + string(strerror(errno)));
150  throw ex;
151  }
152  }
153 
154  return newSocket;
155 }
156 
157 }} // namespace
158 
Exception class that is used for all transfer exceptions.
Definition: exceptions.h:33
Nerian Vision Technologies