File: udp.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 (115 lines) | stat: -rw-r--r-- 3,023 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
/*****************************************************************
 * 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 <fcntl.h>
#include <sys/types.h>

#include <errno.h>
#include <string.h>

#ifdef _WIN32
#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 "udp"

int bgav_udp_open(const bgav_options_t * opt, int port)
  {
  int ret;
  size_t tmp = 0;
  struct addrinfo * addr;
  addr = bgav_hostbyname(opt, NULL, port, SOCK_DGRAM, AI_PASSIVE);

  /* Create the socket */
  if((ret = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
    {
    gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Cannot create socket");
    return -1;
    }
  
  /* Give the socket a name. */
  
  if(bind(ret, addr->ai_addr, addr->ai_addrlen) < 0)
    {
    gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN,
             "Cannot bind inet socket: %s", strerror(errno));
    return -1;
    }

  //  getsockopt(ret, SOL_SOCKET, SO_RCVBUF, &tmp, &optlen);
  tmp = 65536;
  setsockopt(ret, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp));
  
  gavl_log(GAVL_LOG_INFO, LOG_DOMAIN,
           "UDP Socket bound on port %d\n", port);
  
  freeaddrinfo(addr);
  return ret;
  }

int bgav_udp_read(int fd, uint8_t * data, int len)
  {
  int bytes_read;
  for(;;)
    {
    bytes_read = recv(fd, data, len, 0);
    if (bytes_read < 0)
      {
      if((errno == EAGAIN) ||
         (errno == EINTR))
        return -1;
      }
    else
      break;
    }
  return bytes_read;
  }

int bgav_udp_write(const bgav_options_t * opt,
                   int fd, uint8_t * data, int len,
                   struct addrinfo * addr)
  {
  for(;;)
    {
    if(sendto(fd, data, len, 0, addr->ai_addr, addr->ai_addrlen) < len)
      {
      if((errno == EAGAIN) ||
         (errno == EINTR))
        {
        gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN,
               "Sending UDP packet failed: %s\n", strerror(errno));
        return -1;
        }
      }
    }
  return len;
  }