aboutsummaryrefslogtreecommitdiff
path: root/lib/urlconn.c
blob: acf42d0b0981d8a7c414fd6e11e0f5a9b593a103 (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
/* This file is part of GNU Pies.
   Copyright (C) 2019 Sergey Poznyakoff

   GNU Pies 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 3, or (at your option)
   any later version.

   GNU Pies 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 GNU Pies.  If not, see <http://www.gnu.org/licenses/>. */

#include <config.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <libpies.h>
#include <grecs.h>

int
url_connect (struct pies_url *url, struct grecs_sockaddr *source_addr)
{
  int fd;
  union pies_sockaddr_storage addr;
  socklen_t socklen;
  int flags;
  
  if (strcmp (url->scheme, "unix") == 0
      || strcmp (url->scheme, "file") == 0
      || strcmp (url->scheme, "socket") == 0)
    {
      struct stat st;
		
      if (url->port)
	{
	  grecs_error (NULL, 0, _("%s: invalid connection type: "
				  "port is meaningless for UNIX sockets"),
		       url->string);
	  errno = EINVAL;
	  return -1;
	}
		
      if (strlen (url->path) > sizeof addr.s_un.sun_path)
	{
	  grecs_error (NULL, 0,
		       _("%s: UNIX socket name too long"),
		       url->path);
	  errno = EINVAL;
	  return -1;
	}
		
      addr.s.sa_family = PF_UNIX;
      socklen = sizeof (addr.s_un);
      strcpy (addr.s_un.sun_path, url->path);
		
      if (stat (url->path, &st))
	{
	  if (errno != ENOENT)
	    {
	      grecs_error (NULL, errno, _("%s: %s failed"), url->path, "stat");
	      return -1;
	    }
	}
      else
	{
	  if (!S_ISSOCK (st.st_mode))
	    {
	      grecs_error (NULL, 0, _("%s: not a socket"), url->path);
	      errno = EINVAL;
	      return -1;
	    }
	}
    }
  else if (strcmp (url->scheme, "inet") == 0)
    {
      struct addrinfo hints;
      struct addrinfo *res;
      int rc;
      
      memset (&hints, 0, sizeof (hints));
      hints.ai_socktype = SOCK_STREAM;
      hints.ai_flags = 0;
      hints.ai_family = AF_INET;

      rc = getaddrinfo (url->host, url->port_s, &hints, &res);
      switch (rc)
	{
	case 0:
	  break;
	  
	case EAI_SYSTEM:
	  grecs_error (NULL, errno, "%s", _("cannot parse address"));
	  return -1;
	  
	case EAI_BADFLAGS:
	case EAI_SOCKTYPE:
	  grecs_error (NULL, 0,
		       _("%s:%d: internal error while converting network address"),
		       __FILE__,__LINE__);
	  return -1;
	  
	case EAI_MEMORY:
	  grecs_alloc_die ();
	  
	default:
	  grecs_error (NULL, 0, "getaddrinfo: %s", gai_strerror(rc));
	  return -1;
	}

      socklen = sizeof (addr.s_in);
      memcpy (&addr.s_in, res->ai_addr, socklen);
      freeaddrinfo (res);
    }
      
  fd = socket (addr.s.sa_family, SOCK_STREAM, 0);
  if (fd == -1)
    {
      grecs_error (NULL, errno, _("%s: %s failed"), url->string, "socket");
      return -1;
    }
  
  if ((flags = fcntl (fd, F_GETFD, 0)) == -1
      || fcntl (fd, F_SETFD, flags | FD_CLOEXEC) == -1)
    grecs_error (NULL, 0,  _("%s: cannot set close-on-exec: %s"),
		 url->string, strerror (errno));
  
  if (source_addr)
    {
      if (source_addr->sa->sa_family != addr.s.sa_family)
	grecs_error (NULL, 0,
		     _("source and destination address family differ"));
      else if (bind (fd, source_addr->sa, source_addr->len) < 0)
	{
	  grecs_error (NULL, errno, _("%s: %s failed"), url->string, "bind");
	  close (fd);
	  return -1;
	}
    }
  
  if (connect (fd, &addr.s, socklen))
    {
      grecs_error (NULL, errno, _("%s: %s failed"), url->string, "connect");
      close (fd);
      return -1;
    }

  return fd;
}

Return to:

Send suggestions and report system problems to the System administrator.