… | |
… | |
35 | using namespace std; |
35 | using namespace std; |
36 | |
36 | |
37 | #include <global.h> |
37 | #include <global.h> |
38 | #include <newclient.h> |
38 | #include <newclient.h> |
39 | #include <sproto.h> |
39 | #include <sproto.h> |
|
|
40 | #include <cstdarg> |
40 | |
41 | |
41 | #ifdef __linux__ |
42 | #ifdef __linux__ |
42 | # include <sys/types.h> |
43 | # include <sys/types.h> |
43 | # include <sys/socket.h> |
44 | # include <sys/socket.h> |
44 | # include <netinet/in.h> |
45 | # include <netinet/in.h> |
… | |
… | |
83 | * |
84 | * |
84 | * SockList functions/utilities |
85 | * SockList functions/utilities |
85 | * |
86 | * |
86 | **********************************************************************/ |
87 | **********************************************************************/ |
87 | |
88 | |
|
|
89 | SockList &SockList::operator <<(const data &v) |
|
|
90 | { |
|
|
91 | if (v.len) |
|
|
92 | { |
|
|
93 | memcpy (buf + len, v.ptr, v.len); |
|
|
94 | len += v.len; |
|
|
95 | } |
|
|
96 | |
|
|
97 | return *this; |
|
|
98 | } |
|
|
99 | |
88 | SockList &SockList::operator <<(const data8 &v) |
100 | SockList &SockList::operator <<(const data8 &v) |
89 | { |
101 | { |
90 | *this << uint8 (v.len); |
102 | unsigned int len = min (v.len, 0x00FF); |
91 | |
103 | return *this << uint8 (len) << data (v.ptr, len); |
92 | memcpy (buf + len, v.data, v.len); |
|
|
93 | len += v.len; |
|
|
94 | |
|
|
95 | return *this; |
|
|
96 | } |
104 | } |
97 | |
105 | |
98 | SockList &SockList::operator <<(const data16 &v) |
106 | SockList &SockList::operator <<(const data16 &v) |
99 | { |
107 | { |
100 | *this << uint16 (v.len); |
108 | unsigned int len = min (v.len, 0xFFFF); |
101 | |
109 | return *this << uint16 (len) << data (v.ptr, len); |
102 | memcpy (buf + len, v.data, v.len); |
|
|
103 | len += v.len; |
|
|
104 | |
|
|
105 | return *this; |
|
|
106 | } |
110 | } |
107 | |
111 | |
108 | /* Basically does the reverse of SockList_AddInt, but on |
112 | SockList &SockList::operator <<(const char *v) |
109 | * strings instead. Same for the GetShort, but for 16 bits. |
113 | { |
|
|
114 | return *this << data (v, strlen (v ? v : 0)); |
|
|
115 | } |
|
|
116 | |
|
|
117 | void |
|
|
118 | SockList::printf (const char *format, ...) |
|
|
119 | { |
|
|
120 | va_list ap; |
|
|
121 | va_start (ap, format); |
|
|
122 | |
|
|
123 | len += vsprintf ((char *)buf + len, format, ap); |
|
|
124 | |
|
|
125 | va_end (ap); |
|
|
126 | } |
|
|
127 | |
|
|
128 | /****************************************************************************** |
110 | */ |
129 | * |
|
|
130 | * Start of read routines. |
|
|
131 | * |
|
|
132 | ******************************************************************************/ |
|
|
133 | |
111 | int |
134 | int |
112 | GetInt_String (unsigned char *data) |
135 | NewSocket::read_packet () |
113 | { |
136 | { |
114 | return ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]); |
137 | for (;;) |
115 | } |
|
|
116 | |
|
|
117 | short |
|
|
118 | GetShort_String (unsigned char *data) |
|
|
119 | { |
|
|
120 | return ((data[0] << 8) + data[1]); |
|
|
121 | } |
|
|
122 | |
|
|
123 | /****************************************************************************** |
|
|
124 | * |
|
|
125 | * Start of read routines. |
|
|
126 | * |
|
|
127 | ******************************************************************************/ |
|
|
128 | |
|
|
129 | /** |
|
|
130 | * This reads from fd and puts the data in sl. We return true if we think |
|
|
131 | * we have a full packet, 0 if we have a partial packet. The only processing |
|
|
132 | * we do is remove the intial size value. len (As passed) is the size of the |
|
|
133 | * buffer allocated in the socklist. We make the assumption the buffer is |
|
|
134 | * at least 2 bytes long. |
|
|
135 | */ |
|
|
136 | |
|
|
137 | int |
|
|
138 | SockList_ReadPacket (int fd, SockList * sl, int len) |
|
|
139 | { |
|
|
140 | int stat, toread; |
|
|
141 | |
|
|
142 | /* Sanity check - shouldn't happen */ |
|
|
143 | if (sl->len < 0) |
|
|
144 | { |
|
|
145 | abort (); |
|
|
146 | } |
138 | { |
147 | /* We already have a partial packet */ |
139 | if (inbuf_len >= 2) |
148 | if (sl->len < 2) |
|
|
149 | { |
|
|
150 | do |
|
|
151 | { |
|
|
152 | stat = read (fd, sl->buf + sl->len, 2 - sl->len); |
|
|
153 | } |
140 | { |
154 | while ((stat == -1) && (errno == EINTR)); |
141 | unsigned int pkt_len = (inbuf [0] << 8) | inbuf [1]; |
155 | |
142 | |
156 | if (stat < 0) |
143 | if (inbuf_len >= 2 + pkt_len) |
|
|
144 | return pkt_len + 2; |
157 | { |
145 | } |
158 | /* In non blocking mode, EAGAIN is set when there is no |
146 | |
159 | * data available. |
147 | int amount = sizeof (inbuf) - inbuf_len; |
160 | */ |
148 | |
|
|
149 | if (amount <= 0) |
|
|
150 | { |
|
|
151 | LOG (llevError, "packet too large");//TODO |
|
|
152 | return -1; |
|
|
153 | } |
|
|
154 | |
|
|
155 | amount = read (fd, inbuf + inbuf_len, amount); |
|
|
156 | |
|
|
157 | if (!amount) |
|
|
158 | { |
|
|
159 | status = Ns_Dead; |
|
|
160 | return -1; |
|
|
161 | } |
|
|
162 | else if (amount < 0) |
|
|
163 | { |
161 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
164 | if (errno != EAGAIN && errno != EINTR) |
162 | { |
165 | { |
163 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
166 | LOG (llevError, "read error: %s", strerror (errno)); |
|
|
167 | return -1; |
164 | } |
168 | } |
165 | return 0; /*Error */ |
|
|
166 | } |
|
|
167 | if (stat == 0) |
|
|
168 | return -1; |
|
|
169 | sl->len += stat; |
|
|
170 | #ifdef CS_LOGSTATS |
|
|
171 | cst_tot.ibytes += stat; |
|
|
172 | cst_lst.ibytes += stat; |
|
|
173 | #endif |
|
|
174 | if (stat < 2) |
|
|
175 | return 0; /* Still don't have a full packet */ |
|
|
176 | } |
|
|
177 | /* Figure out how much more data we need to read. Add 2 from the |
|
|
178 | * end of this - size header information is not included. |
|
|
179 | */ |
|
|
180 | toread = 2 + (sl->buf[0] << 8) + sl->buf[1] - sl->len; |
|
|
181 | if ((toread + sl->len) >= len) |
|
|
182 | { |
|
|
183 | LOG (llevError, "SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", toread + sl->len, len); |
|
|
184 | /* Quick hack in case for 'oldsocketmode' input. If we are |
|
|
185 | * closing the socket anyways, then reading this extra 100 bytes |
|
|
186 | * shouldn't hurt. |
|
|
187 | */ |
|
|
188 | read (fd, sl->buf + 2, 100); |
|
|
189 | |
169 | |
190 | /* return error so the socket is closed */ |
|
|
191 | return -1; |
|
|
192 | } |
|
|
193 | do |
|
|
194 | { |
|
|
195 | do |
|
|
196 | { |
|
|
197 | stat = read (fd, sl->buf + sl->len, toread); |
|
|
198 | } |
|
|
199 | while ((stat < 0) && (errno == EINTR)); |
|
|
200 | if (stat < 0) |
|
|
201 | { |
|
|
202 | |
|
|
203 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
|
|
204 | { |
|
|
205 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
|
|
206 | } |
|
|
207 | return 0; /*Error */ |
|
|
208 | } |
|
|
209 | if (stat == 0) |
|
|
210 | return -1; |
|
|
211 | sl->len += stat; |
|
|
212 | #ifdef CS_LOGSTATS |
|
|
213 | cst_tot.ibytes += stat; |
|
|
214 | cst_lst.ibytes += stat; |
|
|
215 | #endif |
|
|
216 | toread -= stat; |
|
|
217 | if (toread == 0) |
|
|
218 | return 1; |
|
|
219 | if (toread < 0) |
|
|
220 | { |
|
|
221 | LOG (llevError, "SockList_ReadPacket: Read more bytes than desired.\n"); |
|
|
222 | return 1; |
170 | return 0; |
223 | } |
171 | } |
|
|
172 | |
|
|
173 | inbuf_len += amount; |
|
|
174 | |
|
|
175 | cst_tot.ibytes += amount; |
|
|
176 | cst_lst.ibytes += amount; |
224 | } |
177 | } |
225 | while (toread > 0); |
178 | } |
226 | return 0; |
179 | |
|
|
180 | void |
|
|
181 | NewSocket::skip_packet (int len) |
|
|
182 | { |
|
|
183 | inbuf_len -= len; |
|
|
184 | memmove (inbuf, inbuf + len, inbuf_len); |
227 | } |
185 | } |
228 | |
186 | |
229 | /******************************************************************************* |
187 | /******************************************************************************* |
230 | * |
188 | * |
231 | * Start of write related routines. |
189 | * Start of write related routines. |
… | |
… | |
238 | * ns is the socket we are adding the data to, buf is the start of the |
196 | * ns is the socket we are adding the data to, buf is the start of the |
239 | * data, and len is the number of bytes to add. |
197 | * data, and len is the number of bytes to add. |
240 | */ |
198 | */ |
241 | |
199 | |
242 | static void |
200 | static void |
243 | add_to_buffer (NewSocket * ns, char *buf, int len) |
201 | add_to_buffer (NewSocket *ns, char *buf, int len) |
244 | { |
202 | { |
245 | int avail, end; |
203 | int avail, end; |
246 | |
204 | |
247 | if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) |
205 | if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) |
248 | { |
206 | { |
… | |
… | |
408 | /** |
366 | /** |
409 | * Takes a string of data, and writes it out to the socket. A very handy |
367 | * Takes a string of data, and writes it out to the socket. A very handy |
410 | * shortcut function. |
368 | * shortcut function. |
411 | */ |
369 | */ |
412 | void |
370 | void |
413 | cs_write_string (NewSocket * ns, const char *buf, int len) |
371 | cs_write_string (NewSocket *ns, const char *buf, int len) |
414 | { |
372 | { |
415 | SockList sl; |
373 | SockList sl; |
416 | |
374 | |
417 | sl.len = len; |
375 | sl.len = len; |
418 | sl.buf = (unsigned char *) buf; |
376 | sl.buf = (unsigned char *) buf; |
… | |
… | |
425 | * |
383 | * |
426 | * The only difference in this function is that we take a SockList |
384 | * The only difference in this function is that we take a SockList |
427 | *, and we prepend the length information. |
385 | *, and we prepend the length information. |
428 | */ |
386 | */ |
429 | void |
387 | void |
430 | Send_With_Handling (NewSocket * ns, SockList * msg) |
388 | Send_With_Handling (NewSocket *ns, SockList *msg) |
431 | { |
389 | { |
432 | unsigned char sbuf[4]; |
390 | unsigned char sbuf[4]; |
433 | |
391 | |
434 | if (ns->status == Ns_Dead || !msg) |
392 | if (ns->status == Ns_Dead || !msg) |
435 | return; |
393 | return; |
… | |
… | |
452 | /** |
410 | /** |
453 | * Takes a string of data, and writes it out to the socket. A very handy |
411 | * Takes a string of data, and writes it out to the socket. A very handy |
454 | * shortcut function. |
412 | * shortcut function. |
455 | */ |
413 | */ |
456 | void |
414 | void |
457 | Write_String_To_Socket (NewSocket * ns, char *buf, int len) |
415 | Write_String_To_Socket (NewSocket *ns, char *buf, int len) |
458 | { |
416 | { |
459 | SockList sl; |
417 | SockList sl; |
460 | |
418 | |
461 | sl.len = len; |
419 | sl.len = len; |
462 | sl.buf = (unsigned char *) buf; |
420 | sl.buf = (unsigned char *) buf; |