WinRTSockAddr.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #ifndef __SOCK_ADDR_H
  11. #define __SOCK_ADDR_H
  12. #include "NativeTypes.h"
  13. namespace RakNet
  14. {
  15. // All pointers to socket address structures are often cast to pointers
  16. // to this type before use in various functions and system calls:
  17. struct sockaddr {
  18. unsigned short sa_family; // address family, AF_xxx
  19. char sa_data[14]; // 14 bytes of protocol address
  20. };
  21. struct in_addr {
  22. unsigned long s_addr; // load with inet_pton()
  23. };
  24. // IPv4 AF_INET sockets:
  25. struct sockaddr_in {
  26. short sin_family; // e.g. AF_INET, AF_INET6
  27. unsigned short sin_port; // e.g. htons(3490)
  28. struct in_addr sin_addr; // see struct in_addr, below
  29. char sin_zero[8]; // zero this if you want to
  30. };
  31. // IPv6 AF_INET6 sockets:
  32. struct in6_addr {
  33. unsigned char s6_addr[16]; // load with inet_pton()
  34. };
  35. struct sockaddr_in6 {
  36. uint16_t sin6_family; // address family, AF_INET6
  37. uint16_t sin6_port; // port number, Network Byte Order
  38. uint32_t sin6_flowinfo; // IPv6 flow information
  39. struct in6_addr sin6_addr; // IPv6 address
  40. uint32_t sin6_scope_id; // Scope ID
  41. };
  42. // General socket address holding structure, big enough to hold either
  43. // struct sockaddr_in or struct sockaddr_in6 data:
  44. #define _SS_MAXSIZE 128
  45. #define _SS_ALIGNSIZE (sizeof(int64_t))
  46. typedef uint32_t sa_family_t;
  47. #define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(sa_family_t))
  48. #define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof(sa_family_t)+ \
  49. _SS_PAD1SIZE + _SS_ALIGNSIZE))
  50. struct sockaddr_storage {
  51. sa_family_t ss_family;
  52. char _ss_pad1[_SS_PAD1SIZE];
  53. int64_t _ss_align;
  54. char _ss_pad2[_SS_PAD2SIZE];
  55. };
  56. #define INADDR_ANY (unsigned long)0x00000000
  57. #define INADDR_LOOPBACK 0x7f000001
  58. #define INADDR_BROADCAST (unsigned long)0xffffffff
  59. #define INADDR_NONE 0xffffffff
  60. /* Supported address families. */
  61. #define AF_UNSPEC 0
  62. #define AF_UNIX 1 /* Unix domain sockets */
  63. #define AF_INET 2 /* Internet IP Protocol */
  64. #define AF_AX25 3 /* Amateur Radio AX.25 */
  65. #define AF_IPX 4 /* Novell IPX */
  66. #define AF_APPLETALK 5 /* Appletalk DDP */
  67. #define AF_NETROM 6 /* Amateur radio NetROM */
  68. #define AF_BRIDGE 7 /* Multiprotocol bridge */
  69. #define AF_AAL5 8 /* Reserved for Werner's ATM */
  70. #define AF_X25 9 /* Reserved for X.25 project */
  71. #define AF_INET6 10 /* IP version 6 */
  72. #define AF_MAX 12 /* For now.. */
  73. #ifndef IPPROTO_IP
  74. #define IPPROTO_IP 0 /* dummy for IP */
  75. #endif
  76. #ifndef IPPROTO_HOPOPTS
  77. #define IPPROTO_HOPOPTS 0 /* IPv6 hop-by-hop options */
  78. #endif
  79. #ifndef IPPROTO_ICMP
  80. #define IPPROTO_ICMP 1 /* control message protocol */
  81. #endif
  82. #ifndef IPPROTO_IGMP
  83. #define IPPROTO_IGMP 2 /* group mgmt protocol */
  84. #endif
  85. #ifndef IPPROTO_IPV4
  86. #define IPPROTO_IPV4 4
  87. #endif
  88. #ifndef IPPROTO_TCP
  89. #define IPPROTO_TCP 6 /* tcp */
  90. #endif
  91. #ifndef IPPROTO_EGP
  92. #define IPPROTO_EGP 8 /* exterior gateway protocol */
  93. #endif
  94. #ifndef IPPROTO_PIGP
  95. #define IPPROTO_PIGP 9
  96. #endif
  97. #ifndef IPPROTO_UDP
  98. #define IPPROTO_UDP 17 /* user datagram protocol */
  99. #endif
  100. #ifndef IPPROTO_DCCP
  101. #define IPPROTO_DCCP 33 /* datagram congestion control protocol */
  102. #endif
  103. #ifndef IPPROTO_IPV6
  104. #define IPPROTO_IPV6 41
  105. #endif
  106. #ifndef IPPROTO_ROUTING
  107. #define IPPROTO_ROUTING 43 /* IPv6 routing header */
  108. #endif
  109. #ifndef IPPROTO_FRAGMENT
  110. #define IPPROTO_FRAGMENT 44 /* IPv6 fragmentation header */
  111. #endif
  112. #ifndef IPPROTO_RSVP
  113. #define IPPROTO_RSVP 46 /* resource reservation */
  114. #endif
  115. #ifndef IPPROTO_GRE
  116. #define IPPROTO_GRE 47 /* General Routing Encap. */
  117. #endif
  118. #ifndef IPPROTO_ESP
  119. #define IPPROTO_ESP 50 /* SIPP Encap Sec. Payload */
  120. #endif
  121. #ifndef IPPROTO_AH
  122. #define IPPROTO_AH 51 /* SIPP Auth Header */
  123. #endif
  124. #ifndef IPPROTO_MOBILE
  125. #define IPPROTO_MOBILE 55
  126. #endif
  127. #ifndef IPPROTO_ICMPV6
  128. #define IPPROTO_ICMPV6 58 /* ICMPv6 */
  129. #endif
  130. #ifndef IPPROTO_NONE
  131. #define IPPROTO_NONE 59 /* IPv6 no next header */
  132. #endif
  133. #ifndef IPPROTO_DSTOPTS
  134. #define IPPROTO_DSTOPTS 60 /* IPv6 destination options */
  135. #endif
  136. /*Standard socket types */
  137. #define SOCK_STREAM 1 /*virtual circuit*/
  138. #define SOCK_DGRAM 2 /*datagram*/
  139. #define SOCK_RAW 3 /*raw socket*/
  140. #define SOCK_RDM 4 /*reliably-delivered message*/
  141. #define SOCK_CONN_DGRAM 5 /*connection datagram*/
  142. inline uint16_t ntohs(uint16_t s) {return (((s>> 8)) | (s << 8));};
  143. inline uint16_t htons(uint16_t s) {return (((s>> 8)) | (s << 8));};
  144. inline uint32_t ntohl(uint32_t s) {return (((s&0x000000FF)<<24)+((s&0x0000FF00)<<8)+((s&0x00FF0000)>>8)+((s&0xFF000000)>>24)); };
  145. inline uint32_t htonl(uint32_t s) {return (((s&0x000000FF)<<24)+((s&0x0000FF00)<<8)+((s&0x00FF0000)>>8)+((s&0xFF000000)>>24)); };
  146. /*
  147. * Level number for (get/set)sockopt() to apply to socket itself.
  148. */
  149. #define SOL_SOCKET 0xffff /* options for socket level */
  150. /*
  151. * Additional options, not kept in so_options.
  152. */
  153. #define SO_SNDBUF 0x1001 /* send buffer size */
  154. #define SO_RCVBUF 0x1002 /* receive buffer size */
  155. #define SO_SNDLOWAT 0x1003 /* send low-water mark */
  156. #define SO_RCVLOWAT 0x1004 /* receive low-water mark */
  157. #define SO_SNDTIMEO 0x1005 /* send timeout */
  158. #define SO_RCVTIMEO 0x1006 /* receive timeout */
  159. #define SO_ERROR 0x1007 /* get error status and clear */
  160. #define SO_TYPE 0x1008 /* get socket type */
  161. /*
  162. * Option flags per-socket.
  163. */
  164. #define SO_DEBUG 0x0001 /* turn on debugging info recording */
  165. #define SO_ACCEPTCONN 0x0002 /* socket has had listen() */
  166. #define SO_REUSEADDR 0x0004 /* allow local address reuse */
  167. #define SO_KEEPALIVE 0x0008 /* keep connections alive */
  168. #define SO_DONTROUTE 0x0010 /* just use interface addresses */
  169. #define SO_BROADCAST 0x0020 /* permit sending of broadcast msgs */
  170. #define SO_USELOOPBACK 0x0040 /* bypass hardware when possible */
  171. #define SO_LINGER 0x0080 /* linger on close if data present */
  172. #define SO_OOBINLINE 0x0100 /* leave received OOB data in line */
  173. #define IP_TOS 1
  174. #define IP_TTL 2
  175. #define IP_HDRINCL 3
  176. #define IP_OPTIONS 4
  177. #define IP_ROUTER_ALERT 5
  178. #define IP_RECVOPTS 6
  179. #define IP_RETOPTS 7
  180. #define IP_PKTINFO 8
  181. #define IP_PKTOPTIONS 9
  182. #define IP_MTU_DISCOVER 10
  183. #define IP_RECVERR 11
  184. #define IP_RECVTTL 12
  185. #define IP_RECVTOS 13
  186. #define IP_MTU 14
  187. #define IP_FREEBIND 15
  188. #ifndef AI_PASSIVE
  189. # define AI_PASSIVE 1
  190. #endif /* AI_PASSIVE */
  191. #define SD_RECEIVE 0
  192. #define SD_SEND 1
  193. #define SD_BOTH 2
  194. struct addrinfo {
  195. int ai_flags;
  196. int ai_family;
  197. int ai_socktype;
  198. int ai_protocol;
  199. size_t ai_addrlen;
  200. struct sockaddr *ai_addr;
  201. char *ai_canonname;
  202. struct addrinfo *ai_next;
  203. };
  204. inline char *inet_ntoa(in_addr in)
  205. {
  206. static char staticBuff[32];
  207. char workingbuff[8];
  208. int part1 = ((in.s_addr & 0xFF000000) >> 24);
  209. int part2 = ((in.s_addr & 0x00FF0000) >> 16);
  210. int part3 = ((in.s_addr & 0x0000FF00) >> 8);
  211. int part4 = ((in.s_addr & 0x000000FF) >> 0);
  212. _itoa(part1,staticBuff,10);
  213. strcat(staticBuff,".");
  214. _itoa(part2,workingbuff,10);
  215. strcat(staticBuff,workingbuff);
  216. strcat(staticBuff,".");
  217. _itoa(part3,workingbuff,10);
  218. strcat(staticBuff,workingbuff);
  219. strcat(staticBuff,".");
  220. _itoa(part4,workingbuff,10);
  221. strcat(staticBuff,workingbuff);
  222. return (char*) staticBuff;
  223. }
  224. };
  225. #endif
粤ICP备19079148号