File: tcp.c

package info (click to toggle)
gmerlin-avdecoder 2.0.0~svn6298~dfsg0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,112 kB
  • sloc: ansic: 94,580; sh: 705; makefile: 705; awk: 43; sed: 16
file content (261 lines) | stat: -rw-r--r-- 6,303 bytes parent folder | download | duplicates (4)
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
/*****************************************************************
 * gmerlin-avdecoder - a general purpose multimedia decoding library
 *
 * Copyright (c) 2001 - 2012 Members of the Gmerlin project
 * gmerlin-general@lists.sourceforge.net
 * http://gmerlin.sourceforge.net
 *
 * This program is free software: 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, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/


#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
                                                                               
#include <fcntl.h>
#include <sys/types.h>

#ifdef _WIN32
#include <bgavdefs.h>
#include <winsock2.h>
#include <ws2spi.h>
#include <ws2tcpip.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#endif


#include <avdec_private.h>
#define LOG_DOMAIN "tcp"

#if !HAVE_DECL_MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif

/* for MacOSX */
#ifndef AI_NUMERICSERV
#define AI_NUMERICSERV 0
#endif

/* Utility functions */

static int create_socket(int domain, int type, int protocol)
  {
  int ret;
#if HAVE_DECL_SO_NOSIGPIPE // OSX
  int value = 1;
#endif

  ret = socket(domain, type, protocol);

#if HAVE_DECL_SO_NOSIGPIPE // OSX
  if(ret < 0)
    return ret;
  if(setsockopt(ret, SOL_SOCKET, SO_NOSIGPIPE, &value,
                sizeof(int)) == -1)
    return -1;
#endif
  return ret;
  }

static void address_set_port(struct addrinfo * info, int port)
  {
  while(info)
    {
    switch(info->ai_family)
      {
      case AF_INET:
        {
        struct sockaddr_in * addr;
        addr = (struct sockaddr_in*)info->ai_addr;
        addr->sin_port = htons(port);
        }
        break;
      case AF_INET6:
        {
        struct sockaddr_in6 * addr;
        addr = (struct sockaddr_in6*)info->ai_addr;
        addr->sin6_port = htons(port);
        }
        break;
      default:
        break;
      }
    info = info->ai_next;
    }
  }

struct addrinfo *
bgav_hostbyname(const bgav_options_t * opt,
                const char * hostname, int port, int socktype, int flags)
  {
  int err;
  struct in_addr ipv4_addr;
  
  struct addrinfo hints;
  struct addrinfo * ret;
  char * service = NULL;
  
  memset(&hints, 0, sizeof(hints));
  //  hints.ai_family   = AF_UNSPEC;
  hints.ai_family   = AF_INET;
  hints.ai_socktype = socktype; // SOCK_STREAM, SOCK_DGRAM
  hints.ai_protocol = 0; // PF_INET, PF_INET6
  hints.ai_flags    = flags;
  
  /* prevent DNS lookup for numeric IP addresses */
  
  if(hostname && bgav_inet_aton(hostname, &ipv4_addr))
    hints.ai_flags |= AI_NUMERICHOST;
  
  if(!hostname)
    {
    service = bgav_sprintf("%d", port);
    hints.ai_flags |= AI_NUMERICSERV;
    }
  
  if((err = getaddrinfo(hostname, service /* service */,
                        &hints, &ret)))
    {
    gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN,
             "Cannot resolve address of %s: %s",
             hostname, gai_strerror(err));
    return NULL;
    }

  if(hostname)
    address_set_port(ret, port);
  
  if(service)
    free(service);
  
  return ret;
  }


/* Client connection (stream oriented) */
                                                                               
static int socket_connect_inet(const bgav_options_t * opt, struct addrinfo * addr)
  {
  int ret = -1;
  int err;
  socklen_t err_len;

  struct timeval timeout;
  fd_set write_fds;
#ifdef _WIN32
  unsigned long flags = 1;
#endif
  
  /* Create the socket */
  if((ret = create_socket(addr->ai_family, SOCK_STREAM, 0)) < 0)
    {
    gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Cannot create socket");
    return -1;
    }
  
  /* Set nonblocking mode */
#ifdef _WIN32
  if (ioctlsocket(ret,FIONBIO, &flags) == SOCKET_ERROR)
#else
  if(fcntl(ret, F_SETFL, O_NONBLOCK) < 0)
#endif   
    {
    gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Cannot set nonblocking mode");
    return -1;
    }
  
  /* Connect the thing */
  if(connect(ret, addr->ai_addr, addr->ai_addrlen)<0)
    {
    if(errno == EINPROGRESS)
      {
      timeout.tv_sec = opt->connect_timeout / 1000;
      timeout.tv_usec = 1000 * (opt->connect_timeout % 1000);
      FD_ZERO (&write_fds);
      FD_SET (ret, &write_fds);
      if(!select(ret+1, NULL, &write_fds,NULL,&timeout))
        {
        gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Connection timed out");
        return -1;
        }
      }
    else
      {
      gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN,
               "Connecting failed: %s", strerror(errno));
      return -1;
      }
    }

  /* Check for errors */
  err_len = sizeof(err);
  getsockopt(ret, SOL_SOCKET, SO_ERROR, &err, &err_len);

  if(err)
    {
    gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Connecting failed: %s",
             strerror(err));
    return -1;
    }
  
  /* Set back to blocking mode */
#ifdef _WIN32
  flags = 0;
  if (ioctlsocket(ret,FIONBIO, &flags) == SOCKET_ERROR)
#else
  if(fcntl(ret, F_SETFL, 0) < 0)
#endif   
    {
    gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN,
             "Cannot set blocking mode");
    return -1;
    }
  return ret;
  }

int bgav_tcp_connect(const bgav_options_t * opt,
                     const char * host, int port)
  {
  struct addrinfo * addr;
  int ret;
  
  addr = bgav_hostbyname(opt, host, port, SOCK_STREAM, 0);
  if(!addr)
    return -1;
  ret = socket_connect_inet(opt, addr);
  freeaddrinfo(addr);
  return ret;
  }

int bgav_tcp_send(const bgav_options_t * opt, int fd,
                  uint8_t * data, int len)
  {
  int result;
  result = send(fd, data, len, MSG_NOSIGNAL);
  if(result != len)
    {
    gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN,
             "Could not send data: %s", strerror(errno));
    return 0;
    }
  return 1;
  }