//-----------------------------------------------------------------------------
// Copyright © 2004 - Philip Howard - All rights reserved
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//-----------------------------------------------------------------------------
// package	libh/net
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------

#include "net_lib.h"

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	get_port_by_name
//
// purpose	Get the port number, in host byte order, from a service name,
//		using getservbyname(), correcting for known errors in the way
//		the s_port member is defined in struct servent.
//
// arguments    1 (const char *) service name or numeric port string.
//		2 (const char *) protocol, "tcp" or "udp".
//
// returns      (int) < 0 : error
//              (int) > 0 : port number in host byte order
//-----------------------------------------------------------------------------
int
get_port_by_name (
    const char *        arg_service
    ,
    const char *        arg_protocol
    )
__PROTO_END__
{
    struct servent *    servent_p       ;
    char *              end_ptr         ;
    int                 port_num        ;


    if ( ! arg_service ) goto error_bad_arg;

    end_ptr = NULL;
    port_num = strtol( arg_service, & end_ptr, 0 );
    if ( * end_ptr ) {

        if ( ! arg_protocol ) arg_protocol = "tcp";

        //-- Get the struct pointer.
        servent_p = getservbyname( arg_service, arg_protocol );
        if ( ! servent_p ) goto error_bad_name;

        //-- If the type for s_port is just 2 bytes, use ntohs.
        if ( sizeof (servent_p->s_port) == 2 ) {
            port_num = ntohs( servent_p->s_port );
        }

        //-- If the type for s_port is 4 bytes, it may be stored as if 2.
        else if ( sizeof (servent_p->s_port) == 4 ) {
            port_num = ntohl( servent_p->s_port );
            if ( ( port_num & 65535 ) == 0 ) port_num >>= 16;
        }

        else goto error_bad_type;
    }

    if ( port_num > 65535 ) goto error_bad_port;

 do_return:
    return port_num;

 error_bad_arg:
 error_bad_name:
 error_bad_type:
 error_bad_port:
    port_num = -1;
    goto do_return;

}

