3 * This file is part of the libCEC(R) library.
5 * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
6 * libCEC(R) is an original work, containing original code.
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
10 * This program is dual-licensed; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
28 * For more information contact:
29 * Pulse-Eight Licensing <license@pulse-eight.com>
30 * http://www.pulse-eight.com/
31 * http://www.pulse-eight.net/
35 #include "lib/platform/os.h"
36 #include "lib/platform/util/timeutils.h"
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <netinet/tcp.h>
43 #include <arpa/inet.h>
47 /* Needed on Mac OS/X */
49 #define SOL_TCP IPPROTO_TCP
56 inline void SocketClose(socket_t socket
)
58 if (socket
!= INVALID_SOCKET_VALUE
)
62 inline void SocketSetBlocking(socket_t socket
, bool bSetTo
)
64 if (socket
!= INVALID_SOCKET_VALUE
)
67 fcntl(socket
, F_SETFL
, fcntl(socket
, F_GETFL
) & ~O_NONBLOCK
);
69 fcntl(socket
, F_SETFL
, fcntl(socket
, F_GETFL
) | O_NONBLOCK
);
73 inline ssize_t
SocketWrite(socket_t socket
, int *iError
, void* data
, size_t len
)
77 if (socket
== INVALID_SOCKET_VALUE
)
83 ssize_t
iBytesWritten(0);
84 struct timeval
*tv(NULL
);
86 while (iBytesWritten
< (ssize_t
)len
)
89 FD_SET(socket
, &port
);
90 ssize_t returnv
= (ssize_t
)select(socket
+ 1, NULL
, &port
, NULL
, tv
);
96 else if (returnv
== 0)
102 returnv
= write(socket
, (char*)data
+ iBytesWritten
, len
- iBytesWritten
);
108 iBytesWritten
+= returnv
;
111 return iBytesWritten
;
114 inline ssize_t
SocketRead(socket_t socket
, int *iError
, void* data
, size_t len
, uint64_t iTimeoutMs
/*= 0*/)
117 struct timeval timeout
, *tv
;
118 int64_t iNow(0), iTarget(0);
119 ssize_t
iBytesRead(0);
122 if (socket
== INVALID_SOCKET_VALUE
)
131 iTarget
= iNow
+ (int64_t) iTimeoutMs
;
134 while (iBytesRead
>= 0 && iBytesRead
< (ssize_t
)len
&& (iTimeoutMs
== 0 || iTarget
> iNow
))
142 timeout
.tv_sec
= ((long int)iTarget
- (long int)iNow
) / (long int)1000.;
143 timeout
.tv_usec
= ((long int)iTarget
- (long int)iNow
) % (long int)1000.;
148 FD_SET(socket
, &port
);
149 ssize_t returnv
= (ssize_t
)select(socket
+ 1, &port
, NULL
, NULL
, tv
);
156 else if (returnv
== 0)
158 break; //nothing to read
161 returnv
= read(socket
, (char*)data
+ iBytesRead
, len
- iBytesRead
);
168 iBytesRead
+= returnv
;
177 inline int SocketIoctl(socket_t socket
, int *iError
, int request
, void* data
)
179 if (socket
== INVALID_SOCKET_VALUE
)
185 int iReturn
= ioctl(socket
, request
, data
);
194 inline void TcpSocketClose(tcp_socket_t socket
)
199 inline void TcpSocketShutdown(tcp_socket_t socket
)
201 if (socket
!= INVALID_SOCKET_VALUE
)
202 shutdown(socket
, SHUT_RDWR
);
205 inline ssize_t
TcpSocketWrite(tcp_socket_t socket
, int *iError
, void* data
, size_t len
)
207 if (socket
== INVALID_SOCKET_VALUE
)
213 ssize_t iReturn
= send(socket
, data
, len
, 0);
214 if (iReturn
< (ssize_t
)len
)
219 inline ssize_t
TcpSocketRead(tcp_socket_t socket
, int *iError
, void* data
, size_t len
, uint64_t iTimeoutMs
/*= 0*/)
221 int64_t iNow(0), iTarget(0);
222 ssize_t
iBytesRead(0);
225 if (socket
== INVALID_SOCKET_VALUE
)
234 iTarget
= iNow
+ (int64_t) iTimeoutMs
;
242 while (iBytesRead
>= 0 && iBytesRead
< (ssize_t
)len
&& (iTimeoutMs
== 0 || iTarget
> iNow
))
246 int iPollResult
= poll(&fds
, 1, (int)(iTarget
- iNow
));
247 if (iPollResult
== 0)
254 ssize_t iReadResult
= (iTimeoutMs
> 0) ?
255 recv(socket
, (char*)data
+ iBytesRead
, len
- iBytesRead
, MSG_DONTWAIT
) :
256 recv(socket
, data
, len
, MSG_WAITALL
);
259 if (errno
== EAGAIN
&& iTimeoutMs
> 0)
264 else if (iReadResult
== 0 || (iReadResult
!= (ssize_t
)len
&& iTimeoutMs
== 0))
266 *iError
= ECONNRESET
;
270 iBytesRead
+= iReadResult
;
276 if (iBytesRead
< (ssize_t
)len
)
281 inline bool TcpResolveAddress(const char *strHost
, uint16_t iPort
, int *iError
, struct addrinfo
**info
)
283 struct addrinfo hints
;
285 memset(&hints
, 0, sizeof(hints
));
286 hints
.ai_family
= AF_UNSPEC
;
287 hints
.ai_socktype
= SOCK_STREAM
;
288 hints
.ai_protocol
= IPPROTO_TCP
;
289 sprintf(service
, "%d", iPort
);
291 *iError
= getaddrinfo(strHost
, service
, &hints
, info
);
295 inline int TcpGetSocketError(tcp_socket_t socket
)
298 socklen_t optLen
= sizeof(socket_t
);
299 getsockopt(socket
, SOL_SOCKET
, SO_ERROR
, (void *)&iReturn
, &optLen
);
303 inline bool TcpSetNoDelay(tcp_socket_t socket
)
306 setsockopt(socket
, SOL_TCP
, TCP_NODELAY
, &iSetTo
, sizeof(iSetTo
));
310 inline bool TcpConnectSocket(tcp_socket_t socket
, struct addrinfo
* addr
, int *iError
, uint64_t iTimeout
= 0)
313 int iConnectResult
= connect(socket
, addr
->ai_addr
, addr
->ai_addrlen
);
314 if (iConnectResult
== -1)
316 if (errno
== EINPROGRESS
)
320 pfd
.events
= POLLOUT
;
323 int iPollResult
= poll(&pfd
, 1, (int)iTimeout
);
324 if (iPollResult
== 0)
326 else if (iPollResult
== -1)
329 socklen_t errlen
= sizeof(int);
330 getsockopt(socket
, SOL_SOCKET
, SO_ERROR
, (void *)iError
, &errlen
);