/***************************************************************************
 *
 * Copyright (c) 2000, 2001 BalaBit IT Ltd, Budapest, Hungary
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: io.h,v 1.8.2.3.2.2 2001/09/19 19:22:17 bazsi Exp $
 *
 ***************************************************************************/

#ifndef ZORP_IO_H_INCLUDED
#define ZORP_IO_H_INCLUDED

#include <zorp/zorp.h>
#include <zorp/sockaddr.h>

/* z_io_listen public interface */

/*+

  Listen on the given socket address, and call a given callback for
  each incoming connection.

  +*/
typedef struct _ZIOListen 
{
  ZSockAddr *local;
} ZIOListen;

typedef void (*ZAcceptFunc)(int fd, ZSockAddr *client, gboolean last_connection, gpointer user_data);

ZIOListen *z_io_listen_new(ZSockAddr *local, 
			   gboolean accept_one,
			   gint backlog,
			   ZAcceptFunc callback,
			   gpointer user_data);

void z_io_listen_ref(ZIOListen *l);
void z_io_listen_unref(ZIOListen *l);
void z_io_listen_start(ZIOListen *self);

/* z_io_connect public interface */

/*+

  Connect to the given socket address using the given local address,
  and call a given callback function if the connection is established.

  +*/
typedef struct _ZIOConnect 
{
  ZSockAddr *local;
} ZIOConnect;

typedef void (*ZConnectFunc)(int fd, gpointer user_data);

ZIOConnect *z_io_connect_new(ZSockAddr *local, 
			     ZSockAddr *remote,
			     ZConnectFunc callback,
			     gpointer user_data);

ZSockAddr *z_io_connect_start(ZIOConnect *self);
void z_io_connect_ref(ZIOConnect *l);
void z_io_connect_unref(ZIOConnect *l);

/* z_io_watch public interface */

typedef gboolean (*ZWatchFDFunc)(int fd, GIOCondition events, gpointer user_data);

/*+

  Watch the given fd for I/O events, and call the given callback if
  something becomes available.

  +*/
typedef struct _ZIOWatchFD
{
  GIOCondition cond;
} ZIOWatchFD;

ZIOWatchFD *
z_io_watch_fd_new(int fd, 
		     GIOCondition cond,
		     ZWatchFDFunc callback, 
		     gpointer user_data);

void
z_io_watch_fd_destroy(ZIOWatchFD *watch);

gboolean z_fd_set_nonblock(int fd, gboolean enable);
gboolean z_fd_set_keepalive(int fd, gboolean enable);
gboolean z_fd_set_oobinline(int fd, gboolean enable);

#endif
