1 /* 2 * Copyright 2006-2009, 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 struct selectsync; 15 16 17 #define NET_SOCKET_MODULE_NAME "network/stack/socket/v1" 18 19 typedef struct net_socket { 20 struct net_protocol *first_protocol; 21 struct net_protocol_module_info *first_info; 22 23 int family; 24 int type; 25 int protocol; 26 27 struct sockaddr_storage address; 28 struct sockaddr_storage peer; 29 30 int options; 31 int linger; 32 int bound_to_device; 33 34 struct { 35 uint32 buffer_size; 36 uint32 low_water_mark; 37 bigtime_t timeout; 38 } send, receive; 39 40 status_t error; 41 } net_socket; 42 43 struct net_socket_module_info { 44 struct module_info info; 45 46 status_t (*open_socket)(int family, int type, int protocol, 47 net_socket **_socket); 48 status_t (*close)(net_socket *socket); 49 void (*free)(net_socket *socket); 50 51 status_t (*readv)(net_socket *socket, const iovec *vecs, 52 size_t vecCount, size_t *_length); 53 status_t (*writev)(net_socket *socket, const iovec *vecs, 54 size_t vecCount, size_t *_length); 55 status_t (*control)(net_socket *socket, int32 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 #endif // NET_SOCKET_H 119