/* 
** CVS: $Id: comfunc.c,v 1.3 2001/01/20 19:58:49 zhangsz Exp $
**
** Title:       comfunc.c 
** Author:      Zhang Shuzhong
** Date:        2000.07.03
**
** Description: Functions of comunication for PACS WLL.
**
*/

#include "../../public/src/include/includes.h"

/*
**  passivesock - allocate & bind a server socket using TCP or UDP
*/

/*@ignore@*/
int
passivesock( const char * transport, u_short portbase, int qlen)
{
struct sockaddr_in    sin;
int    s, type;

    memset(& sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_port = htons(portbase);
    sin.sin_addr.s_addr = INADDR_ANY;
    bzero(&(sin.sin_zero), 8);

    if ( strcmp(transport, "UDP") == 0)
        type = SOCK_DGRAM;
    else
        type = SOCK_STREAM;
    s = socket(AF_INET, type, 0);
    if (s < 0) {
        perror("socket");
        return -3;
    }
    if (bind(s, (struct sockaddr *) & sin, sizeof(sin)) < 0) {
        perror("bind");
        close(s);
        return -4;
    }

    if (type == SOCK_STREAM && listen(s, qlen) < 0) {
        perror("listen");
        close(s);
        return -5;
    }
    return s;
}

/*
**  passiveUDP - create a passive socket for use in a UDP server
*/

int
passiveUDP(u_short portbase)
{
    return (passivesock("UDP", portbase, 0) );
}

const char *
Inet_ntoa(const long addr)
{
	struct sockaddr_in sin;

	sin.sin_addr.s_addr = addr;
	return ((char *) inet_ntoa(sin.sin_addr) );
}

const long
Inet_pton(const char *sp)
{
	struct in_addr    i_addr;

	inet_pton(AF_INET, sp, &i_addr);
	return i_addr.s_addr;
}
/*@end@*/
