dn_socket.c
Go to the documentation of this file.
1 
25 #include "stdint.h"
26 
27 #if defined(_USE_WIN_API)
28 #include <winsock2.h>
29 #pragma comment(lib, "ws2_32.lib")
30 #elif defined(_USE_LINUX_API)
31 #include <arpa/inet.h>
32 #include <errno.h>
33 #include <ifaddrs.h>
34 #include <sys/socket.h>
35 #include <unistd.h>
36 #else
37 #include "dn_additional.h"
38 #endif
39 
40 #include "dn_common.h"
41 #include "dn_device.h"
42 #include "dn_socket.h"
43 
45 
46 #if defined(_USE_WIN_API)
47 static int _socket_close(int sock)
48 {
49  shutdown(sock, SD_BOTH);
50  return closesocket(sock);
51 }
52 
53 static HRESULT _socket_bind(const struct CONN_PARAM_ETH *param, int *sock)
54 {
55  int i, ret;
56  HRESULT hr;
57  DWORD d;
58  char buffer[1024];
59  SOCKET_ADDRESS_LIST *addressList = (SOCKET_ADDRESS_LIST *)buffer;
60 
61  /* Gets list of interfaces */
62  if(WSAIoctl(*sock, SIO_ADDRESS_LIST_QUERY, NULL, 0, buffer, 1024, &d, NULL, NULL) != 0) {
63  ret = DNGetLastError();
64  return OSERR2HRESULT(ret);
65  }
66 
67  /* Checks source address */
68  if(param->src_addr == htonl(INADDR_LOOPBACK)) {
69  hr = S_OK;
70  } else {
71  hr = E_FAIL;
72  for(i = 0; i < addressList->iAddressCount; i++) {
73  if(param->src_addr == ((SOCKADDR_IN *)addressList->Address[i].lpSockaddr)->sin_addr.s_addr) {
74  hr = S_OK;
75  break;
76  }
77  }
78  }
79 
80  return hr;
81 }
82 #elif defined(_USE_LINUX_API)
83 static int _socket_close(int sock)
84 {
85  shutdown(sock, SHUT_RDWR);
86  return close(sock);
87 }
88 
89 static HRESULT _socket_bind(const struct CONN_PARAM_ETH *param, int *sock)
90 {
91  int ret;
92  HRESULT hr;
93  struct ifaddrs *ifaddr, *ifa;
94 
95  /* Gets list of interfaces */
96  if(getifaddrs(&ifaddr) == -1) {
97  ret = DNGetLastError();
98  return OSERR2HRESULT(ret);
99  }
100 
101  /* Checks source address */
102  hr = E_FAIL;
103  for(ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
104  if((ifa->ifa_addr == NULL) || (ifa->ifa_addr->sa_family != AF_INET)) {
105  continue;
106  }
107 
108  if(param->src_addr == (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr)) {
109  hr = S_OK;
110  break;
111  }
112  }
113 
114  freeifaddrs(ifaddr);
115 
116  return hr;
117 }
118 #endif
119 
128 HRESULT
129 socket_open(int type, int *sock)
130 {
131  int ret;
132 
133  if ((type != SOCK_STREAM) && (type != SOCK_DGRAM))
134  return E_INVALIDARG;
135 
136  if (sock == NULL)
137  return E_INVALIDARG;
138 
139 #if defined(_USE_WIN_API)
140  if(socket_counter == 0) {
141  WSADATA wsaData;
142 
143  ret = WSAStartup(MAKEWORD(2, 0), &wsaData);
144  if(ret != 0) {
145  ret = DNGetLastError();
146  return OSERR2HRESULT(ret);
147  }
148 
149  if(MAKEWORD(2, 0) != wsaData.wVersion) {
150  WSACleanup();
151  return E_UNEXPECTED;
152  }
153  }
154 #endif
155 
156  *sock = socket(AF_INET, type, 0);
157  if (*sock < 0) {
158  *sock = 0;
159  ret = DNGetLastError();
160  return OSERR2HRESULT(ret);
161  }
162 
163  socket_counter++;
164 
165  return S_OK;
166 }
167 
175 HRESULT
176 socket_close(int *sock)
177 {
178  int ret;
179 
180  if (sock == NULL || *sock <= 0)
181  return E_HANDLE;
182 
183  ret = _socket_close(*sock);
184  if (ret != 0) {
185  ret = DNGetLastError();
186  return OSERR2HRESULT(ret);
187  }
188 
189 #if defined(_USE_WIN_API)
190  if(socket_counter == 1) {
191  WSACleanup();
192  }
193 #endif
194 
195  *sock = 0;
196 
197  if (socket_counter > 0) {
198  socket_counter--;
199  }
200 
201  return S_OK;
202 }
203 
210 HRESULT
211 socket_bind(const struct CONN_PARAM_ETH *param, int *sock)
212 {
213  int ret;
214  struct sockaddr_in sockaddr =
215  { AF_INET, };
216  HRESULT hr = S_OK;
217 
218  if ((param == NULL) || (sock == NULL))
219  return E_INVALIDARG;
220 
221  sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
222  sockaddr.sin_port = param->src_port;
223 
224  if ((param->src_addr != htonl(INADDR_ANY))
225  && (param->src_addr != htonl(INADDR_NONE)))
226  {
227  hr = _socket_bind(param, sock);
228  if (FAILED(hr)) {
229  return hr;
230  }
231 
232  sockaddr.sin_addr.s_addr = param->src_addr;
233  }
234 
235  if ((sockaddr.sin_addr.s_addr != htonl(INADDR_ANY))
236  || (sockaddr.sin_port != 0))
237  {
238  ret = bind(*sock, (struct sockaddr *) &sockaddr, sizeof(sockaddr));
239  if (ret < 0) {
240  ret = DNGetLastError();
241  return OSERR2HRESULT(ret);
242  }
243  }
244 
245  return hr;
246 }
247 
254 HRESULT
255 socket_set_timeout(int sock, uint32_t timeout)
256 {
257  int ret;
258  struct timeval tv;
259 
260  if (sock <= 0)
261  return E_HANDLE;
262 
263  tv.tv_sec = timeout / 1000;
264  tv.tv_usec = (timeout % 1000) * 1000;
265 
266  ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *) &tv, sizeof(tv));
267  if (ret < 0) {
268  ret = DNGetLastError();
269  return OSERR2HRESULT(ret);
270  }
271 
272  ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv, sizeof(tv));
273  if (ret < 0) {
274  ret = DNGetLastError();
275  return OSERR2HRESULT(ret);
276  }
277 
278  return S_OK;
279 }
static int socket(int af, int type, int protocol)
unsigned uint32_t
Definition: stdint.h:43
static int _socket_close(int sock)
long tv_sec
Definition: dn_additional.h:56
HRESULT socket_close(int *sock)
Closes the socket.
Definition: dn_socket.c:176
#define S_OK
Succeeded.
Definition: dn_common.h:89
Socket API file.
#define SOCK_DGRAM
Definition: dn_additional.h:68
#define SOCK_STREAM
Definition: dn_additional.h:67
#define FAILED(hr)
A macro that returns TRUE/FALSE. If hr is less than zero, then returns TRUE.
Definition: dn_common.h:77
HRESULT socket_open(int type, int *sock)
Creates a socket.
Definition: dn_socket.c:129
static uint32_t htonl(uint32_t hostlong)
#define SO_RCVTIMEO
Definition: dn_additional.h:74
#define E_HANDLE
Failed because the handle is invalid.
Definition: dn_common.h:119
#define INADDR_NONE
Definition: dn_additional.h:37
HRESULT socket_bind(const struct CONN_PARAM_ETH *param, int *sock)
Binds the socket with param&#39;s source address and source port.
Definition: dn_socket.c:211
#define E_INVALIDARG
Failed because some arguments are invalid.
Definition: dn_common.h:131
long tv_usec
Definition: dn_additional.h:57
#define E_FAIL
Failed because unspecified error occurs.
Definition: dn_common.h:107
int32_t HRESULT
Definition: dn_common.h:61
uint32_t src_addr
Definition: dn_device.h:148
#define DNGetLastError()
A macro that gets last OS error.
static int bind(SOCKET s, const struct sockaddr *name, int namelen)
#define E_UNEXPECTED
Failed because unexpected error happens.
Definition: dn_common.h:137
uint16_t sin_port
#define SO_SNDTIMEO
Definition: dn_additional.h:73
#define AF_INET
Definition: dn_additional.h:66
uint16_t src_port
Definition: dn_device.h:149
A type definition for Ethernet connection parameters.
Definition: dn_device.h:144
Common device API file.
Common API file.
#define SOL_SOCKET
Definition: dn_additional.h:70
User own API file.
struct in_addr sin_addr
static int setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
static int32_t _socket_bind(const void *param, int *sock)
int socket_counter
Definition: dn_socket.c:44
#define INADDR_ANY
Definition: dn_additional.h:36
HRESULT socket_set_timeout(int sock, uint32_t timeout)
Sets timeout value to the socket.
Definition: dn_socket.c:255
#define OSERR2HRESULT(err)
A macro that returns HREUSLT(0x8091) which means OS error.
Definition: dn_common.h:205


bcap_core
Author(s): DENSO WAVE INCORPORATED
autogenerated on Mon Jun 10 2019 13:12:20