iPXE
portmap.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Marin Hannache <ipxe@mareo.fr>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  */
00019 
00020 #include <stdint.h>
00021 #include <stdlib.h>
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include <assert.h>
00025 #include <errno.h>
00026 #include <byteswap.h>
00027 #include <ipxe/socket.h>
00028 #include <ipxe/tcpip.h>
00029 #include <ipxe/in.h>
00030 #include <ipxe/iobuf.h>
00031 #include <ipxe/xfer.h>
00032 #include <ipxe/open.h>
00033 #include <ipxe/uri.h>
00034 #include <ipxe/features.h>
00035 #include <ipxe/timer.h>
00036 #include <ipxe/oncrpc.h>
00037 #include <ipxe/oncrpc_iob.h>
00038 #include <ipxe/portmap.h>
00039 
00040 /** @file
00041  *
00042  * PORTMAPPER protocol.
00043  *
00044  */
00045 
00046 /** PORTMAP GETPORT procedure. */
00047 #define PORTMAP_GETPORT 3
00048 
00049 /**
00050  * Send a GETPORT request
00051  *
00052  * @v intf              Interface to send the request on
00053  * @v session           ONC RPC session
00054  * @v prog              ONC RPC program number
00055  * @v vers              ONC RPC rogram version number
00056  * @v proto             Protocol (TCP or UDP)
00057  * @ret rc              Return status code
00058  */
00059 int portmap_getport ( struct interface *intf, struct oncrpc_session *session,
00060                       uint32_t prog, uint32_t vers, uint32_t proto ) {
00061         struct oncrpc_field fields[] = {
00062                 ONCRPC_FIELD ( int32, prog ),
00063                 ONCRPC_FIELD ( int32, vers ),
00064                 ONCRPC_FIELD ( int32, proto ),
00065                 ONCRPC_FIELD ( int32, 0 ), /* The port field is only meaningful
00066                                               in GETPORT reply */
00067                 ONCRPC_FIELD_END,
00068         };
00069 
00070         return oncrpc_call ( intf, session, PORTMAP_GETPORT, fields );
00071 }
00072 
00073 /**
00074  * Parse a GETPORT reply
00075  *
00076  * @v getport_reply     A structure where the data will be saved
00077  * @v reply             The ONC RPC reply to get data from
00078  * @ret rc              Return status code
00079  */
00080 int portmap_get_getport_reply ( struct portmap_getport_reply *getport_reply,
00081                                 struct oncrpc_reply *reply ) {
00082         if ( ! getport_reply || ! reply )
00083                 return -EINVAL;
00084 
00085         getport_reply->port = oncrpc_iob_get_int ( reply->data );
00086         if ( getport_reply == 0 || getport_reply->port >= 65536 )
00087                 return -EINVAL;
00088 
00089         return 0;
00090 }