1c22d69bfSAxel Dörfler/*
203e02ed9SAxel Dörfler * Copyright 2006-2010, Haiku, Inc. All Rights Reserved.
3c22d69bfSAxel Dörfler * Distributed under the terms of the MIT License.
4c22d69bfSAxel Dörfler */
5c22d69bfSAxel Dörfler#ifndef NET_SOCKET_H
6c22d69bfSAxel Dörfler#define NET_SOCKET_H
7c22d69bfSAxel Dörfler
8c22d69bfSAxel Dörfler
9c22d69bfSAxel Dörfler#include <net_buffer.h>
10c22d69bfSAxel Dörfler#include <sys/socket.h>
11c22d69bfSAxel Dörfler
12a7028ce6SAxel Dörfler#include <lock.h>
13a7028ce6SAxel Dörfler
1403e02ed9SAxel Dörfler
1503e02ed9SAxel Dörflerstruct net_stat;
1615ab0bcfSAxel Dörflerstruct selectsync;
1715ab0bcfSAxel Dörfler
18c22d69bfSAxel Dörfler
19c22d69bfSAxel Dörfler#define NET_SOCKET_MODULE_NAME "network/stack/socket/v1"
20c22d69bfSAxel Dörfler
2103e02ed9SAxel Dörfler
22c22d69bfSAxel Dörflertypedef struct net_socket {
2303e02ed9SAxel Dörfler	struct net_protocol*	first_protocol;
2403e02ed9SAxel Dörfler	struct net_protocol_module_info* first_info;
25c22d69bfSAxel Dörfler
26c22d69bfSAxel Dörfler	int						family;
27c22d69bfSAxel Dörfler	int						type;
28c22d69bfSAxel Dörfler	int						protocol;
29c22d69bfSAxel Dörfler
30c22d69bfSAxel Dörfler	struct sockaddr_storage	address;
31c22d69bfSAxel Dörfler	struct sockaddr_storage	peer;
32c22d69bfSAxel Dörfler
33c22d69bfSAxel Dörfler	int						options;
34c22d69bfSAxel Dörfler	int						linger;
3589bf19ccSAxel Dörfler	uint32					bound_to_device;
36c22d69bfSAxel Dörfler
37c22d69bfSAxel Dörfler	struct {
38c22d69bfSAxel Dörfler		uint32		buffer_size;
39c22d69bfSAxel Dörfler		uint32		low_water_mark;
40c22d69bfSAxel Dörfler		bigtime_t	timeout;
41c22d69bfSAxel Dörfler	}						send, receive;
42a7028ce6SAxel Dörfler
43a7028ce6SAxel Dörfler	status_t				error;
44c22d69bfSAxel Dörfler} net_socket;
45c22d69bfSAxel Dörfler
4603e02ed9SAxel Dörfler
47c22d69bfSAxel Dörflerstruct net_socket_module_info {
48c22d69bfSAxel Dörfler	struct module_info info;
49c22d69bfSAxel Dörfler
5015ab0bcfSAxel Dörfler	status_t	(*open_socket)(int family, int type, int protocol,
5103e02ed9SAxel Dörfler					net_socket** _socket);
5203e02ed9SAxel Dörfler	status_t	(*close)(net_socket* socket);
5303e02ed9SAxel Dörfler	void		(*free)(net_socket* socket);
5403e02ed9SAxel Dörfler
5503e02ed9SAxel Dörfler	status_t	(*readv)(net_socket* socket, const iovec* vecs,
5603e02ed9SAxel Dörfler					size_t vecCount, size_t* _length);
5703e02ed9SAxel Dörfler	status_t	(*writev)(net_socket* socket, const iovec* vecs,
5803e02ed9SAxel Dörfler					size_t vecCount, size_t* _length);
59f1e8af17SAugustin Cavalier	status_t	(*control)(net_socket* socket, uint32 op, void* data,
6075015ff5SIngo Weinhold					size_t length);
61c22d69bfSAxel Dörfler
6203e02ed9SAxel Dörfler	ssize_t		(*read_avail)(net_socket* socket);
6303e02ed9SAxel Dörfler	ssize_t		(*send_avail)(net_socket* socket);
64c22d69bfSAxel Dörfler
6503e02ed9SAxel Dörfler	status_t	(*send_data)(net_socket* socket, net_buffer* buffer);
6603e02ed9SAxel Dörfler	status_t	(*receive_data)(net_socket* socket, size_t length,
6703e02ed9SAxel Dörfler					uint32 flags, net_buffer** _buffer);
68c22d69bfSAxel Dörfler
6903e02ed9SAxel Dörfler	status_t	(*get_option)(net_socket* socket, int level, int option,
7003e02ed9SAxel Dörfler					void* value, int* _length);
7103e02ed9SAxel Dörfler	status_t	(*set_option)(net_socket* socket, int level, int option,
7203e02ed9SAxel Dörfler					const void* value, int length);
732445c00eSHugo Santos
7403e02ed9SAxel Dörfler	status_t	(*get_next_stat)(uint32* cookie, int family,
7503e02ed9SAxel Dörfler					struct net_stat* stat);
768dfd7ea7SAxel Dörfler
771a0e92a3SAxel Dörfler	// connections
7803e02ed9SAxel Dörfler	bool		(*acquire_socket)(net_socket* socket);
7903e02ed9SAxel Dörfler	bool		(*release_socket)(net_socket* socket);
8003e02ed9SAxel Dörfler
8103e02ed9SAxel Dörfler	status_t	(*spawn_pending_socket)(net_socket* parent,
8203e02ed9SAxel Dörfler					net_socket** _socket);
8303e02ed9SAxel Dörfler	status_t	(*dequeue_connected)(net_socket* parent, net_socket** _socket);
8403e02ed9SAxel Dörfler	ssize_t		(*count_connected)(net_socket* parent);
8503e02ed9SAxel Dörfler	status_t	(*set_max_backlog)(net_socket* socket, uint32 backlog);
8603e02ed9SAxel Dörfler	bool		(*has_parent)(net_socket* socket);
8703e02ed9SAxel Dörfler	status_t	(*set_connected)(net_socket* socket);
8803e02ed9SAxel Dörfler	status_t	(*set_aborted)(net_socket* socket);
891a0e92a3SAxel Dörfler
90a7028ce6SAxel Dörfler	// notifications
9103e02ed9SAxel Dörfler	status_t	(*request_notification)(net_socket* socket, uint8 event,
9203e02ed9SAxel Dörfler					struct selectsync* sync);
9303e02ed9SAxel Dörfler	status_t	(*cancel_notification)(net_socket* socket, uint8 event,
9403e02ed9SAxel Dörfler					struct selectsync* sync);
9503e02ed9SAxel Dörfler	status_t	(*notify)(net_socket* socket, uint8 event, int32 value);
96a7028ce6SAxel Dörfler
97c22d69bfSAxel Dörfler	// standard socket API
9803e02ed9SAxel Dörfler	int			(*accept)(net_socket* socket, struct sockaddr* address,
9903e02ed9SAxel Dörfler					socklen_t* _addressLength, net_socket** _acceptedSocket);
10003e02ed9SAxel Dörfler	int			(*bind)(net_socket* socket, const struct sockaddr* address,
101c22d69bfSAxel Dörfler					socklen_t addressLength);
10203e02ed9SAxel Dörfler	int			(*connect)(net_socket* socket, const struct sockaddr* address,
103c22d69bfSAxel Dörfler					socklen_t addressLength);
10403e02ed9SAxel Dörfler	int			(*getpeername)(net_socket* socket, struct sockaddr* address,
10503e02ed9SAxel Dörfler					socklen_t* _addressLength);
10603e02ed9SAxel Dörfler	int			(*getsockname)(net_socket* socket, struct sockaddr* address,
10703e02ed9SAxel Dörfler					socklen_t* _addressLength);
10803e02ed9SAxel Dörfler	int			(*getsockopt)(net_socket* socket, int level, int option,
10903e02ed9SAxel Dörfler					void* optionValue, int* _optionLength);
11003e02ed9SAxel Dörfler	int			(*listen)(net_socket* socket, int backlog);
11103e02ed9SAxel Dörfler	ssize_t		(*receive)(net_socket* socket, struct msghdr* , void* data,
11217b77c3bSHugo Santos					size_t length, int flags);
11303e02ed9SAxel Dörfler	ssize_t		(*send)(net_socket* socket, struct msghdr* , const void* data,
11417b77c3bSHugo Santos					size_t length, int flags);
11503e02ed9SAxel Dörfler	int			(*setsockopt)(net_socket* socket, int level, int option,
11603e02ed9SAxel Dörfler					const void* optionValue, int optionLength);
11703e02ed9SAxel Dörfler	int			(*shutdown)(net_socket* socket, int direction);
118df50f7a9SIngo Weinhold	status_t	(*socketpair)(int family, int type, int protocol,
119df50f7a9SIngo Weinhold					net_socket* _sockets[2]);
120c22d69bfSAxel Dörfler};
121c22d69bfSAxel Dörfler
12203e02ed9SAxel Dörfler
123c22d69bfSAxel Dörfler#endif	// NET_SOCKET_H
124