lib/eggdrop/net.c File Reference

#include <eggdrop/eggdrop.h>

Go to the source code of this file.

Data Structures

struct  connect_info

Defines

#define EGGNET_LEVEL   (SOCKBUF_LEVEL_INTERNAL+1)

Typedefs

typedef struct connect_info connect_info_t

Functions

static int connect_host_resolved (void *client_data, const char *host, char **ips)
static int egg_connect_timeout (void *client_data)
static int egg_on_connect (void *client_data, int idx, const char *peer_ip, int peer_port)
static int egg_on_eof (void *client_data, int idx, int err, const char *errmsg)
static int egg_on_delete (void *client_data, int idx)
static connect_info_tattach (int idx, const char *host, int port, int timeout)
static int detach (void *client_data, int idx)
int egg_net_init (void)
int egg_net_shutdown (void)
int egg_iprintf (int idx, const char *format,...)
int egg_server (const char *vip, int port, int *real_port)
int egg_client (int idx, const char *host, int port, const char *vip, int vport, int timeout)
int egg_listen (int port, int *real_port)
int egg_reconnect (int idx, const char *host, int port, int timeout)
int egg_connect (const char *host, int port, int timeout)
int egg_proxy_add (egg_proxy_t *proxy)
int egg_proxy_del (egg_proxy_t *proxy)
egg_proxy_tegg_proxy_lookup (const char *name)
int egg_proxy_set_default (const char *name)
const char * egg_proxy_get_default ()

Variables

static const char rcsid [] = "$Id: net.c,v 1.7 2006-09-12 01:50:50 sven Exp $"
static sockbuf_filter_t net_connect_filter
static char * default_proxy_name = NULL
static int default_timeout = 30
static egg_proxy_t ** proxies = NULL
static int nproxies = 0


Define Documentation

#define EGGNET_LEVEL   (SOCKBUF_LEVEL_INTERNAL+1)

Definition at line 26 of file net.c.

Referenced by connect_host_resolved(), egg_connect_timeout(), egg_on_connect(), and egg_on_eof().


Typedef Documentation

typedef struct connect_info connect_info_t


Function Documentation

static connect_info_t * attach ( int  idx,
const char *  host,
int  port,
int  timeout 
) [static]

Definition at line 240 of file net.c.

References default_timeout, connect_info::dns_id, egg_connect_timeout(), connect_info::idx, NULL, connect_info::port, egg_timeval::sec, sockbuf_attach_filter(), timer_create_complex(), connect_info::timer_id, and egg_timeval::usec.

Referenced by egg_client(), and egg_reconnect().

00241 {
00242   connect_info_t *connect_info = calloc(1, sizeof(*connect_info));
00243 
00244   connect_info->port = port;
00245   connect_info->idx = idx;
00246   connect_info->dns_id = -1;
00247   connect_info->timer_id = -1;
00248   sockbuf_attach_filter(connect_info->idx, &net_connect_filter, connect_info);
00249   if (timeout == 0) timeout = default_timeout;
00250   if (timeout > 0) {
00251     char buf[128];
00252     egg_timeval_t howlong;
00253 
00254     snprintf(buf, sizeof(buf), "idx %d to %s/%d", idx, host, port);
00255     howlong.sec = timeout;
00256     howlong.usec = 0;
00257     connect_info->timer_id = timer_create_complex(&howlong, buf, egg_connect_timeout, connect_info, 0, NULL);
00258   }
00259   return(connect_info);
00260 }

static int connect_host_resolved ( void *  client_data,
const char *  host,
char **  ips 
) [static]

Definition at line 166 of file net.c.

References detach(), connect_info::dns_id, EGGNET_LEVEL, connect_info::idx, NULL, connect_info::port, SOCKBUF_CLIENT, SOCKBUF_CONNECTING, sockbuf_on_eof(), sockbuf_set_sock(), SOCKET_CLIENT, socket_create(), SOCKET_NONBLOCK, and SOCKET_TCP.

Referenced by egg_client().

00167 {
00168   connect_info_t *connect_info = client_data;
00169   int sock, idx;
00170 
00171   connect_info->dns_id = -1;
00172 
00173   if (!ips || !ips[0]) {
00174     idx = connect_info->idx;
00175     detach(client_data, idx);
00176     sockbuf_on_eof(idx, EGGNET_LEVEL, -1, "Could not resolve host");
00177     return(0);
00178   }
00179 
00180   sock = socket_create(ips[0], connect_info->port, NULL, 0, SOCKET_CLIENT|SOCKET_TCP|SOCKET_NONBLOCK);
00181   if (sock < 0) {
00182     idx = connect_info->idx;
00183     detach(client_data, idx);
00184     sockbuf_on_eof(idx, EGGNET_LEVEL, -1, "Could not create socket");
00185     return(0);
00186   }
00187 
00188   /* Put this socket into the original sockbuf as a client. This allows
00189    * its connect event to trigger. We don't detach() from it yet, since
00190    * we provide the timeout services. */
00191   sockbuf_set_sock(connect_info->idx, sock, SOCKBUF_CLIENT|SOCKBUF_CONNECTING);
00192   return(0);
00193 }

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

