The friendly Operating System for the Internet of Things
inet.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013-15 Freie Universit├Ąt Berlin
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
23 #ifndef ARPA_INET_H
24 #define ARPA_INET_H
25 
26 #include <errno.h>
27 #include <inttypes.h>
28 #include <stdio.h>
29 
30 #include "byteorder.h"
31 #include "net/af.h"
32 #include "net/ipv4/addr.h"
33 #include "net/ipv6/addr.h"
34 #include "sys/bytes.h"
35 #include "netinet/in.h"
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
44 #ifndef INADDRSZ
45 #define INADDRSZ (sizeof(ipv4_addr_t))
46 #endif
47 
51 #ifndef IN6ADDRSZ
52 #define IN6ADDRSZ (sizeof(ipv6_addr_t))
53 #endif
54 
69 static inline const char *inet_ntop(int af, const void *restrict src, char *restrict dst,
70  socklen_t size)
71 {
72  switch (af) {
73 #ifdef MODULE_IPV4_ADDR
74  case AF_INET:
75  if (ipv4_addr_to_str(dst, src, (size_t)size) == NULL) {
76  errno = ENOSPC;
77  return NULL;
78  }
79  break;
80 #endif
81 #ifdef MODULE_IPV6_ADDR
82  case AF_INET6:
83  if (ipv6_addr_to_str(dst, src, (size_t)size) == NULL) {
84  errno = ENOSPC;
85  return NULL;
86  }
87  break;
88 #endif
89  default:
90  (void)src;
91  (void)dst;
92  (void)size;
94  return NULL;
95  }
96  return dst;
97 }
98 
113 static inline int inet_pton(int af, const char *src, void *dst)
114 {
115  switch (af) {
116 #ifdef MODULE_IPV4_ADDR
117  case AF_INET:
118  if (ipv4_addr_from_str(dst, src) == NULL) {
119  return 0;
120  }
121  break;
122 #endif
123 #ifdef MODULE_IPV6_ADDR
124  case AF_INET6:
125  if (ipv6_addr_from_str(dst, src) == NULL) {
126  return 0;
127  }
128  break;
129 #endif
130  default:
131  (void)src;
132  (void)dst;
134  return -1;
135  }
136  return 1;
137 }
138 
139 #ifdef __cplusplus
140 }
141 #endif
142 
146 #endif /* ARPA_INET_H */
IPv6 address type and helper functions definitions.
Definitions for IPv6 addresses.
#define EAFNOSUPPORT
Address family not supported.
Definition: errno.h:74
size_t socklen_t
socket address length
Definition: bytes.h:89
Main socket header.
#define ENOSPC
No space left on device.
Definition: errno.h:121
static int inet_pton(int af, const char *src, void *dst)
Converts an IP address string representation to a byte-represented IP address.
Definition: inet.h:113
char * ipv6_addr_to_str(char *result, const ipv6_addr_t *addr, uint8_t result_len)
Converts an IPv6 address to its string representation.
ipv4_addr_t * ipv4_addr_from_str(ipv4_addr_t *result, const char *addr)
Converts an IPv4 address string representation to a byte-represented IPv4 address.
int errno
number of last error
Functions to work with different byte orders.
Adds include for missing inttype definitions.
internetwork address family: UDP, TCP, etc.
Definition: af.h:36
stdio.h wrapper for MSP430
System-internal byte operations.
Global UNIX address family definitions.
internetwork address family with IPv6: UDP, TCP, etc.
Definition: af.h:38
ipv6_addr_t * ipv6_addr_from_str(ipv6_addr_t *result, const char *addr)
Converts an IPv6 address string representation to a byte-represented IPv6 address.
static const char * inet_ntop(int af, const void *restrict src, char *restrict dst, socklen_t size)
Converts an IP address to its string representation.
Definition: inet.h:69
char * ipv4_addr_to_str(char *result, const ipv4_addr_t *addr, uint8_t result_len)
Converts an IPv4 address to its string representation.