Subversion Repositories HelenOS

Rev

Rev 4243 | 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
34
 *  
35
 */
36
 
4261 mejdrech 37
#include <errno.h>
4192 mejdrech 38
#include <mem.h>
3846 mejdrech 39
#include <stdio.h>
40
 
41
#include "include/sockaddr.h"
42
#include "include/socket.h"
43
 
44
int inet_pton( uint16_t family, const char * address, uint8_t * data ){
45
    const char *    next;
46
    char *          last;
47
    int             index;
48
    int             count;
49
    int             base;
50
    size_t          bytes;
51
    size_t          shift;
52
    unsigned long   value;
53
 
54
    if( ! data ) return EINVAL;
55
    switch( family ){
56
        case AF_INET:
57
            count = 4;
58
            base = 10;
59
            bytes = 1;
60
            break;
61
        case AF_INET6:
62
            count = 16;
63
            base = 16;
64
            bytes = 4;
65
            break;
66
        default:
67
            return ENOTSUP;
68
    }
69
    if( ! address ){
4192 mejdrech 70
        bzero( data, count );
3846 mejdrech 71
        return ENOENT;
72
    }
73
    next = address;
74
    index = 0;
75
    do{
76
        if( next && ( * next )){
77
            if( index ) ++ next;
78
            value = strtoul( next, & last, base );
79
            next = last;
80
            shift = bytes - 1;
81
            do{
82
                // like little endian
83
                data[ index + shift ] = value;
84
//              data[ index ] = value;
85
                value >>= 8;
86
//              ++ index;
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
 */