xref: /netbsd-src/sbin/mount_nfs/udp_xfer.c (revision 3028e483e4ef836741029ab781da4462ae95176b)
1*3028e483Smartin /*	$NetBSD: udp_xfer.c,v 1.2 2008/04/29 06:53:01 martin Exp $	*/
2a16ca184Sdsl 
3a16ca184Sdsl /*-
4a16ca184Sdsl  * Copyright (c) 2006 The NetBSD Foundation, Inc.
5a16ca184Sdsl  * All rights reserved.
6a16ca184Sdsl  *
7a16ca184Sdsl  * This code is derived from software contributed to The NetBSD Foundation
8a16ca184Sdsl  * by David Laight.
9a16ca184Sdsl  *
10a16ca184Sdsl  * Redistribution and use in source and binary forms, with or without
11a16ca184Sdsl  * modification, are permitted provided that the following conditions
12a16ca184Sdsl  * are met:
13a16ca184Sdsl  * 1. Redistributions of source code must retain the above copyright
14a16ca184Sdsl  *    notice, this list of conditions and the following disclaimer.
15a16ca184Sdsl  * 2. Redistributions in binary form must reproduce the above copyright
16a16ca184Sdsl  *    notice, this list of conditions and the following disclaimer in the
17a16ca184Sdsl  *    documentation and/or other materials provided with the distribution.
18a16ca184Sdsl  *
19a16ca184Sdsl  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20a16ca184Sdsl  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21a16ca184Sdsl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22a16ca184Sdsl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23a16ca184Sdsl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24a16ca184Sdsl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25a16ca184Sdsl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26a16ca184Sdsl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27a16ca184Sdsl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28a16ca184Sdsl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29a16ca184Sdsl  * POSSIBILITY OF SUCH DAMAGE.
30a16ca184Sdsl  */
31a16ca184Sdsl 
32a16ca184Sdsl #include <sys/cdefs.h>
33*3028e483Smartin __RCSID("$NetBSD: udp_xfer.c,v 1.2 2008/04/29 06:53:01 martin Exp $");
34a16ca184Sdsl 
35a16ca184Sdsl #include <sys/types.h>
36a16ca184Sdsl #include <unistd.h>
37a16ca184Sdsl #include <sys/socket.h>
38a16ca184Sdsl #include <netinet/in.h>
39a16ca184Sdsl #include <sys/poll.h>
40a16ca184Sdsl #include <netdb.h>
41a16ca184Sdsl #include <err.h>
42a16ca184Sdsl 
43a16ca184Sdsl typedef uint32_t n_long;
44a16ca184Sdsl #include <net.h>
45a16ca184Sdsl 
46a16ca184Sdsl void
set_port(struct sockaddr * sa,int port)47a16ca184Sdsl set_port(struct sockaddr *sa, int port)
48a16ca184Sdsl {
49a16ca184Sdsl 
50a16ca184Sdsl 	switch (sa->sa_family) {
51a16ca184Sdsl 	case AF_INET:
52a16ca184Sdsl 		((struct sockaddr_in *)sa)->sin_port = port;
53a16ca184Sdsl 		break;
54a16ca184Sdsl #ifdef INET6
55a16ca184Sdsl 	case AF_INET6:
56a16ca184Sdsl 		((struct sockaddr_in6 *)sa)->sin6_port = port;
57a16ca184Sdsl 		break;
58a16ca184Sdsl #endif
59a16ca184Sdsl 	default:
60a16ca184Sdsl 		errx(1, "Unsupported socket family %d",
61a16ca184Sdsl 		    sa->sa_family);
62a16ca184Sdsl 	}
63a16ca184Sdsl }
64a16ca184Sdsl 
65a16ca184Sdsl ssize_t
sendudp(struct iodesc * d,void * pkt,size_t len)66a16ca184Sdsl sendudp(struct iodesc *d, void *pkt, size_t len)
67a16ca184Sdsl {
68a16ca184Sdsl 	int sock;
69a16ca184Sdsl 	int range = IP_PORTRANGE_LOW;
70a16ca184Sdsl 
71a16ca184Sdsl 	if (d->socket >= 0) {
72a16ca184Sdsl 		close(d->socket);
73a16ca184Sdsl 		d->socket = -1;
74a16ca184Sdsl 	}
75a16ca184Sdsl 
76a16ca184Sdsl 	sock = socket(d->ai->ai_family, SOCK_DGRAM, IPPROTO_UDP);
77a16ca184Sdsl 	if (sock < 0)
78a16ca184Sdsl 		return -1;
79a16ca184Sdsl 	d->socket = sock;
80a16ca184Sdsl 	set_port(d->ai->ai_addr, d->destport);
81a16ca184Sdsl 
82a16ca184Sdsl 	setsockopt(sock, IPPROTO_IP, IP_PORTRANGE, &range, sizeof(range));
83a16ca184Sdsl 
84a16ca184Sdsl 	if (connect(sock, d->ai->ai_addr, d->ai->ai_addrlen) != 0)
85a16ca184Sdsl 		return -1;
86a16ca184Sdsl 
87a16ca184Sdsl 	return send(sock, pkt, len, 0);
88a16ca184Sdsl }
89a16ca184Sdsl 
90a16ca184Sdsl ssize_t
readudp(struct iodesc * d,void * pkt,size_t len,time_t tleft)91a16ca184Sdsl readudp(struct iodesc *d, void *pkt, size_t len, time_t tleft)
92a16ca184Sdsl {
93a16ca184Sdsl 	struct pollfd pfd = {d->socket, POLLIN, 0};
94a16ca184Sdsl 
95a16ca184Sdsl 	if (poll(&pfd, 1, tleft * 1000) != 1)
96a16ca184Sdsl 		return -1;
97a16ca184Sdsl 
98a16ca184Sdsl 	return recv(d->socket, pkt, len, 0);
99a16ca184Sdsl }
100