socket.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <Kernel/API/POSIX/sys/socket.h>
  8. #include <sys/un.h>
  9. __BEGIN_DECLS
  10. int socket(int domain, int type, int protocol);
  11. int bind(int sockfd, const struct sockaddr* addr, socklen_t);
  12. int listen(int sockfd, int backlog);
  13. int accept(int sockfd, struct sockaddr*, socklen_t*);
  14. int accept4(int sockfd, struct sockaddr*, socklen_t*, int);
  15. int connect(int sockfd, const struct sockaddr*, socklen_t);
  16. int shutdown(int sockfd, int how);
  17. ssize_t send(int sockfd, void const*, size_t, int flags);
  18. ssize_t sendmsg(int sockfd, const struct msghdr*, int flags);
  19. ssize_t sendto(int sockfd, void const*, size_t, int flags, const struct sockaddr*, socklen_t);
  20. ssize_t recv(int sockfd, void*, size_t, int flags);
  21. ssize_t recvmsg(int sockfd, struct msghdr*, int flags);
  22. ssize_t recvfrom(int sockfd, void*, size_t, int flags, struct sockaddr*, socklen_t*);
  23. int getsockopt(int sockfd, int level, int option, void*, socklen_t*);
  24. int setsockopt(int sockfd, int level, int option, void const*, socklen_t);
  25. int getsockname(int sockfd, struct sockaddr*, socklen_t*);
  26. int getpeername(int sockfd, struct sockaddr*, socklen_t*);
  27. int socketpair(int domain, int type, int protocol, int sv[2]);
  28. int sendfd(int sockfd, int fd);
  29. int recvfd(int sockfd, int options);
  30. // These three are non-POSIX, but common:
  31. #define CMSG_ALIGN(x) (((x) + sizeof(void*) - 1) & ~(sizeof(void*) - 1))
  32. #define CMSG_SPACE(x) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(x))
  33. #define CMSG_LEN(x) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (x))
  34. static inline struct cmsghdr* CMSG_FIRSTHDR(struct msghdr* msg)
  35. {
  36. if (msg->msg_controllen < sizeof(struct cmsghdr))
  37. return 0;
  38. return (struct cmsghdr*)msg->msg_control;
  39. }
  40. static inline struct cmsghdr* CMSG_NXTHDR(struct msghdr* msg, struct cmsghdr* cmsg)
  41. {
  42. struct cmsghdr* next = (struct cmsghdr*)((char*)cmsg + CMSG_ALIGN(cmsg->cmsg_len));
  43. unsigned offset = (char*)next - (char*)msg->msg_control;
  44. if (msg->msg_controllen < offset + sizeof(struct cmsghdr))
  45. return NULL;
  46. return next;
  47. }
  48. static inline void* CMSG_DATA(struct cmsghdr* cmsg)
  49. {
  50. return (void*)(cmsg + 1);
  51. }
  52. __END_DECLS