summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2022-05-17 15:23:33 +0200
committerDirk Engling <erdgeist@erdgeist.org>2022-05-17 15:23:33 +0200
commit34a4541114318a7eda1c96ed58f83cdcd2622207 (patch)
tree77d13dae58b6b1a39c14046658d392b7d6b72e69
parent986fb5638542a943a6d9cd0d38d73605a642c55a (diff)
Move packet handler and line splitting to vchat-connection.c
-rwxr-xr-xvchat-client.c2
-rw-r--r--vchat-connection.c69
-rw-r--r--vchat-connection.h10
-rwxr-xr-xvchat-protocol.c64
-rwxr-xr-xvchat.h5
5 files changed, 73 insertions, 77 deletions
diff --git a/vchat-client.c b/vchat-client.c
index d6a3db3..db31c7c 100755
--- a/vchat-client.c
+++ b/vchat-client.c
@@ -411,7 +411,7 @@ eventloop (void)
411 411
412 /* something to read from server? */ 412 /* something to read from server? */
413 if (poll_result & 2) 413 if (poll_result & 2)
414 networkinput (); 414 vc_receive ();
415 break; 415 break;
416 } 416 }
417} 417}
diff --git a/vchat-connection.c b/vchat-connection.c
index 6b38168..06ea187 100644
--- a/vchat-connection.c
+++ b/vchat-connection.c
@@ -35,6 +35,10 @@
35static int serverfd = -1; 35static int serverfd = -1;
36unsigned int want_tcp_keepalive = 0; 36unsigned int want_tcp_keepalive = 0;
37 37
38/* TODO: NEEDS TO GO. status-variable from vchat-client.c
39 * eventloop is done as long as this is true */
40extern int status;
41
38/* Generic tcp connector, blocking */ 42/* Generic tcp connector, blocking */
39static int connect_tcp_socket( const char *server, const char *port ) { 43static int connect_tcp_socket( const char *server, const char *port ) {
40 struct addrinfo hints, *res, *res0; 44 struct addrinfo hints, *res, *res0;
@@ -213,10 +217,65 @@ vc_sendmessage (const char *msg)
213 } 217 }
214} 218}
215 219
216ssize_t 220/* offset in buffer (for linebreaks at packet borders) */
217vc_receivemessage (char *buffer, size_t size) { 221#define BUFSIZE 4096
218 if (getintoption(CF_USESSL)) 222static char _buf[BUFSIZE];
219 return vc_tls_receivemessage (buffer, size); 223static size_t _buf_fill;
224
225/* get data from servers connection */
226void
227vc_receive (void)
228{
229 char *endmsg;
230 size_t freebytes = BUFSIZE - _buf_fill;
231 ssize_t bytes;
232
233 if (!getintoption(CF_USESSL))
234 bytes = read(serverfd, _buf + _buf_fill, freebytes);
220 else 235 else
221 return read(serverfd, buffer, size); 236 bytes = vc_tls_receivemessage(_buf + _buf_fill, freebytes);
237
238 /* Our tls functions may require retries with handshakes etc, this is signalled by -2 */
239 if (bytes == -2)
240 return;
241
242 /* Error on the socket read? raise error message, bail out */
243 if (bytes == -1) {
244 snprintf (tmpstr, TMPSTRSIZE, "Receive fails, %s.", strerror(errno));
245 snprintf (errstr, ERRSTRSIZE, "Receive fails, %s.\n", strerror(errno));
246 writecf (FS_ERR,tmpstr);
247 status = 0;
248 return;
249 }
250
251 /* end of file from server? */
252 if (bytes == 0) {
253 /* inform user, bail out */
254 writecf (FS_SERV, "* EOF from server.");
255 snprintf (errstr, ERRSTRSIZE, "* EOF from server.\n");
256 status = 0;
257 return;
258 }
259
260 _buf_fill += bytes;
261
262 /* as long as there are lines .. */
263 while ((endmsg = memchr(_buf, '\n', _buf_fill)) != NULL) {
264 if (endmsg > _buf) {
265 /* Zero terminate message, optionally chomp CR */
266 endmsg[0] = 0;
267 if (endmsg[-1] == '\r')
268 endmsg[-1] = 0;
269 /* If terminating and chomping left us with a message, give it to line handler */
270 if (_buf[0]) {
271#ifdef DEBUG
272 /* debugging? log network input! */
273 fprintf (stderr, "<| %s\n", _buf);
274#endif
275 protocol_parsemsg (_buf);
276 }
277 }
278 _buf_fill -= 1 + endmsg - _buf;
279 memmove(_buf, endmsg + 1, _buf_fill);
280 }
222} 281}
diff --git a/vchat-connection.h b/vchat-connection.h
index 140e467..a56dc29 100644
--- a/vchat-connection.h
+++ b/vchat-connection.h
@@ -2,8 +2,8 @@
2 2
3#include <stdint.h> 3#include <stdint.h>
4 4
5int vc_connect(const char *host, const char *port); 5int vc_connect(const char *host, const char *port);
6void vc_sendmessage(const char *message); 6void vc_sendmessage(const char *message);
7ssize_t vc_receivemessage(char *buffer, size_t size); 7void vc_receive();
8int vc_poll(); 8int vc_poll();
9void vc_disconnect(); 9void vc_disconnect();
diff --git a/vchat-protocol.c b/vchat-protocol.c
index 0451b77..77e45ec 100755
--- a/vchat-protocol.c
+++ b/vchat-protocol.c
@@ -57,9 +57,6 @@ static void pmnotsent (char *message);
57/* declaration of server message array */ 57/* declaration of server message array */
58#include "vchat-messages.h" 58#include "vchat-messages.h"
59 59
60/* status-variable from vchat-client.c
61 * eventloop is done as long as this is true */
62extern int status;
63char *encoding; 60char *encoding;
64 61
65/* handle a pm not sent error 62/* handle a pm not sent error
@@ -577,8 +574,8 @@ usernickchange (char *message)
577} 574}
578 575
579/* handle received message from server */ 576/* handle received message from server */
580static void 577void
581parsemsg (char *message) 578protocol_parsemsg (char *message)
582{ 579{
583 char *str1, *str2; 580 char *str1, *str2;
584 int i; 581 int i;
@@ -691,60 +688,3 @@ parsemsg (char *message)
691 } 688 }
692} 689}
693 690
694/* offset in buffer (for linebreaks at packet borders) */
695#define BUFSIZE 4096
696static char _buf[BUFSIZE];
697static size_t _buf_fill;
698
699/* get data from servers filedescriptor */
700void
701networkinput (void)
702{
703 char *endmsg;
704 size_t freebytes = BUFSIZE - _buf_fill;
705 ssize_t bytes = vc_receivemessage(&_buf[_buf_fill], freebytes);
706
707 /* Our tls functions may require retries with handshakes etc, this is signalled by -2 */
708 if (bytes == -2)
709 return;
710
711 /* Error on the socket read? raise error message, bail out */
712 if (bytes == -1) {
713 snprintf (tmpstr, TMPSTRSIZE, "Receive fails, %s.", strerror(errno));
714 snprintf (errstr, ERRSTRSIZE, "Receive fails, %s.\n", strerror(errno));
715 writecf (FS_ERR,tmpstr);
716 status = 0;
717 return;
718 }
719
720 /* end of file from server? */
721 if (bytes == 0) {
722 /* inform user, bail out */
723 writecf (FS_SERV, "* EOF from server.");
724 snprintf (errstr, ERRSTRSIZE, "* EOF from server.\n");
725 status = 0;
726 return;
727 }
728
729 _buf_fill += bytes;
730
731 /* as long as there are lines .. */
732 while ((endmsg = memchr(_buf, '\n', _buf_fill)) != NULL) {
733 if (endmsg > _buf) {
734 /* Zero terminate message, optionally chomp CR */
735 endmsg[0] = 0;
736 if (endmsg[-1] == '\r')
737 endmsg[-1] = 0;
738 /* If terminating and chomping left us with a message, give it to line handler */
739 if (_buf[0]) {
740#ifdef DEBUG
741 /* debugging? log network input! */
742 fprintf (stderr, "<| %s\n", _buf);
743#endif
744 parsemsg (_buf);
745 }
746 }
747 _buf_fill -= 1 + endmsg - _buf;
748 memmove(_buf, endmsg + 1, _buf_fill);
749 }
750}
diff --git a/vchat.h b/vchat.h
index 31615ec..0045127 100755
--- a/vchat.h
+++ b/vchat.h
@@ -142,10 +142,7 @@ void handlequery ( char *line );
142 142
143/* vchat-protocol.c */ 143/* vchat-protocol.c */
144extern const char *vchat_io_version; 144extern const char *vchat_io_version;
145 145void protocol_parsemsg (char *message);
146/* network I/O */
147void networkinput (void);
148void networkoutput (char *);
149 146
150/* helpers for vchat-user.c */ 147/* helpers for vchat-user.c */
151void ownjoin (int channel); 148void ownjoin (int channel);