1/*
2 * Copyright 2006-2010, 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 <lock.h>
13
14
15struct net_stat;
16struct selectsync;
17
18
19#define NET_SOCKET_MODULE_NAME "network/stack/socket/v1"
20
21
22typedef struct net_socket {
23	struct net_protocol*	first_protocol;
24	struct net_protocol_module_info* first_info;
25
26	int						family;
27	int						type;
28	int						protocol;
29
30	struct sockaddr_storage	address;
31	struct sockaddr_storage	peer;
32
33	int						options;
34	int						linger;
35	uint32					bound_to_device;
36
37	struct {
38		uint32		buffer_size;
39		uint32		low_water_mark;
40		bigtime_t	timeout;
41	}						send, receive;
42
43	status_t				error;
44} net_socket;
45
46
47struct net_socket_module_info {
48	struct module_info info;
49
50	status_t	(*open_socket)(int family, int type, int protocol,
51					net_socket** _socket);
52	status_t	(*close)(net_socket* socket);
53	void		(*free)(net_socket* socket);
54
55	status_t	(*readv)(net_socket* socket, const iovec* vecs,
56					size_t vecCount, size_t* _length);
57	status_t	(*writev)(net_socket* socket, const iovec* vecs,
58					size_t vecCount, size_t* _length);
59	status_t	(*control)(net_socket* socket, int32 op, void* data,
60					size_t length);
61
62	ssize_t		(*read_avail)(net_socket* socket);
63	ssize_t		(*send_avail)(net_socket* socket);
64
65	status_t	(*send_data)(net_socket* socket, net_buffer* buffer);
66	status_t	(*receive_data)(net_socket* socket, size_t length,
67					uint32 flags, net_buffer** _buffer);
68
69	status_t	(*get_option)(net_socket* socket, int level, int option,
70					void* value, int* _length);
71	status_t	(*set_option)(net_socket* socket, int level, int option,
72					const void* value, int length);
73
74	status_t	(*get_next_stat)(uint32* cookie, int family,
75					struct net_stat* stat);
76
77	// connections
78	bool		(*acquire_socket)(net_socket* socket);
79	bool		(*release_socket)(net_socket* socket);
80
81	status_t	(*spawn_pending_socket)(net_socket* parent,
82					net_socket** _socket);
83	status_t	(*dequeue_connected)(net_socket* parent, net_socket** _socket);
84	ssize_t		(*count_connected)(net_socket* parent);
85	status_t	(*set_max_backlog)(net_socket* socket, uint32 backlog);
86	bool		(*has_parent)(net_socket* socket);
87	status_t	(*set_connected)(net_socket* socket);
88	status_t	(*set_aborted)(net_socket* socket);
89
90	// notifications
91	status_t	(*request_notification)(net_socket* socket, uint8 event,
92					struct selectsync* sync);
93	status_t	(*cancel_notification)(net_socket* socket, uint8 event,
94					struct selectsync* sync);
95	status_t	(*notify)(net_socket* socket, uint8 event, int32 value);
96
97	// standard socket API
98	int			(*accept)(net_socket* socket, struct sockaddr* address,
99					socklen_t* _addressLength, net_socket** _acceptedSocket);
100	int			(*bind)(net_socket* socket, const struct sockaddr* address,
101					socklen_t addressLength);
102	int			(*connect)(net_socket* socket, const struct sockaddr* address,
103					socklen_t addressLength);
104	int			(*getpeername)(net_socket* socket, struct sockaddr* address,
105					socklen_t* _addressLength);
106	int			(*getsockname)(net_socket* socket, struct sockaddr* address,
107					socklen_t* _addressLength);
108	int			(*getsockopt)(net_socket* socket, int level, int option,
109					void* optionValue, int* _optionLength);
110	int			(*listen)(net_socket* socket, int backlog);
111	ssize_t		(*receive)(net_socket* socket, struct msghdr* , void* data,
112					size_t length, int flags);
113	ssize_t		(*send)(net_socket* socket, struct msghdr* , const void* data,
114					size_t length, int flags);
115	int			(*setsockopt)(net_socket* socket, int level, int option,
116					const void* optionValue, int optionLength);
117	int			(*shutdown)(net_socket* socket, int direction);
118	status_t	(*socketpair)(int family, int type, int protocol,
119					net_socket* _sockets[2]);
120};
121
122
123#endif	// NET_SOCKET_H
124