Definition at line 262 of file net.c.

References NULL, sockbuf_detach_filter(), timer_destroy(), and connect_info::timer_id.

Referenced by connect_host_resolved(), egg_connect_timeout(), egg_on_connect(), and egg_on_eof().

00263 {
00264   connect_info_t *connect_info = client_data;
00265 
00266   if (connect_info->timer_id != -1) timer_destroy(connect_info->timer_id);
00267   sockbuf_detach_filter(idx, &net_connect_filter, NULL);
00268   free(connect_info);
00269   return(0);
00270 }

int egg_client ( int  idx,
const char *  host,
int  port,
const char *  vip,
int  vport,
int  timeout 
)

Definition at line 106 of file net.c.

References attach(), connect_host_resolved(), connect_info::dns_id, egg_dns_lookup(), NULL, and sockbuf_new().

Referenced by egg_reconnect(), http_reconnect(), and socks5_reconnect().

00107 {
00108   connect_info_t *connect_info;
00109 
00110   /* If they don't have their own idx (-1), create one. */
00111   if (idx < 0) idx = sockbuf_new();
00112 
00113   /* Resolve the hostname. */
00114   connect_info = attach(idx, host, port, timeout);
00115   connect_info->dns_id = egg_dns_lookup(host, -1, connect_host_resolved, connect_info, NULL);
00116   return(idx);
00117 }

int egg_connect ( const char *  host,
int  port,
int  timeout 
)

Definition at line 155 of file net.c.

References egg_reconnect(), and sockbuf_new().

Referenced by connect_to_next_server(), dcc_accept_send(), do_link(), egg_ident_lookup(), got_chat_request(), and script_net_open().

00156 {
00157   int idx;
00158 
00159   idx = sockbuf_new();
00160   egg_reconnect(idx, host, port, timeout);
00161   return(idx);
00162 }

static int egg_connect_timeout ( void *  client_data  )  [static]

Definition at line 196 of file net.c.

References detach(), connect_info::dns_id, egg_dns_cancel(), EGGNET_LEVEL, connect_info::idx, sockbuf_on_eof(), and connect_info::timer_id.

Referenced by attach().

00197 {
00198   connect_info_t *connect_info = client_data;
00199   int idx, dns_id;
00200 
00201   idx = connect_info->idx;
00202   dns_id = connect_info->dns_id;
00203   connect_info->timer_id = -1;
00204   if (dns_id != -1) {
00205     /* dns_cancel will call connect_host_resolved for us, which
00206      * will filter up a "dns failed" error. */
00207     egg_dns_cancel(dns_id, 1);
00208   }
00209   else {
00210     detach(client_data, idx);
00211     sockbuf_on_eof(idx, EGGNET_LEVEL, -1, "Connect timed out");
00212   }
00213   return(0);
00214 }

int egg_iprintf ( int  idx,
const char *  format,
  ... 
)

int egg_listen ( int  port,
int *  real_port 
)

Definition at line 121 of file net.c.

References egg_server(), and NULL.

Referenced by dcc_listen(), and script_net_listen().

00122 {
00123   int idx;
00124 
00125   /* Proxy/firewall/vhost code goes here. */
00126   idx = egg_server(NULL, port, real_port);
00127   return(idx);
00128 }

int egg_net_init ( void   ) 

Definition at line 53 of file net.c.

References egg_dns_init(), and sockbuf_init().

Referenced by eggdrop_init().

00054 {
00055   if (egg_dns_init() != 0)
00056     return (-1);
00057   return sockbuf_init();
00058 }

int egg_net_shutdown ( void   ) 

Definition at line 60 of file net.c.

References egg_dns_shutdown(), and sockbuf_shutdown().

Referenced by eggdrop_shutdown().

00061 {
00062   if (egg_dns_shutdown() != 0)
00063     ;
00064   return sockbuf_shutdown();
00065 }

static int egg_on_connect ( void *  client_data,
int  idx,
const char *  peer_ip,
int  peer_port 
) [static]

Definition at line 217 of file net.c.

References detach(), EGGNET_LEVEL, and sockbuf_on_connect().

00218 {
00219   detach(client_data, idx);
00220   sockbuf_on_connect(idx, EGGNET_LEVEL, peer_ip, peer_port);
00221   return(0);
00222 }

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

Definition at line 230 of file net.c.

References connect_info::dns_id, egg_dns_cancel(), timer_destroy(), and connect_info::timer_id.

00231 {
00232   connect_info_t *connect_info = client_data;
00233 
00234   if (connect_info->dns_id != -1) egg_dns_cancel(connect_info->dns_id, 0);
00235   if (connect_info->timer_id != -1) timer_destroy(connect_info->timer_id);
00236   free(connect_info);
00237   return(0);
00238 }

static int egg_on_eof ( void *  client_data,
int  idx,
int  err,
const char *  errmsg 
) [static]

Definition at line 224 of file net.c.

References detach(), EGGNET_LEVEL, and sockbuf_on_eof().

00225 {
00226   detach(client_data, idx);
00227   return sockbuf_on_eof(idx, EGGNET_LEVEL, err, errmsg);
00228 }

