Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / net / oncrpc / portmap.c
1 /*
2  * Copyright (C) 2013 Marin Hannache <ipxe@mareo.fr>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <assert.h>
25 #include <errno.h>
26 #include <byteswap.h>
27 #include <ipxe/socket.h>
28 #include <ipxe/tcpip.h>
29 #include <ipxe/in.h>
30 #include <ipxe/iobuf.h>
31 #include <ipxe/xfer.h>
32 #include <ipxe/open.h>
33 #include <ipxe/uri.h>
34 #include <ipxe/features.h>
35 #include <ipxe/timer.h>
36 #include <ipxe/oncrpc.h>
37 #include <ipxe/oncrpc_iob.h>
38 #include <ipxe/portmap.h>
39
40 /** @file
41  *
42  * PORTMAPPER protocol.
43  *
44  */
45
46 /** PORTMAP GETPORT procedure. */
47 #define PORTMAP_GETPORT 3
48
49 /**
50  * Send a GETPORT request
51  *
52  * @v intf              Interface to send the request on
53  * @v session           ONC RPC session
54  * @v prog              ONC RPC program number
55  * @v vers              ONC RPC rogram version number
56  * @v proto             Protocol (TCP or UDP)
57  * @ret rc              Return status code
58  */
59 int portmap_getport ( struct interface *intf, struct oncrpc_session *session,
60                       uint32_t prog, uint32_t vers, uint32_t proto ) {
61         struct oncrpc_field fields[] = {
62                 ONCRPC_FIELD ( int32, prog ),
63                 ONCRPC_FIELD ( int32, vers ),
64                 ONCRPC_FIELD ( int32, proto ),
65                 ONCRPC_FIELD ( int32, 0 ), /* The port field is only meaningful
66                                               in GETPORT reply */
67                 ONCRPC_FIELD_END,
68         };
69
70         return oncrpc_call ( intf, session, PORTMAP_GETPORT, fields );
71 }
72
73 /**
74  * Parse a GETPORT reply
75  *
76  * @v getport_reply     A structure where the data will be saved
77  * @v reply             The ONC RPC reply to get data from
78  * @ret rc              Return status code
79  */
80 int portmap_get_getport_reply ( struct portmap_getport_reply *getport_reply,
81                                 struct oncrpc_reply *reply ) {
82         if ( ! getport_reply || ! reply )
83                 return -EINVAL;
84
85         getport_reply->port = oncrpc_iob_get_int ( reply->data );
86         if ( getport_reply == 0 || getport_reply->port >= 65536 )
87                 return -EINVAL;
88
89         return 0;
90 }