xref: /haiku/headers/private/net/net_socket.h (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
1 /*
2  * Copyright 2006, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef NET_SOCKET_H
6 #define NET_SOCKET_H
7 
8 
9 #include <net_buffer.h>
10 #include <sys/socket.h>
11 
12 #include <Select.h>
13 #include <lock.h>
14 
15 
16 #define NET_SOCKET_MODULE_NAME "network/stack/socket/v1"
17 
18 typedef struct net_socket {
19 	struct net_protocol		*first_protocol;
20 	struct net_protocol_module_info *first_info;
21 
22 	int						family;
23 	int						type;
24 	int						protocol;
25 
26 	struct sockaddr_storage	address;
27 	struct sockaddr_storage	peer;
28 
29 	int						options;
30 	int						linger;
31 
32 	struct {
33 		uint32		buffer_size;
34 		uint32		low_water_mark;
35 		bigtime_t	timeout;
36 	}						send, receive;
37 
38 	status_t				error;
39 	struct net_socket		*parent;
40 } net_socket;
41 
42 struct net_socket_module_info {
43 	struct module_info info;
44 
45 	status_t	(*open_socket)(int family, int type, int protocol, net_socket **_socket);
46 	status_t	(*close)(net_socket *socket);
47 	status_t	(*free)(net_socket *socket);
48 
49 	status_t	(*readv)(net_socket *socket, const iovec *vecs, size_t vecCount,
50 					size_t *_length);
51 	status_t	(*writev)(net_socket *socket, const iovec *vecs, size_t vecCount,
52 					size_t *_length);
53 	status_t	(*control)(net_socket *socket, int32 op, void *data, size_t length);
54 
55 	ssize_t		(*read_avail)(net_socket *socket);
56 	ssize_t		(*send_avail)(net_socket *socket);
57 
58 	status_t	(*send_data)(net_socket *socket, net_buffer *buffer);
59 	status_t	(*receive_data)(net_socket *socket, size_t length, uint32 flags,
60 					net_buffer **_buffer);
61 
62 	status_t	(*get_next_stat)(uint32 *cookie, int family, struct net_stat *stat);
63 
64 	// connections
65 	status_t	(*spawn_pending_socket)(net_socket *parent, net_socket **_socket);
66 	void		(*delete_socket)(net_socket *socket);
67 	status_t	(*dequeue_connected)(net_socket *parent, net_socket **_socket);
68 	status_t	(*set_max_backlog)(net_socket *socket, uint32 backlog);
69 	status_t	(*set_connected)(net_socket *socket);
70 
71 	// notifications
72 	status_t	(*request_notification)(net_socket *socket, uint8 event, uint32 ref,
73 					selectsync *sync);
74 	status_t	(*cancel_notification)(net_socket *socket, uint8 event,
75 					selectsync *sync);
76 	status_t	(*notify)(net_socket *socket, uint8 event, int32 value);
77 
78 	// standard socket API
79 	int			(*accept)(net_socket *socket, struct sockaddr *address,
80 					socklen_t *_addressLength, net_socket **_acceptedSocket);
81 	int			(*bind)(net_socket *socket, const struct sockaddr *address,
82 					socklen_t addressLength);
83 	int			(*connect)(net_socket *socket, const struct sockaddr *address,
84 					socklen_t addressLength);
85 	int			(*getpeername)(net_socket *socket, struct sockaddr *address,
86 					socklen_t *_addressLength);
87 	int			(*getsockname)(net_socket *socket, struct sockaddr *address,
88 					socklen_t *_addressLength);
89 	int			(*getsockopt)(net_socket *socket, int level, int option,
90 					void *optionValue, int *_optionLength);
91 	int			(*listen)(net_socket *socket, int backlog);
92 	ssize_t		(*receive)(net_socket *socket, struct msghdr *, void *data,
93 					size_t length, int flags);
94 	ssize_t		(*send)(net_socket *socket, struct msghdr *, const void *data,
95 					size_t length, int flags);
96 	int			(*setsockopt)(net_socket *socket, int level, int option,
97 					const void *optionValue, int optionLength);
98 	int			(*shutdown)(net_socket *socket, int direction);
99 };
100 
101 #endif	// NET_SOCKET_H
102