anode.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /* libanode: the Anode C reference implementation
  2. * Copyright (C) 2009-2010 Adam Ierymenko <[email protected]>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #ifndef _ANODE_ANODE_H
  17. #define _ANODE_ANODE_H
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #ifndef NULL
  22. #define NULL ((void *)0)
  23. #endif
  24. #define ANODE_ADDRESS_LENGTH_ANODE_256_40 40
  25. #define ANODE_ADDRESS_MAX_LENGTH 40
  26. #define ANODE_ADDRESS_SECRET_LENGTH_ANODE_256_40 32
  27. #define ANODE_ADDRESS_MAX_SECRET_LENGTH 32
  28. #define ANODE_ADDRESS_ID_LENGTH 8
  29. #define ANODE_ZONE_LENGTH 4
  30. #define ANODE_ERR_NONE 0
  31. #define ANODE_ERR_INVALID_ARGUMENT (-10000)
  32. #define ANODE_ERR_OUT_OF_MEMORY (-10001)
  33. #define ANODE_ERR_INVALID_URI (-10002)
  34. #define ANODE_ERR_BUFFER_TOO_SMALL (-10003)
  35. #define ANODE_ERR_ADDRESS_INVALID (-10010)
  36. #define ANODE_ERR_ADDRESS_TYPE_NOT_SUPPORTED (-10011)
  37. #define ANODE_ERR_CONNECTION_CLOSED (-10012)
  38. #define ANODE_ERR_CONNECTION_CLOSED_BY_REMOTE (-10013)
  39. #define ANODE_ERR_CONNECT_FAILED (-10014)
  40. #define ANODE_ERR_UNABLE_TO_BIND (-10015)
  41. #define ANODE_ERR_TOO_MANY_OPEN_SOCKETS (-10016)
  42. #define ANODE_ERR_DNS_NAME_NOT_FOUND_OR_TIMED_OUT (-10017)
  43. /**
  44. * Get a human-readable error description for an error code
  45. *
  46. * The value of 'err' can be either negative or positive.
  47. *
  48. * @param err Error code
  49. * @return Human-readable description
  50. */
  51. extern const char *Anode_strerror(int err);
  52. /* ----------------------------------------------------------------------- */
  53. /* Secure random source */
  54. /* ----------------------------------------------------------------------- */
  55. /**
  56. * Opaque secure random instance
  57. */
  58. typedef void AnodeSecureRandom;
  59. /**
  60. * Initialize a secure random source
  61. *
  62. * No cleanup/destructor is necessary.
  63. *
  64. * @param srng Random structure to initialize
  65. */
  66. extern AnodeSecureRandom *AnodeSecureRandom_new();
  67. /**
  68. * Generate random bytes
  69. *
  70. * @param srng Secure random source
  71. * @param buf Buffer to fill
  72. * @param count Number of bytes to generate
  73. */
  74. extern void AnodeSecureRandom_gen_bytes(AnodeSecureRandom *srng,void *buf,long count);
  75. /**
  76. * Destroy and free a secure random instance
  77. *
  78. * @param srng Secure random source
  79. */
  80. extern void AnodeSecureRandom_delete(AnodeSecureRandom *srng);
  81. /* ----------------------------------------------------------------------- */
  82. /* AES-256 derived Davis-Meyer hash function */
  83. /* ----------------------------------------------------------------------- */
  84. /**
  85. * Digest a message using AES-DIGEST to yield a 16-byte hash code
  86. *
  87. * @param message Message to digest
  88. * @param message_len Length of message in bytes
  89. * @param hash Buffer to store 16 byte hash code
  90. */
  91. extern void Anode_aes_digest(
  92. const void *const message,
  93. unsigned long message_len,
  94. void *const hash);
  95. /* ----------------------------------------------------------------------- */
  96. /* Address Types and Components */
  97. /* ----------------------------------------------------------------------- */
  98. /**
  99. * Anode address
  100. *
  101. * The first byte always identifies the address type, which right now can
  102. * only be type 1 (ANODE-256-40).
  103. */
  104. typedef struct
  105. {
  106. char bits[ANODE_ADDRESS_MAX_LENGTH];
  107. } AnodeAddress;
  108. /**
  109. * 8-byte short Anode address ID
  110. */
  111. typedef struct
  112. {
  113. char bits[ANODE_ADDRESS_ID_LENGTH];
  114. } AnodeAddressId;
  115. /**
  116. * 4-byte Anode zone ID
  117. */
  118. typedef struct
  119. {
  120. char bits[ANODE_ZONE_LENGTH];
  121. } AnodeZone;
  122. /**
  123. * Anode address types
  124. */
  125. enum AnodeAddressType
  126. {
  127. ANODE_ADDRESS_ANODE_256_40 = 1
  128. };
  129. /**
  130. * Get the type of an Anode address
  131. *
  132. * This is a shortcut macro for just looking at the first byte and casting
  133. * it to the AnodeAddressType enum.
  134. *
  135. * @param a Pointer to address
  136. * @return Type as enum AnodeAddressType
  137. */
  138. #define AnodeAddress_get_type(a) ((enum AnodeAddressType)((a)->bits[0]))
  139. /**
  140. * Calculate the short 8 byte address ID from an address
  141. *
  142. * @param address Binary address
  143. * @param short_address_id Buffer to store 8-byte short address ID
  144. * @return 0 on success or error code on failure
  145. */
  146. extern int AnodeAddress_calc_short_id(
  147. const AnodeAddress *address,
  148. AnodeAddressId *short_address_id);
  149. /**
  150. * Extract the zone from an anode address
  151. *
  152. * @param address Binary address
  153. * @param zone Zone value-result parameter to fill on success
  154. * @return 0 on success or error code on failure
  155. */
  156. extern int AnodeAddress_get_zone(const AnodeAddress *address,AnodeZone *zone);
  157. /**
  158. * Convert an address to an ASCII string
  159. *
  160. * Anode addresses are 64 characters in ASCII form, so the buffer should
  161. * have 65 bytes of space.
  162. *
  163. * @param address Address to convert
  164. * @param buf Buffer to receive address in string form (should have 65 bytes of space)
  165. * @param len Length of buffer
  166. * @return Length of resulting string or a negative error code on error
  167. */
  168. extern int AnodeAddress_to_string(const AnodeAddress *address,char *buf,int len);
  169. /**
  170. * Convert a string into an address
  171. *
  172. * @param str Address in string form
  173. * @param address Address buffer to receive result
  174. * @return Zero on sucess or error code on error
  175. */
  176. extern int AnodeAddress_from_string(const char *str,AnodeAddress *address);
  177. /**
  178. * Supported network address types
  179. */
  180. enum AnodeNetworkAddressType
  181. {
  182. ANODE_NETWORK_ADDRESS_IPV4 = 0,
  183. ANODE_NETWORK_ADDRESS_IPV6 = 1,
  184. ANODE_NETWORK_ADDRESS_ETHERNET = 2, /* reserved but unused */
  185. ANODE_NETWORK_ADDRESS_USB = 3, /* reserved but unused */
  186. ANODE_NETWORK_ADDRESS_BLUETOOTH = 4, /* reserved but unused */
  187. ANODE_NETWORK_ADDRESS_IPC = 5, /* reserved but unused */
  188. ANODE_NETWORK_ADDRESS_80211S = 6, /* reserved but unused */
  189. ANODE_NETWORK_ADDRESS_SERIAL = 7, /* reserved but unused */
  190. ANODE_NETWORK_ADDRESS_ANODE_256_40 = 8
  191. };
  192. /**
  193. * Anode network address
  194. *
  195. * This can contain an address of any type: IPv4, IPv6, or Anode, and is used
  196. * with the common transport API.
  197. *
  198. * The length of the address stored in bits[] is determined by the type.
  199. */
  200. typedef struct
  201. {
  202. enum AnodeNetworkAddressType type;
  203. char bits[ANODE_ADDRESS_MAX_LENGTH];
  204. } AnodeNetworkAddress;
  205. /**
  206. * An endpoint with an address and a port
  207. */
  208. typedef struct
  209. {
  210. AnodeNetworkAddress address;
  211. int port;
  212. } AnodeNetworkEndpoint;
  213. /* Constants for binding to any address (v4 or v6) */
  214. extern const AnodeNetworkAddress AnodeNetworkAddress_IP_ANY_V4;
  215. extern const AnodeNetworkAddress AnodeNetworkAddress_IP_ANY_V6;
  216. /* Local host address in v4 and v6 */
  217. extern const AnodeNetworkAddress AnodeNetworkAddress_IP_LOCAL_V4;
  218. extern const AnodeNetworkAddress AnodeNetworkAddress_IP_LOCAL_V6;
  219. /**
  220. * Convert a network address to an ASCII string
  221. *
  222. * The buffer must have room for a 15 character string for IPv4, a 40 byte
  223. * string for IPv6, and a 64 byte string for Anode addresses. This does not
  224. * include the trailing null.
  225. *
  226. * @param address Address to convert
  227. * @param buf Buffer to receive address in string form
  228. * @param len Length of buffer
  229. * @return Length of resulting string or a negative error code on error
  230. */
  231. extern int AnodeNetworkAddress_to_string(const AnodeNetworkAddress *address,char *buf,int len);
  232. /**
  233. * Convert a string into a network address of the correct type
  234. *
  235. * @param str Address in string form
  236. * @param address Address buffer to receive result
  237. * @return Zero on sucess or error code on error
  238. */
  239. extern int AnodeNetworkAddress_from_string(const char *str,AnodeNetworkAddress *address);
  240. /**
  241. * Fill a network endpoint from a C-API sockaddr structure
  242. *
  243. * The argument must be struct sockaddr_in for IPv4 or sockaddr_in6 for IPv6.
  244. * The common sin_family field will be used to differentiate.
  245. *
  246. * @param sockaddr Pointer to proper sockaddr structure
  247. * @param endpoint Endpoint structure to fill
  248. * @return Zero on success or error on failure
  249. */
  250. extern int AnodeNetworkEndpoint_from_sockaddr(const void *sockaddr,AnodeNetworkEndpoint *endpoint);
  251. /**
  252. * Fill a sockaddr from a network endpoint
  253. *
  254. * To support either IPv4 or IPv6 addresses, there is a sockaddr_storage
  255. * structure in most C APIs. If you supply anything other than an IP address
  256. * such as an Anode address, this will return an error.
  257. *
  258. * @param endpoint Endpoint structure to convert
  259. * @param sockaddr Sockaddr structure storage
  260. * @param sockaddr_len Length of sockaddr structure storage in bytes
  261. * @return Zero on success or error on failure
  262. */
  263. extern int AnodeNetworkEndpoint_to_sockaddr(const AnodeNetworkEndpoint *endpoint,void *sockaddr,int sockaddr_len);
  264. /* ----------------------------------------------------------------------- */
  265. /* Identity Generation and Management */
  266. /* ----------------------------------------------------------------------- */
  267. /**
  268. * Anode identity structure containing address and secret key
  269. *
  270. * This structure is memcpy-safe, and its members are accessible.
  271. */
  272. typedef struct
  273. {
  274. /* The public Anode address */
  275. AnodeAddress address;
  276. /* Short address ID */
  277. AnodeAddressId address_id;
  278. /* The secret key corresponding with the public address */
  279. /* Secret length is determined by address type */
  280. char secret[ANODE_ADDRESS_MAX_SECRET_LENGTH];
  281. } AnodeIdentity;
  282. /**
  283. * Generate a new identity
  284. *
  285. * This generates a public/private key pair and from that generates an
  286. * identity containing an address and a secret key.
  287. *
  288. * @param identity Destination structure to store new identity
  289. * @param zone Zone ID
  290. * @param type Type of identity to generate
  291. * @return Zero on success, error on failure
  292. */
  293. extern int AnodeIdentity_generate(
  294. AnodeIdentity *identity,
  295. const AnodeZone *zone,
  296. enum AnodeAddressType type);
  297. /**
  298. * Convert an Anode identity to a string representation
  299. *
  300. * @param identity Identity to convert
  301. * @param dest String buffer
  302. * @param dest_len Length of string buffer
  303. * @return Length of string created or negative error code on failure
  304. */
  305. extern int AnodeIdentity_to_string(
  306. const AnodeIdentity *identity,
  307. char *dest,
  308. int dest_len);
  309. /**
  310. * Convert a string representation to an Anode identity structure
  311. *
  312. * @param identity Destination structure to fill
  313. * @param str C-string containing string representation
  314. * @return Zero on success or negative error code on failure
  315. */
  316. extern int AnodeIdentity_from_string(
  317. AnodeIdentity *identity,
  318. const char *str);
  319. /* ----------------------------------------------------------------------- */
  320. /* Transport API */
  321. /* ----------------------------------------------------------------------- */
  322. struct _AnodeTransport;
  323. typedef struct _AnodeTransport AnodeTransport;
  324. struct _AnodeEvent;
  325. typedef struct _AnodeEvent AnodeEvent;
  326. /**
  327. * Anode socket
  328. */
  329. typedef struct
  330. {
  331. /* Type of socket (read-only) */
  332. enum {
  333. ANODE_SOCKET_DATAGRAM = 1,
  334. ANODE_SOCKET_STREAM_LISTEN = 2,
  335. ANODE_SOCKET_STREAM_CONNECTION = 3
  336. } type;
  337. /* Socket state */
  338. enum {
  339. ANODE_SOCKET_CLOSED = 0,
  340. ANODE_SOCKET_OPEN = 1,
  341. ANODE_SOCKET_CONNECTING = 2,
  342. } state;
  343. /* Local address or remote address for stream connections (read-only) */
  344. AnodeNetworkEndpoint endpoint;
  345. /* Name of owning class (read-only) */
  346. const char *class_name;
  347. /* Pointers for end user use (writable) */
  348. void *user_ptr[2];
  349. /* Special handler to receive events or null for default (writable) */
  350. void (*event_handler)(const AnodeEvent *event);
  351. } AnodeSocket;
  352. /**
  353. * Anode transport I/O event
  354. */
  355. struct _AnodeEvent
  356. {
  357. enum {
  358. ANODE_TRANSPORT_EVENT_DATAGRAM_RECEIVED = 1,
  359. ANODE_TRANSPORT_EVENT_STREAM_INCOMING_CONNECT = 2,
  360. ANODE_TRANSPORT_EVENT_STREAM_OUTGOING_CONNECT_ESTABLISHED = 3,
  361. ANODE_TRANSPORT_EVENT_STREAM_OUTGOING_CONNECT_FAILED = 4,
  362. ANODE_TRANSPORT_EVENT_STREAM_CLOSED = 5,
  363. ANODE_TRANSPORT_EVENT_STREAM_DATA_RECEIVED = 6,
  364. ANODE_TRANSPORT_EVENT_STREAM_AVAILABLE_FOR_WRITE = 7,
  365. ANODE_TRANSPORT_EVENT_DNS_RESULT = 8
  366. } type;
  367. AnodeTransport *transport;
  368. /* Anode socket corresponding to this event */
  369. AnodeSocket *sock;
  370. /* Originating endpoint for incoming datagrams */
  371. AnodeNetworkEndpoint *datagram_from;
  372. /* DNS lookup results */
  373. const char *dns_name;
  374. AnodeNetworkAddress *dns_addresses;
  375. int dns_address_count;
  376. /* Error code or 0 for none */
  377. int error_code;
  378. /* Data for incoming datagrams and stream received events */
  379. int data_length;
  380. char *data;
  381. };
  382. /**
  383. * Enum used for dns_resolve method in transport to specify query rules
  384. *
  385. * This can be specified for ipv4, ipv6, and Anode address types to tell the
  386. * DNS resolver when to bother querying for addresses of the given type.
  387. * NEVER means to never query for this type, and ALWAYS means to always
  388. * query. IF_NO_PREVIOUS means to query for this type if no addresses were
  389. * found in previous queries. Addresses are queried in the order of ipv4,
  390. * ipv6, then Anode, so if you specify IF_NO_PREVIOUS for all three you will
  391. * get addresses in that order of priority.
  392. */
  393. enum AnodeTransportDnsIncludeMode
  394. {
  395. ANODE_TRANSPORT_DNS_QUERY_NEVER = 0,
  396. ANODE_TRANSPORT_DNS_QUERY_ALWAYS = 1,
  397. ANODE_TRANSPORT_DNS_QUERY_IF_NO_PREVIOUS = 2
  398. };
  399. struct _AnodeTransport
  400. {
  401. /**
  402. * Set the default event handler
  403. *
  404. * @param transport Transport engine
  405. * @param event_handler Default event handler
  406. */
  407. void (*set_default_event_handler)(AnodeTransport *transport,
  408. void (*event_handler)(const AnodeEvent *event));
  409. /**
  410. * Enqueue a function to be executed during a subsequent call to poll()
  411. *
  412. * This can be called from other threads, so it can be used to pass a
  413. * message to the I/O thread in multithreaded applications.
  414. *
  415. * If it is called from the same thread, the function is still queued to be
  416. * run later rather than being run instantly.
  417. *
  418. * The order in which invoked functions are called is undefined.
  419. *
  420. * @param transport Transport engine
  421. * @param ptr Arbitrary pointer to pass to function to be called
  422. * @param func Function to be called
  423. */
  424. void (*invoke)(AnodeTransport *transport,
  425. void *ptr,
  426. void (*func)(void *));
  427. /**
  428. * Initiate a forward DNS query
  429. *
  430. * @param transport Transport instance
  431. * @param name DNS name to query
  432. * @param event_handler Event handler or null for default event path
  433. * @param ipv4_include_mode Inclusion mode for IPv4 addresses
  434. * @param ipv6_include_mode Inclusion mode for IPv6 addresses
  435. * @param anode_include_mode Inclusion mode for Anode addresses
  436. */
  437. void (*dns_resolve)(AnodeTransport *transport,
  438. const char *name,
  439. void (*event_handler)(const AnodeEvent *),
  440. enum AnodeTransportDnsIncludeMode ipv4_include_mode,
  441. enum AnodeTransportDnsIncludeMode ipv6_include_mode,
  442. enum AnodeTransportDnsIncludeMode anode_include_mode);
  443. /**
  444. * Open a datagram socket
  445. *
  446. * @param transport Transport instance
  447. * @param local_address Local address to bind
  448. * @param local_port Local port to bind
  449. * @param error_code Value-result parameter to receive error code on error
  450. * @return Listen socket or null if error (check error_code in error case)
  451. */
  452. AnodeSocket *(*datagram_listen)(AnodeTransport *transport,
  453. const AnodeNetworkAddress *local_address,
  454. int local_port,
  455. int *error_code);
  456. /**
  457. * Open a socket to listen for incoming stream connections
  458. *
  459. * @param transport Transport instance
  460. * @param local_address Local address to bind
  461. * @param local_port Local port to bind
  462. * @param error_code Value-result parameter to receive error code on error
  463. * @return Listen socket or null if error (check error_code in error case)
  464. */
  465. AnodeSocket *(*stream_listen)(AnodeTransport *transport,
  466. const AnodeNetworkAddress *local_address,
  467. int local_port,
  468. int *error_code);
  469. /**
  470. * Send a datagram to a network endpoint
  471. *
  472. * @param transport Transport instance
  473. * @param socket Originating datagram socket
  474. * @param data Data to send
  475. * @param data_len Length of data to send
  476. * @param to_endpoint Destination endpoint
  477. * @return Zero on success or error code on error
  478. */
  479. int (*datagram_send)(AnodeTransport *transport,
  480. AnodeSocket *sock,
  481. const void *data,
  482. int data_len,
  483. const AnodeNetworkEndpoint *to_endpoint);
  484. /**
  485. * Initiate an outgoing stream connection attempt
  486. *
  487. * For IPv4 and IPv6 addresses, this will initiate a TCP connection. For
  488. * Anode addresses, Anode's internal streaming protocol will be used.
  489. *
  490. * @param transport Transport instance
  491. * @param to_endpoint Destination endpoint
  492. * @param error_code Error code value-result parameter, filled on error
  493. * @return Stream socket object or null on error (check error_code)
  494. */
  495. AnodeSocket *(*stream_connect)(AnodeTransport *transport,
  496. const AnodeNetworkEndpoint *to_endpoint,
  497. int *error_code);
  498. /**
  499. * Indicate that you are interested in writing to a stream
  500. *
  501. * This does nothing if the socket is not a stream connection or is not
  502. * connected.
  503. *
  504. * @param transport Transport instance
  505. * @param sock Stream connection
  506. */
  507. void (*stream_start_writing)(AnodeTransport *transport,
  508. AnodeSocket *sock);
  509. /**
  510. * Indicate that you are no longer interested in writing to a stream
  511. *
  512. * This does nothing if the socket is not a stream connection or is not
  513. * connected.
  514. *
  515. * @param transport Transport instance
  516. * @param sock Stream connection
  517. */
  518. void (*stream_stop_writing)(AnodeTransport *transport,
  519. AnodeSocket *sock);
  520. /**
  521. * Send data to a stream connection
  522. *
  523. * This must be called after a stream is indicated to be ready for writing.
  524. * It returns the number of bytes actually written, or a negative error
  525. * code on failure.
  526. *
  527. * A return value of zero can occur here, and simply indicates that nothing
  528. * was sent. This may occur with certain network stacks on certain
  529. * platforms.
  530. *
  531. * @param transport Transport engine
  532. * @param sock Stream socket
  533. * @param data Data to send
  534. * @param data_len Maximum data to send in bytes
  535. * @return Actual data sent or negative error code on error
  536. */
  537. int (*stream_send)(AnodeTransport *transport,
  538. AnodeSocket *sock,
  539. const void *data,
  540. int data_len);
  541. /**
  542. * Close a socket
  543. *
  544. * If the socket is a stream connection in the connected state, this
  545. * will generate a stream closed event with a zero error_code to indicate
  546. * a normal close.
  547. *
  548. * @param transport Transport engine
  549. * @param sock Socket object
  550. */
  551. void (*close)(AnodeTransport *transport,
  552. AnodeSocket *sock);
  553. /**
  554. * Run main polling loop
  555. *
  556. * This should be called repeatedly from the I/O thread of your main
  557. * process. It blocks until one or more events occur, and then returns
  558. * the number of events. Error returns here are fatal and indicate
  559. * serious problems such as build or platform issues or a lack of any
  560. * network interface.
  561. *
  562. * Functions queued with invoke() are also called inside here.
  563. *
  564. * @param transport Transport engine
  565. * @return Number of events handled or negative on (fatal) error
  566. */
  567. int (*poll)(AnodeTransport *transport);
  568. /**
  569. * Check whether transport supports an address type
  570. *
  571. * Inheriting classes should call their base if they do not natively
  572. * speak the specified type.
  573. *
  574. * @param transport Transport engine
  575. * @param at Address type
  576. * @return Nonzero if true
  577. */
  578. int (*supports_address_type)(const AnodeTransport *transport,
  579. enum AnodeNetworkAddressType at);
  580. /**
  581. * Get the instance of AnodeTransport under this one (if any)
  582. *
  583. * @param transport Transport engine
  584. * @return Base instance or null if none
  585. */
  586. AnodeTransport *(*base_instance)(const AnodeTransport *transport);
  587. /**
  588. * @param transport Transport engine
  589. * @return Class name of this instance
  590. */
  591. const char *(*class_name)(AnodeTransport *transport);
  592. /**
  593. * Delete this transport and its base transports
  594. *
  595. * The 'transport' pointer and any streams or sockets it owns are no longer
  596. * valid after this call.
  597. *
  598. * @param transport Transport engine
  599. */
  600. void (*delete)(AnodeTransport *transport);
  601. };
  602. /**
  603. * Construct a new system transport
  604. *
  605. * This is the default base for AnodeTransport, and it is constructed
  606. * automatically if 'base' is null in AnodeTransport_new(). However, it also
  607. * exposed to the user so that specialized transports (such as those that use
  608. * proxy servers) can be developed on top of it. These in turn can be supplied
  609. * as 'base' to AnodeTransport_new() to talk Anode over these transports.
  610. *
  611. * The system transport supports IP protocols and possibly others.
  612. *
  613. * @param base Base class or null for none (usually null)
  614. * @return Base transport engine instance
  615. */
  616. extern AnodeTransport *AnodeSystemTransport_new(AnodeTransport *base);
  617. /**
  618. * Construct a new Anode core transport
  619. *
  620. * This is the transport that talks Anode using the specified base transport.
  621. * Requests for other address types are passed through to the base. If the
  622. * base is null, an instance of AnodeSystemTransport is used.
  623. *
  624. * Since transport engines inherit their functionality, this transport
  625. * will also do standard IP and everything else that the system transport
  626. * supports. Most users will just want to construct this with a null base.
  627. *
  628. * @param base Base transport to use, or null to use SystemTransport
  629. * @return Anode transport engine or null on error
  630. */
  631. extern AnodeTransport *AnodeCoreTransport_new(AnodeTransport *base);
  632. /* ----------------------------------------------------------------------- */
  633. /* URI Parser */
  634. /* ----------------------------------------------------------------------- */
  635. /**
  636. * URI broken down by component
  637. */
  638. typedef struct
  639. {
  640. char scheme[8];
  641. char username[64];
  642. char password[64];
  643. char host[128];
  644. char path[256];
  645. char query[256];
  646. char fragment[64];
  647. int port;
  648. } AnodeURI;
  649. /**
  650. * URI parser
  651. *
  652. * A buffer too small error will occur if any field is too large for the
  653. * AnodeURI structure.
  654. *
  655. * @param parsed_uri Structure to fill with parsed URI data
  656. * @param uri_string URI in string format
  657. * @return Zero on success or error on failure
  658. */
  659. extern int AnodeURI_parse(AnodeURI *parsed_uri,const char *uri_string);
  660. /**
  661. * Output a URI in string format
  662. *
  663. * @param uri URI to output as string
  664. * @param buf Buffer to store URI string
  665. * @param len Length of buffer
  666. * @return Buffer or null on error
  667. */
  668. extern char *AnodeURI_to_string(const AnodeURI *uri,char *buf,int len);
  669. /* ----------------------------------------------------------------------- */
  670. /* Zone File Lookup and Dictionary */
  671. /* ----------------------------------------------------------------------- */
  672. /**
  673. * Zone file dictionary
  674. */
  675. typedef void AnodeZoneFile;
  676. /**
  677. * Start asynchronous zone fetch
  678. *
  679. * When the zone is retrieved, the lookup handler is called. If zone lookup
  680. * failed, the zone file argument to the handler will be null.
  681. *
  682. * @param transport Transport engine
  683. * @param zone Zone ID
  684. * @param user_ptr User pointer
  685. * @param zone_lookup_handler Handler for Anode zone lookup
  686. */
  687. extern void AnodeZoneFile_lookup(
  688. AnodeTransport *transport,
  689. const AnodeZone *zone,
  690. void *ptr,
  691. void (*zone_lookup_handler)(const AnodeZone *,AnodeZoneFile *,void *));
  692. /**
  693. * Look up a key in a zone file
  694. *
  695. * @param zone Zone file object
  696. * @param key Key to get in zone file
  697. */
  698. extern const char *AnodeZoneFile_get(const AnodeZoneFile *zone,const char *key);
  699. /**
  700. * Free a zone file
  701. *
  702. * @param zone Zone to free
  703. */
  704. extern void AnodeZoneFile_free(AnodeZoneFile *zone);
  705. /* ----------------------------------------------------------------------- */
  706. #ifdef __cplusplus
  707. }
  708. #endif
  709. #endif