/***************************************************************************
 *
 * 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: sockaddr.h,v 1.9.4.2 2001/09/19 19:22:17 bazsi Exp $
 *
 ***************************************************************************/

#ifndef ZORP_SOCKADDR_H_INCLUDED
#define ZORP_SOCKADDR_H_INCLUDED

#include <zorp/zorp.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>

/* sockaddr public interface */

#define ZSA_LOCAL		0x0001 /* within host */

typedef struct _ZSockAddrFuncs ZSockAddrFuncs;

typedef struct _ZSockAddr
{
  gint refcnt;
  guint32 flags;
  ZSockAddrFuncs *sa_funcs;
  int salen;
  struct sockaddr sa;
} ZSockAddr;

struct _ZSockAddrFuncs 
{
  GIOError (*sa_bind_prepare)   (int sock, ZSockAddr *addr);
  GIOError (*sa_bind)		(int sock, ZSockAddr *addr);
  gchar   *(*sa_format)         (ZSockAddr *addr,   /* format to text form */
  				 gchar *text,
  				 gulong n);
  void     (*freefn)            (ZSockAddr *addr);
};

ZSockAddr *z_sockaddr_new(struct sockaddr *sa, int salen);
gchar *z_sockaddr_format(ZSockAddr *a, gchar *text, gulong n);
ZSockAddr *z_sockaddr_ref(ZSockAddr *a);
void z_sockaddr_unref(ZSockAddr *a);

ZSockAddr *z_sockaddr_inet_new(gchar *ip, guint16 port);
ZSockAddr *z_sockaddr_inet_new2(struct sockaddr_in *sin);
ZSockAddr *z_sockaddr_inet_range_new(gchar *ip, guint16 min_port, guint16 max_port);

#if ENABLE_IPOPTIONS
void z_sockaddr_inet_get_options(ZSockAddr *s, gpointer *options, guint *options_length);
void z_sockaddr_inet_set_options(ZSockAddr *s, gpointer options, guint options_length);
#endif

ZSockAddr *z_sockaddr_unix_new(gchar *name);
ZSockAddr *z_sockaddr_unix_new2(struct sockaddr_un *s_un, int sunlen);

GIOError z_bind(int fd, ZSockAddr *addr);
GIOError z_accept(int fd, int *newfd, ZSockAddr **addr);
GIOError z_connect(int fd, ZSockAddr *remote);
gchar *z_inet_ntoa(char *buf, size_t bufsize, struct in_addr a);
gint z_inet_aton(char *buf, struct in_addr *a);

#endif
