iPXE
socket.h
Go to the documentation of this file.
1 #ifndef _IPXE_SOCKET_H
2 #define _IPXE_SOCKET_H
3 
4 /** @file
5  *
6  * Socket addresses
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 
12 #include <stdint.h>
13 #include <ipxe/tables.h>
14 
15 /**
16  * @defgroup commtypes Communication semantics
17  *
18  * @{
19  */
20 
21 /** Connection-based, reliable streams */
22 extern int tcp_sock_stream;
23 #define TCP_SOCK_STREAM 0x1
24 #define SOCK_STREAM tcp_sock_stream
25 
26 /** Connectionless, unreliable streams */
27 extern int udp_sock_dgram;
28 #define UDP_SOCK_DGRAM 0x2
29 #define SOCK_DGRAM udp_sock_dgram
30 
31 /** Echo testing streams */
32 extern int ping_sock_echo;
33 #define PING_SOCK_ECHO 0x3
34 #define SOCK_ECHO ping_sock_echo
35 
36 /** @} */
37 
38 /**
39  * Name communication semantics
40  *
41  * @v semantics Communication semantics (e.g. SOCK_STREAM)
42  * @ret name Name of communication semantics
43  */
44 static inline __attribute__ (( always_inline )) const char *
45 socket_semantics_name ( int semantics ) {
46  /* Cannot use a switch() because of the {TCP_UDP}_SOCK_XXX hack */
47  if ( semantics == SOCK_STREAM ) {
48  return "SOCK_STREAM";
49  } else if ( semantics == SOCK_DGRAM ) {
50  return "SOCK_DGRAM";
51  } else if ( semantics == SOCK_ECHO ) {
52  return "SOCK_ECHO";
53  } else {
54  return "SOCK_UNKNOWN";
55  }
56 }
57 
58 /**
59  * @defgroup addrfam Address families
60  *
61  * @{
62  */
63 #define AF_INET 1 /**< IPv4 Internet addresses */
64 #define AF_INET6 2 /**< IPv6 Internet addresses */
65 #define AF_FC 3 /**< Fibre Channel addresses */
66 /** @} */
67 
68 /**
69  * Name address family
70  *
71  * @v family Address family (e.g. AF_INET)
72  * @ret name Name of address family
73  */
74 static inline __attribute__ (( always_inline )) const char *
75 socket_family_name ( int family ) {
76  switch ( family ) {
77  case AF_INET: return "AF_INET";
78  case AF_INET6: return "AF_INET6";
79  case AF_FC: return "AF_FC";
80  default: return "AF_UNKNOWN";
81  }
82 }
83 
84 /** A socket address family */
86 
87 /** Length of a @c struct @c sockaddr */
88 #define SA_LEN 32
89 
90 /**
91  * Generalized socket address structure
92  *
93  * This contains the fields common to socket addresses for all address
94  * families.
95  */
96 struct sockaddr {
97  /** Socket address family
98  *
99  * This is an AF_XXX constant.
100  */
102  /** Padding
103  *
104  * This ensures that a struct @c sockaddr_tcpip is large
105  * enough to hold a socket address for any TCP/IP address
106  * family.
107  */
108  char pad[ SA_LEN - sizeof ( sa_family_t ) ];
109 } __attribute__ (( packed, may_alias ));
110 
111 /**
112  * Socket address converter
113  *
114  */
116  /** Socket address family
117  *
118  * This is an AF_XXX constant.
119  */
121  /** Transcribe socket address
122  *
123  * @v sa Socket address
124  * @ret string Socket address string
125  */
126  const char * ( * ntoa ) ( struct sockaddr *sa );
127  /** Parse socket address
128  *
129  * @v string Socket address stringh
130  * @v sa Socket address to fill in
131  * @ret rc Return status code
132  */
133  int ( * aton ) ( const char *string, struct sockaddr *sa );
134 };
135 
136 /** Socket address converter table */
137 #define SOCKADDR_CONVERTERS \
138  __table ( struct sockaddr_converter, "sockaddr_converters" )
139 
140 /** Declare a socket address converter */
141 #define __sockaddr_converter __table_entry ( SOCKADDR_CONVERTERS, 01 )
142 
143 extern const char * sock_ntoa ( struct sockaddr *sa );
144 extern int sock_aton ( const char *string, struct sockaddr *sa );
145 
146 #endif /* _IPXE_SOCKET_H */
#define __attribute__(x)
Definition: compiler.h:10
unsigned short uint16_t
Definition: stdint.h:11
static const char * socket_semantics_name(int semantics)
Name communication semantics.
Definition: socket.h:45
#define AF_INET6
IPv6 Internet addresses.
Definition: socket.h:64
struct sockaddr sa
Definition: dns.c:68
#define SOCK_DGRAM
Definition: socket.h:29
sa_family_t sa_family
Socket address family.
Definition: socket.h:101
uint32_t string
Definition: multiboot.h:14
int ping_sock_echo
Echo testing streams.
Definition: ping.c:277
#define AF_FC
Fibre Channel addresses.
Definition: socket.h:65
int sock_aton(const char *string, struct sockaddr *sa)
Parse socket address.
Definition: socket.c:59
#define SA_LEN
Length of a struct sockaddr.
Definition: socket.h:88
Socket address converter.
Definition: socket.h:115
#define SOCK_ECHO
Definition: socket.h:34
uint16_t sa_family_t
A socket address family.
Definition: socket.h:85
Generalized socket address structure.
Definition: socket.h:96
#define SOCK_STREAM
Definition: socket.h:24
static const char * socket_family_name(int family)
Name address family.
Definition: socket.h:75
char pad[SA_LEN - sizeof(sa_family_t)]
Padding.
Definition: socket.h:108
sa_family_t family
Socket address family.
Definition: socket.h:120
int tcp_sock_stream
Connection-based, reliable streams.
Definition: tcp.c:1761
const char * sock_ntoa(struct sockaddr *sa)
Transcribe socket address.
Definition: socket.c:42
int udp_sock_dgram
Connectionless, unreliable streams.
Definition: udp.c:414
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
int(* aton)(const char *string, struct sockaddr *sa)
Parse socket address.
Definition: socket.h:133
Linker tables.
#define AF_INET
IPv4 Internet addresses.
Definition: socket.h:63