]> git.meshlink.io Git - meshlink/blob - src/buffer.c
Correctly handle incoming retransmissions of SYN packets.
[meshlink] / src / buffer.c
1 /*
2     buffer.c -- buffer management
3     Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "buffer.h"
23 #include "xalloc.h"
24
25 void buffer_compact(buffer_t *buffer, size_t maxsize) {
26         if(buffer->len >= maxsize || buffer->offset / 7 > buffer->len / 8) {
27                 memmove(buffer->data, buffer->data + buffer->offset, buffer->len - buffer->offset);
28                 buffer->len -= buffer->offset;
29                 buffer->offset = 0;
30         }
31 }
32
33 // Make sure we can add size bytes to the buffer, and return a pointer to the start of those bytes.
34
35 char *buffer_prepare(buffer_t *buffer, size_t size) {
36         if(!buffer->data) {
37                 assert(!buffer->maxlen);
38
39                 buffer->maxlen = size;
40                 buffer->data = xmalloc(size);
41         } else {
42                 if(buffer->offset && buffer->len + size > buffer->maxlen) {
43                         memmove(buffer->data, buffer->data + buffer->offset, buffer->len - buffer->offset);
44                         buffer->len -= buffer->offset;
45                         buffer->offset = 0;
46                 }
47
48                 if(buffer->len + size > buffer->maxlen) {
49                         buffer->maxlen = buffer->len + size;
50                         buffer->data = xrealloc(buffer->data, buffer->maxlen);
51                 }
52         }
53
54         char *start = buffer->data + buffer->len;
55
56         buffer->len += size;
57
58         return start;
59 }
60
61 // Copy data into the buffer.
62
63 void buffer_add(buffer_t *buffer, const char *data, size_t size) {
64         assert(data);
65         assert(size);
66
67         memcpy(buffer_prepare(buffer, size), data, size);
68 }
69
70 // Remove given number of bytes from the buffer, return a pointer to the start of them.
71
72 static char *buffer_consume(buffer_t *buffer, size_t size) {
73         assert(size);
74         assert(buffer->len - buffer->offset >= size);
75
76         char *start = buffer->data + buffer->offset;
77
78         buffer->offset += size;
79
80         if(buffer->offset >= buffer->len) {
81                 buffer->offset = 0;
82                 buffer->len = 0;
83         }
84
85         return start;
86 }
87
88 // Check if there is a complete line in the buffer, and if so, return it NULL-terminated.
89
90 char *buffer_readline(buffer_t *buffer) {
91         char *newline = memchr(buffer->data + buffer->offset, '\n', buffer->len - buffer->offset);
92
93         if(!newline) {
94                 return NULL;
95         }
96
97         size_t len = newline + 1 - (buffer->data + buffer->offset);
98         *newline = 0;
99         return buffer_consume(buffer, len);
100 }
101
102 // Check if we have enough bytes in the buffer, and if so, return a pointer to the start of them.
103
104 char *buffer_read(buffer_t *buffer, size_t size) {
105         assert(size);
106
107         if(buffer->len - buffer->offset < size) {
108                 return NULL;
109         }
110
111         return buffer_consume(buffer, size);
112 }
113
114 void buffer_clear(buffer_t *buffer) {
115         assert(!buffer->data == !buffer->maxlen);
116
117         free(buffer->data);
118         buffer->data = NULL;
119         buffer->maxlen = 0;
120         buffer->len = 0;
121         buffer->offset = 0;
122 }