/*
Copyright (C) 2003-2004 Narcis Ilisei

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.
*/
  
#include "os.h"
#include <stdio.h>
#include <string.h>

/*
    returns the inet addr of a char* begining with an IP address: "nnn.nnn.nnn.nnn"
*/
RC_TYPE os_convert_ip_to_inet_addr(unsigned long *p_addr, const char *p_name)
{
    RC_TYPE rc = RC_OK;

    if (p_name == NULL || p_addr == NULL)
    {
        return RC_INVALID_POINTER;
    }

    *p_addr = 0;
    if (strlen(p_name) == 0)
    {
        rc = RC_OS_INVALID_IP_ADDRESS;
    }

    {
        unsigned long b3;
        unsigned long b2;
        unsigned long b1;
        unsigned long b0;
        unsigned long ipa;
        int n;

        ipa = 0x0;
        n = sscanf(p_name, IP_V4_IP_ADDR_FORMAT, &b3, &b2, &b1, &b0);
        if (n == 4)
        {
            ipa = (b3 << 24) | (b2 << 16) | (b1 << 8) | b0;
            *p_addr = htonl(ipa);
            
        }
        else
        {
            rc = RC_OS_INVALID_IP_ADDRESS;
        }
    }
    return rc;
}
