sock.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef SOCK_H_
  2. #define SOCK_H_
  3. /**
  4. * Address strings are of the following forms:
  5. * /path/to/unix/socket
  6. * [ip.v4.ad.dr]:port
  7. * [ipv6:add::ress]:port
  8. * host.name:port
  9. */
  10. /* Opaque address structure. */
  11. struct sock_addr;
  12. /**
  13. * sock_resolve(addr):
  14. * Return a NULL-terminated array of pointers to sock_addr structures.
  15. */
  16. struct sock_addr ** sock_resolve(const char *);
  17. /**
  18. * sock_resolve_one(addr, addport):
  19. * Return a single sock_addr structure, or NULL if there are no addresses.
  20. * Warn if there is more than one address, and return the first one.
  21. * If ${addport} is non-zero, use sock_addr_ensure_port() to add a port number
  22. * of ":0" if appropriate.
  23. */
  24. struct sock_addr * sock_resolve_one(const char *, int);
  25. /**
  26. * sock_listener(sa):
  27. * Create a socket, attempt to set SO_REUSEADDR, bind it to the socket address
  28. * ${sa}, mark it for listening, and mark it as non-blocking.
  29. */
  30. int sock_listener(const struct sock_addr *);
  31. /**
  32. * sock_connect(sas):
  33. * Iterate through the addresses in ${sas}, attempting to create a socket and
  34. * connect (blockingly). Once connected, stop iterating, mark the socket as
  35. * non-blocking, and return it.
  36. */
  37. int sock_connect(struct sock_addr * const *);
  38. /**
  39. * sock_connect_nb(sa):
  40. * Create a socket, mark it as non-blocking, and attempt to connect to the
  41. * address ${sa}. Return the socket (connected or in the process of
  42. * connecting) or -1 on error.
  43. */
  44. int sock_connect_nb(const struct sock_addr *);
  45. /**
  46. * sock_connect_bind_nb(sa, sa_b):
  47. * Create a socket, mark it as non-blocking, and attempt to connect to the
  48. * address ${sa}. If ${sa_b} is not NULL, attempt to set SO_REUSEADDR on the
  49. * socket and bind it to ${sa_b} immediately after creating it. Return the
  50. * socket (connected or in the process of connecting) or -1 on error.
  51. */
  52. int sock_connect_bind_nb(const struct sock_addr *, const struct sock_addr *);
  53. /**
  54. * sock_addr_free(sa):
  55. * Free the provided sock_addr structure.
  56. */
  57. void sock_addr_free(struct sock_addr *);
  58. /**
  59. * sock_addr_freelist(sas):
  60. * Free the provided NULL-terminated array of sock_addr structures.
  61. */
  62. void sock_addr_freelist(struct sock_addr **);
  63. #endif /* !SOCK_H_ */