• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef _GPXE_SOCKET_H
2 #define _GPXE_SOCKET_H
3 
4 /** @file
5  *
6  * Socket addresses
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER );
11 
12 #include <stdint.h>
13 
14 /**
15  * @defgroup commtypes Communication semantics
16  *
17  * @{
18  */
19 
20 /** Connection-based, reliable streams */
21 extern int tcp_sock_stream;
22 #define TCP_SOCK_STREAM 0x1
23 #define SOCK_STREAM tcp_sock_stream
24 
25 /** Connectionless, unreliable streams */
26 extern int udp_sock_dgram;
27 #define UDP_SOCK_DGRAM 0x2
28 #define SOCK_DGRAM udp_sock_dgram
29 
30 /** @} */
31 
32 /**
33  * Name communication semantics
34  *
35  * @v semantics		Communication semantics (e.g. SOCK_STREAM)
36  * @ret name		Name of communication semantics
37  */
38 static inline __attribute__ (( always_inline )) const char *
socket_semantics_name(int semantics)39 socket_semantics_name ( int semantics ) {
40 	/* Cannot use a switch() because of the {TCP_UDP}_SOCK_XXX hack */
41 	if ( semantics == SOCK_STREAM ) {
42 		return "SOCK_STREAM";
43 	} else if ( semantics == SOCK_DGRAM ) {
44 		return "SOCK_DGRAM";
45 	} else {
46 		return "SOCK_UNKNOWN";
47 	}
48 }
49 
50 /**
51  * @defgroup addrfam Address families
52  *
53  * @{
54  */
55 #define AF_INET		1	/**< IPv4 Internet addresses */
56 #define AF_INET6	2	/**< IPv6 Internet addresses */
57 /** @} */
58 
59 /**
60  * Name address family
61  *
62  * @v family		Address family (e.g. AF_INET)
63  * @ret name		Name of address family
64  */
65 static inline __attribute__ (( always_inline )) const char *
socket_family_name(int family)66 socket_family_name ( int family ) {
67 	switch ( family ) {
68 	case AF_INET:		return "AF_INET";
69 	case AF_INET6:		return "AF_INET6";
70 	default:		return "AF_UNKNOWN";
71 	}
72 }
73 
74 /** A socket address family */
75 typedef uint16_t sa_family_t;
76 
77 /** Length of a @c struct @c sockaddr */
78 #define SA_LEN 32
79 
80 /**
81  * Generalized socket address structure
82  *
83  * This contains the fields common to socket addresses for all address
84  * families.
85  */
86 struct sockaddr {
87 	/** Socket address family
88 	 *
89 	 * This is an AF_XXX constant.
90 	 */
91         sa_family_t sa_family;
92 	/** Padding
93 	 *
94 	 * This ensures that a struct @c sockaddr_tcpip is large
95 	 * enough to hold a socket address for any TCP/IP address
96 	 * family.
97 	 */
98 	char pad[ SA_LEN - sizeof ( sa_family_t ) ];
99 } __attribute__ (( may_alias ));
100 
101 #endif /* _GPXE_SOCKET_H */
102