forked from cotillion/cd-gamedriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nqueue.c
301 lines (260 loc) · 6.66 KB
/
nqueue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*-
* Copyright (c) 1997 Dave Richards <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* and exceptions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* 4. The code can not be used by Gary Random, Random Communications, Inc.,
* the employees of Random Communications, Inc. or its subsidiaries,
* including Defiance MUD, without prior written permission from the
* authors.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "config.h"
#include "lint.h"
#include "nqueue.h"
/*
* Network Queues
*/
/*
* Initialize a network queue. The read and write pointers are set to the
* beginning of the queue, and the length is set to 0.
*/
void
nq_init(nqueue_t *nq)
{
nq->nq_len = 0;
nq->nq_rptr = 0;
nq->nq_wptr = 0;
}
/*
* Allocate a network queue of a given (data) size.
*/
nqueue_t *
nq_alloc(u_short size)
{
nqueue_t *nq;
nq = xalloc(sizeof (nqueue_t) + size);
nq->nq_size = size;
nq_init(nq);
return nq;
}
/*
* Free a network queue.
*/
void
nq_free(nqueue_t *nq)
{
free(nq);
}
/*
* Return the (maximum) size of a network queue.
*/
u_short
nq_size(nqueue_t *nq)
{
return nq->nq_size;
}
/*
* Return the (current) length of a network queue.
*/
u_short
nq_len(nqueue_t *nq)
{
return nq->nq_len;
}
/*
* Return the amount of available space on a network queue. The available
* space is defined as the maximum size less the current length.
*/
u_short
nq_avail(nqueue_t *nq)
{
return nq->nq_size - nq->nq_len;
}
/*
* Return a pointer to the next character to be read in a network queue.
*/
u_char *
nq_rptr(nqueue_t *nq)
{
return (u_char *)(nq + 1) + nq->nq_rptr;
}
/*
* Return a pointer to the next character to be written in a network queue.
*/
u_char *
nq_wptr(nqueue_t *nq)
{
return (u_char *)(nq + 1) + nq->nq_wptr;
}
/*
* Determine whether a network queue is empty. A network queue is empty when
* its length is 0.
*/
int
nq_empty(nqueue_t *nq)
{
return nq->nq_len == 0;
}
/*
* Determine whether a network queue is full. A network queue is full when
* its length is equal to its size.
*/
int
nq_full(nqueue_t *nq)
{
return nq->nq_len == nq->nq_size;
}
/*
* Get a character from the head of a network queue. N.B. It is assumed
* that the caller has determined the queue was not empty prior to calling
* this function.
*/
u_char
nq_getc(nqueue_t *nq)
{
u_char c;
c = *nq_rptr(nq);
nq->nq_len--, nq->nq_rptr++;
if (nq->nq_rptr == nq->nq_size)
nq->nq_rptr = 0;
return c;
}
/*
* Return a character to the head of a network queue. N.B. It is assumed
* that the caller has determined the queue was not full prior to calling
* this function.
*/
void
nq_ungetc(nqueue_t *nq, u_char c)
{
nq->nq_len++;
if (nq->nq_rptr-- == 0)
nq->nq_rptr = nq->nq_size - 1;
*nq_rptr(nq) = c;
}
/*
* Append a character to the tail of a network queue. N.B. It is assumed
* that the caller has determined the queue was not full prior to calling
* this function.
*/
void
nq_putc(nqueue_t *nq, u_char c)
{
*nq_wptr(nq) = c;
nq->nq_len++, nq->nq_wptr++;
if (nq->nq_wptr == nq->nq_size)
nq->nq_wptr = 0;
}
/*
* Append a string to the tail of a network queue. N.B. It is assumed
* that the caller has determined the queue has adequate space available
* prior to calling this function.
*/
void
nq_puts(nqueue_t *nq, u_char *cp)
{
int len, size;
len = strlen((char *)cp);
while (len > 0)
{
if (nq->nq_rptr > nq->nq_wptr)
size = nq->nq_rptr - nq->nq_wptr;
else
size = nq->nq_size - nq->nq_wptr;
if (size > len)
size = len;
memcpy(nq_wptr(nq), cp, size);
nq->nq_len += size, nq->nq_wptr += size;
if (nq->nq_wptr == nq->nq_size)
nq->nq_wptr = 0;
cp += size, len -= size;
}
}
/*
* Remove a character from the tail of a network queue. N.B. It is assumed
* that the caller has determined the queue was not empty prior to calling
* this function.
*/
void
nq_unputc(nqueue_t *nq)
{
nq->nq_len--;
if (nq->nq_wptr-- == 0)
nq->nq_wptr = nq->nq_size - 1;
}
/*
* Receive data from a socket and append it to a network queue. N.B. It is
* assumed that the caller has determined the queue is not full prior to
* calling this function.
*/
int
nq_recv(nqueue_t *nq, int fd, u_int *uip)
{
int len, cc;
if (nq->nq_rptr > nq->nq_wptr)
len = nq->nq_rptr - nq->nq_wptr;
else
len = nq->nq_size - nq->nq_wptr;
cc = recv(fd, nq_wptr(nq), len, 0);
if (cc > 0)
{
nq->nq_len += cc, nq->nq_wptr += cc;
if (nq->nq_wptr == nq->nq_size)
nq->nq_wptr = 0;
if (uip != NULL)
*uip += cc;
}
return cc;
}
/*
* Send data from a network queue to a socket.
*/
int
nq_send(nqueue_t *nq, int fd, u_int *uip)
{
int len, cc = 0;
while (nq->nq_len > 0)
{
len = nq->nq_size - nq->nq_rptr;
if (len > nq->nq_len)
len = nq->nq_len;
cc = send(fd, nq_rptr(nq), len, 0);
if (cc > 0)
{
nq->nq_len -= cc, nq->nq_rptr += cc;
if (nq->nq_rptr == nq->nq_size)
nq->nq_rptr = 0;
if (uip != NULL)
*uip += cc;
}
if (cc != len)
break;
}
return cc;
}