File: getaddrinfo.c

package info (click to toggle)
hamlib 4.6.5-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,984 kB
  • sloc: ansic: 262,996; sh: 6,135; cpp: 1,578; perl: 876; makefile: 855; python: 148; awk: 58; xml: 26
file content (152 lines) | stat: -rw-r--r-- 3,858 bytes parent folder | download | duplicates (3)
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
/*
 *  Hamlib Interface - getaddrinfo replacement
 *  Copyright (c) 2000-2010 by Stephane Fillod
 *
 *
 *   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
 *
 */

/* Forcing WINVER in MinGW yanks in getaddrinfo(), but locks out Win95/Win98 */
/* #define WINVER 0x0501 */

#include <hamlib/config.h>

#include <stdlib.h>
#include <stdio.h>   /* Standard input/output definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <sys/types.h>
#include <signal.h>


#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#if HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#elif HAVE_WS2TCPIP_H
#include <ws2tcpip.h>
#   if defined(HAVE_WSPIAPI_H)
#       include <wspiapi.h>
#   endif
#endif


/*
 * Replacement for getaddrinfo. Only one addrinfo is returned.
 * Weak checking.
 * Return 0 when success, otherwise -1.
 */
#ifndef HAVE_GETADDRINFO
int getaddrinfo(const char *node, const char *service,
                const struct addrinfo *hints, struct addrinfo **res)
{
    struct addrinfo *p;
    int ai_family, ai_socktype, ai_protocol, ai_flags;

    /* limitation: service must be non null */
    if (!service)
    {
        return -1;
    }

    if (hints == NULL)
    {
        ai_family = AF_UNSPEC;
        ai_socktype = 0;
        ai_protocol = 0;
        ai_flags = 0;
    }
    else
    {
        ai_family = hints->ai_family;
        ai_socktype = hints->ai_socktype;
        ai_protocol = hints->ai_protocol;
        ai_flags = hints->ai_flags;
    }

    /* limitation: this replacement function only for IPv4 */
    if (ai_family == AF_UNSPEC)
    {
        ai_family = AF_INET;
    }

    if (ai_family != AF_INET)
    {
        return -1;
    }

    p = calloc(1, sizeof(struct addrinfo));

    if (!p)
    {
        return -1;
    }

    memset(p, 0, sizeof(struct addrinfo));
    p->ai_family = ai_family;
    p->ai_socktype = ai_socktype;
    p->ai_protocol = ai_protocol;
    p->ai_addrlen = sizeof(struct sockaddr_in);
    p->ai_addr = calloc(1, p->ai_addrlen);

    if (!p->ai_addr)
    {
        free(p);
        return -1;
    }

    memset((char *) p->ai_addr, 0, p->ai_addrlen);

    ((struct sockaddr_in *)p->ai_addr)->sin_family = p->ai_family;
    /* limitation: the service must be a port _number_ */
    ((struct sockaddr_in *)p->ai_addr)->sin_port = htons(atoi(service));

    /* limitation: the node must be in numbers-and-dots notation */
    if (!node && (ai_flags & AI_PASSIVE))
    {
        ((struct sockaddr_in *)p->ai_addr)->sin_addr.s_addr = INADDR_ANY;
    }
    else
    {
        ((struct sockaddr_in *)p->ai_addr)->sin_addr.s_addr = inet_addr(node);
    }

    *res = p;

    return 0;
}

void freeaddrinfo(struct addrinfo *res)
{
    free(res->ai_addr);
    free(res);
}
#endif /* !HAVE_GETADDRINFO */

#if !defined(HAVE_DECL_GAI_STRERROR) && !defined(gai_strerror)
const char *gai_strerror(int errcode)
{
    return strerror(errcode);
}
#endif /* !HAVE_DECL_GAI_STRERROR */