1 /* 2 * Copyright 2006-2008, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Axel Dörfler, axeld@pinc-software.de 7 */ 8 #ifndef NET_UTILITY_H 9 #define NET_UTILITY_H 10 11 12 #include <net_stack.h> 13 14 15 class UserBuffer { 16 public: 17 UserBuffer(void* buffer, size_t size); 18 19 void* Copy(void* source, size_t size); 20 status_t Status() const; 21 size_t ConsumedAmount() const; 22 23 private: 24 uint8* fBuffer; 25 size_t fBufferSize, fAvailable; 26 status_t fStatus; 27 }; 28 29 30 inline 31 UserBuffer::UserBuffer(void* buffer, size_t size) 32 : 33 fBuffer((uint8*)buffer), 34 fBufferSize(size), 35 fAvailable(size), 36 fStatus(B_OK) 37 { 38 } 39 40 41 inline status_t 42 UserBuffer::Status() const 43 { 44 return fStatus; 45 } 46 47 48 inline size_t 49 UserBuffer::ConsumedAmount() const 50 { 51 return fBufferSize - fAvailable; 52 } 53 54 55 // checksums 56 uint16 compute_checksum(uint8* _buffer, size_t length); 57 uint16 checksum(uint8* buffer, size_t length); 58 59 // notifications 60 status_t notify_socket(net_socket* socket, uint8 event, int32 value); 61 62 // fifos 63 status_t init_fifo(net_fifo* fifo, const char *name, size_t maxBytes); 64 void uninit_fifo(net_fifo* fifo); 65 status_t fifo_enqueue_buffer(net_fifo* fifo, struct net_buffer* buffer); 66 ssize_t fifo_dequeue_buffer(net_fifo* fifo, uint32 flags, bigtime_t timeout, 67 struct net_buffer** _buffer); 68 status_t clear_fifo(net_fifo* fifo); 69 status_t fifo_socket_enqueue_buffer(net_fifo* fifo, net_socket* socket, 70 uint8 event, net_buffer* buffer); 71 72 // timer 73 void init_timer(net_timer* timer, net_timer_func hook, void* data); 74 void set_timer(net_timer* timer, bigtime_t delay); 75 bool cancel_timer(struct net_timer* timer); 76 void wait_for_timer(struct net_timer* timer); 77 bool is_timer_active(net_timer* timer); 78 status_t init_timers(void); 79 void uninit_timers(void); 80 81 // syscall restart 82 bool is_syscall(void); 83 bool is_restarted_syscall(void); 84 void store_syscall_restart_timeout(bigtime_t timeout); 85 bigtime_t restore_syscall_restart_timeout(void); 86 87 #endif // NET_UTILITY_H 88