source: mainline/uspace/srv/net/inet.c@ 9f2ea28

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9f2ea28 was aadf01e, checked in by Lukas Mejdrech <lukasmejdrech@…>, 15 years ago

Coding style (no functional change)

  • Property mode set to 100644
File size: 3.3 KB
Line 
1/*
2 * Copyright (c) 2009 Lukas 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 * Internet protocol address conversion functions implementation.
35 */
36
37#include <errno.h>
38#include <mem.h>
39#include <stdio.h>
40#include <string.h>
41
42#include "include/in.h"
43#include "include/in6.h"
44#include "include/inet.h"
45#include "include/socket_codes.h"
46
47int inet_pton(uint16_t family, const char * address, uint8_t * data){
48 const char * next;
49 char * last;
50 int index;
51 int count;
52 int base;
53 size_t bytes;
54 size_t shift;
55 unsigned long value;
56
57 if(! data){
58 return EINVAL;
59 }
60 switch(family){
61 case AF_INET:
62 count = 4;
63 base = 10;
64 bytes = 1;
65 break;
66 case AF_INET6:
67 count = 16;
68 base = 16;
69 bytes = 4;
70 break;
71 default:
72 return ENOTSUP;
73 }
74 if(! address){
75 bzero(data, count);
76 return ENOENT;
77 }
78 next = address;
79 index = 0;
80 do{
81 if(next && (*next)){
82 if(index){
83 ++ next;
84 }
85 value = strtoul(next, &last, base);
86 next = last;
87 shift = bytes - 1;
88 do{
89 // like little endian
90 data[index + shift] = value;
91 value >>= 8;
92 }while(shift --);
93 index += bytes;
94 }else{
95 bzero(data + index, count - index);
96 return EOK;
97 }
98 }while(index < count);
99 return EOK;
100}
101
102int inet_ntop(uint16_t family, const uint8_t * data, char * address, size_t length){
103 if((! data) || (! address)){
104 return EINVAL;
105 }
106 switch(family){
107 case AF_INET:
108 if(length < INET_ADDRSTRLEN){
109 return ENOMEM;
110 }
111 snprintf(address, length, "%hhu.%hhu.%hhu.%hhu", data[0], data[1], data[2], data[3]);
112 return EOK;
113 case AF_INET6:
114 if(length < INET6_ADDRSTRLEN){
115 return ENOMEM;
116 }
117 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]);
118 return EOK;
119 default:
120 return ENOTSUP;
121 }
122}
123
124/** @}
125 */
Note: See TracBrowser for help on using the repository browser.