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	(*control)(net_socket* socket, uint32 op, void* data,
56					size_t length);
57
58	ssize_t		(*read_avail)(net_socket* socket);
59	ssize_t		(*send_avail)(net_socket* socket);
60
61	status_t	(*send_data)(net_socket* socket, net_buffer* buffer);
62	status_t	(*receive_data)(net_socket* socket, size_t length,
63					uint32 flags, net_buffer** _buffer);
64
65	status_t	(*get_option)(net_socket* socket, int level, int option,
66					void* value, int* _length);
67	status_t	(*set_option)(net_socket* socket, int level, int option,
68					const void* value, int length);
69
70	status_t	(*get_next_stat)(uint32* cookie, int family,
71					struct net_stat* stat);
72
73	// connections
74	bool		(*acquire_socket)(net_socket* socket);
75	bool		(*release_socket)(net_socket* socket);
76
77	status_t	(*spawn_pending_socket)(net_socket* parent,
78					net_socket** _socket);
79	status_t	(*dequeue_connected)(net_socket* parent, net_socket** _socket);
80	ssize_t		(*count_connected)(net_socket* parent);
81	status_t	(*set_max_backlog)(net_socket* socket, uint32 backlog);
82	bool		(*has_parent)(net_socket* socket);
83	status_t	(*set_connected)(net_socket* socket);
84	status_t	(*set_aborted)(net_socket* socket);
85
86	// notifications
87	status_t	(*request_notification)(net_socket* socket, uint8 event,
88					struct selectsync* sync);
89	status_t	(*cancel_notification)(net_socket* socket, uint8 event,
90					struct selectsync* sync);
91	status_t	(*notify)(net_socket* socket, uint8 event, int32 value);
92
93	// standard socket API
94	int			(*accept)(net_socket* socket, struct sockaddr* address,
95					socklen_t* _addressLength, net_socket** _acceptedSocket);
96	int			(*bind)(net_socket* socket, const struct sockaddr* address,
97					socklen_t addressLength);
98	int			(*connect)(net_socket* socket, const struct sockaddr* address,
99					socklen_t addressLength);
100	int			(*getpeername)(net_socket* socket, struct sockaddr* address,
101					socklen_t* _addressLength);
102	int			(*getsockname)(net_socket* socket, struct sockaddr* address,
103					socklen_t* _addressLength);
104	int			(*getsockopt)(net_socket* socket, int level, int option,
105					void* optionValue, int* _optionLength);
106	int			(*listen)(net_socket* socket, int backlog);
107	ssize_t		(*receive)(net_socket* socket, struct msghdr* , void* data,
108					size_t length, int flags);
109	ssize_t		(*send)(net_socket* socket, struct msghdr* , const void* data,
110					size_t length, int flags);
111	int			(*setsockopt)(net_socket* socket, int level, int option,
112					const void* optionValue, int optionLength);
113	int			(*shutdown)(net_socket* socket, int direction);
114	status_t	(*socketpair)(int family, int type, int protocol,
115					net_socket* _sockets[2]);
116};
117
118
119#endif	// NET_SOCKET_H
120