PageRenderTime 3ms CodeModel.GetById 28ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/include/ipxe/socket.h

https://github.com/Shopify/ipxe
C++ Header | 146 lines | 55 code | 19 blank | 72 comment | 10 complexity | e79e9daac44a4883aac75c7022022962 MD5 | raw file
  1#ifndef _IPXE_SOCKET_H
  2#define _IPXE_SOCKET_H
  3
  4/** @file
  5 *
  6 * Socket addresses
  7 *
  8 */
  9
 10FILE_LICENCE ( GPL2_OR_LATER );
 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 */
 22extern int tcp_sock_stream;
 23#define TCP_SOCK_STREAM 0x1
 24#define SOCK_STREAM tcp_sock_stream
 25
 26/** Connectionless, unreliable streams */
 27extern int udp_sock_dgram;
 28#define UDP_SOCK_DGRAM 0x2
 29#define SOCK_DGRAM udp_sock_dgram
 30
 31/** Echo testing streams */
 32extern 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 */
 44static inline __attribute__ (( always_inline )) const char *
 45socket_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 */
 74static inline __attribute__ (( always_inline )) const char *
 75socket_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 */
 85typedef uint16_t sa_family_t;
 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 */
 96struct sockaddr {
 97	/** Socket address family
 98	 *
 99	 * This is an AF_XXX constant.
100	 */
101        sa_family_t sa_family;
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 */
115struct sockaddr_converter {
116	/** Socket address family
117	 *
118	 * This is an AF_XXX constant.
119	 */
120        sa_family_t family;
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
143extern const char * sock_ntoa ( struct sockaddr *sa );
144extern int sock_aton ( const char *string, struct sockaddr *sa );
145
146#endif /* _IPXE_SOCKET_H */