iPXE
socket.h
Go to the documentation of this file.
00001 #ifndef _IPXE_SOCKET_H
00002 #define _IPXE_SOCKET_H
00003 
00004 /** @file
00005  *
00006  * Socket addresses
00007  *
00008  */
00009 
00010 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00011 
00012 #include <stdint.h>
00013 #include <ipxe/tables.h>
00014 
00015 /**
00016  * @defgroup commtypes Communication semantics
00017  *
00018  * @{
00019  */
00020 
00021 /** Connection-based, reliable streams */
00022 extern int tcp_sock_stream;
00023 #define TCP_SOCK_STREAM 0x1
00024 #define SOCK_STREAM tcp_sock_stream
00025 
00026 /** Connectionless, unreliable streams */
00027 extern int udp_sock_dgram;
00028 #define UDP_SOCK_DGRAM 0x2
00029 #define SOCK_DGRAM udp_sock_dgram
00030 
00031 /** Echo testing streams */
00032 extern int ping_sock_echo;
00033 #define PING_SOCK_ECHO 0x3
00034 #define SOCK_ECHO ping_sock_echo
00035 
00036 /** @} */
00037 
00038 /**
00039  * Name communication semantics
00040  *
00041  * @v semantics         Communication semantics (e.g. SOCK_STREAM)
00042  * @ret name            Name of communication semantics
00043  */
00044 static inline __attribute__ (( always_inline )) const char *
00045 socket_semantics_name ( int semantics ) {
00046         /* Cannot use a switch() because of the {TCP_UDP}_SOCK_XXX hack */
00047         if ( semantics == SOCK_STREAM ) {
00048                 return "SOCK_STREAM";
00049         } else if ( semantics == SOCK_DGRAM ) {
00050                 return "SOCK_DGRAM";
00051         } else if ( semantics == SOCK_ECHO ) {
00052                 return "SOCK_ECHO";
00053         } else {
00054                 return "SOCK_UNKNOWN";
00055         }
00056 }
00057 
00058 /**
00059  * @defgroup addrfam Address families
00060  *
00061  * @{
00062  */
00063 #define AF_INET         1       /**< IPv4 Internet addresses */
00064 #define AF_INET6        2       /**< IPv6 Internet addresses */
00065 #define AF_FC           3       /**< Fibre Channel addresses */
00066 /** @} */
00067 
00068 /**
00069  * Name address family
00070  *
00071  * @v family            Address family (e.g. AF_INET)
00072  * @ret name            Name of address family
00073  */
00074 static inline __attribute__ (( always_inline )) const char *
00075 socket_family_name ( int family ) {
00076         switch ( family ) {
00077         case AF_INET:           return "AF_INET";
00078         case AF_INET6:          return "AF_INET6";
00079         case AF_FC:             return "AF_FC";
00080         default:                return "AF_UNKNOWN";
00081         }
00082 }
00083 
00084 /** A socket address family */
00085 typedef uint16_t sa_family_t;
00086 
00087 /** Length of a @c struct @c sockaddr */
00088 #define SA_LEN 32
00089 
00090 /**
00091  * Generalized socket address structure
00092  *
00093  * This contains the fields common to socket addresses for all address
00094  * families.
00095  */
00096 struct sockaddr {
00097         /** Socket address family
00098          *
00099          * This is an AF_XXX constant.
00100          */
00101         sa_family_t sa_family;
00102         /** Padding
00103          *
00104          * This ensures that a struct @c sockaddr_tcpip is large
00105          * enough to hold a socket address for any TCP/IP address
00106          * family.
00107          */
00108         char pad[ SA_LEN - sizeof ( sa_family_t ) ];
00109 } __attribute__ (( packed, may_alias ));
00110 
00111 /**
00112  * Socket address converter
00113  *
00114  */
00115 struct sockaddr_converter {
00116         /** Socket address family
00117          *
00118          * This is an AF_XXX constant.
00119          */
00120         sa_family_t family;
00121         /** Transcribe socket address
00122          *
00123          * @v sa                Socket address
00124          * @ret string          Socket address string
00125          */
00126         const char * ( * ntoa ) ( struct sockaddr *sa );
00127         /** Parse socket address
00128          *
00129          * @v string            Socket address stringh
00130          * @v sa                Socket address to fill in
00131          * @ret rc              Return status code
00132          */
00133         int ( * aton ) ( const char *string, struct sockaddr *sa );
00134 };
00135 
00136 /** Socket address converter table */
00137 #define SOCKADDR_CONVERTERS \
00138         __table ( struct sockaddr_converter, "sockaddr_converters" )
00139 
00140 /** Declare a socket address converter */
00141 #define __sockaddr_converter __table_entry ( SOCKADDR_CONVERTERS, 01 )
00142 
00143 extern const char * sock_ntoa ( struct sockaddr *sa );
00144 extern int sock_aton ( const char *string, struct sockaddr *sa );
00145 
00146 #endif /* _IPXE_SOCKET_H */