dclib-network-linux.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /***************************************************************************
  2. * *
  3. * _____ ____ *
  4. * | __ \ / __ \ _ _ _____ *
  5. * | | \ \ / / \_\ | | | | _ \ *
  6. * | | \ \| | | | | | |_| | *
  7. * | | | || | | | | | ___/ *
  8. * | | / /| | __ | | | | _ \ *
  9. * | |__/ / \ \__/ / | |___| | |_| | *
  10. * |_____/ \____/ |_____|_|_____/ *
  11. * *
  12. * Wiimms source code library *
  13. * *
  14. ***************************************************************************
  15. * *
  16. * Copyright (c) 2012-2022 by Dirk Clemens <wiimm@wiimm.de> *
  17. * *
  18. ***************************************************************************
  19. * *
  20. * This library is free software; you can redistribute it and/or modify *
  21. * it under the terms of the GNU General Public License as published by *
  22. * the Free Software Foundation; either version 2 of the License, or *
  23. * (at your option) any later version. *
  24. * *
  25. * This library is distributed in the hope that it will be useful, *
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  28. * GNU General Public License for more details. *
  29. * *
  30. * See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt *
  31. * *
  32. ***************************************************************************/
  33. #ifndef DCLIB_NETWORK_LINUX_H
  34. #define DCLIB_NETWORK_LINUX_H 1
  35. #ifndef DCLIB_NETWORK_H
  36. #include "dclib-network.h"
  37. #endif
  38. //
  39. ///////////////////////////////////////////////////////////////////////////////
  40. /////////////// IP/UDP ///////////////
  41. ///////////////////////////////////////////////////////////////////////////////
  42. ssize_t ReceiveUDPv4
  43. (
  44. int sock, // valid socket
  45. void *buf, // buffer tostore incoming data
  46. size_t buf_size, // size of buffer
  47. int flags, // flags for recffrom() or recvmsg()
  48. struct sockaddr_in *src_addr, // not NULL: stire source address
  49. in_addr_t *dest_addr // not NULL: store destination IPv4 (NBO)
  50. );
  51. ///////////////////////////////////////////////////////////////////////////////
  52. ssize_t SendUDPv4
  53. (
  54. int sock, // valid socket
  55. const void *data, // data to send
  56. size_t data_size, // size of data
  57. int flags, // flags for sendto() or sendmsg()
  58. struct sockaddr_in *dest_addr, // destination address (NULL possible)
  59. in_addr_t src_addr // not 0: source IPv4 (NBO)
  60. );
  61. ///////////////////////////////////////////////////////////////////////////////
  62. ssize_t recvfrom2
  63. (
  64. int sock,
  65. void *buf,
  66. size_t buf_size,
  67. int flags,
  68. struct sockaddr *src_addr,
  69. socklen_t *src_addrlen,
  70. struct sockaddr *dest_addr,
  71. socklen_t *dest_addrlen
  72. );
  73. //-----------------------------------------------------------------------------
  74. ssize_t sendto2
  75. (
  76. int sock,
  77. const void *buf,
  78. size_t buf_size,
  79. int flags,
  80. struct sockaddr *dest_addr,
  81. socklen_t dest_addrlen,
  82. struct sockaddr_in *src_addr,
  83. socklen_t src_addrlen
  84. );
  85. //
  86. ///////////////////////////////////////////////////////////////////////////////
  87. /////////////// SendRawUDP() ///////////////
  88. ///////////////////////////////////////////////////////////////////////////////
  89. enumError SendRawUDP
  90. (
  91. // Capability CAP_NET_RAW needed (or effective user ID of 0).
  92. int sock, // RAW socket to use,
  93. // if -1: open and close private socket
  94. ccp send_addr, // NULL or sender address:port
  95. u32 send_ip4, // fall back IP4, if 'send_addr' empty
  96. u16 send_port, // default sender port
  97. ccp recv_addr, // NULL or receiver address:port
  98. u32 recv_ip4, // fall back IP4, if 'recv_addr' empty
  99. u16 recv_port, // default receiver port
  100. const void *data, // data to send
  101. uint size, // size of 'data'
  102. uint log_mode // 0:silent, >0:print errors,
  103. // >=0x10: hexdump data, 'lmode' bytes max
  104. );
  105. //-----------------------------------------------------------------------------
  106. enumError SendRawUDPsa
  107. (
  108. // Capability CAP_NET_RAW needed (or effective user ID of 0).
  109. int sock, // RAW socket to use,
  110. // if -1: open and close private socket
  111. struct sockaddr_in *sa_send, // sockaddr of sender
  112. struct sockaddr_in *sa_recv, // sockaddr of receiver
  113. const void *data, // data to send
  114. uint size, // size of 'data'
  115. uint log_mode // 0:silent, >0:print errors,
  116. // >=0x10: hexdump data, 'lmode' bytes max
  117. );
  118. //-----------------------------------------------------------------------------
  119. uint SetupRawUDPsa
  120. (
  121. // returns total packet len (mabe with limited 'size')
  122. udp_packet_t *pkt, // paket header to setup (cleared and written)
  123. struct sockaddr_in *sa_send, // sockaddr of sender
  124. struct sockaddr_in *sa_recv, // sockaddr of receiver
  125. const void *data, // data to send
  126. uint size // size of 'data'
  127. );
  128. //
  129. ///////////////////////////////////////////////////////////////////////////////
  130. /////////////// Routing Support ///////////////
  131. ///////////////////////////////////////////////////////////////////////////////
  132. // all IP4 in network byte order
  133. typedef struct RouteIP4_t
  134. {
  135. //--- returned data
  136. int index; // entry index, starts with 0, incremented for each found
  137. // -1: invalid data
  138. char *iface; // pointer into 'buf': interface name
  139. u32 dest; // destination address, network byte order
  140. u32 mask; // destination network mask, network byte order
  141. u32 gate; // gateway address, network byte order
  142. u32 flags; // routing flags
  143. //--- internal data
  144. FILE *f; // open file
  145. uint col_iface; // column index for 'iface'
  146. uint col_dest; // column index for 'dest'
  147. uint col_mask; // column index for 'mask'
  148. uint col_gate; // column index for 'gate'
  149. uint col_flags; // column index for 'flags'
  150. char buf[200]; // line buffer for /proc/net/route
  151. }
  152. RouteIP4_t;
  153. ///////////////////////////////////////////////////////////////////////////////
  154. bool OpenRouteScanIP4
  155. (
  156. // returns TRUE on success and FALSE if no element is found
  157. RouteIP4_t *rt // valid data, will be initialized
  158. );
  159. bool NextRouteScanIP4
  160. (
  161. // returns TRUE on success and FALSE if no more element is found
  162. RouteIP4_t *rt // valid data
  163. );
  164. void CloseRouteScanIP4
  165. (
  166. RouteIP4_t *rt // valid data
  167. );
  168. ///////////////////////////////////////////////////////////////////////////////
  169. bool FindGatewayIP4
  170. (
  171. // returns TRUE if gateway found
  172. RouteIP4_t *rt, // valid data, will be initialized
  173. u32 ip4 // search gateway for this address (network byte order)
  174. );
  175. u32 GetIP4ByInterface
  176. ( // return 0 or IP4 in network byte order
  177. ccp iface // interface name. If NULL: Use default gateway
  178. );
  179. //
  180. ///////////////////////////////////////////////////////////////////////////////
  181. /////////////// E N D ///////////////
  182. ///////////////////////////////////////////////////////////////////////////////
  183. #endif // DCLIB_NETWORK_LINUX_H