net.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* Licensed under BSD-MIT - see LICENSE file for details */
  2. #ifndef CCAN_NET_H
  3. #define CCAN_NET_H
  4. /**
  5. * net_client_lookup - look up a network name to connect to.
  6. * @hostname: the name to look up
  7. * @service: the service to look up
  8. * @family: Usually AF_UNSPEC, otherwise AF_INET or AF_INET6.
  9. * @socktype: SOCK_DGRAM or SOCK_STREAM.
  10. *
  11. * This will do a synchronous lookup of a given name, returning a linked list
  12. * of results, or NULL on error. You should use freeaddrinfo() to free it.
  13. *
  14. * Example:
  15. * #include <sys/types.h>
  16. * #include <sys/socket.h>
  17. * #include <stdio.h>
  18. * #include <netdb.h>
  19. * #include <err.h>
  20. * ...
  21. * struct addrinfo *addr;
  22. *
  23. * // Get a TCP connection to ccan.ozlabs.org daytime port.
  24. * addr = net_client_lookup("ccan.ozlabs.org", "daytime",
  25. * AF_UNSPEC, SOCK_STREAM);
  26. * if (!addr)
  27. * errx(1, "Failed to look up daytime at ccan.ozlabs.org");
  28. */
  29. struct addrinfo *net_client_lookup(const char *hostname,
  30. const char *service,
  31. int family,
  32. int socktype);
  33. /**
  34. * net_connect - connect to a server
  35. * @addrinfo: linked list struct addrinfo (usually from net_client_lookup).
  36. *
  37. * This synchronously connects to a server described by @addrinfo, or returns
  38. * -1 on error (and sets errno).
  39. *
  40. * Example:
  41. * int fd;
  42. * ...
  43. * fd = net_connect(addr);
  44. * if (fd < 0)
  45. * err(1, "Failed to connect to ccan.ozlabs.org");
  46. * freeaddrinfo(addr);
  47. */
  48. int net_connect(const struct addrinfo *addrinfo);
  49. #endif /* CCAN_NET_H */