This file is indexed.

/usr/include/cec-platform/posix/os-socket.h is in libcec-platform-dev 1.0.10+dfsg1-8.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#pragma once
/*
 * This file is part of the libCEC(R) library.
 *
 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited.  All rights reserved.
 * libCEC(R) is an original work, containing original code.
 *
 * libCEC(R) is a trademark of Pulse-Eight Limited.
 *
 * This program is dual-licensed; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *
 * Alternatively, you can license this library under a commercial license,
 * please contact Pulse-Eight Licensing for more information.
 *
 * For more information contact:
 * Pulse-Eight Licensing       <license@pulse-eight.com>
 *     http://www.pulse-eight.com/
 *     http://www.pulse-eight.net/
 */


#include "../os.h"
#include "../util/timeutils.h"
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <poll.h>

/* Needed on Mac OS/X */
#ifndef SOL_TCP
#define SOL_TCP IPPROTO_TCP
#endif

namespace PLATFORM
{
  // Standard sockets
  //@{
  inline void SocketClose(socket_t socket)
  {
    if (socket != INVALID_SOCKET_VALUE)
      close(socket);
  }

  inline void SocketSetBlocking(socket_t socket, bool bSetTo)
  {
    if (socket != INVALID_SOCKET_VALUE)
    {
      if (bSetTo)
        fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
      else
        fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
    }
  }

  inline ssize_t SocketWrite(socket_t socket, int *iError, void* data, size_t len)
  {
    fd_set port;

    if (socket == INVALID_SOCKET_VALUE)
    {
      *iError = EINVAL;
      return -EINVAL;
    }

    ssize_t iBytesWritten(0);
    struct timeval *tv(NULL);

    while (iBytesWritten < (ssize_t)len)
    {
      FD_ZERO(&port);
      FD_SET(socket, &port);
      int returnv = select(socket + 1, NULL, &port, NULL, tv);
      if (returnv < 0)
      {
        *iError = errno;
        return -errno;
      }
      else if (returnv == 0)
      {
        *iError = ETIMEDOUT;
        return -ETIMEDOUT;
      }

      returnv = write(socket, (char*)data + iBytesWritten, len - iBytesWritten);
      if (returnv == -1)
      {
        *iError = errno;
        return -errno;
      }
      iBytesWritten += returnv;
    }

    return iBytesWritten;
  }

  inline ssize_t SocketRead(socket_t socket, int *iError, void* data, size_t len, uint64_t iTimeoutMs /*= 0*/)
  {
    fd_set port;
    struct timeval timeout, *tv;
    ssize_t iBytesRead(0);
    *iError = 0;
    CTimeout readTimeout(iTimeoutMs);

    if (socket == INVALID_SOCKET_VALUE)
    {
      *iError = EINVAL;
      return -EINVAL;
    }

    while (iBytesRead >= 0 && iBytesRead < (ssize_t)len && (iTimeoutMs == 0 || readTimeout.TimeLeft() > 0))
    {
      if (iTimeoutMs == 0)
      {
        tv = NULL;
      }
      else
      {
        long iTimeLeft = (long)readTimeout.TimeLeft();
        timeout.tv_sec  = iTimeLeft / (long int)1000.;
        timeout.tv_usec = iTimeLeft % (long int)1000.;
        tv = &timeout;
      }

      FD_ZERO(&port);
      FD_SET(socket, &port);
      int32_t returnv = select(socket + 1, &port, NULL, NULL, tv);

      if (returnv == -1)
      {
        *iError = errno;
        return -errno;
      }
      else if (returnv == 0)
      {
        break; //nothing to read
      }

      returnv = read(socket, (char*)data + iBytesRead, len - iBytesRead);
      if (returnv == -1)
      {
        *iError = errno;
        return -errno;
      }

      iBytesRead += returnv;
    }

    return iBytesRead;
  }

  inline int SocketIoctl(socket_t socket, int *iError, int request, void* data)
  {
    if (socket == INVALID_SOCKET_VALUE)
    {
      *iError = EINVAL;
      return -1;
    }

    int iReturn = ioctl(socket, request, data);
    if (iReturn < 0)
      *iError = errno;
    return iReturn;
  }
  //@}


  // TCP
  //@{
  inline void TcpSocketClose(tcp_socket_t socket)
  {
    SocketClose(socket);
  }

  inline void TcpSocketShutdown(tcp_socket_t socket)
  {
    if (socket != INVALID_SOCKET_VALUE)
      shutdown(socket, SHUT_RDWR);
  }

