27 #if defined(_USE_WIN_API) 29 #pragma comment(lib, "ws2_32.lib") 30 #elif defined(_USE_LINUX_API) 31 #include <arpa/inet.h> 34 #include <sys/socket.h> 46 #if defined(_USE_WIN_API) 49 shutdown(sock, SD_BOTH);
50 return closesocket(sock);
59 SOCKET_ADDRESS_LIST *addressList = (SOCKET_ADDRESS_LIST *)buffer;
62 if(WSAIoctl(*sock, SIO_ADDRESS_LIST_QUERY, NULL, 0, buffer, 1024, &d, NULL, NULL) != 0) {
72 for(i = 0; i < addressList->iAddressCount; i++) {
73 if(param->
src_addr == ((SOCKADDR_IN *)addressList->Address[i].lpSockaddr)->sin_addr.s_addr) {
82 #elif defined(_USE_LINUX_API) 85 shutdown(sock, SHUT_RDWR);
93 struct ifaddrs *ifaddr, *ifa;
96 if(getifaddrs(&ifaddr) == -1) {
103 for(ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
104 if((ifa->ifa_addr == NULL) || (ifa->ifa_addr->sa_family !=
AF_INET)) {
139 #if defined(_USE_WIN_API) 143 ret = WSAStartup(MAKEWORD(2, 0), &wsaData);
149 if(MAKEWORD(2, 0) != wsaData.wVersion) {
180 if (sock == NULL || *sock <= 0)
189 #if defined(_USE_WIN_API) 218 if ((param == NULL) || (sock == NULL))
238 ret =
bind(*sock, (
struct sockaddr *) &sockaddr,
sizeof(sockaddr));
263 tv.
tv_sec = timeout / 1000;
264 tv.
tv_usec = (timeout % 1000) * 1000;
static int socket(int af, int type, int protocol)
static int _socket_close(int sock)
HRESULT socket_close(int *sock)
Closes the socket.
#define FAILED(hr)
A macro that returns TRUE/FALSE. If hr is less than zero, then returns TRUE.
HRESULT socket_open(int type, int *sock)
Creates a socket.
static uint32_t htonl(uint32_t hostlong)
#define E_HANDLE
Failed because the handle is invalid.
HRESULT socket_bind(const struct CONN_PARAM_ETH *param, int *sock)
Binds the socket with param's source address and source port.
#define E_INVALIDARG
Failed because some arguments are invalid.
#define E_FAIL
Failed because unspecified error occurs.
#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.
A type definition for Ethernet connection parameters.
static int setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
static int32_t _socket_bind(const void *param, int *sock)
HRESULT socket_set_timeout(int sock, uint32_t timeout)
Sets timeout value to the socket.
#define OSERR2HRESULT(err)
A macro that returns HREUSLT(0x8091) which means OS error.