Contents of /trunk/mkinitrd-magellan/busybox/libbb/xconnect.c
Parent Directory | Revision Log
Revision 532 -
(show annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (17 years ago) by niro
File MIME type: text/plain
File size: 6697 byte(s)
Sat Sep 1 22:45:15 2007 UTC (17 years ago) by niro
File MIME type: text/plain
File size: 6697 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
1 | /* vi: set sw=4 ts=4: */ |
2 | /* |
3 | * Utility routines. |
4 | * |
5 | * Connect to host at port using address resolution from getaddrinfo |
6 | * |
7 | */ |
8 | |
9 | #include <netinet/in.h> |
10 | #include "libbb.h" |
11 | |
12 | static const int one = 1; |
13 | int setsockopt_reuseaddr(int fd) |
14 | { |
15 | return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); |
16 | } |
17 | int setsockopt_broadcast(int fd) |
18 | { |
19 | return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one)); |
20 | } |
21 | |
22 | void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen) |
23 | { |
24 | if (connect(s, s_addr, addrlen) < 0) { |
25 | if (ENABLE_FEATURE_CLEAN_UP) |
26 | close(s); |
27 | if (s_addr->sa_family == AF_INET) |
28 | bb_perror_msg_and_die("%s (%s)", |
29 | "cannot connect to remote host", |
30 | inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr)); |
31 | bb_perror_msg_and_die("cannot connect to remote host"); |
32 | } |
33 | } |
34 | |
35 | /* Return port number for a service. |
36 | * If "port" is a number use it as the port. |
37 | * If "port" is a name it is looked up in /etc/services, if it isnt found return |
38 | * default_port */ |
39 | unsigned bb_lookup_port(const char *port, const char *protocol, unsigned default_port) |
40 | { |
41 | unsigned port_nr = default_port; |
42 | if (port) { |
43 | int old_errno; |
44 | |
45 | /* Since this is a lib function, we're not allowed to reset errno to 0. |
46 | * Doing so could break an app that is deferring checking of errno. */ |
47 | old_errno = errno; |
48 | port_nr = bb_strtou(port, NULL, 10); |
49 | if (errno || port_nr > 65535) { |
50 | struct servent *tserv = getservbyname(port, protocol); |
51 | port_nr = default_port; |
52 | if (tserv) |
53 | port_nr = ntohs(tserv->s_port); |
54 | } |
55 | errno = old_errno; |
56 | } |
57 | return (uint16_t)port_nr; |
58 | } |
59 | |
60 | |
61 | /* "Old" networking API - only IPv4 */ |
62 | |
63 | |
64 | void bb_lookup_host(struct sockaddr_in *s_in, const char *host) |
65 | { |
66 | struct hostent *he; |
67 | |
68 | memset(s_in, 0, sizeof(struct sockaddr_in)); |
69 | s_in->sin_family = AF_INET; |
70 | he = xgethostbyname(host); |
71 | memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length); |
72 | } |
73 | |
74 | int xconnect_tcp_v4(struct sockaddr_in *s_addr) |
75 | { |
76 | int s = xsocket(AF_INET, SOCK_STREAM, 0); |
77 | xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr)); |
78 | return s; |
79 | } |
80 | |
81 | |
82 | /* "New" networking API */ |
83 | |
84 | |
85 | int get_nport(len_and_sockaddr *lsa) |
86 | { |
87 | #if ENABLE_FEATURE_IPV6 |
88 | if (lsa->sa.sa_family == AF_INET6) { |
89 | return lsa->sin6.sin6_port; |
90 | } |
91 | #endif |
92 | if (lsa->sa.sa_family == AF_INET) { |
93 | return lsa->sin.sin_port; |
94 | } |
95 | return -1; |
96 | /* What? UNIX socket? IPX?? :) */ |
97 | } |
98 | |
99 | void set_nport(len_and_sockaddr *lsa, unsigned port) |
100 | { |
101 | #if ENABLE_FEATURE_IPV6 |
102 | if (lsa->sa.sa_family == AF_INET6) { |
103 | lsa->sin6.sin6_port = port; |
104 | return; |
105 | } |
106 | #endif |
107 | if (lsa->sa.sa_family == AF_INET) { |
108 | lsa->sin.sin_port = port; |
109 | return; |
110 | } |
111 | /* What? UNIX socket? IPX?? :) */ |
112 | } |
113 | |
114 | /* peer: "1.2.3.4[:port]", "www.google.com[:port]" |
115 | * port: if neither of above specifies port # |
116 | */ |
117 | static len_and_sockaddr* str2sockaddr(const char *host, int port, int ai_flags) |
118 | { |
119 | int rc; |
120 | len_and_sockaddr *r; // = NULL; |
121 | struct addrinfo *result = NULL; |
122 | const char *org_host = host; /* only for error msg */ |
123 | const char *cp; |
124 | struct addrinfo hint; |
125 | |
126 | /* Ugly parsing of host:addr */ |
127 | if (ENABLE_FEATURE_IPV6 && host[0] == '[') { |
128 | host++; |
129 | cp = strchr(host, ']'); |
130 | if (!cp || cp[1] != ':') /* Malformed: must have [xx]:nn */ |
131 | bb_error_msg_and_die("bad address '%s'", org_host); |
132 | //return r; /* return NULL */ |
133 | } else { |
134 | cp = strrchr(host, ':'); |
135 | if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) { |
136 | /* There is more than one ':' (e.g. "::1") */ |
137 | cp = NULL; /* it's not a port spec */ |
138 | } |
139 | } |
140 | if (cp) { |
141 | int sz = cp - host + 1; |
142 | host = safe_strncpy(alloca(sz), host, sz); |
143 | if (ENABLE_FEATURE_IPV6 && *cp != ':') |
144 | cp++; /* skip ']' */ |
145 | cp++; /* skip ':' */ |
146 | port = xatou16(cp); |
147 | } |
148 | |
149 | memset(&hint, 0 , sizeof(hint)); |
150 | /* hint.ai_family = AF_UNSPEC; - zero anyway */ |
151 | #if !ENABLE_FEATURE_IPV6 |
152 | hint.ai_family = AF_INET; /* do not try to find IPv6 */ |
153 | #endif |
154 | /* Needed. Or else we will get each address thrice (or more) |
155 | * for each possible socket type (tcp,udp,raw...): */ |
156 | hint.ai_socktype = SOCK_STREAM; |
157 | hint.ai_flags = ai_flags; |
158 | rc = getaddrinfo(host, NULL, &hint, &result); |
159 | if (rc || !result) |
160 | bb_error_msg_and_die("bad address '%s'", org_host); |
161 | r = xmalloc(offsetof(len_and_sockaddr, sa) + result->ai_addrlen); |
162 | r->len = result->ai_addrlen; |
163 | memcpy(&r->sa, result->ai_addr, result->ai_addrlen); |
164 | set_nport(r, htons(port)); |
165 | freeaddrinfo(result); |
166 | return r; |
167 | } |
168 | |
169 | len_and_sockaddr* host2sockaddr(const char *host, int port) |
170 | { |
171 | return str2sockaddr(host, port, 0); |
172 | } |
173 | |
174 | static len_and_sockaddr* dotted2sockaddr(const char *host, int port) |
175 | { |
176 | return str2sockaddr(host, port, NI_NUMERICHOST); |
177 | } |
178 | |
179 | int xsocket_stream(len_and_sockaddr **lsap) |
180 | { |
181 | len_and_sockaddr *lsa; |
182 | int fd; |
183 | int len = sizeof(struct sockaddr_in); |
184 | int family = AF_INET; |
185 | |
186 | #if ENABLE_FEATURE_IPV6 |
187 | fd = socket(AF_INET6, SOCK_STREAM, 0); |
188 | if (fd >= 0) { |
189 | len = sizeof(struct sockaddr_in6); |
190 | family = AF_INET6; |
191 | } else |
192 | #endif |
193 | { |
194 | fd = xsocket(AF_INET, SOCK_STREAM, 0); |
195 | } |
196 | lsa = xzalloc(offsetof(len_and_sockaddr, sa) + len); |
197 | lsa->len = len; |
198 | lsa->sa.sa_family = family; |
199 | *lsap = lsa; |
200 | return fd; |
201 | } |
202 | |
203 | int create_and_bind_stream_or_die(const char *bindaddr, int port) |
204 | { |
205 | int fd; |
206 | len_and_sockaddr *lsa; |
207 | |
208 | if (bindaddr && bindaddr[0]) { |
209 | lsa = dotted2sockaddr(bindaddr, port); |
210 | /* currently NULL check is in str2sockaddr */ |
211 | //if (!lsa) |
212 | // bb_error_msg_and_die("bad address '%s'", bindaddr); |
213 | /* user specified bind addr dictates family */ |
214 | fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0); |
215 | } else { |
216 | fd = xsocket_stream(&lsa); |
217 | set_nport(lsa, htons(port)); |
218 | } |
219 | setsockopt_reuseaddr(fd); |
220 | xbind(fd, &lsa->sa, lsa->len); |
221 | free(lsa); |
222 | return fd; |
223 | } |
224 | |
225 | int create_and_connect_stream_or_die(const char *peer, int port) |
226 | { |
227 | int fd; |
228 | len_and_sockaddr *lsa; |
229 | |
230 | lsa = host2sockaddr(peer, port); |
231 | /* currently NULL check is in str2sockaddr */ |
232 | //if (!lsa) |
233 | // bb_error_msg_and_die("bad address '%s'", peer); |
234 | fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0); |
235 | setsockopt_reuseaddr(fd); |
236 | xconnect(fd, &lsa->sa, lsa->len); |
237 | free(lsa); |
238 | return fd; |
239 | } |
240 | |
241 | int xconnect_stream(const len_and_sockaddr *lsa) |
242 | { |
243 | int fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0); |
244 | xconnect(fd, &lsa->sa, lsa->len); |
245 | return fd; |
246 | } |
247 | |
248 | static char* sockaddr2str(const struct sockaddr *sa, socklen_t salen, int flags) |
249 | { |
250 | char host[128]; |
251 | char serv[16]; |
252 | int rc = getnameinfo(sa, salen, |
253 | host, sizeof(host), |
254 | serv, sizeof(serv), |
255 | flags | NI_NUMERICSERV /* do not resolve port# */ |
256 | ); |
257 | if (rc) return NULL; |
258 | // We probably need to use [%s]:%s for IPv6... |
259 | return xasprintf("%s:%s", host, serv); |
260 | } |
261 | |
262 | char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen) |
263 | { |
264 | return sockaddr2str(sa, salen, 0); |
265 | } |
266 | |
267 | char* xmalloc_sockaddr2dotted(const struct sockaddr *sa, socklen_t salen) |
268 | { |
269 | return sockaddr2str(sa, salen, NI_NUMERICHOST); |
270 | } |