source: MondoRescue/branches/stable/mindi-busybox/libbb/xconnect.c@ 1770

Last change on this file since 1770 was 1770, checked in by Bruno Cornec, 16 years ago
  • Better output for mindi-busybox revision
  • Remove dummy file created on NFS - report from Arnaud Tiger <arnaud.tiger_at_hp.com>
  • strace useful for debug
  • fix new versions for pb (2.0.0 for mindi and 1.7.2 for mindi-busybox)
  • fix build process for mindi-busybox + options used in that version (dd for label-partitions-as-necessary)
  • fix typo in label-partitions-as-necessary which doesn't seem to work
  • Update to busybox 1.7.2
  • perl is now required at restore time to support uuid swap partitions (and will be used for many other thigs

in the future for sure)

  • next mindi version will be 2.0.0 due to all the changes made in it (udev may break working distros)
  • small optimization in mindi on keyboard handling (one single find instead of multiple)
  • better interaction for USB device when launching mindi manually
  • attempt to automatically guess block disk size for ramdisk
  • fix typos in bkphw
  • Fix the remaining problem with UUID support for swap partitions
  • Updates mondoarchive man page for USB support
  • Adds preliminary Hardware support to mindi (Proliant SSSTK)
  • Tries to add udev support also for rhel4
  • Fix UUID support which was still broken.
  • Be conservative in test for the start-nfs script
  • Update config file for mindi-busybox for 1.7.2 migration
  • Try to run around a busybox bug (1.2.2 pb on inexistant links)
  • Add build content for mindi-busybox in pb
  • Remove distributions content for mindi-busybox
  • Fix a warning on inexistant raidtab
  • Solve problem on tmpfs in restore init (Problem of inexistant symlink and busybox)
  • Create MONDO_CACHE and use it everywhere + creation at start
  • Really never try to eject a USB device
  • Fix a issue with &> usage (replaced with 1> and 2>)
  • Adds magic file to depllist in order to have file working + ldd which helps for debugging issues
  • tty modes correct to avoid sh error messages
  • Use ext3 normally and not ext2 instead
  • USB device should be corrected after reading (take 1st part)
  • Adds a mount_USB_here function derived from mount_CDROM_here
  • usb detection place before /dev detection in device name at restore time
  • Fix when restoring from USB: media is asked in interactive mode
  • Adds USB support for mondorestore
  • mount_cdrom => mount_media
  • elilo.efi is now searched throughout /boot/efi and not in a fixed place as there is no standard
  • untar-and-softlink => untar (+ interface change)
  • suppress useless softlinks creation/removal in boot process
  • avoids udevd messages on groups
  • Increase # of disks to 99 as in mindi at restore time (should be a conf file parameter)
  • skip existing big file creation
  • seems to work correctly for USB mindi boot
  • Adds group and tty link to udev conf
  • Always load usb-torage (even 2.6) to initiate USB bus discovery
  • Better printing of messages
  • Attempt to fix a bug in supporting OpenSusE 10.3 kernel for initramfs (mindi may now use multiple regex for kernel initrd detection)
  • Links were not correctly done as non relative for modules in mindi
  • exclusion of modules denied now works
  • Also create modules in their ordinary place, so that classical modprobe works + copy modules.dep
  • Fix bugs for DENY_MODS handling
  • Add device /dev/console for udev
  • ide-generic should now really be excluded
  • Fix a bug in major number for tty
  • If udev then adds modprobe/insmod to rootfs
  • tty0 is also cretaed with udev
  • ide-generic put rather in DENY_MODS
  • udevd remove from deplist s handled in mindi directly
  • better default for mindi when using --usb
  • Handles dynamically linked busybox (in case we want to use it soon ;-)
  • Adds fixed devices to create for udev
  • ide-generic should not be part of the initrd when using libata v2
  • support a dynamically linked udev (case on Ubuntu 7.10 and Mandriva 2008.0 so should be quite generic) This will give incitation to move to dyn. linked binaries in the initrd which will help for other tasks (ia6 4)
  • Improvement in udev support (do not use cl options not available in busybox)
  • Udev in mindi
    • auto creation of the right links at boot time with udev-links.conf(from Mandriva 2008.0)
    • rework startup of udev as current makes kernel crash (from Mandriva 2008.0)
    • add support for 64 bits udev
  • Try to render MyInsmod silent at boot time
  • Adds udev support (mandatory for newest distributions to avoid remapping of devices in a different way as on the original system)
  • We also need vaft format support for USB boot
  • Adds libusual support (Ubuntu 7.10 needs it for USB)
  • Improve Ubuntu/Debian keyboard detection and support
  • pbinit adapted to new pb (0.8.10). Filtering of docs done in it
  • Suppress some mondo warnings and errors on USB again
  • Tries to fix lack of files in deb mindi package
  • Verify should now work for USB devices
  • More log/mesages improvement for USB support
  • - Supress g_erase_tmpdir_and_scratchdir
  • Improve some log messages for USB support
  • Try to improve install in mindi to avoid issues with isolinux.cfg not installed vene if in the pkg :-(
  • Improve mindi-busybox build
  • In conformity with pb 0.8.9
  • Add support for Ubuntu 7.10 in build process
  • Add USB Key button to Menu UI (CD streamer removed)
  • Attempt to fix error messages on tmp/scratch files at the end by removing those dir at the latest possible.
  • Fix a bug linked to the size of the -E param which could be used (Arnaud Tiger/René Ribaud).
  • Integrate ~/.pbrc content into mondorescue.pb (required project-builder >= 0.8.7)
  • Put mondorescue in conformity with new pb filtering rules
  • Add USB support at restore time (no test done yet). New start-usb script PB varibale added where useful
  • Unmounting USB device before removal of temporary scratchdir
  • Stil refining USB copy back to mondo (one command was not executed)
  • No need to have the image subdor in the csratchdir when USB.
  • umount the USB partition before attempting to use it
  • Remove useless copy from mindi to mondo at end of USB handling

(risky merge, we are raising the limits of 2 diverging branches. The status of stable is not completely sure as such. Will need lots of tests, but it's not yet done :-()
(merge -r1692:1769 $SVN_M/branches/2.2.5)

File size: 8.9 KB
RevLine 
[821]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
[1770]12void setsockopt_reuseaddr(int fd)
13{
14 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &const_int_1, sizeof(const_int_1));
15}
16int setsockopt_broadcast(int fd)
17{
18 return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1));
19}
20
21void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
22{
23 if (connect(s, s_addr, addrlen) < 0) {
24 if (ENABLE_FEATURE_CLEAN_UP)
25 close(s);
26 if (s_addr->sa_family == AF_INET)
27 bb_perror_msg_and_die("%s (%s)",
28 "cannot connect to remote host",
29 inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
30 bb_perror_msg_and_die("cannot connect to remote host");
31 }
32}
33
34/* Return port number for a service.
[821]35 * If "port" is a number use it as the port.
36 * If "port" is a name it is looked up in /etc/services, if it isnt found return
[1770]37 * default_port */
38unsigned bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
[821]39{
[1770]40 unsigned port_nr = default_port;
[821]41 if (port) {
42 int old_errno;
43
44 /* Since this is a lib function, we're not allowed to reset errno to 0.
45 * Doing so could break an app that is deferring checking of errno. */
46 old_errno = errno;
[1770]47 port_nr = bb_strtou(port, NULL, 10);
48 if (errno || port_nr > 65535) {
[821]49 struct servent *tserv = getservbyname(port, protocol);
[1770]50 port_nr = default_port;
51 if (tserv)
52 port_nr = ntohs(tserv->s_port);
[821]53 }
54 errno = old_errno;
55 }
[1770]56 return (uint16_t)port_nr;
[821]57}
58
[1770]59
60/* "Old" networking API - only IPv4 */
61
62/*
[821]63void bb_lookup_host(struct sockaddr_in *s_in, const char *host)
64{
65 struct hostent *he;
66
67 memset(s_in, 0, sizeof(struct sockaddr_in));
68 s_in->sin_family = AF_INET;
69 he = xgethostbyname(host);
70 memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
71}
72
[1770]73
74int xconnect_tcp_v4(struct sockaddr_in *s_addr)
[821]75{
[1770]76 int s = xsocket(AF_INET, SOCK_STREAM, 0);
77 xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr));
[821]78 return s;
79}
[1770]80*/
81
82/* "New" networking API */
83
84
85int get_nport(const struct sockaddr *sa)
86{
87#if ENABLE_FEATURE_IPV6
88 if (sa->sa_family == AF_INET6) {
89 return ((struct sockaddr_in6*)sa)->sin6_port;
90 }
91#endif
92 if (sa->sa_family == AF_INET) {
93 return ((struct sockaddr_in*)sa)->sin_port;
94 }
95 /* What? UNIX socket? IPX?? :) */
96 return -1;
97}
98
99void 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/* We hijack this constant to mean something else */
115/* It doesn't hurt because we will remove this bit anyway */
116#define DIE_ON_ERROR AI_CANONNAME
117
118/* host: "1.2.3.4[:port]", "www.google.com[:port]"
119 * port: if neither of above specifies port # */
120static len_and_sockaddr* str2sockaddr(
121 const char *host, int port,
122USE_FEATURE_IPV6(sa_family_t af,)
123 int ai_flags)
124{
125 int rc;
126 len_and_sockaddr *r = NULL;
127 struct addrinfo *result = NULL;
128 const char *org_host = host; /* only for error msg */
129 const char *cp;
130 struct addrinfo hint;
131
132 /* Ugly parsing of host:addr */
133 if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
134 host++;
135 cp = strchr(host, ']');
136 if (!cp || cp[1] != ':') /* Malformed: must have [xx]:nn */
137 bb_error_msg_and_die("bad address '%s'", org_host);
138 //return r; /* return NULL */
139 } else {
140 cp = strrchr(host, ':');
141 if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
142 /* There is more than one ':' (e.g. "::1") */
143 cp = NULL; /* it's not a port spec */
144 }
145 }
146 if (cp) {
147 int sz = cp - host + 1;
148 host = safe_strncpy(alloca(sz), host, sz);
149 if (ENABLE_FEATURE_IPV6 && *cp != ':')
150 cp++; /* skip ']' */
151 cp++; /* skip ':' */
152 port = xatou16(cp);
153 }
154
155 memset(&hint, 0 , sizeof(hint));
156#if !ENABLE_FEATURE_IPV6
157 hint.ai_family = AF_INET; /* do not try to find IPv6 */
158#else
159 hint.ai_family = af;
160#endif
161 /* Needed. Or else we will get each address thrice (or more)
162 * for each possible socket type (tcp,udp,raw...): */
163 hint.ai_socktype = SOCK_STREAM;
164 hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
165 rc = getaddrinfo(host, NULL, &hint, &result);
166 if (rc || !result) {
167 bb_error_msg("bad address '%s'", org_host);
168 if (ai_flags & DIE_ON_ERROR)
169 xfunc_die();
170 goto ret;
171 }
172 r = xmalloc(offsetof(len_and_sockaddr, sa) + result->ai_addrlen);
173 r->len = result->ai_addrlen;
174 memcpy(&r->sa, result->ai_addr, result->ai_addrlen);
175 set_nport(r, htons(port));
176 ret:
177 freeaddrinfo(result);
178 return r;
179}
180#if !ENABLE_FEATURE_IPV6
181#define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
182#endif
183
184#if ENABLE_FEATURE_IPV6
185len_and_sockaddr* host_and_af2sockaddr(const char *host, int port, sa_family_t af)
186{
187 return str2sockaddr(host, port, af, 0);
188}
189
190len_and_sockaddr* xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
191{
192 return str2sockaddr(host, port, af, DIE_ON_ERROR);
193}
194#endif
195
196len_and_sockaddr* host2sockaddr(const char *host, int port)
197{
198 return str2sockaddr(host, port, AF_UNSPEC, 0);
199}
200
201len_and_sockaddr* xhost2sockaddr(const char *host, int port)
202{
203 return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR);
204}
205
206len_and_sockaddr* xdotted2sockaddr(const char *host, int port)
207{
208 return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
209}
210
211int xsocket_type(len_and_sockaddr **lsap, USE_FEATURE_IPV6(int family,) int sock_type)
212{
213 SKIP_FEATURE_IPV6(enum { family = AF_INET };)
214 len_and_sockaddr *lsa;
215 int fd;
216 int len;
217
218#if ENABLE_FEATURE_IPV6
219 if (family == AF_UNSPEC) {
220 fd = socket(AF_INET6, sock_type, 0);
221 if (fd >= 0) {
222 family = AF_INET6;
223 goto done;
224 }
225 family = AF_INET;
226 }
227#endif
228 fd = xsocket(family, sock_type, 0);
229 len = sizeof(struct sockaddr_in);
230#if ENABLE_FEATURE_IPV6
231 if (family == AF_INET6) {
232 done:
233 len = sizeof(struct sockaddr_in6);
234 }
235#endif
236 lsa = xzalloc(offsetof(len_and_sockaddr, sa) + len);
237 lsa->len = len;
238 lsa->sa.sa_family = family;
239 *lsap = lsa;
240 return fd;
241}
242
243int xsocket_stream(len_and_sockaddr **lsap)
244{
245 return xsocket_type(lsap, USE_FEATURE_IPV6(AF_UNSPEC,) SOCK_STREAM);
246}
247
248static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
249{
250 int fd;
251 len_and_sockaddr *lsa;
252
253 if (bindaddr && bindaddr[0]) {
254 lsa = xdotted2sockaddr(bindaddr, port);
255 /* user specified bind addr dictates family */
256 fd = xsocket(lsa->sa.sa_family, sock_type, 0);
257 } else {
258 fd = xsocket_type(&lsa, USE_FEATURE_IPV6(AF_UNSPEC,) sock_type);
259 set_nport(lsa, htons(port));
260 }
261 setsockopt_reuseaddr(fd);
262 xbind(fd, &lsa->sa, lsa->len);
263 free(lsa);
264 return fd;
265}
266
267int create_and_bind_stream_or_die(const char *bindaddr, int port)
268{
269 return create_and_bind_or_die(bindaddr, port, SOCK_STREAM);
270}
271
272int create_and_bind_dgram_or_die(const char *bindaddr, int port)
273{
274 return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM);
275}
276
277
278int create_and_connect_stream_or_die(const char *peer, int port)
279{
280 int fd;
281 len_and_sockaddr *lsa;
282
283 lsa = xhost2sockaddr(peer, port);
284 fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
285 setsockopt_reuseaddr(fd);
286 xconnect(fd, &lsa->sa, lsa->len);
287 free(lsa);
288 return fd;
289}
290
291int xconnect_stream(const len_and_sockaddr *lsa)
292{
293 int fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
294 xconnect(fd, &lsa->sa, lsa->len);
295 return fd;
296}
297
298/* We hijack this constant to mean something else */
299/* It doesn't hurt because we will add this bit anyway */
300#define IGNORE_PORT NI_NUMERICSERV
301static char* sockaddr2str(const struct sockaddr *sa, int flags)
302{
303 char host[128];
304 char serv[16];
305 int rc;
306 socklen_t salen;
307
308 salen = LSA_SIZEOF_SA;
309#if ENABLE_FEATURE_IPV6
310 if (sa->sa_family == AF_INET)
311 salen = sizeof(struct sockaddr_in);
312 if (sa->sa_family == AF_INET6)
313 salen = sizeof(struct sockaddr_in6);
314#endif
315 rc = getnameinfo(sa, salen,
316 host, sizeof(host),
317 /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
318 serv, sizeof(serv),
319 /* do not resolve port# into service _name_ */
320 flags | NI_NUMERICSERV
321 );
322 if (rc)
323 return NULL;
324 if (flags & IGNORE_PORT)
325 return xstrdup(host);
326#if ENABLE_FEATURE_IPV6
327 if (sa->sa_family == AF_INET6) {
328 if (strchr(host, ':')) /* heh, it's not a resolved hostname */
329 return xasprintf("[%s]:%s", host, serv);
330 /*return xasprintf("%s:%s", host, serv);*/
331 /* - fall through instead */
332 }
333#endif
334 /* For now we don't support anything else, so it has to be INET */
335 /*if (sa->sa_family == AF_INET)*/
336 return xasprintf("%s:%s", host, serv);
337 /*return xstrdup(host);*/
338}
339
340char* xmalloc_sockaddr2host(const struct sockaddr *sa)
341{
342 return sockaddr2str(sa, 0);
343}
344
345char* xmalloc_sockaddr2host_noport(const struct sockaddr *sa)
346{
347 return sockaddr2str(sa, IGNORE_PORT);
348}
349
350char* xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa)
351{
352 return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT);
353}
354char* xmalloc_sockaddr2dotted(const struct sockaddr *sa)
355{
356 return sockaddr2str(sa, NI_NUMERICHOST);
357}
358
359char* xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa)
360{
361 return sockaddr2str(sa, NI_NUMERICHOST | IGNORE_PORT);
362}
Note: See TracBrowser for help on using the repository browser.