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 // TODO: could be moved into a private structure 39 struct net_socket *parent; 40 struct list_link link; 41 uint32 max_backlog; 42 uint32 child_count; 43 struct list pending_children; 44 struct list connected_children; 45 46 status_t error; 47 struct select_sync_pool *select_pool; 48 benaphore lock; 49 } net_socket; 50 51 struct net_socket_module_info { 52 struct module_info info; 53 54 status_t (*open_socket)(int family, int type, int protocol, net_socket **_socket); 55 status_t (*close)(net_socket *socket); 56 status_t (*free)(net_socket *socket); 57 58 status_t (*readv)(net_socket *socket, const iovec *vecs, size_t vecCount, 59 size_t *_length); 60 status_t (*writev)(net_socket *socket, const iovec *vecs, size_t vecCount, 61 size_t *_length); 62 status_t (*control)(net_socket *socket, int32 op, void *data, size_t length); 63 64 ssize_t (*read_avail)(net_socket *socket); 65 ssize_t (*send_avail)(net_socket *socket); 66 67 status_t (*send_data)(net_socket *socket, net_buffer *buffer); 68 status_t (*receive_data)(net_socket *socket, size_t length, uint32 flags, 69 net_buffer **_buffer); 70 71 // connections 72 status_t (*spawn_pending_socket)(net_socket *parent, net_socket **_socket); 73 void (*delete_socket)(net_socket *socket); 74 status_t (*dequeue_connected)(net_socket *parent, net_socket **_socket); 75 status_t (*set_max_backlog)(net_socket *socket, uint32 backlog); 76 status_t (*set_connected)(net_socket *socket); 77 78 // notifications 79 status_t (*request_notification)(net_socket *socket, uint8 event, uint32 ref, 80 selectsync *sync); 81 status_t (*cancel_notification)(net_socket *socket, uint8 event, 82 selectsync *sync); 83 status_t (*notify)(net_socket *socket, uint8 event, int32 value); 84 85 // standard socket API 86 int (*accept)(net_socket *socket, struct sockaddr *address, 87 socklen_t *_addressLength, net_socket **_acceptedSocket); 88 int (*bind)(net_socket *socket, const struct sockaddr *address, 89 socklen_t addressLength); 90 int (*connect)(net_socket *socket, const struct sockaddr *address, 91 socklen_t addressLength); 92 int (*getpeername)(net_socket *socket, struct sockaddr *address, 93 socklen_t *_addressLength); 94 int (*getsockname)(net_socket *socket, struct sockaddr *address, 95 socklen_t *_addressLength); 96 int (*getsockopt)(net_socket *socket, int level, int option, 97 void *optionValue, int *_optionLength); 98 int (*listen)(net_socket *socket, int backlog); 99 ssize_t (*recv)(net_socket *socket, void *data, size_t length, int flags); 100 ssize_t (*recvfrom)(net_socket *socket, void *data, size_t length, int flags, 101 struct sockaddr *address, socklen_t *_addressLength); 102 ssize_t (*send)(net_socket *socket, const void *data, size_t length, int flags); 103 ssize_t (*sendto)(net_socket *socket, const void *data, size_t length, 104 int flags, const struct sockaddr *address, socklen_t addressLength); 105 int (*setsockopt)(net_socket *socket, int level, int option, 106 const void *optionValue, int optionLength); 107 int (*shutdown)(net_socket *socket, int direction); 108 }; 109 110 #endif // NET_SOCKET_H 111