diff options
Diffstat (limited to 'lib/urlconn.c')
-rw-r--r-- | lib/urlconn.c | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/lib/urlconn.c b/lib/urlconn.c new file mode 100644 index 0000000..5994626 --- /dev/null +++ b/lib/urlconn.c | |||
@@ -0,0 +1,157 @@ | |||
1 | /* This file is part of GNU Pies. | ||
2 | Copyright (C) 2019 Sergey Poznyakoff | ||
3 | |||
4 | GNU Pies is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 3, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | GNU Pies is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with GNU Pies. If not, see <http://www.gnu.org/licenses/>. */ | ||
16 | |||
17 | #include <config.h> | ||
18 | #include <sys/types.h> | ||
19 | #include <sys/socket.h> | ||
20 | #include <sys/un.h> | ||
21 | #include <sys/stat.h> | ||
22 | #include <netinet/in.h> | ||
23 | #include <netdb.h> | ||
24 | #include <errno.h> | ||
25 | #include <unistd.h> | ||
26 | #include <fcntl.h> | ||
27 | #include <libpies.h> | ||
28 | #include <grecs.h> | ||
29 | |||
30 | int | ||
31 | url_connect (struct pies_url *url, struct grecs_sockaddr *source_addr) | ||
32 | { | ||
33 | int fd; | ||
34 | union pies_sockaddr_storage addr; | ||
35 | socklen_t socklen; | ||
36 | int flags; | ||
37 | |||
38 | if (strcmp (url->scheme, "unix") == 0 | ||
39 | || strcmp (url->scheme, "file") == 0 | ||
40 | || strcmp (url->scheme, "socket") == 0) | ||
41 | { | ||
42 | struct stat st; | ||
43 | |||
44 | if (url->port) | ||
45 | { | ||
46 | grecs_error (NULL, 0, _("%s: invalid connection type: " | ||
47 | "port is meaningless for UNIX sockets"), | ||
48 | url->string); | ||
49 | errno = EINVAL; | ||
50 | return -1; | ||
51 | } | ||
52 | |||
53 | if (strlen (url->path) > sizeof addr.s_un.sun_path) | ||
54 | { | ||
55 | grecs_error (NULL, 0, | ||
56 | _("%s: UNIX socket name too long"), | ||
57 | url->path); | ||
58 | errno = EINVAL; | ||
59 | return -1; | ||
60 | } | ||
61 | |||
62 | addr.s.sa_family = PF_UNIX; | ||
63 | socklen = sizeof (addr.s_un); | ||
64 | strcpy (addr.s_un.sun_path, url->path); | ||
65 | |||
66 | if (stat (url->path, &st)) | ||
67 | { | ||
68 | if (errno != ENOENT) | ||
69 | { | ||
70 | grecs_error (NULL, errno, _("%s: %s failed"), url->path, "stat"); | ||
71 | return -1; | ||
72 | } | ||
73 | } | ||
74 | else | ||
75 | { | ||
76 | if (!S_ISSOCK (st.st_mode)) | ||
77 | { | ||
78 | grecs_error (NULL, 0, _("%s: not a socket"), url->path); | ||
79 | errno = EINVAL; | ||
80 | return -1; | ||
81 | } | ||
82 | } | ||
83 | } | ||
84 | else if (strcmp (url->scheme, "inet") == 0) | ||
85 | { | ||
86 | struct addrinfo hints; | ||
87 | struct addrinfo *res; | ||
88 | int rc; | ||
89 | |||
90 | memset (&hints, 0, sizeof (hints)); | ||
91 | hints.ai_socktype = SOCK_STREAM; | ||
92 | hints.ai_flags = 0; | ||
93 | hints.ai_family = AF_INET; | ||
94 | |||
95 | rc = getaddrinfo (url->host, url->port_s, &hints, &res); | ||
96 | switch (rc) | ||
97 | { | ||
98 | case 0: | ||
99 | break; | ||
100 | |||
101 | case EAI_SYSTEM: | ||
102 | grecs_error (NULL, errno, "%s", _("cannot parse address")); | ||
103 | return -1; | ||
104 | |||
105 | case EAI_BADFLAGS: | ||
106 | case EAI_SOCKTYPE: | ||
107 | grecs_error (NULL, 0, _("%s:%d: internal error converting address"), | ||
108 | __FILE__,__LINE__); | ||
109 | return -1; | ||
110 | |||
111 | case EAI_MEMORY: | ||
112 | grecs_alloc_die (); | ||
113 | |||
114 | default: | ||
115 | grecs_error (NULL, 0, "getaddrinfo: %s", gai_strerror(rc)); | ||
116 | return -1; | ||
117 | } | ||
118 | |||
119 | socklen = sizeof (addr.s_in); | ||
120 | memcpy (&addr.s_in, res->ai_addr, socklen); | ||
121 | freeaddrinfo (res); | ||
122 | } | ||
123 | |||
124 | fd = socket (addr.s.sa_family, SOCK_STREAM, 0); | ||
125 | if (fd == -1) | ||
126 | { | ||
127 | grecs_error (NULL, errno, _("%s: %s failed"), url->string, "socket"); | ||
128 | return -1; | ||
129 | } | ||
130 | |||
131 | if ((flags = fcntl (fd, F_GETFD, 0)) == -1 | ||
132 | || fcntl (fd, F_SETFD, flags | FD_CLOEXEC) == -1) | ||
133 | grecs_error (NULL, 0, _("%s: cannot set close-on-exec: %s"), | ||
134 | url->string, strerror (errno)); | ||
135 | |||
136 | if (source_addr) | ||
137 | { | ||
138 | if (source_addr->sa->sa_family != addr.s.sa_family) | ||
139 | grecs_error (NULL, 0, | ||
140 | _("source and destination address family differ")); | ||
141 | else if (bind (fd, source_addr->sa, source_addr->len) < 0) | ||
142 | { | ||
143 | grecs_error (NULL, errno, _("%s: %s failed"), url->string, "bind"); | ||
144 | close (fd); | ||
145 | return -1; | ||
146 | } | ||
147 | } | ||
148 | |||
149 | if (connect (fd, &addr.s, socklen)) | ||
150 | { | ||
151 | grecs_error (NULL, errno, _("%s: %s failed"), url->string, "connect"); | ||
152 | close (fd); | ||
153 | return -1; | ||
154 | } | ||
155 | |||
156 | return fd; | ||
157 | } | ||