plan9port/src/cmd/vbackup/vmount.c
David du Colombier 3409bc9ae1 lib9/dial: add support for IPv6
The function p9dialparse() returns the host as a sockaddr_storage
structure instead of a u32int, to be able to handle both IPv4
and IPv6 addresses. Because the sockaddr_storage structure also
handle port numbers and Unix path names, there is no longer
need to set them in the calling functions. However, these values
are still returned for convenience.

The sockaddr_in and sockaddr_un structures have been replaced
by sockaddr_storage to handle Unix, IPv4 and IPv6 sockets.

Names and addresses are resolved using either gethostbyname()
or getaddrinfo() functions.

The getaddrinfo() function is documented in RFC2553 and standardized
since POSIX.1-2001. It supports both IPv4 and IPv6 addresses.
The gethostbyname() function is deprecated since POSIX.1-2008.
However, some libc implementations don't handle getaddrinfo()
properly, thus we preferred to try gethostbyname() first.

I've tried to preserve most of the old code logic to prevent
from surprising or unwanted behavior.

R=rsc
http://codereview.appspot.com/6255068
2012-06-02 21:50:59 +02:00

76 lines
1.4 KiB
C

#include <u.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <libc.h>
#include "mountnfs.h"
void
usage(void)
{
fprint(2, "usage: vmount [-v] [-h handle] address mountpoint\n");
exits("usage");
}
int handlelen = 1;
uchar handle[64] = {
0x00
};
void
main(int argc, char **argv)
{
char *p, *net, *unx;
char host[INET_ADDRSTRLEN];
int n, port, proto, verbose;
struct sockaddr_in sa;
verbose = 0;
ARGBEGIN{
case 'h':
p = EARGF(usage());
n = strlen(p);
if(n%2)
sysfatal("bad handle '%s'", p);
if(n > 2*sizeof handle)
sysfatal("handle too long '%s'", p);
handlelen = n/2;
if(dec16(handle, n/2, p, n) != n/2)
sysfatal("bad hex in handle '%s'", p);
break;
case 'v':
verbose = 1;
break;
default:
usage();
}ARGEND
if(argc != 2)
usage();
p = p9netmkaddr(argv[0], "udp", "nfs");
if(p9dialparse(strdup(p), &net, &unx, &sa, &port) < 0)
sysfatal("bad address '%s'", p);
if(sa.sin_family != AF_INET)
sysfatal("only IPv4 is supported");
inet_ntop(AF_INET, &(sa.sin_addr), host, INET_ADDRSTRLEN);
if(verbose)
print("nfs server is net=%s addr=%s port=%d\n",
net, host, port);
proto = 0;
if(strcmp(net, "tcp") == 0)
proto = SOCK_STREAM;
else if(strcmp(net, "udp") == 0)
proto = SOCK_DGRAM;
else
sysfatal("bad proto %s: can only handle tcp and udp", net);
mountnfs(proto, &sa, handle, handlelen, argv[1]);
exits(0);
}