summaryrefslogtreecommitdiff
path: root/lib/tcpconnect.cc
blob: d4cf9a9216ab37da490db30c8b725277683ae4e9 (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
// nullmailer -- a simple relay-only MTA
// Copyright (C) 2017  Bruce Guenter <bruce@untroubled.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// You can contact me at <bruce@untroubled.org>.  There is also a mailing list
// available to discuss this package.  To subscribe, send an email to
// <nullmailer-subscribe@lists.untroubled.org>.

#include "config.h"
#include <errno.h>
#include <netdb.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include "errcodes.h"
#include "itoa.h"
#include "connect.h"

static int err_return(int errn, int dflt)
{
  if (errn == HOST_NOT_FOUND)
    return -ERR_HOST_NOT_FOUND;
  if (errn == NO_ADDRESS)
    return -ERR_NO_ADDRESS;
  if (errn == NO_RECOVERY || errn == EAI_FAIL)
    return -ERR_GHBN_FATAL;
  if (errn == TRY_AGAIN || errn == EAI_AGAIN)
    return -ERR_GHBN_TEMP;
  if (errn == EAI_NONAME)
    return -ERR_HOST_NOT_FOUND;
  if (errn == ECONNREFUSED)
    return -ERR_CONN_REFUSED;
  if (errn == ETIMEDOUT)
    return -ERR_CONN_TIMEDOUT;
  if (errn == ENETUNREACH)
    return -ERR_CONN_UNREACHABLE;
  return -dflt;
}

#ifdef HAVE_GETADDRINFO

static int getaddr(const char* hostname, int port, struct addrinfo** result)
{
  const char *service = itoa(port, 6);
  struct addrinfo req;
  memset(&req, 0, sizeof(req));
  req.ai_flags = AI_NUMERICSERV;
  req.ai_socktype = SOCK_STREAM;
  int e = getaddrinfo(hostname, service, &req, result);
  return e ? err_return(e, ERR_GHBN_TEMP) : 0;
}

static bool canbind(int family, const struct addrinfo* ai)
{
  for (; ai; ai = ai->ai_next)
    if (ai->ai_family == family)
      return true;
  return false;
}

static bool bindit(int fd, int family, const struct addrinfo* ai)
{
  for (; ai; ai = ai->ai_next)
    if (ai->ai_family == family)
      if (bind(fd, ai->ai_addr, ai->ai_addrlen) == 0)
        return true;
  return false;
}

int tcpconnect(const char* hostname, int port, const char* source)
{
  struct addrinfo* res;
  int err = getaddr(hostname, port, &res);
  if (err)
    return err;
  struct addrinfo* source_addr = NULL;
  if (source) {
    err = getaddr(source, 0, &source_addr);
    if (err)
      return err;
  }
  int s = -1;
  err = ERR_CONN_FAILED;
  struct addrinfo* orig_res = res;

  if (source_addr)
    // Check if some address is the same family as the source
    for (; res != NULL; res = res->ai_next)
      if (canbind(res->ai_family, source_addr))
        break;
  if (res == NULL)
    return -ERR_BIND_FAILED;

  for (; res != NULL; res = res->ai_next) {
    if (!source_addr || canbind(res->ai_family, source_addr)) {
      s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
      if(s > 0) {
        if(source_addr && !bindit(s, res->ai_family, source_addr)) {
          close(s);
          err = ERR_BIND_FAILED;
          s = -1;
          break;
        }
        if(connect(s, res->ai_addr, res->ai_addrlen) == 0)
          break;
        close(s);
        s = -1;
      }
    }
  }

  freeaddrinfo(orig_res);
  if (source_addr)
    freeaddrinfo(source_addr);

  if(s < 0)
    return err_return(errno, err);
  return s;
}

#else

static int sethostbyname(const char* hostname, struct sockaddr_in& sa)
{
  struct hostent *he = gethostbyname(hostname);
  if(!he)
    return err_return(h_errno, ERR_GHBN_TEMP);
  memcpy(&sa.sin_addr, he->h_addr, he->h_length);
  return 0;
}

int tcpconnect(const char* hostname, int port, const char* source)
{
  struct sockaddr_in sa;
  memset(&sa, 0, sizeof(sa));
  int e = sethostbyname(hostname, sa);
  if(e) return e;
  struct sockaddr_in source_sa;
  memset(&source_sa, 0, sizeof source_sa);
  if(source) {
    e = sethostbyname(source, source_sa);
    if(e) return e;
  }
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  int s = socket(PF_INET, SOCK_STREAM, 0);
  if(s == -1)
    return -ERR_SOCKET;
  if(source && bind(s, (sockaddr*)&source_sa, sizeof source_sa) != 0) {
    close(s);
    return err_return(errno, ERR_BIND_FAILED);
  }
  if(connect(s, (sockaddr*)&sa, sizeof(sa)) != 0) {
    close(s);
    return err_return(errno, ERR_CONN_FAILED);
  }
  return s;
}

#endif