libvisiontransfer  6.4.0
networking.cpp
1 /*******************************************************************************
2  * Copyright (c) 2019 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 void Networking::initNetworking() {
25 #ifdef _WIN32
26  // In windows, we first have to initialize winsock
27  WSADATA wsaData;
28  if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
29  throw TransferException("WSAStartup failed!");
30  }
31 #endif
32 }
33 
34 addrinfo* Networking::resolveAddress(const char* address, const char* service) {
35  addrinfo hints;
36  memset(&hints, 0, sizeof(hints));
37  hints.ai_family = AF_INET; // Use IPv4
38  hints.ai_socktype = SOCK_STREAM;
39  hints.ai_flags = 0;
40  hints.ai_protocol = 0;
41 
42  addrinfo* addressInfo = nullptr;
43 
44  if(getaddrinfo(address, service, &hints, &addressInfo) != 0 || addressInfo == nullptr) {
45  TransferException ex("Error resolving address: " + string(strerror(errno)));
46  throw ex;
47  }
48 
49  if(addressInfo->ai_addrlen != sizeof(sockaddr_in)) {
50  throw TransferException("Illegal address length");
51  }
52 
53  return addressInfo;
54 }
55 
56 SOCKET Networking::connectTcpSocket(const addrinfo* address) {
57  SOCKET sock = ::socket(address->ai_family, address->ai_socktype,
58  address->ai_protocol);
59  if(sock == INVALID_SOCKET) {
60  TransferException ex("Error creating socket: " + string(strerror(errno)));
61  throw ex;
62  }
63 
64  if(connect(sock, address->ai_addr, static_cast<int>(address->ai_addrlen)) < 0) {
65  TransferException ex("Error connection to destination address: " + string(strerror(errno)));
66  throw ex;
67  }
68 
69  return sock;
70 }
71 
72 void Networking::setSocketTimeout(SOCKET socket, int timeoutMillisec) {
73 #ifdef _WIN32
74  unsigned int timeout = timeoutMillisec;
75 #else
76  struct timeval timeout;
77  timeout.tv_sec = timeoutMillisec/1000;
78  timeout.tv_usec = timeoutMillisec*1000;
79 #endif
80 
81  setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<char*>(&timeout), sizeof(timeout));
82  setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<char*>(&timeout), sizeof(timeout));
83 }
84 
85 void Networking::closeSocket(SOCKET& socket) {
86  setSocketBlocking(socket, false);
87  shutdown(socket, SHUT_WR);
88 
89  // Receive remaining data
90  char buffer[1024];
91  for(int i=0; i<3; i++) {
92  int received = recv(socket, buffer, sizeof(buffer), 0);
93  if(received <= 0) {
94  break;
95  }
96  }
97 
98  close(socket);
99  socket = INVALID_SOCKET;
100 }
101 
102 void Networking::setSocketBlocking(SOCKET socket, bool blocking) {
103 #ifdef _WIN32
104  unsigned long on = (blocking ? 0 : 1);
105  ioctlsocket(socket, FIONBIO, &on);
106 #else
107  int flags = fcntl(socket, F_GETFL, 0);
108  if(flags != -1) {
109  if(blocking) {
110  flags &= ~O_NONBLOCK;
111  } else {
112  flags |= O_NONBLOCK;
113  }
114  fcntl(socket, F_SETFL, flags);
115  }
116 #endif
117 }
118 
119 void Networking::enableReuseAddress(SOCKET socket, bool reuse) {
120  int enable = reuse ? 1 : 0;
121  setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char*>(&enable), sizeof(int));
122 }
123 
124 void Networking::bindSocket(SOCKET socket, const addrinfo* addressInfo) {
125  if (::bind(socket, addressInfo->ai_addr, static_cast<int>(addressInfo->ai_addrlen)) < 0) {
126  TransferException ex("Error binding socket: " + string(strerror(errno)));
127  throw ex;
128  }
129 }
130 
131 SOCKET Networking::acceptConnection(SOCKET socket, sockaddr_in& remoteAddress) {
132  socklen_t clientAddressLength = sizeof(sockaddr_in);
133 
134  SOCKET newSocket = accept(socket, reinterpret_cast<sockaddr *>(&remoteAddress),
135  &clientAddressLength);
136 
137  if(clientAddressLength != sizeof(sockaddr_in)) {
138  throw TransferException("Received network address with invalid length");
139  }
140 
141  if(newSocket == INVALID_SOCKET) {
142  if(errno == EWOULDBLOCK || errno == ETIMEDOUT) {
143  // No connection
144  return INVALID_SOCKET;
145  } else {
146  TransferException ex("Error accepting connection: " + string(strerror(errno)));
147  throw ex;
148  }
149  }
150 
151  return newSocket;
152 }
Exception class that is used for all transfer exceptions.
Definition: exceptions.h:33
Nerian Vision Technologies