Subversion Repositories HelenOS

Rev

Rev 4582 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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