1 /* 2 * Copyright 2006-2010, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef NET_PROTOCOL_H 6 #define NET_PROTOCOL_H 7 8 9 #include <net_buffer.h> 10 #include <net_socket.h> 11 12 13 struct ancillary_data_container; 14 struct ancillary_data_header; 15 struct iovec; 16 typedef struct net_domain net_domain; 17 typedef struct net_route net_route; 18 19 20 // level flags to pass to control() 21 #define LEVEL_SET_OPTION 0x10000000 22 #define LEVEL_GET_OPTION 0x20000000 23 #define LEVEL_DRIVER_IOCTL 0x0f000000 24 #define LEVEL_MASK 0x0fffffff 25 26 // Error codes for error_received(), and error_reply() 27 enum net_error { 28 B_NET_ERROR_REDIRECT_HOST = 1, 29 B_NET_ERROR_UNREACH_NET, 30 B_NET_ERROR_UNREACH_HOST, 31 B_NET_ERROR_UNREACH_PROTOCOL, 32 B_NET_ERROR_UNREACH_PORT, 33 B_NET_ERROR_MESSAGE_SIZE, 34 B_NET_ERROR_TRANSIT_TIME_EXCEEDED, 35 B_NET_ERROR_REASSEMBLY_TIME_EXCEEDED, 36 B_NET_ERROR_PARAMETER_PROBLEM, 37 B_NET_ERROR_QUENCH, 38 }; 39 40 typedef union net_error_data { 41 struct sockaddr_storage gateway; 42 uint32 mtu; 43 uint32 error_offset; 44 } net_error_data; 45 46 typedef struct net_protocol { 47 struct net_protocol* next; 48 struct net_protocol_module_info* module; 49 net_socket* socket; 50 } net_protocol; 51 52 53 // net_protocol_module_info::flags field 54 #define NET_PROTOCOL_ATOMIC_MESSAGES 0x01 55 56 57 struct net_protocol_module_info { 58 module_info info; 59 uint32 flags; 60 61 net_protocol* (*init_protocol)(net_socket* socket); 62 status_t (*uninit_protocol)(net_protocol* self); 63 64 status_t (*open)(net_protocol* self); 65 status_t (*close)(net_protocol* self); 66 status_t (*free)(net_protocol* self); 67 68 status_t (*connect)(net_protocol* self, const struct sockaddr* address); 69 status_t (*accept)(net_protocol* self, net_socket** _acceptedSocket); 70 status_t (*control)(net_protocol* self, int level, int option, 71 void* value, size_t* _length); 72 status_t (*getsockopt)(net_protocol* self, int level, int option, 73 void* value, int* _length); 74 status_t (*setsockopt)(net_protocol* self, int level, int option, 75 const void* value, int length); 76 77 status_t (*bind)(net_protocol* self, const struct sockaddr* address); 78 status_t (*unbind)(net_protocol* self, struct sockaddr* address); 79 status_t (*listen)(net_protocol* self, int count); 80 status_t (*shutdown)(net_protocol* self, int direction); 81 82 status_t (*send_data)(net_protocol* self, net_buffer* buffer); 83 status_t (*send_routed_data)(net_protocol* self, net_route* route, 84 net_buffer* buffer); 85 ssize_t (*send_avail)(net_protocol* self); 86 87 status_t (*read_data)(net_protocol* self, size_t numBytes, uint32 flags, 88 net_buffer** _buffer); 89 ssize_t (*read_avail)(net_protocol* self); 90 91 net_domain* (*get_domain)(net_protocol* self); 92 size_t (*get_mtu)(net_protocol* self, const struct sockaddr* address); 93 94 status_t (*receive_data)(net_buffer* data); 95 status_t (*deliver_data)(net_protocol* self, net_buffer* data); 96 97 status_t (*error_received)(net_error error, net_buffer* data); 98 status_t (*error_reply)(net_protocol* self, net_buffer* cause, 99 net_error error, net_error_data* errorData); 100 101 status_t (*add_ancillary_data)(net_protocol* self, 102 ancillary_data_container* container, const cmsghdr* header); 103 ssize_t (*process_ancillary_data)(net_protocol* self, 104 const ancillary_data_header* header, const void* data, 105 void* buffer, size_t bufferSize); 106 ssize_t (*process_ancillary_data_no_container)(net_protocol* self, 107 net_buffer* buffer, void* data, size_t bufferSize); 108 109 ssize_t (*send_data_no_buffer)(net_protocol* self, const iovec* vecs, 110 size_t vecCount, ancillary_data_container* ancillaryData, 111 const struct sockaddr* address, socklen_t addressLength, 112 int flags); 113 ssize_t (*read_data_no_buffer)(net_protocol* self, const iovec* vecs, 114 size_t vecCount, ancillary_data_container** _ancillaryData, 115 struct sockaddr* _address, socklen_t* _addressLength, 116 int flags); 117 }; 118 119 120 #endif // NET_PROTOCOL_H 121