dclib-network.h 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868
  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_H
  34. #define DCLIB_NETWORK_H 1
  35. #include "dclib-debug.h"
  36. #include "dclib-system.h"
  37. #include "dclib-basics.h"
  38. #include "dclib-file.h"
  39. #include <stddef.h>
  40. //
  41. ///////////////////////////////////////////////////////////////////////////////
  42. /////////////// definitions ///////////////
  43. ///////////////////////////////////////////////////////////////////////////////
  44. #define GOOD_SOCKADR_SIZE sizeof(struct sockaddr_un)
  45. //
  46. ///////////////////////////////////////////////////////////////////////////////
  47. /////////////// host name support ///////////////
  48. ///////////////////////////////////////////////////////////////////////////////
  49. // [[NetworkHost_t]]
  50. typedef struct NetworkHost_t
  51. {
  52. bool ip4_valid; // true: 'ip4' is valid
  53. u32 ip4; // ip4 or 0 if invalid
  54. int port; // port number, <0: invalid
  55. ccp name; // NULL or pointer to canonicalized name
  56. struct sockaddr_in sa; // setup for AF_INET if 'ip4_valid'
  57. ccp filename; // not NULL: unix filename detected
  58. // in this case: ip4_valid := FALSE
  59. mem_t not_scanned; // (0,0) or not by ResolveHost*() scanned name
  60. }
  61. NetworkHost_t;
  62. //-----------------------------------------------------------------------------
  63. static inline void InitializeHost ( NetworkHost_t *host )
  64. { memset(host,0,sizeof(*host)); }
  65. static inline void ResetHost ( NetworkHost_t *host )
  66. { FREE((char*)host->name); memset(host,0,sizeof(*host)); }
  67. static inline ccp GetHostName ( NetworkHost_t *host )
  68. { return host->name ? host->name : PrintIP4(0,0,host->ip4,-1); }
  69. //-----------------------------------------------------------------------------
  70. bool ResolveHost
  71. (
  72. // returns TRUE (=ip4_valid), if a hostname is detected
  73. // if result is FALSE => check host->filename
  74. NetworkHost_t *host, // valid pointer to NetworkHost_t
  75. bool init_host, // true: initialize 'host'
  76. ccp name, // name to analyze
  77. int default_port, // use this as port, if no other is found
  78. bool check_filename, // true: check for unix filename
  79. bool store_name // true: setup 'host->name'
  80. );
  81. bool ResolveHostMem
  82. (
  83. // returns TRUE (=ip4_valid), if a hostname is detected
  84. // if result is FALSE => check host->filename
  85. NetworkHost_t *host, // valid pointer to NetworkHost_t
  86. bool init_host, // true: initialize 'host'
  87. mem_t name, // name to analyze
  88. int default_port, // use this as port, if no other is found
  89. bool check_filename, // true: check for unix filename
  90. bool store_name // true: setup 'host->name'
  91. );
  92. //
  93. ///////////////////////////////////////////////////////////////////////////////
  94. /////////////// enum PrintModeIP_t ///////////////
  95. ///////////////////////////////////////////////////////////////////////////////
  96. // [[PrintModeIP_t]]
  97. typedef enum PrintModeIP_t
  98. {
  99. // characters for ScanPrintModeIP(): nria
  100. PMIP_NEVER = 0x000, ///< '/Bits' unterdrücken.
  101. PMIP_RELEVANT = 0x001, ///< '/Bits' nur falls relevant.
  102. PMIP_IF_SET = 0x002, ///< '/Bits' nur falls gesetzt.
  103. PMIP_ALWAYS = 0x003, ///< '/Bits' immer ausgeben.
  104. PMIP_M_BITS = 0x003, ///< Maske für Bit-Modi.
  105. // characters for ScanPrintModeIP(): psmb
  106. PMIP_PORT = 0x004, ///< Flag: Print port if >0.
  107. PMIP_SERVICE = 0x008, ///< Flag: Print service instead of port number.
  108. PMIP_MASK = 0x010, ///< Flag: Print netmask instead of bits for IPv4.
  109. PMIP_BRACKETS = 0x020, ///< Flag: Print bracktes if [IPv6].
  110. // characters for ScanPrintModeIP(): f
  111. PMIP_FULL_IPV6 = 0x100, ///< Print IPv6 without short numbers.
  112. // characters for ScanPrintModeIP(): 0123
  113. PMIP_0DOTS = 0x200, ///< Print IPv4 as single number (highest priority).
  114. PMIP_1DOT = 0x400, ///< Print IPv4 as "1.2" (A-Class notation).
  115. PMIP_2DOTS = 0x800, ///< Print IPv4 as "1.2.3" (B-Class notation).
  116. PMIP_3DOTS = 0, ///< Print IPv4 as "1.2.3.4" (C-Class notation, default).
  117. PMIP_M_DOTS = 0xc00 ///< Maske für Dot-Modi.
  118. }
  119. PrintModeIP_t;
  120. PrintModeIP_t ScanPrintModeIP ( PrintModeIP_t base, ccp arg );
  121. ccp PrintPrintModeIP ( PrintModeIP_t val, bool align );
  122. //
  123. ///////////////////////////////////////////////////////////////////////////////
  124. /////////////// enum IPClass_t ///////////////
  125. ///////////////////////////////////////////////////////////////////////////////
  126. // [[IPClass_t]]
  127. typedef enum IPClass_t
  128. {
  129. IPCL_INVALID, // 0.0.0.0 ::
  130. IPCL_LOOPBACK, // 127.0/8 ::1
  131. IPCL_LOCAL, // 10.0/8 172.16/12 192.168/16 169.254/16 fe80::/10
  132. IPCL_STANDARD, // * *
  133. IPCL_SPECIAL, // 224.0.0.0/3 f00::/8
  134. IPCL__N,
  135. }
  136. IPClass_t;
  137. //-----------------------------------------------------------------------------
  138. ccp GetIPClassColor ( const ColorSet_t *colset, IPClass_t ipc );
  139. //
  140. ///////////////////////////////////////////////////////////////////////////////
  141. /////////////// struct SplitIP_t ///////////////
  142. ///////////////////////////////////////////////////////////////////////////////
  143. // [[SplitIP_t]]
  144. typedef struct SplitIP_t
  145. {
  146. int bits;
  147. u16 port;
  148. char name[260];
  149. }
  150. SplitIP_t;
  151. //-----------------------------------------------------------------------------
  152. bool SplitIP ( SplitIP_t *sip, ccp addr );
  153. static inline void NormalizeNameSIP ( SplitIP_t *sip )
  154. { DASSERT(sip); NormalizeIP4(sip->name,sizeof(sip->name),true,sip->name,-1); }
  155. //
  156. ///////////////////////////////////////////////////////////////////////////////
  157. /////////////// struct NamesIP_t ///////////////
  158. ///////////////////////////////////////////////////////////////////////////////
  159. // [[NamesIP_t]]
  160. typedef struct NamesIP_t
  161. {
  162. ccp binaddr;
  163. ccp ipvers;
  164. ccp addr1;
  165. ccp addr2;
  166. ccp bits;
  167. ccp mask1;
  168. ccp mask2;
  169. }
  170. NamesIP_t;
  171. //-----------------------------------------------------------------------------
  172. // reset if src==NULL
  173. void SetupNamesIP ( NamesIP_t *dest, const NamesIP_t *src );
  174. void SetupNamesByListIP ( NamesIP_t *dest, const exmem_list_t *src );
  175. ccp PrintNamesIP ( const NamesIP_t *nip );
  176. //
  177. ///////////////////////////////////////////////////////////////////////////////
  178. /////////////// struct BinIP_t ///////////////
  179. ///////////////////////////////////////////////////////////////////////////////
  180. enum
  181. {
  182. IP_SIZE_ADDR = 39, ///< Maximale Zeichenlänge einer Adresse ohne Maske.
  183. IP_SIZE_ADDR_MASK = 43, ///< Maximale Zeichenlänge einer Adresse mit Maske.
  184. IP_SIZE_PREFIX = 22, ///< Maximale Zeichenlänge einer Adresse, die mit MaskPrefixMIP() gekürzt wurde.
  185. IP_SIZE_ADDR_BIN = 22, ///< Size für eine binäre IP (ohne Maske).
  186. IP_SIZE_BINIP_C = 20, ///< Size für binäres Tupel (padding,ipvers,bits,addr).
  187. IP_SIZE_BINIP = 18, ///< Size für binäres Tupel (ipvers,bits,addr).
  188. IP_OFFSET_BINIP = 2, ///< Offset für DB-Transfer.
  189. };
  190. ///////////////////////////////////////////////////////////////////////////////
  191. // [[BinIP_t]]
  192. typedef struct BinIP_t
  193. {
  194. u16 user_id; // any id defined by user
  195. u8 ipvers; // 0:invalid, 4:IPv4, 6:IPv6
  196. u8 bits; // number of 1-bits in netmask
  197. union // different views of ip address
  198. {
  199. u8 ip6_8[16];
  200. u16 ip6_16[8];
  201. u32 ip6_32[4];
  202. u64 ip6_64[2];
  203. ipv6_t ip6;
  204. struct
  205. {
  206. ipv4_t ip4_padding[3];
  207. ipv4_t ip4;
  208. };
  209. struct
  210. {
  211. ipv4_t ip4x_padding[3];
  212. ipv4x_t ip4x;
  213. };
  214. };
  215. }
  216. __attribute__ ((packed)) BinIP_t;
  217. _Static_assert( offsetof(BinIP_t,ipvers) == IP_OFFSET_BINIP, "wrong order of BinIP_t members" );
  218. _Static_assert( sizeof(BinIP_t) == IP_SIZE_BINIP_C, "wrong size of BinIP_t" );
  219. //-----------------------------------------------------------------------------
  220. static inline u8 * GetCorePtrBIP ( const BinIP_t *src ) { return (u8*)&src->ipvers; }
  221. static inline uint GetCoreSizeBIP ( const BinIP_t *src ) { return IP_SIZE_BINIP; }
  222. BinIP_t GetIPMask ( const BinIP_t *src );
  223. void ApplyIPMask ( BinIP_t *bip );
  224. IPClass_t GetIPClassBIP ( const BinIP_t * bip );
  225. static inline ccp GetColorBIP( const ColorSet_t *colset, const BinIP_t * bip )
  226. { return GetIPClassColor(colset,GetIPClassBIP(bip)); }
  227. ccp PrintBIP ( const BinIP_t * bip );
  228. // returns false, if !a1 || !a2 || invalid || differ_after_mask
  229. // use_mask: bit 0 == 1 : use mask of a1; bit 1 == 2 : use mask of a2
  230. bool IsSameBIP ( const BinIP_t *a1, const BinIP_t *a2, uint use_mask );
  231. //
  232. ///////////////////////////////////////////////////////////////////////////////
  233. /////////////// struct BinIPList_t ///////////////
  234. ///////////////////////////////////////////////////////////////////////////////
  235. // [[BinIPItem_t]]
  236. typedef struct BinIPItem_t
  237. {
  238. ccp name; // optional name, alloced
  239. BinIP_t bip; // IP data
  240. }
  241. BinIPItem_t;
  242. ///////////////////////////////////////////////////////////////////////////////
  243. // [[BinIPList_t]]
  244. typedef struct BinIPList_t
  245. {
  246. BinIPItem_t *list; // list of elements
  247. uint used; // number of used elements in 'list'
  248. uint size; // number of allocated elements in 'list'
  249. }
  250. BinIPList_t;
  251. ///////////////////////////////////////////////////////////////////////////////
  252. // [[BinIPIterate_t]]
  253. typedef struct BinIPIterate_t
  254. {
  255. bool valid; // iterator is valid
  256. const BinIPList_t *bil; // list data
  257. const BinIPItem_t *cur; // current element
  258. BinIP_t addr; // address to serch
  259. uint use_mask; // flags for IsSameBIP()
  260. }
  261. BinIPIterate_t;
  262. ///////////////////////////////////////////////////////////////////////////////
  263. static inline void InitializeBIL ( BinIPList_t *bil )
  264. { DASSERT(bil); memset(bil,0,sizeof(*bil)); }
  265. void ResetBIL ( BinIPList_t *bil );
  266. void ClearBIL ( BinIPList_t *bil );
  267. void SetSizeBIL ( BinIPList_t *bil, int size );
  268. void SetMinSizeBIL ( BinIPList_t *bil, int min_size );
  269. BinIPItem_t * CreateItemBIL ( BinIPList_t *bil, int n_item );
  270. #if 0 // not implemented
  271. BinIPItem_t * InsertBIL ( BinIPList_t *bil, int index, ccp key, CopyMode_t copy_mode );
  272. BinIPItem_t * InsertByKeyBIL ( BinIPList_t *bil, ccp key, CopyMode_t copy_mode );
  273. static inline BinIPItem_t * Append ( BinIPList_t *bil, int index, ccp key, CopyMode_t copy_mode )
  274. { return InsertBIL(bil,INT_MAX,key,copy_mode); }
  275. int FindIndexBIL ( const BinIPList_t *bil, ccp key );
  276. #endif
  277. ///////////////////////////////////////////////////////////////////////////////
  278. // bits for use_mask:
  279. // bit 0 == 1 : use mask of BinIPList_t::items
  280. // bit 1 == 2 : use mask of addr
  281. BinIPIterate_t FindAddressBIL ( const BinIPList_t *bil, BinIP_t *addr, uint use_mask );
  282. void FindNextAddressBIL ( BinIPIterate_t *iter );
  283. uint ScanInterfaceAddresses ( BinIPList_t *bil, bool init_bil );
  284. const BinIPList_t * GetInterfaceAddressList ( bool update );
  285. bool IsLocalBIL ( BinIP_t *addr, uint use_mask );
  286. static inline bool IsLocalAddress ( BinIP_t *addr ) { return IsLocalBIL(addr,2); }
  287. static inline bool IsLocalNetwork ( BinIP_t *addr ) { return IsLocalBIL(addr,3); }
  288. //
  289. ///////////////////////////////////////////////////////////////////////////////
  290. /////////////// struct ManageIP_t ///////////////
  291. ///////////////////////////////////////////////////////////////////////////////
  292. // [[ManageIP_t]]
  293. typedef struct ManageIP_t
  294. {
  295. BinIP_t bin; // binary data
  296. u16 port; // 0: not specified
  297. bool have_bits; // true: bits were specified on input
  298. u8 relevant_bits; // number of relevant bits
  299. ccp name; // NULL or getnameinfo(), alloced
  300. ccp decoded_name; // NULL or punycode-decoded 'name' if differ, alloced
  301. }
  302. ManageIP_t;
  303. ///////////////////////////////////////////////////////////////////////////////
  304. static inline bool IsIPv4MIP ( const ManageIP_t *mip )
  305. { return mip && mip->bin.ipvers == 4; }
  306. static inline bool IsIPv6MIP ( const ManageIP_t *mip )
  307. { return mip && mip->bin.ipvers == 6; }
  308. //-----------------------------------------------------------------------------
  309. // if bits==-1: ignore parameter
  310. void ResetMIP ( ManageIP_t *mip ); // mip can be NULL
  311. void ClearNameMIP ( ManageIP_t *mip ); // mip can be NULL
  312. bool AssignMIP ( ManageIP_t *dest, const ManageIP_t *src );
  313. bool AssignBinMIP ( ManageIP_t *mip, cvp bin, int bits );
  314. bool AssignSockaddrMIP ( ManageIP_t *mip, cvp sockaddr, u16 port, int bits );
  315. bool ScanMIP ( ManageIP_t *mip, ccp addr );
  316. ManageIP_t GetCopyMIP ( const ManageIP_t *mip );
  317. ManageIP_t GetSockNameMIP ( int sock );
  318. ManageIP_t GetPeerNameMIP ( int sock );
  319. // protocol: protocol to use: 0=all, -1=default (IPPROTO_TCP at the moment)
  320. ManageIP_t GetAddrInfoMIP ( ccp addr, int protocol, IpMode_t ipm );
  321. sockaddr_in46_t GetSockaddrMIP ( const ManageIP_t *mip );
  322. ccp GetNameInfoMIP ( ManageIP_t *mip );
  323. void ApplyMaskMIP ( ManageIP_t *mip, int bits );
  324. void MaskPrefixMIP ( ManageIP_t *mip ); // includes ApplyMaskMIP()
  325. //-----------------------------------------------------------------------------
  326. static inline bool PrintBitsMIP ( const ManageIP_t *mip, PrintModeIP_t pmode )
  327. {
  328. DASSERT(mip);
  329. pmode &= PMIP_M_BITS;
  330. return pmode > PMIP_NEVER
  331. && ( mip->bin.bits < mip->relevant_bits
  332. || pmode == PMIP_IF_SET && mip->have_bits
  333. || pmode >= PMIP_ALWAYS
  334. );
  335. }
  336. ccp PrintAddrMIP ( const ManageIP_t *mip, PrintModeIP_t pmode );
  337. ccp PrintVersAddrMIP ( const ManageIP_t *mip, PrintModeIP_t pmode );
  338. static inline IPClass_t GetIPClassMIP ( const ManageIP_t * mip )
  339. { DASSERT(mip); return GetIPClassBIP(&mip->bin); }
  340. //-----------------------------------------------------------------------------
  341. ccp GetSqlSetAddrMIP ( const ManageIP_t *mip, const NamesIP_t *names );
  342. ccp GetSqlSetMaskMIP ( const ManageIP_t *mip, const NamesIP_t *names );
  343. ccp GetSqlSetBinMIP ( const ManageIP_t *mip, const NamesIP_t *names );
  344. ccp GetSqlCondAddrMIP ( const ManageIP_t *mip, const NamesIP_t *names );
  345. ccp GetSqlCondMaskMIP ( const ManageIP_t *mip, const NamesIP_t *names );
  346. ccp GetSqlCondBinMaskMIP ( const ManageIP_t *mip, const NamesIP_t *names );
  347. static inline ccp GetSqlCondBinAddrMIP ( const ManageIP_t *mip, const NamesIP_t *names )
  348. { return GetSqlSetBinMIP(mip,names); }
  349. //
  350. ///////////////////////////////////////////////////////////////////////////////
  351. /////////////// struct ResolveIP_t ///////////////
  352. ///////////////////////////////////////////////////////////////////////////////
  353. // [[ResolveIP_t]]
  354. typedef struct ResolveIP_t
  355. {
  356. IpMode_t ip_mode; // used mode
  357. ManageIP_t mip4; // resolved IPv4
  358. ManageIP_t mip6; // resolved IPv6
  359. ManageIP_t *mip_list[3]; // ordered and 0-termiated list of valid 'mip*'
  360. }
  361. ResolveIP_t;
  362. //-----------------------------------------------------------------------------
  363. void ResetRIP ( ResolveIP_t *rip ); // rip can be NULL
  364. void ClearNamesRIP ( ResolveIP_t *rip ); // rip can be NULL
  365. ResolveIP_t GetCopyRIP ( const ResolveIP_t *rip );
  366. void AssignRIP ( ResolveIP_t *dest, const ResolveIP_t *src );
  367. int ScanRIP
  368. (
  369. // return 0..2 = number of records
  370. ResolveIP_t *rip, // valid data, ResetRIP()
  371. ccp addr, // address to scan
  372. int protocol, // protocol to use: 0=all, -1=default (IPPROTO_TCP at the moment)
  373. IpMode_t ip_mode, // IPv4 and/or IPV6, order of 'mip_list'
  374. bool get_names // true: call GetNameInfoMIP()
  375. );
  376. // not implemented yet
  377. // void DumpRIP ( FILE *f, int indent, const ResolveIP_t *rip );
  378. //
  379. ///////////////////////////////////////////////////////////////////////////////
  380. /////////////// struct AllowIP4_t ///////////////
  381. ///////////////////////////////////////////////////////////////////////////////
  382. // standard values
  383. #define ALLOW_MODE_DENY 0 // access denied
  384. #define ALLOW_MODE_ALLOW 0x0001 // access allowed
  385. #define ALLOW_MODE_AKEY 0x0002 // access allowed by access key
  386. // locations
  387. #define ALLOW_MODE_EXTERN 0x0004 // keyword 'EXTERN' set
  388. #define ALLOW_MODE_LAN 0x0008 // keyword 'LAN' set
  389. #define ALLOW_MODE_LOCAL 0x0010 // keyword 'LOCAL' set
  390. // users
  391. #define ALLOW_MODE_PUBLIC 0x0020 // keyword 'PUBLIC' set
  392. #define ALLOW_MODE_USER 0x0040 // keyword 'USER' set
  393. #define ALLOW_MODE_MOD 0x0080 // keyword 'MOD' or 'MODERATOR' set
  394. #define ALLOW_MODE_ADMIN 0x0100 // keyword 'ADMIN' or 'ADMINISTRATOR' set
  395. #define ALLOW_MODE_DEVELOP 0x0200 // keyword 'DEVELOP' or 'DEVELOPER' set
  396. // logging
  397. #define ALLOW_MODE_LOG 0x0400 // keyword 'LOG' set
  398. #define ALLOW_MODE_VERBOSE 0x0800 // keyword 'VERBOSE' set
  399. // context
  400. #define ALLOW_RELEASE 0x1000 // accept this record if neither DEBUG nor IS_DEVELOP is set
  401. #define ALLOW_DEBUG 0x2000 // accept this record if DEBUG or IS_DEVELOP is set
  402. // special processing values
  403. #define ALLOW_MODE_NOT 0x1000000000000000ull // negate bits
  404. #define ALLOW_MODE_SET 0x2000000000000000ull // set bits, ignore AND
  405. #define ALLOW_MODE_AND 0x4000000000000000ull // use AND operation (otherwise OR)
  406. #define ALLOW_MODE_CONTINUE 0x8000000000000000ull // continue search (otherwise break)
  407. #define ALLOW_MODE__OP 0x6000000000000000ull // mask for operation
  408. #define ALLOW_MODE__MASK 0x0fffffffffffffffull // mask out control bits
  409. //-----------------------------------------------------------------------------
  410. extern const KeywordTab_t AllowIP4KeyTable[];
  411. //-----------------------------------------------------------------------------
  412. // [[AllowIP4Item_t]]
  413. typedef struct AllowIP4Item_t
  414. {
  415. u32 addr; // IP address
  416. u32 mask; // netmask
  417. u64 mode; // new mode, see ALLOW_MODE_*
  418. u64 count; // incremented on IP match
  419. }
  420. AllowIP4Item_t;
  421. //-----------------------------------------------------------------------------
  422. // [[AllowIP4_t]]
  423. typedef struct AllowIP4_t
  424. {
  425. AllowIP4Item_t *list; // list of addresses, alloced
  426. uint used; // number of used elements
  427. uint size; // number of alloced elements
  428. int ref_counter; // reference counter
  429. // for automatic usage
  430. u64 fallback_mode; // mode if IP not found
  431. u64 allow_mode; // allow, if any of these bits is set
  432. }
  433. AllowIP4_t;
  434. ///////////////////////////////////////////////////////////////////////////////
  435. void InitializeAllowIP4 ( AllowIP4_t *ai );
  436. void ResetAllowIP4 ( AllowIP4_t *ai );
  437. void ClearAllowIP4 ( AllowIP4_t *ai );
  438. // if ai==NULL: create a new ai
  439. AllowIP4_t * NewReferenceAllowIP4 ( AllowIP4_t *ai );
  440. //returns always NULL
  441. AllowIP4_t * DeleteReferenceAllowIP4 ( AllowIP4_t *ai );
  442. void DumpAllowIP4
  443. (
  444. FILE *f, // NULL or output file
  445. int indent, // indention
  446. const AllowIP4_t *ai, // NULL or source
  447. const KeywordTab_t *keytab, // NULL or key table for output
  448. ccp title, // not NULL: Add title
  449. bool print_tab_head // true: add table headings and separators
  450. );
  451. //-----------------------------------------------------------------------------
  452. enumError ScanLineAllowIP4
  453. (
  454. AllowIP4_t *ai, // valid structure; new elements are appended
  455. mem_t line, // single line to analyse
  456. const KeywordTab_t *tab // NULL or keyword table.
  457. // If NULL, a local table with keywords 0 DENY
  458. // ALLOW SET REMOVE AND OR and CONTINUE is used.
  459. );
  460. enumError ScanFileAllowIP4
  461. (
  462. AllowIP4_t *ai, // valid structure; new elements are appended
  463. ccp fname, // ffilename of the source
  464. FileMode_t fmode, // flags for OpenFile()
  465. const KeywordTab_t *tab // NULL or keyword table -> see ScanLineAllowIP4()
  466. );
  467. //-----------------------------------------------------------------------------
  468. void ResetCountersAllowIP4
  469. (
  470. AllowIP4_t *ai // NULL or dest for addition
  471. );
  472. void AddCountersAllowIP4
  473. (
  474. AllowIP4_t *dest, // NULL or dest for addition
  475. const AllowIP4_t *src // NULL or source for addition
  476. );
  477. //-----------------------------------------------------------------------------
  478. // if found, result is masked by ALLOWIP4_MODE__MASK
  479. u64 GetAllowIP4ByAddr ( const AllowIP4_t *ai, u32 addr, u64 if_not_found );
  480. u64 GetAllowIP4ByName ( const AllowIP4_t *ai, mem_t name, u64 if_not_found );
  481. // if ai==NULL => return ALLOW_MODE_ALLOW
  482. // if found, result is masked by ALLOWIP4_MODE__MASK and by 'ai->allow_mode'
  483. // if not found, 'ai->fallback_mode' is returned.
  484. u64 GetAutoAllowIP4ByAddr ( const AllowIP4_t *ai, u32 addr );
  485. u64 GetAutoAllowIP4ByName ( const AllowIP4_t *ai, mem_t name );
  486. u64 GetAutoAllowIP4BySock ( const AllowIP4_t *ai, int sock );
  487. //-----------------------------------------------------------------------------
  488. void SaveCurrentStateAllowIP4
  489. (
  490. FILE *f, // output file
  491. ccp section, // not NULL: create own section
  492. ccp name_prefix, // NULL or prefix of name
  493. uint tab_pos, // tab pos of '='
  494. const AllowIP4_t *ai // valid struct
  495. );
  496. //-----------------------------------------------------------------------------
  497. bool ScanAllowIP4Item
  498. (
  499. // return true on success; counter is optional
  500. AllowIP4Item_t *it, // store data here, cleared first
  501. ccp line // NULL or line to scan
  502. );
  503. void RestoreStateAllowIP4
  504. (
  505. RestoreState_t *rs, // info data, can be modified (cleaned after call)
  506. cvp user_table // pointer provided by RestoreStateTab_t[]
  507. );
  508. void RestoreStateAllowIP4Ex
  509. (
  510. RestoreState_t *rs, // info data, modified
  511. AllowIP4_t *ai, // valid struct
  512. ccp name_prefix // NULL or prefix of name
  513. );
  514. //
  515. ///////////////////////////////////////////////////////////////////////////////
  516. /////////////// TCP + UDP connections ///////////////
  517. ///////////////////////////////////////////////////////////////////////////////
  518. int ConnectByHost
  519. (
  520. NetworkHost_t *host, // valid pointer to NetworkHost_t
  521. int type, // AF_INET type, e.g. SOCK_STREAM, SOCK_DGRAM
  522. int protocol, // AF_INET protocol, e.g. IPPROTO_TCP, IPPROTO_UDP
  523. bool silent // true: suppress error messages
  524. );
  525. ///////////////////////////////////////////////////////////////////////////////
  526. int ConnectByHostTCP
  527. (
  528. NetworkHost_t *host, // valid pointer to NetworkHost_t
  529. bool silent // true: suppress error messages
  530. );
  531. int ConnectTCP
  532. (
  533. ccp name, // optional service + server + optional host
  534. // e.g.: "service://host:port/....."
  535. int default_host, // default host
  536. bool silent // true: suppress error messages
  537. );
  538. int ConnectNumericTCP
  539. (
  540. // like ConnectTCP(), but without name resolution (only numeric ip+port)
  541. ccp addr, // TCP address: ['tcp':] IPv4 [:PORT]
  542. int default_port, // NULL or default port, if not found in 'addr'
  543. bool silent // true: suppress error messages
  544. );
  545. extern int (*ConnectTCP_Hook)
  546. (
  547. // if set: ConnectNumericTCP() uses ConnectTCP()
  548. ccp addr, // TCP address: ['tcp':] IPv4 [:PORT]
  549. int default_port, // NULL or default port, if not found in 'addr'
  550. bool silent // true: suppress error messages
  551. );
  552. void Setup_ConnectTCP_Hook ( bool force );
  553. int ConnectUnixTCP
  554. (
  555. ccp fname, // unix socket filename
  556. bool silent // true: suppress error messages
  557. );
  558. ///////////////////////////////////////////////////////////////////////////////
  559. int ConnectByHostUDP
  560. (
  561. NetworkHost_t *host, // valid pointer to NetworkHost_t
  562. bool silent // true: suppress error messages
  563. );
  564. int ConnectUDP
  565. (
  566. ccp name, // optional service + server + optional host
  567. // e.g.: "service://host:port/....."
  568. int default_host, // default host
  569. bool silent // true: suppress error messages
  570. );
  571. ///////////////////////////////////////////////////////////////////////////////
  572. int WillReceiveNotBlock
  573. (
  574. // returns: -1:error, 0:may block, 1:will not block
  575. int fd, // file handle of source
  576. uint msec // timeout: 0:no timeout, >0: milliseconds
  577. );
  578. int WillSendNotBlock
  579. (
  580. // returns: -1:error, 0:may block, 1:will not block
  581. int fd, // file handle of source
  582. uint msec // timeout: 0:no timeout, >0: milliseconds
  583. );
  584. ///////////////////////////////////////////////////////////////////////////////
  585. ssize_t ReceiveTimeout
  586. (
  587. // returns: <0: on error, 0:timeout, >0: bytes read
  588. int fd, // file handle of source
  589. void *buf, // valid pointer to buffer
  590. uint size, // size to receive
  591. int flags, // flags for recv()
  592. int msec // timeout: -1:unlimited, 0:no timeout, >0: milliseconds
  593. );
  594. ssize_t SendTimeout
  595. (
  596. // returns: <0: on error, 0:timeout, >0: bytes written
  597. int fd, // file hande of destination
  598. const void *data, // valid pointer to data
  599. uint size, // size to receive
  600. int flags, // flags for send()
  601. int msec, // total timeout: -1:unlimited, 0:no timeout, >0: milliseconds
  602. bool all // true: send all data until done, total timeout or error
  603. );
  604. //
  605. ///////////////////////////////////////////////////////////////////////////////
  606. /////////////// IP data structures ///////////////
  607. ///////////////////////////////////////////////////////////////////////////////
  608. // [[ether_head_t]]
  609. typedef struct ether_head_t
  610. {
  611. // http://en.wikipedia.org/wiki/Ethertype
  612. u8 mac_dest[6];
  613. u8 mac_src[6];
  614. u16 ether_type;
  615. }
  616. __attribute__ ((packed)) ether_head_t;
  617. //-----------------------------------------------------------------------------
  618. // [[ether_head_vlan_t]]
  619. typedef struct ether_head_vlan_t
  620. {
  621. // http://en.wikipedia.org/wiki/Ethertype
  622. u8 mac_dest[6];
  623. u8 mac_src[6];
  624. u16 vlan_tag_type; // 0x8100 for VLAN
  625. u16 vlan_param; // Bits 0-11: ID / Bits 12-15: flags
  626. u16 ether_type;
  627. //-- secondary params
  628. bool have_vlan; // true: VLAN trag available
  629. u8 head_size; // >0: size of ethernet header (local endian): 14|18
  630. }
  631. __attribute__ ((packed)) ether_head_vlan_t;
  632. //-----------------------------------------------------------------------------
  633. // [[arp_head_t]]
  634. typedef struct arp_head_t
  635. {
  636. // http://de.wikipedia.org/wiki/Address_Resolution_Protocol
  637. /* 0x00 */ u16 hardware_addr_type; // always 0x0001 for IPv4
  638. /* 0x02 */ u16 protocol_addr_type; // always 0x8000 for IPv4
  639. /* 0x04 */ u8 mac_size; // always 6 for IPv4
  640. /* 0x05 */ u8 addr_size; // always 4 for IPv4
  641. /* 0x06 */ u16 operation;
  642. /* 0x08 */ u8 src_mac[6]; // MAC of source
  643. /* 0x0e */ u32 src_ip; // IPv4 of source
  644. /* 0x12 */ u8 dest_mac[6]; // MAC of destination
  645. /* 0x18 */ u32 dest_ip; // IPv4 of destination
  646. /* 0x1c */
  647. }
  648. __attribute__ ((packed)) arp_head_t;
  649. //-----------------------------------------------------------------------------
  650. // [[ip4_head_t]]
  651. typedef struct ip4_head_t
  652. {
  653. // http://de.wikipedia.org/wiki/IP-Paket
  654. /*00*/ u8 vers_ihl; // high nibble: version, low nibble: IHL (Ip Header Len)
  655. /*01*/ u8 tos;
  656. /*02*/ u16 total_len;
  657. /*04*/ u16 id;
  658. /*06*/ u16 frag_off; // low 12 bits: fragment offset, high 4 bits: flags
  659. /*08*/ u8 ttl;
  660. /*09*/ u8 protocol;
  661. /*0a*/ u16 checksum;
  662. /*0c*/ u32 ip_src;
  663. /*10*/ u32 ip_dest;
  664. /*14*/ // optional: options + padding
  665. }
  666. __attribute__ ((packed)) ip4_head_t;
  667. //-----------------------------------------------------------------------------
  668. // [[udp_head_t]]
  669. typedef struct udp_head_t
  670. {
  671. // http://de.wikipedia.org/wiki/User_Datagram_Protocol#UDP-Datagramm
  672. u16 port_src;
  673. u16 port_dest;
  674. u16 data_len;
  675. u16 checksum;
  676. }
  677. __attribute__ ((packed)) udp_head_t;
  678. //-----------------------------------------------------------------------------
  679. // [[udp_packet_t]]
  680. // ethernet packet limit is 1500
  681. #define MAX_UDP_PACKET_DATA ( 1500 - sizeof(ip4_head_t) - sizeof(udp_head_t) )
  682. typedef struct udp_packet_t
  683. {
  684. ip4_head_t ip4;
  685. udp_head_t udp;
  686. u8 data[MAX_UDP_PACKET_DATA];
  687. }
  688. __attribute__ ((packed)) udp_packet_t;
  689. //-----------------------------------------------------------------------------
  690. // [[tcp_flags_t]]
  691. typedef enum tcp_flags_t
  692. {
  693. TCP_F_FINISH = 0x01,
  694. TCP_F_SYNC = 0x02,
  695. TCP_F_RESET = 0x04,
  696. TCP_F_PSH = 0x08,
  697. TCP_F_ACK = 0x10,
  698. TCP_F_URGENT = 0x20,
  699. TCP_M_RESERVED = 0xc0,
  700. }
  701. __attribute__ ((packed)) tcp_flags_t;
  702. //-----------------------------------------------------------------------------
  703. // [[tcp_head_t]]
  704. typedef struct tcp_head_t
  705. {
  706. // http://de.wikipedia.org/wiki/Transmission_Control_Protocol#Aufbau_des_TCP-Headers
  707. u16 port_src;
  708. u16 port_dest;
  709. u32 seq_num;
  710. u32 acc_num;
  711. u8 data_off; // bit 4-7 only, multiply it by 4
  712. u8 flags; // bit field (think big endian) -> tcp_flags_t
  713. u16 window;
  714. u16 checksum;
  715. u16 urgent;
  716. u8 data[];
  717. }
  718. __attribute__ ((packed)) tcp_head_t;
  719. //-----------------------------------------------------------------------------
  720. bool fix_ether_head_vlan ( ether_head_vlan_t *head );
  721. void ntoh_ether_head_vlan ( ether_head_vlan_t *dest, const void *src );
  722. void ntoh_ether_head ( ether_head_t *dest, const void *src );
  723. void ntoh_ip4_head ( ip4_head_t *dest, const void *src );
  724. void ntoh_udp_head ( udp_head_t *dest, const void *src );
  725. void ntoh_tcp_head ( tcp_head_t *dest, const void *src );
  726. //
  727. ///////////////////////////////////////////////////////////////////////////////
  728. /////////////// IP interface ///////////////
  729. ///////////////////////////////////////////////////////////////////////////////
  730. char * ScanNumericIP4
  731. (
  732. // returns next unread character or NULL on error
  733. ccp addr, // address to scan
  734. u32 *r_ipv4, // not NULL: store result here (local endian)
  735. u32 *r_port, // not NULL: scan port too (local endian)
  736. uint default_port // return this if no port found
  737. );
  738. //-----------------------------------------------------------------------------
  739. mem_t ScanNumericIP4Mem
  740. (
  741. // returns unread character or NullMem on error
  742. mem_t addr, // address to scan
  743. u32 *r_ipv4, // not NULL: store result here (local endian)
  744. u32 *r_port, // not NULL: scan port too (local endian)
  745. uint default_port // return this if no port found
  746. );
  747. //-----------------------------------------------------------------------------
  748. // Returns a NETWORK MASK "/a.b.c.d" or as CIDR number "/num" between 0 and 32.
  749. // An optional slash '/' at the beginning is skipped.
  750. // Returns modified 'source' if a MASK or CDIR is detected.
  751. // If no one is detected, source is unmodified and returned mask = ~0.
  752. mem_t ScanNetworkMaskMem ( u32 *mask, mem_t source );
  753. //-----------------------------------------------------------------------------
  754. u16 CalcChecksumIP4
  755. (
  756. const void *data, // ip4 header
  757. // make sure, that the checksum member is NULL
  758. uint size // size of ip4 header
  759. );
  760. //-----------------------------------------------------------------------------
  761. u16 CalcChecksumUDP
  762. (
  763. const ip4_head_t *ip4, // IP4 header
  764. const udp_head_t *udp, // UDP header
  765. const void *data // UDP data, size is 'udp->data_len'
  766. );
  767. //
  768. ///////////////////////////////////////////////////////////////////////////////
  769. /////////////// UNIX sockets ///////////////
  770. ///////////////////////////////////////////////////////////////////////////////
  771. enumError SendUnixUDP
  772. (
  773. ccp path1, // NULL or part #1 of path
  774. ccp path2, // NULL or part #2 of path
  775. bool silent, // suppress all error messages
  776. cvp data, // data to send
  777. uint size // size of 'data'
  778. );
  779. //
  780. ///////////////////////////////////////////////////////////////////////////////
  781. /////////////// Transfer Statistics ///////////////
  782. ///////////////////////////////////////////////////////////////////////////////
  783. // [[TransferStats_t]]
  784. typedef struct TransferStats_t
  785. {
  786. u32 conn_count; // number of connections
  787. u32 recv_count; // number of received packets
  788. u64 recv_size; // total size of received packets
  789. u32 send_count; // number of send packets
  790. u64 send_size; // total size of send packets
  791. }
  792. TransferStats_t;
  793. //-----------------------------------------------------------------------------
  794. static inline void InitializeTransferStats ( TransferStats_t *ts )
  795. { DASSERT(ts); memset(ts,0,sizeof(*ts)); }
  796. //-----------------------------------------------------------------------------
  797. TransferStats_t * Add2TransferStats
  798. (
  799. // return dest
  800. // calculate: dest += src
  801. TransferStats_t *dest, // NULL or destination and first source
  802. const TransferStats_t *src // NULL or second source
  803. );
  804. TransferStats_t * Sub2TransferStats
  805. (
  806. // return dest
  807. // calculate: dest -= src
  808. TransferStats_t *dest, // NULL or destination and first source
  809. const TransferStats_t *src // NULL or second source
  810. );
  811. TransferStats_t * Add3TransferStats
  812. (
  813. // return dest
  814. // calculate: dest = src1 + src2
  815. TransferStats_t *dest, // NULL or destination (maybe same as source)
  816. const TransferStats_t *src1, // NULL or first source
  817. const TransferStats_t *src2 // NULL or second source
  818. );
  819. TransferStats_t * Sub3TransferStats
  820. (
  821. // return dest
  822. // calculate: dest = src1 - src2
  823. TransferStats_t *dest, // NULL or destination (maybe same as source)
  824. const TransferStats_t *src1, // NULL or first source
  825. const TransferStats_t *src2 // NULL or second source
  826. );
  827. TransferStats_t * SumTransferStats
  828. (
  829. // return dest
  830. // calculate: dest = sum(all_sources)
  831. TransferStats_t *dest, // NULL or destination (maybe same as source)
  832. const TransferStats_t * const *src, // NULL or source list, elements may be NULL
  833. int n_src // number of element; if -1: term at first NULL
  834. );
  835. ccp PrintTransferStatsSQL
  836. (
  837. // print statistic as SQL assigning list.
  838. // arguments are speparated by a comma.
  839. char *buf, // result buffer
  840. // NULL: use a local circulary static buffer
  841. size_t buf_size, // size of 'buf', ignored if buf==NULL
  842. const TransferStats_t *ts, // valid source
  843. ccp prefix // not NULL: prefix member names
  844. );
  845. //-----------------------------------------------------------------------------
  846. void PrintTranferStatistics
  847. (
  848. FILE *f, // output file, never NULL
  849. ccp prefix, // NULL or prefix for each line
  850. ccp name, // statistics name, up to 5 chars including colon
  851. const TransferStats_t *stat,// statistics record
  852. TransferStats_t *prev, // NULL statistics record with previous data
  853. u64 duration, // delta duration in usec
  854. ccp duration_info, // text representation of 'duration'
  855. const ColorSet_t *colset // NULL (no colors) or color set to use
  856. );
  857. //-----------------------------------------------------------------------------
  858. void SaveCurrentStateTransferStats
  859. (
  860. FILE *f, // output file
  861. const TransferStats_t *stat // valid stat object
  862. );
  863. void SaveCurrentStateTransferStats1
  864. (
  865. // print as single line
  866. FILE *f, // output file
  867. ccp name, // var name; use "tfer-stat" if NULL or EMPTY
  868. const TransferStats_t *stat // valid stat object
  869. );
  870. void SaveCurrentStateTransferStatsN
  871. (
  872. // print multiple stats each as single line
  873. FILE *f, // output file
  874. ccp name, // var name; use "tfer-stat-" if NULL or EMPTY
  875. const TransferStats_t *stat, // list of valid stat objects
  876. uint n_stat // number of elements in 'stat'
  877. );
  878. //-----------------------------------------------------------------------------
  879. void RestoreStateTransferStats
  880. (
  881. RestoreState_t *rs, // info data, can be modified (cleaned after call)
  882. cvp user_table // pointer provided by RestoreStateTab_t[]
  883. );
  884. void RestoreStateTransferStats1
  885. (
  886. // scan single line
  887. RestoreState_t *rs, // info data, can be modified (cleaned after call)
  888. ccp name, // var name; use "tfer-stat" if NULL or EMPTY
  889. TransferStats_t *stat, // valid stat object
  890. bool fall_back // true: fall back to RestoreStateTransferStats()
  891. );
  892. uint RestoreStateTransferStatsN
  893. (
  894. // print multiple stats each as single line
  895. // returns the number of read elements; all others are set to NULL
  896. RestoreState_t *rs, // info data, can be modified (cleaned after call)
  897. ccp name, // var name; use "tfer-stat-" if NULL or EMPTY
  898. TransferStats_t *stat, // list of valid stat objects
  899. uint n_stat // number of elements in 'stat'
  900. );
  901. //
  902. ///////////////////////////////////////////////////////////////////////////////
  903. /////////////// Socket_t ///////////////
  904. ///////////////////////////////////////////////////////////////////////////////
  905. // define some TCP call back function types
  906. struct TCPStream_t;
  907. typedef int (*TCPStreamFunc) ( struct TCPStream_t * ts );
  908. typedef int (*TCPIOFunc) ( struct TCPStream_t * ts, u8 *buf, uint size );
  909. typedef int (*TCPTimeFunc) ( struct TCPStream_t * ts, u64 now_usec );
  910. typedef enum TCPFDList_t
  911. {
  912. TCP_FM_ADD_SOCK, // add sockets to 'fdl'
  913. TCP_FM_CHECK_SOCK, // check sockts of 'fdl'
  914. TCP_FM_TIMEOUT, // on timeout (fdl is NULL)
  915. }
  916. TCPFDList_t;
  917. typedef int (*TCPFDListFunc)
  918. (
  919. // if return <0, then don't do standard action with the stream
  920. struct TCPStream_t *ts, // valid TCP stream
  921. FDList_t *fdl, // valid socket list
  922. TCPFDList_t mode, // execution mode
  923. bool check_timeout // true: enable timeout checks
  924. );
  925. struct TCPHandler_t;
  926. typedef struct TCPStream_t * (*TCPCreateFunc) ( struct TCPHandler_t * th, int sock );
  927. ///////////////////////////////////////////////////////////////////////////////
  928. // [[Socket_t]]
  929. typedef struct Socket_t
  930. {
  931. int sock; // tcp socket
  932. uint poll_index; // if poll() is used: index of 'poll_list'
  933. bool is_unix; // true: Is a unix file socket
  934. char info[23]; // info about this socket, for debugging
  935. // if defined, these function replace the TCPHandler_t functions.
  936. TCPCreateFunc OnCreateStream; // not NULL: called to create
  937. // an initialize a stream object
  938. TCPStreamFunc OnAddedStream; // not NULL: called after the stream
  939. // is created and added
  940. }
  941. Socket_t;
  942. uint GetSocketNameBySA
  943. ( char *buf, uint bufsize, const struct sockaddr *sa, socklen_t sa_len );
  944. uint GetSocketName ( char *buf, uint bufsize, int sock );
  945. uint GetPeerName ( char *buf, uint bufsize, int sock );
  946. bool DeleteSocketFile ( int sock );
  947. //
  948. ///////////////////////////////////////////////////////////////////////////////
  949. /////////////// TCPStream_t ///////////////
  950. ///////////////////////////////////////////////////////////////////////////////
  951. // [[TCPStream_t]]
  952. typedef struct TCPStream_t
  953. {
  954. //--- double linked list
  955. struct TCPStream_t *prev; // link to previous stream, unused for the pool
  956. struct TCPStream_t *next; // link to next stream
  957. struct TCPHandler_t *handler; // related TCP handler
  958. //--- base data
  959. uint unique_id; // unique id, created by CreateUniqueId()
  960. int sock; // tcp socket
  961. uchar protect; // >0: protect member, don't destroy it
  962. uchar auto_close; // >0: close stream after last byte sent
  963. uchar rescan; // rescan input buffer also without new data
  964. uchar eof; // >0: end of file, bit-0 is set by framework
  965. uchar error; // >0: connection error, bit-0 is set by framework
  966. uchar not_socket; // >0: fd is not a socket! // =0: unknown
  967. GrowBuffer_t ibuf; // input buffer
  968. GrowBuffer_t obuf; // output buffer
  969. u64 accept_usec; // >0: next trigger, not used for timeout
  970. u64 trigger_usec; // >0: next timeout and trigger time
  971. u64 delay_usec; // >0: delay of trigger after accept_usec
  972. u64 timeout_usec; // >0: auto disconnect after inactivity
  973. u64 allow_mode; // >0: access allowed with this code
  974. uint poll_index; // if 'use_poll': index of 'poll_list'
  975. //--- call back functions
  976. TCPTimeFunc OnMaintenance; // not NULL: called on maintenance
  977. TCPTimeFunc OnTimeout; // not NULL: called on timeout
  978. TCPIOFunc OnReceived; // not NULL: called after read, but before buffer insert
  979. TCPIOFunc OnSend; // not NULL: called after write, but before buffer drop
  980. TCPTimeFunc OnClose; // not NULL: called when the stream is closed
  981. TCPFDListFunc OnFDList; // not NULL: Call this for FDList actions
  982. //--- logging
  983. TraceLog_t *tracelog; // NULL or valid trace-log data
  984. //--- statistics
  985. char info[32]; // info about this stream, for debugging
  986. u64 connect_usec; // time of connection, GetTimeUSec(false)
  987. u64 receive_usec; // time of last receive, GetTimeUSec(false)
  988. u64 send_usec; // time of last send, GetTimeUSec(false)
  989. TransferStats_t stat; // transfer statistics
  990. TransferStats_t *xstat; // NULL or pointer to summary statistics
  991. //--- data extension
  992. u8 data[0]; // user specific data
  993. }
  994. TCPStream_t;
  995. ///////////////////////////////////////////////////////////////////////////////
  996. void LogTCPStream
  997. (
  998. FILE *f, // output file
  999. int indent, // indention
  1000. const TCPStream_t *ts, // valid TCP handler
  1001. int recurse, // >0: print stream list, >1: print buffer status
  1002. ccp format, // format string for vfprintf()
  1003. ... // arguments for 'vfprintf(format,...)'
  1004. )
  1005. __attribute__ ((__format__(__printf__,5,6)));
  1006. //-----------------------------------------------------------------------------
  1007. void LogTCPStreamActivity
  1008. (
  1009. const TCPStream_t *ts, // valid TCP handler
  1010. ccp activity
  1011. );
  1012. //-----------------------------------------------------------------------------
  1013. char * BufInfoHeadTCPStream
  1014. (
  1015. // returns a pointer to the head line
  1016. uint line // 0|1
  1017. );
  1018. #define BUF_INFO_TCP_STREAM_SIZE 120
  1019. char * BufInfoTCPStream
  1020. (
  1021. // returns a pointer to the buffer
  1022. char * buf, // result (BUF_INFO_TCP_STREAM_SIZE bytes are good)
  1023. // If NULL, a local circulary static buffer is used
  1024. size_t buf_size, // size of 'buf', ignored if buf==NULL
  1025. const TCPStream_t *ts, // valid TCP handler
  1026. u64 now_usec // NULL or current time, GetTimeUSec(false)
  1027. );
  1028. //-----------------------------------------------------------------------------
  1029. void InitializeTCPStream ( TCPStream_t *ts, int sock );
  1030. void ResetTCPStream ( TCPStream_t *ts );
  1031. void DestroyTCPStream ( TCPStream_t *ts );
  1032. int SendDirectTCPStream
  1033. (
  1034. // Send data direct without blocking and without calling any notifier.
  1035. // If output buf is not empty or send failed, append the data to output buf.
  1036. // Returns the number of bytes added to the output buffer or -1 on error.
  1037. // The data is send+stored completely (returns 'size') or not at all
  1038. // (returns '0'). -1 is returned if the socket is invalid.
  1039. TCPStream_t *ts, // valid TCP handler
  1040. bool flush_output, // true: try to flush out-buf before
  1041. cvp data, // data to send
  1042. uint size // size of 'data', If NULL && flush: flush only
  1043. );
  1044. int PrintArgDirectTCPStream
  1045. (
  1046. // Printing interface for SendDirectTCPStream()
  1047. TCPStream_t *ts, // valid TCP handler
  1048. bool flush_output, // true: try to flush out-buf before
  1049. ccp format, // format string for vsnprintf()
  1050. va_list arg // parameters for 'vfprintf(...,format,...)'
  1051. );
  1052. int PrintDirectTCPStream
  1053. (
  1054. // Printing interface for SendDirectTCPStream()
  1055. TCPStream_t *ts, // valid TCP handler
  1056. bool flush_output, // true: try to flush out-buf before
  1057. ccp format, // format string for vfprintf()
  1058. ... // arguments for 'vfprintf(...,format,...)'
  1059. )
  1060. __attribute__ ((__format__(__printf__,3,4)));
  1061. //-----------------------------------------------------------------------------
  1062. int PrintBinary1TCPStream
  1063. (
  1064. TCPStream_t *ts, // NULL or destination
  1065. ccp cmd, // command name
  1066. cvp bin_data, // pointer to binary data
  1067. uint bin_size, // size of binary data
  1068. ccp format, // NULL or format string with final ';'
  1069. ... // parameters for 'format'
  1070. )
  1071. __attribute__ ((__format__(__printf__,5,6)));
  1072. int PrintBinary2TCPStream
  1073. (
  1074. TCPStream_t *ts, // NULL or destination
  1075. ccp cmd, // command name
  1076. cvp bin1_data, // pointer to binary data
  1077. uint bin1_size, // size of binary data
  1078. cvp bin2_data, // pointer to binary data
  1079. uint bin2_size, // size of binary data
  1080. ccp format, // NULL or format string with final ';'
  1081. ... // parameters for 'format'
  1082. )
  1083. __attribute__ ((__format__(__printf__,7,8)));
  1084. //-----------------------------------------------------------------------------
  1085. static inline int FlushTCPStream
  1086. (
  1087. // Flush the output buffer using SendDirectTCPStream() without data
  1088. TCPStream_t *ts // valid TCP handler
  1089. )
  1090. {
  1091. return SendDirectTCPStream(ts,true,0,0);
  1092. }
  1093. //-----------------------------------------------------------------------------
  1094. ssize_t UpdateRecvStatTCPStream
  1095. (
  1096. // returns 'send_stat'
  1097. TCPStream_t *ts, // valid TCP handler
  1098. ssize_t recv_stat, // result of recv(), update stats on >0
  1099. u64 now_usec // NULL or current time
  1100. );
  1101. ssize_t UpdateSendStatTCPStream
  1102. (
  1103. // returns 'send_stat'
  1104. TCPStream_t *ts, // valid TCP handler
  1105. ssize_t send_stat, // result of send(), update stats on >0
  1106. u64 now_usec // NULL or current time
  1107. );
  1108. //-----------------------------------------------------------------------------
  1109. void AddSocketTCPStream
  1110. (
  1111. TCPStream_t *ts, // valid TCP handler
  1112. FDList_t *fdl // valid file descriptor list
  1113. );
  1114. void CheckSocketTCPStream
  1115. (
  1116. TCPStream_t *ts, // valid TCP handler
  1117. FDList_t *fdl, // valid socket list
  1118. bool check_timeout // true: enable timeout checks
  1119. );
  1120. //-----------------------------------------------------------------------------
  1121. void CheckTimeoutTCPStream ( TCPStream_t *ts );
  1122. u64 GetLastActivityTCPStream ( TCPStream_t *ts );
  1123. u64 GetTimeoutTCPStream ( TCPStream_t *ts );
  1124. int OnTimeoutTCPStream ( TCPStream_t *ts, u64 now_usec );
  1125. void SetupTriggerTCPStream ( TCPStream_t *ts, u64 now_usec );
  1126. //
  1127. ///////////////////////////////////////////////////////////////////////////////
  1128. /////////////// TCPHandler_t ///////////////
  1129. ///////////////////////////////////////////////////////////////////////////////
  1130. typedef int (*TCPHandlerFunc)
  1131. (
  1132. // returns 0 on success, !=0 otherwise
  1133. struct TCPHandler_t *th, // valid TCP handler
  1134. const Socket_t *lsock, // valid listen-socket
  1135. int sock // valid socket to verify
  1136. );
  1137. typedef bool (*TCPAllowFunc)
  1138. (
  1139. // returns true if access is allowed
  1140. struct TCPHandler_t *th, // valid TCP handler
  1141. const Socket_t *lsock, // valid listen-socket
  1142. int sock, // valid socket to verify
  1143. u64 *allow // not NULL: store allow code here
  1144. );
  1145. ///////////////////////////////////////////////////////////////////////////////
  1146. // [[TCPHandler_t]]
  1147. #define TCP_HANDLER_MAX_LISTEN 3
  1148. typedef struct TCPHandler_t
  1149. {
  1150. uint unique_id; // unique id, created by CreateUniqueId()
  1151. uint data_size; // size of TCPStream_t::data
  1152. uint max_conn; // max allowed connections
  1153. TCPStream_t *first; // pointer to first active stream
  1154. uint need_maintenance; // >0: a stream is ready for maintenance
  1155. TCPAllowFunc OnAllowStream; // not NULL: called before allowing a connection
  1156. // initialized with IsStreamAllowed()
  1157. TCPHandlerFunc OnAcceptStream; // not NULL: called before accpeting a connection
  1158. TCPCreateFunc OnCreateStream; // not NULL: called to create
  1159. // and initialize a stream object
  1160. TCPStreamFunc OnAddedStream; // not NULL: called after the stream
  1161. // is created and added
  1162. TCPStreamFunc OnDestroyStream; // not NULL: called by ResetTCPStream()
  1163. Socket_t listen[TCP_HANDLER_MAX_LISTEN];
  1164. // sockets to listen
  1165. AllowIP4_t *allow_ip4; // NULL or filter for accept (not for unix files).
  1166. // Analysis is done by OnAllowStream()
  1167. // before calling OnAcceptStream().
  1168. //--- logging
  1169. TraceLog_t tracelog; // trace activities
  1170. //--- statistics
  1171. uint used_streams; // number of currently used streams
  1172. uint max_used_streams; // number of max used streams
  1173. uint total_streams; // total number of used streams
  1174. TransferStats_t stat; // transfer statistics
  1175. }
  1176. TCPHandler_t;
  1177. ///////////////////////////////////////////////////////////////////////////////
  1178. void LogTCPHandler
  1179. (
  1180. FILE *f, // output file
  1181. int indent, // indention
  1182. const TCPHandler_t *th, // valid TCP handler
  1183. int recurse, // >0: print stream list, >1: print buffer status
  1184. ccp format, // format string for vfprintf()
  1185. ... // arguments for 'vfprintf(format,...)'
  1186. )
  1187. __attribute__ ((__format__(__printf__,5,6)));
  1188. //-----------------------------------------------------------------------------
  1189. void PrintStreamTableTCPHandler
  1190. (
  1191. FILE *f, // output file
  1192. const ColorSet_t *colset, // NULL (no colors) or valid color set
  1193. int indent, // indention
  1194. const TCPHandler_t *th // valid TCP handler
  1195. );
  1196. //-----------------------------------------------------------------------------
  1197. void InitializeTCPHandler
  1198. (
  1199. TCPHandler_t *th, // not NULL
  1200. uint data_size // size of 'TCPStream_t::data'
  1201. );
  1202. void ResetTCPHandler
  1203. (
  1204. TCPHandler_t *th // valid TCP handler
  1205. );
  1206. uint CloseTCPHandler
  1207. (
  1208. // returns the number of waiting clients (obuf not empty and !disabled)
  1209. TCPHandler_t *th // valid TCP handler
  1210. );
  1211. TCPStream_t * AddTCPStream
  1212. (
  1213. TCPHandler_t *th, // valid TCP handler
  1214. TCPStream_t *ts // the new stream
  1215. );
  1216. TCPStream_t * CreateTCPStream
  1217. (
  1218. TCPHandler_t *th, // valid TCP handler
  1219. int sock, // related socket
  1220. u64 allow_mode, // stored in 'allow_mode'
  1221. const Socket_t *listen // NULL or related listen object
  1222. );
  1223. TCPStream_t * ConnectUnixTCPStream
  1224. (
  1225. TCPHandler_t *th, // valid TCP handler
  1226. ccp path, // NULL or path part 1 to socket file
  1227. bool silent // suppress error messages
  1228. );
  1229. TCPStream_t * ConnectTCPStream
  1230. (
  1231. TCPHandler_t *th, // valid TCP handler
  1232. ccp addr, // address -> NetworkHost_t
  1233. u16 default_port, // default port
  1234. bool silent // suppress error messages
  1235. );
  1236. TCPStream_t * FindTCPStreamByUniqueID
  1237. (
  1238. TCPHandler_t *th, // valid TCP handler
  1239. uint unique_id // id to search
  1240. );
  1241. //-----------------------------------------------------------------------------
  1242. uint NumOfSocketsTCP ( const TCPHandler_t *th );
  1243. Socket_t * GetUnusedListenSocketTCP ( TCPHandler_t *th, bool silent );
  1244. //-----------------------------------------------------------------------------
  1245. enumError ListenUnixTCP
  1246. (
  1247. TCPHandler_t *th, // valid TCP handler
  1248. ccp path // path to unix file
  1249. );
  1250. enumError ListenTCP
  1251. (
  1252. TCPHandler_t *th, // valid TCP handler
  1253. ccp addr, // IPv4 address with optional port -> NetworkHost_t
  1254. // fall back to ListenUnixTCP() if addr begins with
  1255. // 'unix:' or '/' or './ or '../'
  1256. u16 default_port // default port
  1257. );
  1258. int UnlistenTCP
  1259. (
  1260. // returns the index of the closed socket, or if not found
  1261. TCPHandler_t *th, // valid TCP handler
  1262. int sock // socket to close
  1263. );
  1264. uint UnlistenAllTCP
  1265. (
  1266. // returns the number of closed sockets
  1267. TCPHandler_t *th // valid TCP handler
  1268. );
  1269. //-----------------------------------------------------------------------------
  1270. void AddSocketsTCP
  1271. (
  1272. TCPHandler_t *th, // valid TCP handler
  1273. FDList_t *fdl // valid file descriptor list
  1274. );
  1275. //-----------------------------------------------------------------------------
  1276. bool IsStreamAllowed
  1277. (
  1278. // returns true if access is allowed
  1279. struct TCPHandler_t *th, // valid TCP handler
  1280. const Socket_t *lsock, // valid listen-socket
  1281. int sock, // valid socket to verify
  1282. u64 *allow // not NULL: store allow code here
  1283. );
  1284. TCPStream_t * OnAcceptStream
  1285. (
  1286. TCPHandler_t *th, // valid TCP handler
  1287. const Socket_t *lsock, // listen-socket
  1288. u64 now_usec // time for timestamps, GetTimeUSec(false)
  1289. );
  1290. void OnCloseStream
  1291. (
  1292. TCPStream_t *ts, // valid TCP stream
  1293. u64 now_usec // time for timestamps, GetTimeUSec(false)
  1294. );
  1295. void SetNotSocketStream
  1296. (
  1297. TCPStream_t *ts // valid TCP stream
  1298. );
  1299. void OnReceivedStream
  1300. (
  1301. TCPStream_t *ts, // valid TCP stream
  1302. u64 now_usec // time for timestamps, GetTimeUSec(false)
  1303. );
  1304. void OnWriteStream
  1305. (
  1306. TCPStream_t *ts, // valid TCP stream
  1307. u64 now_usec // time for timestamps, GetTimeUSec(false)
  1308. );
  1309. void CheckSocketsTCP
  1310. (
  1311. TCPHandler_t *th, // valid TCP handler
  1312. FDList_t *fdl, // valid socket list
  1313. bool check_timeout // true: enable timeout checks
  1314. );
  1315. void CheckTimeoutTCP ( TCPHandler_t *th );
  1316. bool MaintainTCP ( TCPHandler_t *th );
  1317. void ManageSocketsTCP
  1318. (
  1319. // call CheckSocketsTCP(), CheckTimeoutTCP(), MaintainTCP()
  1320. TCPHandler_t *th, // valid TCP handler
  1321. FDList_t *fdl, // valid socket list
  1322. int stat // result of a WAIT function
  1323. );
  1324. ///////////////////////////////////////////////////////////////////////////////
  1325. // special case: send single data packets
  1326. TCPStream_t * SendSingleUnixTCP
  1327. (
  1328. TCPHandler_t *th, // valid handle
  1329. ccp path, // path to socket file
  1330. const void *data, // data to send
  1331. uint size, // size of 'data'
  1332. u64 timeout_usec, // timeout before closing the connection
  1333. TransferStats_t *xstat, // NULL or pointer to summary statistics
  1334. bool silent // suppress error messages
  1335. );
  1336. TCPStream_t * SendSingleTCP
  1337. (
  1338. TCPHandler_t *th, // valid handle
  1339. ccp addr, // address -> NetworkHost_t
  1340. u16 default_port, // default port
  1341. const void *data, // data to send
  1342. uint size, // size of 'data'
  1343. u64 timeout_usec, // timeout before closing the connection
  1344. TransferStats_t *xstat, // NULL or pointer to summary statistics
  1345. bool silent // suppress error messages
  1346. );
  1347. //
  1348. ///////////////////////////////////////////////////////////////////////////////
  1349. /////////////// TCP: CommandTCP ///////////////
  1350. ///////////////////////////////////////////////////////////////////////////////
  1351. typedef char * (*CommandTCPLineFunc)
  1352. (
  1353. // return first non scanned character, or NULL on error
  1354. TCPStream_t *ts, // valid stream data
  1355. char *line, // begin of line
  1356. char *line_end, // end of line
  1357. u64 now_usec // NULL or current time
  1358. );
  1359. ///////////////////////////////////////////////////////////////////////////////
  1360. typedef enumError (*CommandTCPArgFunc)
  1361. (
  1362. TCPStream_t *ts, // valid stream data
  1363. int argc, // number of arguments in 'argv'
  1364. char **argv, // array with 'argc' arguments + a NULL term
  1365. u64 now_usec // NULL or current time in usec
  1366. );
  1367. ///////////////////////////////////////////////////////////////////////////////
  1368. typedef struct CommandTCPInfo_t
  1369. {
  1370. //--- base parameters
  1371. bool comma_is_eol; // true: comma is 'end of command line'
  1372. u64 timeout_usec; // wanted timeout, default 10s
  1373. //--- scanning functions, high priority first
  1374. CommandTCPArgFunc OnScanArg; // function to scan arguments
  1375. CommandTCPLineFunc OnScanLine; // function to scan line
  1376. //--- user specific data extension
  1377. u8 data[0];
  1378. }
  1379. CommandTCPInfo_t;
  1380. ///////////////////////////////////////////////////////////////////////////////
  1381. int OnCreateCommandTCP ( TCPStream_t *ts );
  1382. void SetTimeoutCommandTCP
  1383. (
  1384. TCPStream_t *ts, // valid stream
  1385. u64 now_usec // NULL or current time in usec
  1386. );
  1387. //
  1388. ///////////////////////////////////////////////////////////////////////////////
  1389. /////////////// SaveCurrentState*() ///////////////
  1390. ///////////////////////////////////////////////////////////////////////////////
  1391. void SaveCurrentStateSocket
  1392. (
  1393. FILE *f, // output file
  1394. const Socket_t *sock // valid socket object
  1395. );
  1396. ///////////////////////////////////////////////////////////////////////////////
  1397. typedef void (*TCPStreamSaveFunc)
  1398. (
  1399. FILE *f, // output file
  1400. const TCPStream_t *ts // valid TCP stream
  1401. );
  1402. //-----------------------------------------------------------------------------
  1403. void SaveCurrentStateTCPStream
  1404. (
  1405. FILE *f, // output file
  1406. const TCPStream_t *ts, // valid TCP stream
  1407. TCPStreamSaveFunc func // NULL or function for 'TCPStream_t.data' extend
  1408. );
  1409. void SaveCurrentStateTCPHandler
  1410. (
  1411. FILE *f, // output file
  1412. ccp sect_name, // section base name
  1413. const TCPHandler_t *th, // valid TCP handler
  1414. TCPStreamSaveFunc func // NULL or function for 'TCPStream_t.data' extend
  1415. );
  1416. void SaveCurrentStateCommandTCP
  1417. (
  1418. FILE *f, // output file
  1419. const TCPStream_t *ts // valid TCP stream
  1420. );
  1421. //
  1422. ///////////////////////////////////////////////////////////////////////////////
  1423. /////////////// RestoreState*() ///////////////
  1424. ///////////////////////////////////////////////////////////////////////////////
  1425. void RestoreStateSocket
  1426. (
  1427. RestoreState_t *rs, // info data, can be modified (cleaned after call)
  1428. cvp user_table // pointer provided by RestoreStateTab_t[]
  1429. );
  1430. ///////////////////////////////////////////////////////////////////////////////
  1431. TCPStream_t * RestoreStateTCPStream
  1432. (
  1433. // return 'ts' or the new TCPStream_t
  1434. RestoreState_t *rs, // info data, can be modified (cleaned after call)
  1435. TCPStream_t *ts, // if NULL: create it
  1436. uint extra_size // if 'ts' alloced: alloc some byte more
  1437. );
  1438. ///////////////////////////////////////////////////////////////////////////////
  1439. void RestoreStateTCPHandler_base
  1440. (
  1441. TCPHandler_t *th, // valid TCPHandler_t
  1442. RestoreState_t *rs // info data, can be modified (cleaned after call)
  1443. );
  1444. Socket_t * RestoreStateTCPHandler_socket
  1445. (
  1446. TCPHandler_t *th, // valid TCPHandler_t
  1447. RestoreState_t *rs // info data, can be modified (cleaned after call)
  1448. );
  1449. TCPStream_t * RestoreStateTCPHandler_stream
  1450. (
  1451. TCPHandler_t *th, // valid TCPHandler_t
  1452. RestoreState_t *rs // info data, can be modified (cleaned after call)
  1453. );
  1454. void RestoreStateTCPHandler
  1455. (
  1456. RestoreState_t *rs, // info data, can be modified (cleaned after call)
  1457. cvp user_table // pointer provided by RestoreStateTab_t[]
  1458. );
  1459. ///////////////////////////////////////////////////////////////////////////////
  1460. void RestoreStateCommandTCP
  1461. (
  1462. RestoreState_t *rs, // info data, can be modified (cleaned after call)
  1463. cvp user_table // pointer provided by RestoreStateTab_t[]
  1464. );
  1465. //
  1466. ///////////////////////////////////////////////////////////////////////////////
  1467. /////////////// Linux support ///////////////
  1468. ///////////////////////////////////////////////////////////////////////////////
  1469. #if defined(SYSTEM_LINUX) || defined(__CYGWIN__)
  1470. #include "dclib-network-linux.h"
  1471. #endif
  1472. //
  1473. ///////////////////////////////////////////////////////////////////////////////
  1474. /////////////// E N D ///////////////
  1475. ///////////////////////////////////////////////////////////////////////////////
  1476. #endif // DCLIB_NETWORK_H