Anope IRC Services  Version 1.8
Functions | Variables
sockutil.c File Reference
#include "services.h"
Include dependency graph for sockutil.c:

Go to the source code of this file.

Functions

int32 read_buffer_len ()
 
static int buffered_read (ano_socket_t fd, char *buf, int len)
 
static int buffered_read_one (ano_socket_t fd)
 
int32 write_buffer_len ()
 
static int flush_write_buffer (int wait)
 
static int buffered_write (ano_socket_t fd, char *buf, int len)
 
int sgetc (ano_socket_t s)
 
int sungetc (int c, int s)
 
char * sgets (char *buf, int len, ano_socket_t s)
 
char * sgets2 (char *buf, int len, ano_socket_t s)
 
int sread (ano_socket_t s, char *buf, int len)
 
int sputs (char *str, ano_socket_t s)
 
int sockprintf (ano_socket_t s, char *fmt,...)
 
static char * pack_ip (const char *ipaddr)
 
int conn (const char *host, int port, const char *lhost, int lport)
 
void disconn (ano_socket_t s)
 

Variables

static char read_netbuf [NET_BUFSIZE]
 
static char * read_curpos = read_netbuf
 
static char * read_bufend = read_netbuf
 
static char *const read_buftop = read_netbuf + NET_BUFSIZE
 
int32 total_read = 0
 
static char write_netbuf [NET_BUFSIZE]
 
static char * write_curpos = write_netbuf
 
static char * write_bufend = write_netbuf
 
static char *const write_buftop = write_netbuf + NET_BUFSIZE
 
static int write_fd = -1
 
int32 total_written
 
static int lastchar = EOF
 

Function Documentation

static int buffered_read ( ano_socket_t  fd,
char *  buf,
int  len 
)
static

Read data.

Parameters
fdFile Pointer
bufBuffer
intLength of buffer
Returns
int

Definition at line 55 of file sockutil.c.

References alog(), ano_sockgeterr, ano_sockread, ano_sockseterr, debug, read_bufend, read_buftop, read_curpos, read_netbuf, SOCKERR_EBADF, and total_read.

Referenced by sread().

static int buffered_read_one ( ano_socket_t  fd)
static

Optimized version of the above for reading a single character; returns the character in an int or EOF, like fgetc().

Parameters
fdFile Pointer
Returns
int

Definition at line 132 of file sockutil.c.

References alog(), ano_sockgeterr, ano_sockread, ano_sockseterr, c, debug, read_bufend, read_buftop, read_curpos, read_netbuf, SOCKERR_EBADF, and total_read.

Referenced by sgetc().

static int buffered_write ( ano_socket_t  fd,
char *  buf,
int  len 
)
static

Write data.

Parameters
fdFile Pointer
bufBuffer to write
lenLength to write
Returns
int

Definition at line 252 of file sockutil.c.

References alog(), ano_sockgeterr, ano_sockseterr, debug, flush_write_buffer(), write_bufend, write_buftop, write_curpos, write_fd, and write_netbuf.

Referenced by sockprintf(), and sputs().

int conn ( const char *  host,
int  port,
const char *  lhost,
int  lport 
)

lhost/lport specify the local side of the connection. If they are not given (lhost==NULL, lport==0), then they are left free to vary.

Parameters
hostRemote Host
portRemote Port
lhostLocalHost
lportLocalPort
Returns
int if successful

Definition at line 553 of file sockutil.c.

References alog(), ano_sockclose, ano_sockgeterr, ano_sockseterr, pack_ip(), and SOCKERR_EINVAL.

Referenced by init_secondary().

void disconn ( ano_socket_t  s)

Close up the connection

Parameters
sSocket
Returns
void

Definition at line 632 of file sockutil.c.

References ano_sockclose.

Referenced by init_secondary(), main(), services_restart(), and services_shutdown().

static int flush_write_buffer ( int  wait)
static

Helper routine to try and write up to one chunk of data from the buffer to the socket. Return how much was written.

Parameters
waitWait
Returns
int

Definition at line 208 of file sockutil.c.

References alog(), ano_sockgeterr, ano_sockseterr, ano_sockwrite, debug, total_written, write_bufend, write_buftop, write_curpos, write_fd, and write_netbuf.

Referenced by buffered_write(), and sgets().

static char* pack_ip ( const char *  ipaddr)
static

Translate an IP dotted-quad address to a 4-byte character string. Return NULL if the given string is not in dotted-quad format.

Parameters
ipaddrIP Address
Returns
char 4byte ip char string

