usock.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * usock - socket helper functions
  3. *
  4. * Copyright (C) 2010 Steven Barth <steven@midlink.org>
  5. * Copyright (C) 2011-2012 Felix Fietkau <nbd@openwrt.org>
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  14. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. #ifndef USOCK_H_
  20. #define USOCK_H_
  21. #define USOCK_TCP 0
  22. #define USOCK_UDP 1
  23. #define USOCK_SERVER 0x0100
  24. #define USOCK_NOCLOEXEC 0x0200
  25. #define USOCK_NONBLOCK 0x0400
  26. #define USOCK_NUMERIC 0x0800
  27. #define USOCK_IPV6ONLY 0x2000
  28. #define USOCK_IPV4ONLY 0x4000
  29. #define USOCK_UNIX 0x8000
  30. const char *usock_port(int port);
  31. int usock(int type, const char *host, const char *service);
  32. int usock_inet_timeout(int type, const char *host, const char *service,
  33. void *addr, int timeout);
  34. static inline int
  35. usock_inet(int type, const char *host, const char *service, void *addr)
  36. {
  37. return usock_inet_timeout(type, host, service, addr, -1);
  38. }
  39. /**
  40. * Wait for a socket to become ready.
  41. *
  42. * This may be useful for users of USOCK_NONBLOCK to wait (with a timeout)
  43. * for a socket.
  44. *
  45. * @param fd file descriptor of socket
  46. * @param msecs timeout in microseconds
  47. */
  48. int usock_wait_ready(int fd, int msecs);
  49. #endif /* USOCK_H_ */