summaryrefslogtreecommitdiff
path: root/src/netutil.c
blob: 4a7b452fa38c85f3c98630aa876c412397933047 (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
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
/* -*- c-file-style: "java"; indent-tabs-mode: nil; tab-width: 4; fill-column: 78 -*-
 *
 * distcc -- A simple distributed compiler system
 *
 * Copyright (C) 2002, 2003, 2004 by Martin Pool <mbp@samba.org>
 *
 * 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, 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 <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <sys/param.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <netinet/tcp.h>

#ifdef HAVE_ARPA_NAMESER_H
#  include <arpa/nameser.h>
#endif

#include <arpa/inet.h>

#ifdef HAVE_RESOLV_H
#  include <resolv.h>
#endif

#include <netdb.h>

#include "types.h"
#include "exitcode.h"
#include "distcc.h"
#include "trace.h"
#include "util.h"
#include "srvnet.h"
#include "access.h"
#include "netutil.h"
#include "snprintf.h"


/* work out what fcntl flag to use for non-blocking */
#ifdef O_NONBLOCK
# define NONBLOCK_FLAG O_NONBLOCK
#elif defined(SYSV)
# define NONBLOCK_FLAG O_NDELAY
#else
# define NONBLOCK_FLAG FNDELAY
#endif

#ifndef AF_UNIX
#  define AF_UNIX AF_LOCAL
#endif


#ifndef HAVE_HSTRERROR
/* Missing on e.g. Solaris 2.6 */
const char *hstrerror(int err) {
    switch (err) {
    case HOST_NOT_FOUND:
        return "Host not found";
    case TRY_AGAIN:
        return "Name server not contacted";
    case NO_RECOVERY:
        return "Non-recoverable error";
    case NO_ADDRESS:
        return "No IP address for host";
    default:
        return "Unknown error";
    }
}
#endif


/**
 * Set a fd into blocking mode
 **/
void dcc_set_blocking(int fd)
{
    int val;

    if ((val = fcntl(fd, F_GETFL, 0)) == -1)
        return;
    if (val & NONBLOCK_FLAG) {
        val &= ~NONBLOCK_FLAG;
        fcntl(fd, F_SETFL, val);
    }
}


/**
 * Set a fd into nonblocking mode
 **/
void dcc_set_nonblocking(int fd)
{
    int val;

    if ((val = fcntl(fd, F_GETFL, 0)) == -1)
        return;
    if (!(val & NONBLOCK_FLAG)) {
        val |= NONBLOCK_FLAG;
        fcntl(fd, F_SETFL, val);
    }
}


/* Ask for the server not to be awakened until some data has arrived
 * on the socket.  This works for our protocol because the client
 * sends a request immediately after connection without waiting for
 * anything from the server. */
void dcc_defer_accept(int POSSIBLY_UNUSED(listen_fd))
{
#ifdef TCP_DEFER_ACCEPT
    int val = 1;

    if (!dcc_getenv_bool("DISTCC_TCP_DEFER_ACCEPT", 1)) {
        rs_trace("TCP_DEFER_ACCEPT disabled");
        return;
    }

    if (setsockopt(listen_fd, SOL_TCP, TCP_DEFER_ACCEPT, &val, sizeof val) == -1) {
        rs_log_warning("failed to set TCP_DEFER_ACCEPT: %s", strerror(errno));
    } else {
        rs_trace("TCP_DEFER_ACCEPT turned on");
    }
#endif
}



#ifdef ENABLE_RFC2553
/* TODO: Make the returned strings consistent with the other
 * implementation. */
int dcc_sockaddr_to_string(struct sockaddr *sa,
                           size_t salen,
                           char **p_buf)
{
    int err;
    char host[1024];
    char port[32];

    if (!sa) {
        *p_buf = strdup("NOTSOCKET");
        return 0;
    } else if (sa->sa_family == AF_INET || sa->sa_family == AF_INET6) {
        err = getnameinfo(sa, salen,
                          host, sizeof host,
                          port, sizeof port,
                          NI_NUMERICHOST | NI_NUMERICSERV);
        if (err) {
            rs_log_warning("getnameinfo failed: %s", gai_strerror(err));
            *p_buf = strdup("(UNKNOWN)");
            return 0;               /* it's still a valid string */
        }

        checked_asprintf(p_buf, "%s:%s", host, port);
    } else if (sa->sa_family == AF_UNIX) {
        /* NB: The word 'sun' is predefined on Solaris */
        struct sockaddr_un *sa_un = (struct sockaddr_un *) sa;
        checked_asprintf(p_buf, "UNIX-DOMAIN %s", sa_un->sun_path);
    } else {
        checked_asprintf(p_buf, "UNKNOWN-FAMILY %d", sa->sa_family);
    }

    return 0;
}
#else /* ndef ENABLE_RFC2553 */
int dcc_sockaddr_to_string(struct sockaddr *sa,
                           size_t UNUSED(salen),
                           char **p_buf)
{
    if (!sa) {
        *p_buf = strdup("NOTSOCKET");
        return 0;
    } else if (sa->sa_family == AF_INET) {
        /* The double-cast here suppresses warnings from -Wcast-align. */
        struct sockaddr_in *sain = (struct sockaddr_in *) (void *) sa;

        checked_asprintf(p_buf, "%s:%d", inet_ntoa(sain->sin_addr),
                 ntohs(sain->sin_port));
    } else if (sa->sa_family == AF_UNIX) {
        /* NB: The word 'sun' is predefined on Solaris */
        struct sockaddr_un *sa_un = (struct sockaddr_un *) sa;
        checked_asprintf(p_buf, "UNIX-DOMAIN %s", sa_un->sun_path);
    } else {
        checked_asprintf(p_buf, "UNKNOWN-FAMILY %d", sa->sa_family);
    }

    return 0;
}
#endif /* ndef ENABLE_RFC2553 */