Go to the documentation of this file.00001
00025 #include "stdint.h"
00026 #include <string.h>
00027
00028 #if defined(_USE_WIN_API)
00029 #include <winsock2.h>
00030 #pragma comment(lib, "wsock32.lib")
00031 typedef int socklen_t;
00032 #elif defined(_USE_LINUX_API)
00033 #include <arpa/inet.h>
00034 #include <errno.h>
00035 #include <sys/socket.h>
00036 #else
00037 #include "dn_additional.h"
00038 #endif
00039
00040 #include "dn_common.h"
00041 #include "dn_device.h"
00042 #include "dn_socket.h"
00043 #include "dn_udp.h"
00044
00051 HRESULT
00052 udp_open(void *param, int *sock)
00053 {
00054 HRESULT hr;
00055 const struct CONN_PARAM_ETH *eth_param = (const struct CONN_PARAM_ETH *) param;
00056
00057 if (param == NULL || sock == NULL)
00058 return E_INVALIDARG;
00059
00060 hr = socket_open(SOCK_DGRAM, sock);
00061 if (FAILED(hr))
00062 return hr;
00063
00064
00065 hr = socket_bind(eth_param, sock);
00066 if (FAILED(hr)) {
00067 socket_close(sock);
00068 return hr;
00069 }
00070
00071 return hr;
00072 }
00073
00079 HRESULT
00080 udp_close(int *sock)
00081 {
00082 return socket_close(sock);
00083 }
00084
00093 HRESULT
00094 udp_send(int sock, const char *buf, uint32_t len_buf, void *arg)
00095 {
00096 int ret, flag = 0;
00097 uint32_t len_send, len_sended;
00098 struct udpaddr *opt = (struct udpaddr *) arg;
00099
00100 if (sock <= 0)
00101 return E_HANDLE;
00102 if (buf == NULL || strlen(buf) == 0 || arg == NULL)
00103 return E_INVALIDARG;
00104
00105 len_send = (len_buf != 0) ? len_buf : strlen(buf);
00106
00107 flag |= opt->flag;
00108
00109 ret = sendto(sock, buf, len_send, flag, (struct sockaddr *) &opt->addr,
00110 sizeof(struct sockaddr_in));
00111 len_sended = ret;
00112
00113 if (ret < 0) {
00114 ret = DNGetLastError();
00115 return OSERR2HRESULT(ret);
00116 }
00117
00118 if (len_send > len_sended) {
00119 return E_TIMEOUT;
00120 }
00121
00122 return S_OK;
00123 }
00124
00134 HRESULT
00135 udp_recv(int sock, char *buf, uint32_t len_buf, uint32_t *len_recved,
00136 uint32_t timeout, void *arg)
00137 {
00138 int ret, flag = 0;
00139 socklen_t len_from = sizeof(struct sockaddr_in);
00140 struct udpaddr *opt = (struct udpaddr *) arg;
00141 HRESULT hr;
00142
00143 if (sock <= 0)
00144 return E_HANDLE;
00145 if (buf == NULL || len_recved == NULL || arg == NULL)
00146 return E_INVALIDARG;
00147
00148 hr = check_timeout(sock, timeout);
00149 if (FAILED(hr))
00150 return hr;
00151
00152 flag |= opt->flag;
00153
00154 ret = recvfrom(sock, buf, len_buf, flag, (struct sockaddr *) &opt->addr,
00155 &len_from);
00156 *len_recved = ret;
00157
00158 if (ret < 0) {
00159 ret = DNGetLastError();
00160 return OSERR2HRESULT(ret);
00161 }
00162
00163 return S_OK;
00164 }
00165
00172 HRESULT
00173 udp_set_timeout(int sock, uint32_t timeout)
00174 {
00175 return socket_set_timeout(sock, timeout);
00176 }
00177
00184 HRESULT
00185 udp_clear(int sock, uint32_t timeout)
00186 {
00187 uint32_t len_recv = DEV_BUF_MAX, len_recved;
00188 char buf_tmp[DEV_BUF_MAX];
00189 HRESULT hr;
00190 struct udpaddr opt;
00191
00192 if (sock <= 0)
00193 return E_HANDLE;
00194
00195 hr = udp_set_timeout(sock, 0);
00196 if (FAILED(hr))
00197 return hr;
00198
00199 opt.flag = 0;
00200 do {
00201 hr = udp_recv(sock, buf_tmp, len_recv, &len_recved, 0, &opt);
00202 } while (SUCCEEDED(hr));
00203
00204 hr = udp_set_timeout(sock, timeout);
00205
00206 return hr;
00207 }
00208
00215 HRESULT
00216 udp_check_sockaddr(const struct sockaddr_in *sock_to,
00217 const struct sockaddr_in *sock_from)
00218 {
00219 if (sock_to == NULL || sock_from == NULL)
00220 return E_INVALIDARG;
00221
00222 if ((sock_to->sin_addr.s_addr != sock_from->sin_addr.s_addr)
00223 || (sock_to->sin_port != sock_from->sin_port))
00224 {
00225 return E_ACCESSDENIED;
00226 }
00227
00228 return S_OK;
00229 }