int egg_proxy_add ( egg_proxy_t proxy  ) 

Definition at line 280 of file net.c.

References nproxies.

Referenced by proxy_init().

00281 {
00282   proxies = realloc(proxies, (nproxies+1) * sizeof(*proxies));
00283   proxies[nproxies++] = proxy;
00284   return(0);
00285 }

int egg_proxy_del ( egg_proxy_t proxy  ) 

Definition at line 287 of file net.c.

References nproxies.

00288 {
00289   int i;
00290 
00291   for (i = 0; i < nproxies; i++) {
00292     if (proxies[i] == proxy) break;
00293   }
00294   if (i == nproxies) return(-1);
00295   memmove(proxies+i, proxies+i+1, (nproxies-i-1) * sizeof(*proxies));
00296   nproxies--;
00297   return(0);
00298 }

const char* egg_proxy_get_default (  ) 

Definition at line 316 of file net.c.

References default_proxy_name.

00317 {
00318   return(default_proxy_name);
00319 }

egg_proxy_t* egg_proxy_lookup ( const char *  name  ) 

Definition at line 300 of file net.c.

References nproxies, and NULL.

Referenced by egg_reconnect().

00301 {
00302   int i;
00303 
00304   for (i = 0; i < nproxies; i++) {
00305     if (proxies[i]->name && !strcasecmp(proxies[i]->name, name)) return(proxies[i]);
00306   }
00307   return(NULL);
00308 }

int egg_proxy_set_default ( const char *  name  ) 

Definition at line 310 of file net.c.

References default_proxy_name, and str_redup().

Referenced by on_proxy_set(), and proxy_init().

00311 {
00312   str_redup(&default_proxy_name, name);
00313   return(0);
00314 }

int egg_reconnect ( int  idx,
const char *  host,
int  port,
int  timeout 
)

Definition at line 132 of file net.c.

References attach(), default_proxy_name, egg_client(), egg_proxy_lookup(), LOG_MISC, NULL, putlog(), and egg_proxy_t::reconnect.

Referenced by egg_connect(), and got_accept().

00133 {
00134   egg_proxy_t *proxy;
00135 
00136   if (!default_proxy_name) return egg_client(idx, host, port, NULL, 0, timeout);
00137 
00138   /* If there is a proxy configured, send this request to the right
00139    * handler. */
00140   proxy = egg_proxy_lookup(default_proxy_name);
00141   if (!proxy) {
00142     putlog(LOG_MISC, "*", "Proxy '%s' selected but not loaded!", default_proxy_name);
00143     putlog(LOG_MISC, "*", "Trying to connect without proxy.");
00144     return egg_client(idx, host, port, NULL, 0, timeout);
00145   }
00146 
00147   /* Ok, so we will connect through the selected proxy. But we still have
00148    * to set up the timeout handler (to avoid duplication of code in the
00149    * proxy handlers) if they want one. */
00150   if (timeout > 0) attach(idx, host, port, timeout);
00151   proxy->reconnect(idx, host, port);
00152   return(0);
00153 }

int egg_server ( const char *  vip,
int  port,
int *  real_port 
)

Definition at line 83 of file net.c.

References NULL, sockbuf_new(), SOCKBUF_SERVER, sockbuf_set_sock(), socket_create(), socket_get_name(), SOCKET_NONBLOCK, SOCKET_SERVER, and SOCKET_TCP.

Referenced by bot_init(), egg_listen(), irc_init(), and telnet_init().

00084 {
00085   int idx, sock, ntries;
00086 
00087   ntries = 0;
00088   do {
00089     sock = socket_create(NULL, 0, vip, port+ntries, SOCKET_SERVER|SOCKET_TCP|SOCKET_NONBLOCK);
00090     ntries++;
00091   } while (sock < 0 && ntries < 20 && port);
00092 
00093   if (sock < 0) return(sock);
00094 
00095   if (real_port) socket_get_name(sock, NULL, real_port);
00096 
00097   idx = sockbuf_new();
00098   sockbuf_set_sock(idx, sock, SOCKBUF_SERVER);
00099 
00100   return(idx);
00101 }


Variable Documentation

char* default_proxy_name = NULL [static]

Definition at line 50 of file net.c.

Referenced by egg_proxy_get_default(), egg_proxy_set_default(), and egg_reconnect().

int default_timeout = 30 [static]

Definition at line 51 of file net.c.

Referenced by attach().

Initial value:

 {
  "connect", EGGNET_LEVEL,
  egg_on_connect, egg_on_eof, NULL,
  NULL, NULL, NULL,
  NULL, egg_on_delete
}

Definition at line 43 of file net.c.

int nproxies = 0 [static]

Definition at line 278 of file net.c.

Referenced by egg_proxy_add(), egg_proxy_del(), and egg_proxy_lookup().

egg_proxy_t** proxies = NULL [static]

Definition at line 277 of file net.c.

const char rcsid[] = "$Id: net.c,v 1.7 2006-09-12 01:50:50 sven Exp $" [static]

Definition at line 21 of file net.c.


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