Subversion Repositories HelenOS

Rev

Rev 4559 | Rev 4582 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3846 mejdrech 1
/*
3912 mejdrech 2
 * Copyright (c) 2009 Lukas Mejdrech
3846 mejdrech 3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup net
30
 *  @{
31
 */
32
 
33
/** @file
4350 mejdrech 34
 *  Internet protocol address conversion functions implementation.
3846 mejdrech 35
 */
36
 
4261 mejdrech 37
#include <errno.h>
4192 mejdrech 38
#include <mem.h>
3846 mejdrech 39
#include <stdio.h>
40
 
4578 mejdrech 41
#include "include/in.h"
42
#include "include/in6.h"
4559 mejdrech 43
#include "include/inet.h"
3846 mejdrech 44
#include "include/socket.h"
45
 
46
int inet_pton( uint16_t family, const char * address, uint8_t * data ){
47
    const char *    next;
48
    char *          last;
49
    int             index;
50
    int             count;
51
    int             base;
52
    size_t          bytes;
53
    size_t          shift;
54
    unsigned long   value;
55
 
56
    if( ! data ) return EINVAL;
57
    switch( family ){
58
        case AF_INET:
59
            count = 4;
60
            base = 10;
61
            bytes = 1;
62
            break;
63
        case AF_INET6:
64
            count = 16;
65
            base = 16;
66
            bytes = 4;
67
            break;
68
        default:
69
            return ENOTSUP;
70
    }
71
    if( ! address ){
4192 mejdrech 72
        bzero( data, count );
3846 mejdrech 73
        return ENOENT;
74
    }
75
    next = address;
76
    index = 0;
77
    do{
78
        if( next && ( * next )){
79
            if( index ) ++ next;
80
            value = strtoul( next, & last, base );
81
            next = last;
82
            shift = bytes - 1;
83
            do{
84
                // like little endian
85
                data[ index + shift ] = value;
86
                value >>= 8;
87
            }while( shift -- );
88
            index += bytes;
89
        }else{
4192 mejdrech 90
            bzero( data + index, count - index );
3846 mejdrech 91
            return EOK;
92
        }
93
    }while( index < count );
94
    return EOK;
95
}
96
 
97
int inet_ntop( uint16_t family, const uint8_t * data, char * address, size_t length ){
98
    if(( ! data ) || ( ! address )) return EINVAL;
99
    switch( family ){
100
        case AF_INET:   if( length < INET_ADDRSTRLEN ) return ENOMEM;
4243 mejdrech 101
                        snprintf( address, length, "%hhu.%hhu.%hhu.%hhu", data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ] );
3846 mejdrech 102
                        return EOK;
103
        case AF_INET6:  if( length < INET6_ADDRSTRLEN ) return ENOMEM;
4243 mejdrech 104
                        snprintf( address, length, "%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx", data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ], data[ 6 ], data[ 7 ], data[ 8 ], data[ 9 ], data[ 10 ], data[ 11 ], data[ 12 ], data[ 13 ], data[ 14 ], data[ 15 ] );
3846 mejdrech 105
                        return EOK;
106
        default:        return ENOTSUP;
107
    }
108
}
109
 
110
/** @}
111
 */