bsd_socket.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**************************************************************************
  2. * Copyright (c) 2014 by Michael Fischer (www.emb4fun.de).
  3. * All rights reserved.
  4. *
  5. * Some parts are from the original BSD source, therefor:
  6. *
  7. * Partial Copyright (c) 1982, 1986, 1988, 1990, 1993
  8. * The Regents of the University of California. All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the author nor the names of its contributors may
  20. * be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  27. * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  30. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  31. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  33. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. *
  36. ***************************************************************************
  37. * History:
  38. *
  39. * 26.01.2014 mifi First Version
  40. * 27.01.2014 mifi More comments and connect added
  41. * 28.01.2014 mifi Backlog functionality added
  42. **************************************************************************/
  43. #if !defined(__BSD_SOCKET_H__)
  44. #define __BSD_SOCKET_H__
  45. /**************************************************************************
  46. * Includes
  47. **************************************************************************/
  48. #include <stdint.h>
  49. #include <sys/socket.h>
  50. #include <netinet/in.h>
  51. /**************************************************************************
  52. * All Structures and Common Constants
  53. **************************************************************************/
  54. /*
  55. * Error code of the BSD socket api.
  56. */
  57. #define SOCKET_ERROR -1
  58. /*
  59. * Level number for (get/set)sockopt() to apply to socket itself.
  60. */
  61. #define SOL_SOCKET 0xffff /* Options for socket level */
  62. /*
  63. * shutdown "How" options
  64. */
  65. #define SHUT_RD 0
  66. #define SHUT_WR 1
  67. #define SHUT_RDWR 2
  68. /*
  69. * sockaddr
  70. */
  71. struct sockaddr
  72. {
  73. uint8_t sa_len;
  74. uint8_t sa_family;
  75. char sa_data[14];
  76. };
  77. /*
  78. * in_addr
  79. */
  80. struct in_addr
  81. {
  82. uint32_t s_addr;
  83. };
  84. /*
  85. * sockaddr_in
  86. */
  87. struct sockaddr_in
  88. {
  89. uint8_t sin_len;
  90. uint8_t sin_family;
  91. uint16_t sin_port;
  92. struct in_addr sin_addr;
  93. char sin_zero[8];
  94. };
  95. /*
  96. * socket_t
  97. */
  98. typedef int32_t socket_t;
  99. /**************************************************************************
  100. * Macro Definitions
  101. **************************************************************************/
  102. /**************************************************************************
  103. * Funtions Definitions
  104. **************************************************************************/
  105. /*
  106. * The BSD socket functions are grouped in several categories.
  107. * For more information take a look in: "TCP/IP Illustrated, Volume 2"
  108. */
  109. /*
  110. * Setup category
  111. */
  112. socket_t socket (int domain, int type, int proto);
  113. int bind (socket_t sock, struct sockaddr *addr, int addr_len);
  114. /*
  115. * Server category
  116. */
  117. int listen (socket_t sock, int backlog); /* Only backlog of 1 */
  118. socket_t accept (socket_t sock, struct sockaddr *addr, int *addr_len);
  119. /*
  120. * Client category
  121. */
  122. int connect (socket_t sock, struct sockaddr *addr, int addr_len);
  123. /*
  124. * Input category
  125. */
  126. int recv (socket_t sock, void *data, size_t len, int flags);
  127. int recvfrom (socket_t sock, void *data, size_t len, int flags, struct sockaddr *addr, int *addr_len);
  128. /*
  129. * Output category
  130. */
  131. int send (socket_t sock, void *data, size_t len, int flags);
  132. int sendto (socket_t sock, void *data, size_t len, int flags, struct sockaddr *addr, int addr_len);
  133. /*
  134. * Termination category
  135. */
  136. int closesocket (socket_t sock);
  137. int shutdown (socket_t sock, int how); /* Dummy only */
  138. /*
  139. * Administration category
  140. */
  141. int getsockname (socket_t sock, struct sockaddr *addr, int *addr_len);
  142. int getpeername (socket_t sock, struct sockaddr *addr, int *addr_len);
  143. int getsockopt (socket_t sock, int level, int optname, void *optval, int optlen);
  144. int setsockopt (socket_t sock, int level, int optname, void *optval, int optlen);
  145. #endif /* !__BSD_SOCKET_H__ */
  146. /*** EOF ***/