  inline ssize_t TcpSocketWrite(tcp_socket_t socket, int *iError, void* data, size_t len)
  {
    if (socket == INVALID_SOCKET_VALUE)
    {
      *iError = EINVAL;
      return -1;
    }

    ssize_t iReturn = send(socket, data, len, 0);
    if (iReturn < (ssize_t)len)
      *iError = errno;
    return iReturn;
  }

  inline ssize_t TcpSocketRead(tcp_socket_t socket, int *iError, void* data, size_t len, uint64_t iTimeoutMs /*= 0*/)
  {
    int64_t iNow(0), iTarget(0);
    ssize_t iBytesRead(0);
    *iError = 0;

    if (socket == INVALID_SOCKET_VALUE)
    {
      *iError = EINVAL;
      return -EINVAL;
    }

    if (iTimeoutMs > 0)
    {
      iNow    = GetTimeMs();
      iTarget = iNow + (int64_t) iTimeoutMs;
    }

    struct pollfd fds;
    fds.fd = socket;
    fds.events = POLLIN;
    fds.revents = 0;

    while (iBytesRead >= 0 &&
        iBytesRead < (ssize_t)len &&
        (iTimeoutMs == 0 || iTarget > iNow) &&
        *iError == 0)
    {
      if (iTimeoutMs > 0)
      {
        int iPollResult = poll(&fds, 1, iTarget - iNow);
        if (iPollResult == 0)
          *iError = ETIMEDOUT;
      }

      ssize_t iReadResult = (iTimeoutMs > 0) ?
          recv(socket, (char*)data + iBytesRead, len - iBytesRead, MSG_DONTWAIT) :
          recv(socket, data, len, MSG_WAITALL);
      if (iReadResult < 0)
      {
        if (errno == EAGAIN && iTimeoutMs > 0)
          continue;
        *iError = errno;
        return (iBytesRead > 0) ? iBytesRead : -errno;
      }
      else if (iReadResult == 0 || (iReadResult != (ssize_t)len && iTimeoutMs == 0))
      {
        *iError = ECONNRESET;
      }

      iBytesRead += iReadResult;

      if (iTimeoutMs > 0)
        iNow = GetTimeMs();
    }

    if (iBytesRead < (ssize_t)len && iError == 0)
      *iError = ETIMEDOUT;
    return iBytesRead;
  }

  inline bool TcpResolveAddress(const char *strHost, uint16_t iPort, int *iError, struct addrinfo **info)
  {
    struct   addrinfo hints;
    char     service[33];
    memset(&hints, 0, sizeof(hints));
    hints.ai_family   = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    sprintf(service, "%d", iPort);

    *iError = getaddrinfo(strHost, service, &hints, info);
    return !(*iError);
  }

  inline int TcpGetSocketError(tcp_socket_t socket)
  {
    int iReturn(0);
    socklen_t optLen = sizeof(socket_t);
    getsockopt(socket, SOL_SOCKET, SO_ERROR, (void *)&iReturn, &optLen);
    return iReturn;
  }

  inline bool TcpSetNoDelay(tcp_socket_t socket)
  {
    int iSetTo(1);
    setsockopt(socket, SOL_TCP, TCP_NODELAY, &iSetTo, sizeof(iSetTo));
    return true;
  }

  inline bool TcpConnectSocket(tcp_socket_t socket, struct addrinfo* addr, int *iError, uint64_t iTimeout = 0)
  {
    *iError = 0;
    SocketSetBlocking(socket, false);
    int iConnectResult = connect(socket, addr->ai_addr, addr->ai_addrlen);
    SocketSetBlocking(socket, true);
    if (iConnectResult == -1)
    {
      if (errno == EINPROGRESS)
      {
        struct pollfd pfd;
        pfd.fd = socket;
        pfd.events = POLLOUT;
        pfd.revents = 0;

        int iPollResult = poll(&pfd, 1, iTimeout);
        if (iPollResult == 0)
          *iError = ETIMEDOUT;
        else if (iPollResult == -1)
          *iError = errno;

        if (*iError == 0)
        {
          socklen_t errlen = sizeof(int);
          getsockopt(socket, SOL_SOCKET, SO_ERROR, (void *)iError, &errlen);
        }
      }
      else
      {
        *iError = errno;
      }
    }

    return *iError == 0;
  }
  //@}
}