lib/eggdrop/netstring.c File Reference

#include <eggdrop/eggdrop.h>
#include <errno.h>

Go to the source code of this file.

Data Structures

struct  netstring_t

Defines

#define MAX_LEN   1000000

Functions

static int netstring_on_write (void *client_data, int idx, const char *data, int len)
static int netstring_on_read (void *client_data, int idx, char *data, int len)
static int linemode_on_delete (void *client_data, int idx)
int netstring_on (int idx)
 Enables the filter on an index.
int netstring_off (int idx)
 Enables the filter on an index.
int netstring_check (int idx)
 Checks if an index is in linebuffered mode.

Variables

static const char rcsid [] = "$Id: netstring.c,v 1.1 2007-06-03 23:43:45 sven Exp $"
static sockbuf_filter_t netstring_filter


Define Documentation

#define MAX_LEN   1000000

Definition at line 27 of file netstring.c.

Referenced by netstring_on_read(), and netstring_on_write().


Function Documentation

static int linemode_on_delete ( void *  client_data,
int  idx 
) [static]

Definition at line 136 of file netstring.c.

References netstring_t::data.

00137 {
00138   netstring_t *old_data = client_data;
00139   /* When the sockbuf is deleted, just kill the associated data. */
00140   if (old_data->data) free(old_data->data);
00141   free(old_data);
00142   return 0;
00143 }

int netstring_check ( int  idx  ) 

Checks if an index is in linebuffered mode.

Parameters:
idx the index to check
Returns:
1 if linebuffered, 0 if not linebuffered, -1 on error

Definition at line 210 of file netstring.c.

References sockbuf_get_filter_data(), and sockbuf_isvalid().

Referenced by netstring_on().

00211 {
00212   if (!sockbuf_isvalid(idx)) return -1;
00213   return !sockbuf_get_filter_data(idx, &netstring_filter, 0);
00214 }

int netstring_off ( int  idx  ) 

Enables the filter on an index.

If there is text buffered it's send of immidiatly.

Parameters:
idx the index to disable it for
Returns:
0 on success, -1 on error

Definition at line 182 of file netstring.c.

References netstring_t::data, netstring_t::len, sockbuf_detach_filter(), SOCKBUF_LEVEL_TEXT_BUFFER, and sockbuf_on_read().

00183 {
00184   int ret;
00185   netstring_t *old_data;
00186 
00187   ret = sockbuf_detach_filter(idx, &netstring_filter, &old_data);
00188   if (ret) return ret;
00189   if (old_data) {
00190     if (old_data->data) {
00191       if (old_data->len) {
00192         old_data->data[old_data->len] = 0;
00193         sockbuf_on_read(idx, SOCKBUF_LEVEL_TEXT_BUFFER, old_data->data, old_data->len);
00194         old_data->len = 0;
00195       }
00196       free(old_data->data);
00197     }
00198     free(old_data);
00199   }
00200   return(0);
00201 }

int netstring_on ( int  idx  ) 

Enables the filter on an index.

Parameters:
idx the index to enable it for.
Returns:
0 on success, -1 on error.

Definition at line 160 of file netstring.c.

References netstring_t::data, netstring_t::len, netstring_t::msg_len, netstring_check(), NULL, and sockbuf_attach_filter().

Referenced by do_link(), and idx_on_newclient().

00161 {
00162   netstring_t *old_data;
00163 
00164   if (netstring_check(idx)) return -1;
00165   old_data = malloc(sizeof(*old_data));
00166   old_data->data = NULL;
00167   old_data->len = 0;
00168   old_data->msg_len = 0;
00169   sockbuf_attach_filter(idx, &netstring_filter, old_data);
00170   return(0);
00171 }

static int netstring_on_read ( void *  client_data,
int  idx,
char *  data,
int  len 
) [static]

Definition at line 56 of file netstring.c.

References _, netstring_t::data, netstring_t::len, MAX_LEN, netstring_t::msg_len, netstring_t::msg_offset, NULL, sockbuf_isvalid(), SOCKBUF_LEVEL_INTERNAL, SOCKBUF_LEVEL_TEXT_BUFFER, sockbuf_on_eof(), sockbuf_on_read(), and start.

