summaryrefslogtreecommitdiff
path: root/other/fizzbounce/relay.c
diff options
context:
space:
mode:
Diffstat (limited to 'other/fizzbounce/relay.c')
-rw-r--r--other/fizzbounce/relay.c166
1 files changed, 166 insertions, 0 deletions
diff --git a/other/fizzbounce/relay.c b/other/fizzbounce/relay.c
new file mode 100644
index 0000000..194824c
--- /dev/null
+++ b/other/fizzbounce/relay.c
@@ -0,0 +1,166 @@
1
2/* bounce 4 all
3 * 1999 (c) scut
4 *
5 * relay routines
6 */
7
8#include <sys/socket.h>
9#include <sys/types.h>
10#include <sys/time.h>
11#include <netinet/in.h>
12#include <unistd.h>
13#include <arpa/inet.h>
14#include <pthread.h>
15#include <errno.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include "client.h"
20#include "network.h"
21#include "relay.h"
22
23void
24rly_client (client *cl)
25{
26 int n, i = 0, maxfd;
27 int bc = 1;
28 char cbuff[4096], sbuff[4096];
29 fd_set chainset;
30 struct timeval tval;
31
32 pthread_mutex_lock (&cl->cl_mutex);
33
34 /* now, since we authorized us, we should just chain every control
35 * connection command except it is in our parser array, in this case
36 * we should call the parser function :)
37 */
38
39 pthread_mutex_unlock (&cl->cl_mutex);
40
41 memset (cbuff, '\0', sizeof (cbuff));
42 memset (sbuff, '\0', sizeof (sbuff));
43
44 while (bc) {
45 pthread_mutex_lock (&cl->cl_mutex);
46
47 FD_ZERO (&chainset);
48 FD_SET (cl->cs, &chainset);
49 FD_SET (cl->ss, &chainset);
50 maxfd = ((cl->cs > cl->ss) ? cl->cs : cl->ss) + 1;
51 tval.tv_sec = 1;
52 tval.tv_usec = 0;
53
54 /* choose a dual approach, a bit polling (once per second, but also
55 * select, to let the mutex live
56 */
57 n = select (maxfd, &chainset, NULL, NULL, &tval);
58 switch (n) {
59 case (0): break;
60 case (-1): return;
61 default: if (FD_ISSET (cl->cs, &chainset)) {
62 i = rly_rb (cl->cs, cbuff, sizeof (cbuff));
63 if (i <= 0) {
64 bc = 0;
65 } else {
66 rly_clparse (cl, cbuff, sizeof (cbuff));
67 }
68 } else if (FD_ISSET (cl->ss, &chainset)) {
69 i = rly_rb (cl->ss, sbuff, sizeof (sbuff));
70 if (i <= 0) {
71 bc = 0;
72 } else {
73 rly_srvparse (cl, sbuff, sizeof (sbuff));
74 }
75 }
76 break;
77 }
78
79 pthread_mutex_unlock (&cl->cl_mutex);
80 }
81
82 switch (i) {
83 case (0): printf ("connection close\n");
84 break;
85 case (-1): printf ("system error (%d)\n", errno);
86 break;
87 case (-2): printf ("buffer too short to hold all recv data\n");
88 break;
89 default: printf ("weird i = %d\n", i);
90 break;
91 }
92
93 return;
94}
95
96int
97rly_clparse (client *cl, char *buffer, int buflen)
98{
99 int r, n, llen, prslen;
100
101 while ((llen = net_tline (buffer, buflen)) != -1) {
102
103/* if (strncmp (buffer, "USER", 4) == 0) {
104 char *a, *b;
105 int n;
106
107 n = sscanf (buffer, "USER %a[^ ] \"%*[^\"]\" \"%*[^\"]\"%*[^:]:%as\n",
108 &a, &b);
109 if (n != 2) {
110 printf ("wrong user command: %s\n", buffer);
111 goto pfft;
112 }
113 net_write (cl->ss, "USER %s \"%s\" \"%s\" :%s", a, cl->connip, cl->ircip, b);
114 goto mhhh;
115 }
116*/
117 /* if line was terminated, replace \n with \0 */
118pfft: buffer[llen-1] = '\0';
119 net_write (cl->ss, "%s\n", buffer);
120
121mhhh: memmove (buffer, buffer + llen, buflen - llen);
122
123 memset (buffer + (buflen - llen), '\0', llen);
124 }
125
126 return (1);
127}
128
129int
130rly_srvparse (client *cl, char *buffer, int buflen)
131{
132 int code, r, n, llen;
133
134 while ((llen = net_tline (buffer, buflen)) != -1) {
135 buffer[llen-1] = '\0';
136 net_write (cl->cs, "%s\n", buffer);
137 memmove (buffer, buffer + llen, buflen - llen);
138 memset (buffer + (buflen - llen), '\0', llen);
139 }
140 return (1);
141}
142
143int
144rly_rb (int fd, char *buffer, int buflen)
145{
146 int n, csize;
147
148 /* assume asciiz buffer, line based,
149 * now append the read data to this buffer
150 */
151
152 csize = strlen (buffer);
153 if (csize >= (buflen - 1))
154 return (-2); /* buffer size exceeded, lame script kiddie */
155
156 n = read (fd, buffer + csize, buflen - csize - 1);
157 switch (n) {
158 case (0): return (0);
159 case (-1): return (-1);
160 default: return (strlen (buffer));
161 }
162
163 /* should never happen */
164 return (0);
165}
166