/[cvs]/eggdrop1.9/testcode/linemode.c
ViewVC logotype

Annotation of /eggdrop1.9/testcode/linemode.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (hide annotations) (download) (as text)
Thu Apr 25 04:19:54 2002 UTC (17 years, 11 months ago) by stdarg
Branch: MAIN
Changes since 1.1: +1 -0 lines
File MIME type: text/x-chdr
* sclient.c - test program to telnet using a proxy
* http.c/base64.c - http anonymous and authenticated proxies
* socks5.c - socks5 proxy with support for username/password authentication, ipv4, ipv6, and remote hostname resolution
* socks4.c socks4 proxy (no authentication)

1 stdarg 1.1 #include <stdio.h>
2     #include <stdlib.h>
3     #include <string.h>
4     #include <unistd.h>
5    
6     #include "sockbuf.h"
7    
8     static int linemode_read(int idx, int event, int level, sockbuf_iobuf_t *new_data, sockbuf_iobuf_t *old_data)
9     {
10     unsigned char *line, *stop, *save, *data;
11     int linelen, savelen, datalen;
12     sockbuf_iobuf_t my_iobuf;
13    
14     data = new_data->data;
15     datalen = new_data->len;
16    
17     while (1) {
18     /* If there's a cr or lf, we have a line. */
19     stop = memchr(data, '\n', datalen);
20     if (!stop) {
21     stop = memchr(data, '\r', datalen);
22     if (!stop) {
23     /* No line, save the whole thing. */
24     save = data;
25     savelen = datalen;
26     break;
27     }
28     }
29    
30     /* Terminate the line and get the length. */
31     *stop = 0;
32     linelen = stop - data;
33     save = stop+1;
34     savelen = data + datalen - save;
35    
36     /* Check for crlf. */
37     if (stop > data && *(stop-1) == '\r') linelen--;
38    
39     /* If there is buffered data, concat it all. */
40     if (old_data->len) {
41     int newmax = old_data->len + linelen + 1;
42    
43     if (newmax > old_data->max) {
44     /* Buffer is too small -- enlarge it. */
45     old_data->data = (unsigned char *)realloc(old_data->data, newmax);
46     old_data->max = newmax;
47     }
48    
49     /* Add the new data to the end. */
50     memcpy(old_data->data+old_data->len, data, linelen+1);
51    
52     /* Our line is: */
53     line = old_data->data;
54     linelen += old_data->len;
55    
56     /* Reset stored data. */
57     old_data->len = 0;
58     }
59     else {
60     line = data;
61     /* And linelen is still correct. */
62     }
63    
64     my_iobuf.data = line;
65     my_iobuf.len = linelen;
66     my_iobuf.max = linelen;
67 stdarg 1.2 line[linelen] = 0;
68 stdarg 1.1 sockbuf_filter(idx, event, level, &my_iobuf);
69    
70     /* If we're out of data, we're done. */
71     if (savelen <= 0) return(0);
72     /* Otherwise, do this again to check for another line. */
73     data = save;
74     datalen = savelen;
75     }
76    
77     /* No cr/lf, so we save the remaining data for next time. */
78     if (savelen + old_data->len > old_data->max) {
79     /* Expand the buffer with room for next time. */
80     old_data->data = (unsigned char *)realloc(old_data->data, savelen + old_data->len + 512);
81     old_data->max = savelen + old_data->len + 512;
82     }
83     memmove(old_data->data + old_data->len, save, savelen);
84     old_data->len += savelen;
85     return(0);
86     }
87    
88     static int linemode_eof_and_err(int idx, int event, int level, void *ignore, sockbuf_iobuf_t *old_data)
89     {
90     /* If there is any buffered data, do one more on->read callback. */
91     if (old_data->len) {
92     old_data->data[old_data->len] = 0;
93     sockbuf_filter(idx, SOCKBUF_READ, level, old_data);
94     }
95    
96     /* And now continue the EOF/ERR event chain. */
97     sockbuf_filter(idx, event, level, old_data);
98     return(0);
99     }
100    
101     static sockbuf_event_t linemode_filter = {
102     (Function) 4,
103     (Function) "line-mode",
104     linemode_read,
105     NULL,
106     linemode_eof_and_err,
107     linemode_eof_and_err
108     };
109    
110     int linemode_on(int idx)
111     {
112     sockbuf_iobuf_t *iobuf;
113    
114     iobuf = (sockbuf_iobuf_t *)calloc(sizeof(*iobuf), 0);
115     sockbuf_attach_filter(idx, linemode_filter, iobuf);
116     return(0);
117     }
118    
119     int linemode_off(int idx)
120     {
121     sockbuf_iobuf_t *iobuf;
122    
123     sockbuf_detach_filter(idx, linemode_filter, &iobuf);
124     if (iobuf) {
125     if (iobuf->data) free(iobuf->data);
126     free(iobuf);
127     }
128     return(0);
129     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23