aboutsummaryrefslogtreecommitdiffstats
path: root/examples/dvbinfo/tcp.c
blob: cae9849fabc34fca05d401f840be495aeea1e317 (plain)
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
/*****************************************************************************
 * tcp.c: network socket abstractions
 *****************************************************************************
 * Copyright (C) 2010-2013 M2X BV
 *
 * Authors: Jean-Paul Saman <jpsaman@videolan.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 *****************************************************************************/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

#if defined(HAVE_INTTYPES_H)
#   include <inttypes.h>
#elif defined(HAVE_STDINT_H)
#   include <stdint.h>
#endif

#include <sys/time.h>
#include <sys/types.h>

#ifdef HAVE_SYS_SOCKET_H
#   include <sys/socket.h>
#   include <netinet/in.h>
#   include <net/if.h>
#   if defined(WIN32)
#       include <netinet/if_ether.h>
#   endif
#   include <netdb.h>
#   include <netinet/ip.h>
#   include <netinet/udp.h>
#   include <arpa/inet.h>
#endif

#ifndef SOCK_CLOEXEC
#   include <fcntl.h>
#endif

#include "tcp.h"

#ifdef HAVE_SYS_SOCKET_H

#ifndef SOCK_CLOEXEC
#   include <stdbool.h>
static bool set_fdsocketclosexec(int s)
{
    int flags = fcntl(s, F_GETFD);
    if (flags != -1)
    {
        if (fcntl(s, F_SETFD, flags | FD_CLOEXEC) != -1)
        {
            return true;
        }
    }

    perror("tcp socket error");
    return false;
}
#endif

int tcp_close(int fd)
{
    int result = 0;

    result = shutdown(fd, SHUT_RDWR);
    if (result < 0)
        perror("tcp shutdown error");
    return result;
}

int tcp_open(const char *ipaddress, int port)
{
    int s_ctl = -1;
    int result = -1;

    if (!ipaddress) return -1;

    /* only support ipv4 */
    struct addrinfo hints, *addr;
    char *psz_service;

    if ((port > 65535) || (port < 0))
    {
        fprintf(stderr, "tcp error: invalid port %d specified\n", port);
        return -1;
    }
    if (asprintf(&psz_service, "%d", port) < 0)
        return -1;

    memset (&hints, 0, sizeof (hints));
    hints.ai_family = AF_INET; /* use AF_INET6 for ipv6 */
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_CANONNAME | 0;

    result = getaddrinfo(ipaddress, psz_service, &hints, &addr);
    if (result < 0)
    {
        fprintf(stderr, "tcp address error: %s\n", gai_strerror(result));
        free(psz_service);
        return -1;
    }

    for (struct addrinfo *ptr = addr; ptr != NULL; ptr = ptr->ai_next )
    {
        int sflags = 0;
#ifdef SOCK_CLOEXEC
        sflags = SOCK_CLOEXEC;
#endif
        s_ctl = socket(ptr->ai_family, ptr->ai_socktype | sflags, ptr->ai_protocol);
        if (s_ctl <= 0)
        {
            perror("tcp socket error");
            continue;
        }

#ifndef SOCK_CLOEXEC
        if (!set_fdsocketclosexec(s_ctl))
        {
            close(s_ctl);
            continue;
        }
#endif

        setsockopt (s_ctl, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof (int));

        result = connect( s_ctl, ptr->ai_addr, ptr->ai_addrlen );
        if (result < 0)
        {
            close(s_ctl);
            perror( "tcp connect error" );
            continue;
        }

        break; /* success */
    }

    freeaddrinfo(addr);
    free(psz_service);

    return s_ctl;
}

ssize_t tcp_read(int fd, void *buf, size_t count)
{
    ssize_t err;
again:
    err = recv(fd, buf, count, MSG_WAITALL);
    if (err < 0)
    {
        switch(errno)
        {
            case EINTR:
            case EAGAIN:
                goto again;
            case ECONNREFUSED:
                fprintf(stderr, "remote host refused connection\n");
                break;
            case ENOTCONN:
                fprintf(stderr, "connection not established\n");
                break;
            default:
                fprintf(stderr, "recv error: %s\n", strerror(errno));
                return -1;
        }
    }
    return err;
}
#endif

Privacy Policy