00057 {
00058   netstring_t *old_data = client_data;
00059   char *buffer, *start, *p;
00060   int buflen, msg_len, msg_offset;
00061 
00062   if (!old_data->data) {
00063     buffer = data;
00064     buflen = len;
00065     msg_len = -1;
00066     msg_offset = -1;
00067   } else {
00068     old_data->data = realloc(old_data->data, old_data->len + len);
00069     memcpy(old_data->data + old_data->len, data, len);
00070     old_data->len += len;
00071     buffer = old_data->data;
00072     buflen = old_data->len;
00073     msg_len = old_data->msg_len;
00074     msg_offset = old_data->msg_offset;
00075   }
00076 
00077   start = buffer;
00078   do {
00079     if (msg_len < 0) {
00080       p = memchr(start, ':', buflen);
00081       if (!p) {
00082         if (buflen >= 10) {
00083           sockbuf_on_eof(idx, SOCKBUF_LEVEL_INTERNAL, EIO, _("Invalid line format."));
00084           return 0;
00085         }
00086         break;
00087       }
00088       *p = 0;
00089       msg_len = atoi(start);
00090       if (msg_len > MAX_LEN) {
00091         sockbuf_on_eof(idx, SOCKBUF_LEVEL_INTERNAL, EIO, _("Invalid line format."));
00092         return 0;
00093       }
00094       msg_offset = p - start + 1;
00095     }
00096 
00097     if (buflen <= msg_len + msg_offset) break;
00098 
00099     if (start[msg_len + msg_offset] != ',') {
00100       sockbuf_on_eof(idx, SOCKBUF_LEVEL_INTERNAL, EIO, _("Invalid line format."));
00101       return 0;
00102     }
00103 
00104     start[msg_len + msg_offset] = 0;
00105     sockbuf_on_read(idx, SOCKBUF_LEVEL_TEXT_BUFFER, start + msg_offset, msg_len);
00106     if (!sockbuf_isvalid(idx)) return 0; /* We've been deleted */
00107 
00108     start += msg_offset + msg_len + 1;
00109     buflen -= msg_offset + msg_len + 1;
00110     msg_offset = -1;
00111     msg_len = -1;
00112 
00113   } while (buflen);
00114 
00115   old_data->len = buflen;
00116   old_data->msg_len = msg_len;
00117   old_data->msg_offset = msg_offset;
00118 
00119   if (start == old_data->data) return 0;
00120 
00121   if (!buflen) {
00122     if (old_data->data) free(old_data->data);
00123     old_data->data = NULL;
00124   } else {
00125     if (old_data->data) {
00126       memmove(old_data->data, start, buflen);
00127       old_data->data = realloc(old_data->data, buflen);
00128     } else {
00129       old_data->data = malloc(buflen);
00130       memcpy(old_data->data, start, buflen);
00131     }
00132   }
00133   return 0;
00134 }

static int netstring_on_write ( void *  client_data,
int  idx,
const char *  data,
int  len 
) [static]

Definition at line 36 of file netstring.c.

References MAX_LEN, SOCKBUF_LEVEL_TEXT_BUFFER, and sockbuf_on_write().

00037 {
00038   char buf[517];
00039   char *sendbuf;
00040   int pos;
00041 
00042   if (len > MAX_LEN) len = MAX_LEN;
00043   if (len <= 512) sendbuf = buf;
00044   else sendbuf = malloc(len + 12);
00045 
00046   pos = sprintf(sendbuf, "%d:", len);
00047   memcpy(sendbuf + pos, data, len);
00048   pos += len;
00049   sendbuf[pos] = ',';
00050   sockbuf_on_write(idx, SOCKBUF_LEVEL_TEXT_BUFFER, sendbuf, pos + 1);
00051   if (sendbuf != buf) free(sendbuf);
00052 
00053   return 0;
00054 }


Variable Documentation

Initial value:

Definition at line 145 of file netstring.c.

const char rcsid[] = "$Id: netstring.c,v 1.1 2007-06-03 23:43:45 sven Exp $" [static]

Definition at line 21 of file netstring.c.


Generated on Sun Nov 30 18:43:34 2008 for eggdrop1.9 by  doxygen 1.5.6