make vfs & filesystems use failable copying
[minix3.git] / include / minix / netsock.h
blob1a53813efa24172fa7759c06c65b8a41c3720b65
1 #ifndef __NET_SERVER_SOCKET_H__
2 #define __NET_SERVER_SOCKET_H__
4 #include <stdlib.h>
6 #include <minix/ipc.h>
7 #include <minix/endpoint.h>
8 #include <minix/chardriver.h>
11 * User can set this variable to make the debugging output differ between
12 * various users, e.g. "TCP" or "UDP"
14 extern char * netsock_user_name;
16 #define SOCK_TYPE_IP 0
17 #define SOCK_TYPE_TCP 1
18 #define SOCK_TYPE_UDP 2
19 #define SOCK_TYPES 3
21 struct socket;
22 struct sock_req;
24 typedef int (* sock_op_opcl_t)(struct socket *);
25 typedef int (* sock_op_io_t)(struct socket *, struct sock_req *, int blk);
26 typedef int (* sock_op_select_t)(struct socket *, unsigned int);
27 typedef int (* sock_op_select_reply_t)(struct socket *);
29 struct sock_ops {
30 sock_op_opcl_t open;
31 sock_op_opcl_t close;
32 sock_op_io_t read;
33 sock_op_io_t write;
34 sock_op_io_t ioctl;
35 sock_op_select_t select;
36 sock_op_select_reply_t select_reply;
39 struct recv_q {
40 struct recv_q * next;
41 void * data;
44 #define SOCK_FLG_OP_PENDING 0x1
45 #define SOCK_FLG_OP_IOCTL 0x10
46 #define SOCK_FLG_OP_LISTENING 0x100 /* tcp socket is in a listening mode */
47 #define SOCK_FLG_OP_CONNECTING 0x200 /* set when waiting for a connect */
48 #define SOCK_FLG_OP_READING 0x400 /* reading operation in progress */
49 #define SOCK_FLG_OP_WRITING 0x800 /* writing operation in progress */
50 #define SOCK_FLG_CLOSED 0x1000 /* tcp socket has been closed do not
51 expect any more data */
52 /* select() flags - they say what action do we monitor */
53 #define SOCK_FLG_SEL_WRITE 0x100000
54 #define SOCK_FLG_SEL_READ 0x200000
55 #define SOCK_FLG_SEL_ERROR 0x400000
57 #define sock_select_set(sock) ((sock)->flags & (SOCK_FLG_SEL_WRITE | \
58 SOCK_FLG_SEL_READ | SOCK_FLG_SEL_ERROR))
59 #define sock_select_read_set(sock) ((sock)->flags & SOCK_FLG_SEL_READ)
60 #define sock_select_write_set(sock) ((sock)->flags & SOCK_FLG_SEL_WRITE)
61 #define sock_select_rw_set(sock) ((sock)->flags & (SOCK_FLG_SEL_READ | \
62 SOCK_FLG_SEL_WRITE))
63 #define sock_select_error_set(sock) ((sock)->flags & SOCK_FLG_SEL_ERROR)
64 #define sock_clear_select(sock) do { \
65 (sock)->flags &= ~(SOCK_FLG_SEL_READ | SOCK_FLG_SEL_WRITE | \
66 SOCK_FLG_SEL_ERROR); \
67 } while (0)
69 struct sock_req {
70 enum {
71 SOCK_REQ_READ,
72 SOCK_REQ_WRITE,
73 SOCK_REQ_IOCTL
74 } type;
75 devminor_t minor;
76 endpoint_t endpt;
77 cp_grant_id_t grant;
78 union {
79 size_t size; /* for SOCK_REQ_READ, SOCK_REQ_WRITE */
80 unsigned long req; /* for SOCK_REQ_IOCTL */
82 int flags;
83 cdev_id_t id;
86 struct socket {
87 int type;
88 u32_t flags;
89 unsigned long usr_flags;
90 void * pcb;
91 struct sock_ops * ops;
92 void * buf;
93 size_t buf_size;
94 struct sock_req req;
95 void * shm;
96 size_t shm_size;
97 endpoint_t select_ep;
98 struct recv_q * recv_head;
99 struct recv_q * recv_tail;
100 unsigned recv_data_size; /* sum of data enqueued */
101 void * data;
105 * Each component needs to provide a method how to initially open a socket.
106 * The rest is handled byt the socket library.
108 int socket_open(devminor_t minor);
110 #define get_sock_num(x) ((long int) ((x) - socket))
111 #define is_valid_sock_num(x) (x < MAX_SOCKETS)
112 #define get_sock(x) &socket[x]
114 #define MAX_SOCKETS 255 /* FIXME as log as the sockets are identified by the
115 minor device number 255 is ok */
116 #define MAX_DEVS 5
117 #define RESERVED (SOCK_TYPES + MAX_DEVS) /* rounded to 8 */
119 extern struct socket socket[MAX_SOCKETS];
121 void socket_request(message * m, int ipc_status);
122 void mq_process(void);
124 struct socket * get_unused_sock(void);
125 struct socket * get_nic_sock(unsigned dev);
127 void send_req_reply(struct sock_req * req, int status);
129 typedef void (* recv_data_free_fn)(void *);
131 int sock_enqueue_data(struct socket * sock, void * data, unsigned size);
132 void * sock_dequeue_data(struct socket * sock);
133 void sock_dequeue_data_all(struct socket * sock,
134 recv_data_free_fn data_free);
136 void sock_select_notify(struct socket * sock);
138 static inline void * debug_malloc(size_t s)
140 void * ret;
142 ret = malloc(s);
143 // printf("allocated %p size %d\n", ret, s);
144 return ret;
147 #define debug_free(x) do { \
148 if (0) \
149 printf("free called from %s:%d %s freeing %p\n", __FILE__, \
150 __LINE__, __func__, (x)); \
151 free(x); \
152 } while(0)
154 int generic_op_select(struct socket * sock, unsigned int sel);
155 int generic_op_select_reply(struct socket * sock);
157 /* a function thr user has to provide to reply to the posix server */
158 void posix_reply(endpoint_t ep, message * m);
160 #endif /* __NET_SERVER_SOCKET_H__ */