Definition at line 524 of file sockutil.c.

Referenced by conn().

int32 read_buffer_len ( void  )

Return amount of data in read buffer.

Returns
int32

Definition at line 37 of file sockutil.c.

References NET_BUFSIZE, read_bufend, and read_curpos.

Referenced by m_stats(), and sgets().

int sgetc ( ano_socket_t  s)

Optimized version of the above for writing a single character; returns the character in an int or EOF, like fputc(). Commented out because it isn't currently used.

Parameters
intto write
fdPointer
Returns
int sgetc ?
Parameters
intto read
Returns
int

Definition at line 375 of file sockutil.c.

References buffered_read_one(), c, and lastchar.

Referenced by sgets().

char* sgets ( char *  buf,
int  len,
ano_socket_t  s 
)

If connection was broken, return NULL. If the read timed out, return (char *)-1.

Parameters
bufBuffer to get
lenLength
sSocket
Returns
buffer

Definition at line 410 of file sockutil.c.

References ano_sockgeterr, flush_write_buffer(), read_buffer_len(), ReadTimeout, and sgetc().

Referenced by sgets2().

char* sgets2 ( char *  buf,
int  len,
ano_socket_t  s 
)

sgets2: Read a line of text from a socket, and strip newline and carriage return characters from the end of the line.

Parameters
bufBuffer to get
lenLength
sSocket
Returns
buffer

Definition at line 451 of file sockutil.c.

References sgets().

Referenced by init_secondary(), and main().

int sockprintf ( ano_socket_t  s,
char *  fmt,
  ... 
)

sockprintf : a socket writting printf()

Parameters
sSocket
fmtformat of message
...various args
Returns
int

Definition at line 502 of file sockutil.c.

References buffered_write(), and vsnprintf().

Referenced by vsend_cmd().

int sputs ( char *  str,
ano_socket_t  s 
)

sputs : write buffer

Parameters
sSocket
strBuffer to write
Returns
int

Definition at line 488 of file sockutil.c.

References buffered_write().

int sread ( ano_socket_t  s,
char *  buf,
int  len 
)

Read from a socket. (Use this instead of read() because it has buffering.)

Parameters
sSocket
bufBuffer to get
lenLength
Returns
int

Definition at line 475 of file sockutil.c.

References buffered_read().

int sungetc ( int  c,
int  s 
)

sungetc ?

Parameters
intc
ints
Returns
int

Definition at line 395 of file sockutil.c.

References c, and lastchar.

int32 write_buffer_len ( void  )

Return amount of data in write buffer.

Returns
int

Definition at line 191 of file sockutil.c.

References NET_BUFSIZE, write_bufend, and write_curpos.

Referenced by m_stats().

Variable Documentation

int lastchar = EOF
static

Definition at line 29 of file sockutil.c.

Referenced by sgetc(), and sungetc().

char* read_bufend = read_netbuf
static

Definition at line 20 of file sockutil.c.

Referenced by buffered_read(), buffered_read_one(), and read_buffer_len().

char* const read_buftop = read_netbuf + NET_BUFSIZE
static

Definition at line 21 of file sockutil.c.

Referenced by buffered_read(), and buffered_read_one().

char* read_curpos = read_netbuf
static

Definition at line 19 of file sockutil.c.

Referenced by buffered_read(), buffered_read_one(), and read_buffer_len().

char read_netbuf[NET_BUFSIZE]
static

Definition at line 18 of file sockutil.c.

Referenced by buffered_read(), and buffered_read_one().

int32 total_read = 0

Definition at line 22 of file sockutil.c.

Referenced by buffered_read(), and buffered_read_one().

int32 total_written

Definition at line 28 of file sockutil.c.

Referenced by flush_write_buffer().

char* write_bufend = write_netbuf
static

Definition at line 25 of file sockutil.c.

Referenced by buffered_write(), flush_write_buffer(), and write_buffer_len().

char* const write_buftop = write_netbuf + NET_BUFSIZE
static

Definition at line 26 of file sockutil.c.

Referenced by buffered_write(), and flush_write_buffer().

char* write_curpos = write_netbuf
static

Definition at line 24 of file sockutil.c.

Referenced by buffered_write(), flush_write_buffer(), and write_buffer_len().

int write_fd = -1
static

Definition at line 27 of file sockutil.c.

Referenced by buffered_write(), and flush_write_buffer().

char write_netbuf[NET_BUFSIZE]
static

Definition at line 23 of file sockutil.c.

Referenced by buffered_write(), and flush_write_buffer().