/include/minix/netsock.h

http://www.minix3.org/ · C Header · 150 lines · 106 code · 29 blank · 15 comment · 3 complexity · 0f00a208e7aa9fd05d461c20bfd6e668 MD5 · raw file

  1. #ifndef __NET_SERVER_SOCKET_H__
  2. #define __NET_SERVER_SOCKET_H__
  3. #include <stdlib.h>
  4. #include <minix/ipc.h>
  5. #include <minix/endpoint.h>
  6. /*
  7. * User can set this variable to make the debugging output differ between
  8. * various users, e.g. "TCP" or "UDP"
  9. */
  10. extern char * netsock_user_name;
  11. #define SOCK_TYPE_IP 0
  12. #define SOCK_TYPE_TCP 1
  13. #define SOCK_TYPE_UDP 2
  14. #define SOCK_TYPES 3
  15. struct socket;
  16. typedef void (* sock_op_t)(struct socket *, message *);
  17. typedef void (* sock_op_io_t)(struct socket *, message *, int blk);
  18. typedef int (* sock_op_open_t)(struct socket *, message *);
  19. struct sock_ops {
  20. sock_op_open_t open;
  21. sock_op_t close;
  22. sock_op_io_t read;
  23. sock_op_io_t write;
  24. sock_op_io_t ioctl;
  25. sock_op_t select;
  26. sock_op_t select_reply;
  27. };
  28. struct recv_q {
  29. struct recv_q * next;
  30. void * data;
  31. };
  32. #define SOCK_FLG_OP_PENDING 0x1
  33. #define SOCK_FLG_OP_IOCTL 0x10
  34. #define SOCK_FLG_OP_LISTENING 0x100 /* tcp socket is in a listening mode */
  35. #define SOCK_FLG_OP_CONNECTING 0x200 /* set when waiting for a connect */
  36. #define SOCK_FLG_OP_READING 0x400 /* reading operation in progress */
  37. #define SOCK_FLG_OP_WRITING 0x800 /* writing operation in progress */
  38. #define SOCK_FLG_CLOSED 0x1000 /* tcp socket has been closed do not
  39. expect any more data */
  40. /* select() flags - they say what action do we monitor */
  41. #define SOCK_FLG_SEL_WRITE 0x100000
  42. #define SOCK_FLG_SEL_READ 0x200000
  43. #define SOCK_FLG_SEL_ERROR 0x400000
  44. #define sock_select_set(sock) ((sock)->flags & (SOCK_FLG_SEL_WRITE | \
  45. SOCK_FLG_SEL_READ | SOCK_FLG_SEL_ERROR))
  46. #define sock_select_read_set(sock) ((sock)->flags & SOCK_FLG_SEL_READ)
  47. #define sock_select_write_set(sock) ((sock)->flags & SOCK_FLG_SEL_WRITE)
  48. #define sock_select_rw_set(sock) ((sock)->flags & (SOCK_FLG_SEL_READ | \
  49. SOCK_FLG_SEL_WRITE))
  50. #define sock_select_error_set(sock) ((sock)->flags & SOCK_FLG_SEL_ERROR)
  51. #define sock_clear_select(sock) do { \
  52. (sock)->flags &= ~(SOCK_FLG_SEL_READ | SOCK_FLG_SEL_WRITE | \
  53. SOCK_FLG_SEL_ERROR); \
  54. } while (0)
  55. struct socket {
  56. int type;
  57. u32_t flags;
  58. unsigned long usr_flags;
  59. void * pcb;
  60. struct sock_ops * ops;
  61. void * buf;
  62. size_t buf_size;
  63. message mess; /* store the message which initiated the
  64. last operation on this socket in case
  65. we have to suspend the operation */
  66. void * shm;
  67. size_t shm_size;
  68. endpoint_t select_ep;
  69. struct recv_q * recv_head;
  70. struct recv_q * recv_tail;
  71. unsigned recv_data_size; /* sum of data enqueued */
  72. void * data;
  73. };
  74. /*
  75. * Each component needs to provide a method how to initially open a socket.
  76. * The rest is handled byt the socket library.
  77. */
  78. void socket_open(message * m);
  79. #define get_sock_num(x) ((long int) ((x) - socket))
  80. #define is_valid_sock_num(x) (x < MAX_SOCKETS)
  81. #define get_sock(x) &socket[x]
  82. #define MAX_SOCKETS 255 /* FIXME as log as the sockets are identified by the
  83. minor device number 255 is ok */
  84. #define MAX_DEVS 5
  85. #define RESERVED (SOCK_TYPES + MAX_DEVS) /* rounded to 8 */
  86. extern struct socket socket[MAX_SOCKETS];
  87. void socket_request(message * m);
  88. void mq_process(void);
  89. struct socket * get_unused_sock(void);
  90. struct socket * get_nic_sock(unsigned dev);
  91. void send_reply(message * m, int status);
  92. void send_reply_open(message * m, int status);
  93. void send_reply_close(message * m, int status);
  94. void sock_reply(struct socket * sock, int status);
  95. void sock_reply_close(struct socket * sock, int status);
  96. void sock_reply_select(struct socket * sock, unsigned selops);
  97. typedef void (* recv_data_free_fn)(void *);
  98. int sock_enqueue_data(struct socket * sock, void * data, unsigned size);
  99. void * sock_dequeue_data(struct socket * sock);
  100. void sock_dequeue_data_all(struct socket * sock,
  101. recv_data_free_fn data_free);
  102. void sock_select_notify(struct socket * sock);
  103. static inline void * debug_malloc(size_t s)
  104. {
  105. void * ret;
  106. ret = malloc(s);
  107. // printf("allocated %p size %d\n", ret, s);
  108. return ret;
  109. }
  110. #define debug_free(x) do { \
  111. if (0) \
  112. printf("free called from %s:%d %s freeing %p\n", __FILE__, \
  113. __LINE__, __func__, (x)); \
  114. free(x); \
  115. } while(0)
  116. void generic_op_select(struct socket * sock, message * m);
  117. void generic_op_select_reply(struct socket * sock, message * m);
  118. int mq_enqueue(message * m);
  119. /* a function thr user has to provide to reply to the posix server */
  120. void posix_reply(endpoint_t ep, message * m);
  121. #endif /* __NET_SERVER_SOCKET_H__ */