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