xref: /haiku/src/add-ons/kernel/network/stack/utility.h (revision fc1ca2da5cfcb00ffdf791606d5ae97fdd58a638)
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 class UserBuffer {
15 public:
16 	UserBuffer(void *buffer, size_t size);
17 
18 	void *Copy(void *source, size_t size);
19 	status_t Status() const;
20 	size_t ConsumedAmount() const;
21 
22 private:
23 	uint8 *fBuffer;
24 	size_t fBufferSize, fAvailable;
25 	status_t fStatus;
26 };
27 
28 
29 // internal Fifo class which doesn't maintain it's own lock
30 class Fifo {
31 public:
32 	Fifo(const char *name, size_t maxBytes);
33 	~Fifo();
34 
35 	status_t InitCheck() const;
36 
37 	status_t Enqueue(net_buffer *buffer);
38 	status_t EnqueueAndNotify(net_buffer *_buffer, net_socket *socket, uint8 event);
39 	status_t Wait(benaphore *lock, bigtime_t timeout);
40 	net_buffer *Dequeue(bool clone);
41 	status_t Clear();
42 
43 	void WakeAll();
44 
45 	bool IsEmpty() const { return current_bytes == 0; }
46 
47 //private:
48 	// these field names are kept so we can use templatized
49 	// functions together with net_fifo
50 	sem_id		notify;
51 	int32		waiting;
52 	size_t		max_bytes;
53 	size_t		current_bytes;
54 	struct list	buffers;
55 };
56 
57 inline
58 UserBuffer::UserBuffer(void *buffer, size_t size)
59 	: fBuffer((uint8 *)buffer), fBufferSize(size),
60 	  fAvailable(size), fStatus(B_OK)
61 {
62 }
63 
64 
65 inline status_t
66 UserBuffer::Status() const
67 {
68 	return fStatus;
69 }
70 
71 
72 inline size_t
73 UserBuffer::ConsumedAmount() const
74 {
75 	return fBufferSize - fAvailable;
76 }
77 
78 
79 // checksums
80 uint16		compute_checksum(uint8 *_buffer, size_t length);
81 uint16		checksum(uint8 *buffer, size_t length);
82 
83 // notifications
84 status_t	notify_socket(net_socket *socket, uint8 event, int32 value);
85 
86 // fifos
87 status_t	init_fifo(net_fifo *fifo, const char *name, size_t maxBytes);
88 void		uninit_fifo(net_fifo *fifo);
89 status_t	fifo_enqueue_buffer(net_fifo *fifo, struct net_buffer *buffer);
90 ssize_t		fifo_dequeue_buffer(net_fifo *fifo, uint32 flags, bigtime_t timeout,
91 				struct net_buffer **_buffer);
92 status_t	clear_fifo(net_fifo *fifo);
93 status_t	fifo_socket_enqueue_buffer(net_fifo *, net_socket *, uint8 event,
94 				net_buffer *);
95 
96 // timer
97 void		init_timer(net_timer *timer, net_timer_func hook, void *data);
98 void		set_timer(net_timer *timer, bigtime_t delay);
99 bool		cancel_timer(struct net_timer *timer);
100 bool		is_timer_active(net_timer *timer);
101 status_t	init_timers(void);
102 void		uninit_timers(void);
103 
104 // syscall restart
105 bool		is_syscall(void);
106 bool		is_restarted_syscall(void);
107 void		store_syscall_restart_timeout(bigtime_t timeout);
108 bigtime_t	restore_syscall_restart_timeout(void);
109 
110 #endif	// NET_UTILITY_H
111