dn_udp.c
Go to the documentation of this file.
1 
25 #include "stdint.h"
26 #include <string.h>
27 
28 #if defined(_USE_WIN_API)
29 #include <winsock2.h>
30 #pragma comment(lib, "wsock32.lib")
31 typedef int socklen_t;
32 #elif defined(_USE_LINUX_API)
33 #include <arpa/inet.h>
34 #include <errno.h>
35 #include <sys/socket.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 #include "dn_udp.h"
44 
51 HRESULT
52 udp_open(void *param, int *sock)
53 {
54  HRESULT hr;
55  const struct CONN_PARAM_ETH *eth_param = (const struct CONN_PARAM_ETH *) param;
56 
57  if (param == NULL || sock == NULL)
58  return E_INVALIDARG;
59 
60  hr = socket_open(SOCK_DGRAM, sock);
61  if (FAILED(hr))
62  return hr;
63 
64  /* Binds the created socket */
65  hr = socket_bind(eth_param, sock);
66  if (FAILED(hr)) {
67  socket_close(sock);
68  return hr;
69  }
70 
71  return hr;
72 }
73 
79 HRESULT
80 udp_close(int *sock)
81 {
82  return socket_close(sock);
83 }
84 
93 HRESULT
94 udp_send(int sock, const char *buf, uint32_t len_buf, void *arg)
95 {
96  int ret, flag = 0;
97  uint32_t len_send, len_sended;
98  struct udpaddr *opt = (struct udpaddr *) arg;
99 
100  if (sock <= 0)
101  return E_HANDLE;
102  if (buf == NULL || strlen(buf) == 0 || arg == NULL)
103  return E_INVALIDARG;
104 
105  len_send = (len_buf != 0) ? len_buf : strlen(buf);
106 
107  flag |= opt->flag;
108 
109  ret = sendto(sock, buf, len_send, flag, (struct sockaddr *) &opt->addr,
110  sizeof(struct sockaddr_in));
111  len_sended = ret;
112 
113  if (ret < 0) {
114  ret = DNGetLastError();
115  return OSERR2HRESULT(ret);
116  }
117 
118  if (len_send > len_sended) {
119  return E_TIMEOUT;
120  }
121 
122  return S_OK;
123 }
124 
134 HRESULT
135 udp_recv(int sock, char *buf, uint32_t len_buf, uint32_t *len_recved,
136  uint32_t timeout, void *arg)
137 {
138  int ret, flag = 0;
139  socklen_t len_from = sizeof(struct sockaddr_in);
140  struct udpaddr *opt = (struct udpaddr *) arg;
141  HRESULT hr;
142 
143  if (sock <= 0)
144  return E_HANDLE;
145  if (buf == NULL || len_recved == NULL || arg == NULL)
146  return E_INVALIDARG;
147 
148  hr = check_timeout(sock, timeout);
149  if (FAILED(hr))
150  return hr;
151 
152  flag |= opt->flag;
153 
154  ret = recvfrom(sock, buf, len_buf, flag, (struct sockaddr *) &opt->addr,
155  &len_from);
156  *len_recved = ret;
157 
158  if (ret < 0) {
159  ret = DNGetLastError();
160  return OSERR2HRESULT(ret);
161  }
162 
163  return S_OK;
164 }
165 
172 HRESULT
173 udp_set_timeout(int sock, uint32_t timeout)
174 {
175  return socket_set_timeout(sock, timeout);
176 }
177 
184 HRESULT
185 udp_clear(int sock, uint32_t timeout)
186 {
187  uint32_t len_recv = DEV_BUF_MAX, len_recved;
188  char buf_tmp[DEV_BUF_MAX];
189  HRESULT hr;
190  struct udpaddr opt;
191 
192  if (sock <= 0)
193  return E_HANDLE;
194 
195  hr = udp_set_timeout(sock, 0);
196  if (FAILED(hr))
197  return hr;
198 
199  opt.flag = 0;
200  do {
201  hr = udp_recv(sock, buf_tmp, len_recv, &len_recved, 0, &opt);
202  } while (SUCCEEDED(hr));
203 
204  hr = udp_set_timeout(sock, timeout);
205 
206  return hr;
207 }
208 
215 HRESULT
216 udp_check_sockaddr(const struct sockaddr_in *sock_to,
217  const struct sockaddr_in *sock_from)
218 {
219  if (sock_to == NULL || sock_from == NULL)
220  return E_INVALIDARG;
221 
222  if ((sock_to->sin_addr.s_addr != sock_from->sin_addr.s_addr)
223  || (sock_to->sin_port != sock_from->sin_port))
224  {
225  return E_ACCESSDENIED;
226  }
227 
228  return S_OK;
229 }
HRESULT udp_open(void *param, int *sock)
Opens UDP socket.
Definition: dn_udp.c:52
static int sendto(SOCKET s, const char *buf, int len, int flags, const struct sockaddr *to, int tolen)
unsigned uint32_t
Definition: stdint.h:43
UDP API file.
HRESULT udp_close(int *sock)
Closes the socket.
Definition: dn_udp.c:80
int flag
Definition: dn_udp.h:58
HRESULT udp_recv(int sock, char *buf, uint32_t len_buf, uint32_t *len_recved, uint32_t timeout, void *arg)
Receives UDP packet.
Definition: dn_udp.c:135
#define S_OK
Succeeded.
Definition: dn_common.h:89
Socket API file.
#define SOCK_DGRAM
Definition: dn_additional.h:68
A type definition for parameters of udp_send and udp_recv.
Definition: dn_udp.h:56
#define FAILED(hr)
A macro that returns TRUE/FALSE. If hr is less than zero, then returns TRUE.
Definition: dn_common.h:77
#define E_HANDLE
Failed because the handle is invalid.
Definition: dn_common.h:119
#define DEV_BUF_MAX
The maximum buffer size of a packet.
Definition: dn_device.h:101
#define E_ACCESSDENIED
Failed because the resource is not ready.
Definition: dn_common.h:113
#define E_INVALIDARG
Failed because some arguments are invalid.
Definition: dn_common.h:131
static int recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
int32_t HRESULT
Definition: dn_common.h:61
#define DNGetLastError()
A macro that gets last OS error.
HRESULT udp_clear(int sock, uint32_t timeout)
Clears the received buffer.
Definition: dn_udp.c:185
uint16_t sin_port
_DN_EXP_SOCKET HRESULT socket_close(int *sock)
Closes the socket.
Definition: dn_socket.c:176
#define SUCCEEDED(hr)
A macro that returns TRUE/FALSE. If hr is zero or more, then returns TRUE.
Definition: dn_common.h:71
_DN_EXP_SOCKET 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
struct sockaddr_in addr
Definition: dn_udp.h:59
A type definition for Ethernet connection parameters.
Definition: dn_device.h:144
Common device API file.
int socklen_t
Common API file.
HRESULT udp_send(int sock, const char *buf, uint32_t len_buf, void *arg)
Sends UDP packet.
Definition: dn_udp.c:94
User own API file.
_DN_EXP_SOCKET HRESULT socket_set_timeout(int sock, uint32_t timeout)
Sets timeout value to the socket.
Definition: dn_socket.c:255
struct in_addr sin_addr
_DN_EXP_SOCKET HRESULT socket_open(int type, int *sock)
Creates a socket.
Definition: dn_socket.c:129
_DN_EXP_DEVICE HRESULT check_timeout(int sock, uint32_t timeout)
Checks the communication timeout.
Definition: dn_device.c:385
HRESULT udp_check_sockaddr(const struct sockaddr_in *sock_to, const struct sockaddr_in *sock_from)
Checks the socket address. If sock_to and sock_from are equivalent, then returns S_OK.
Definition: dn_udp.c:216
#define E_TIMEOUT
Failed because the communication timed out.
Definition: dn_common.h:169
HRESULT udp_set_timeout(int sock, uint32_t timeout)
Sets timeout value to the UDP socket.
Definition: dn_udp.c:173
#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