Exemplo n.º 1
0
Arquivo: serwer.c Projeto: batas2/UMK
/*
 * Autor: Bartosz Fr¹ckowiak
 * wwww:  http://www.batas2.boo.pl/
 */
int main(int argc, char *argv[])
{

	int fd = -1, sd = -1;
	int port = -1;
	struct t_call *call;

	if(argc != 2){
		printf("Sposob uruchomienia: %s <port>\n", argv[0]);
		exit(1);
	}else{
		port = atoi(argv[1]);
	}

	fd = local_work(port, &call);

	while (1) {
		if (t_listen(fd, call) < 0)
			error("serwer->t_listen", fd);

		if ((sd = accept_client(fd, call)) != -1)
			serwer_work(sd);
	}
	if (t_close(fd) < 0)
		error("serwer->t_close", fd, sd);
	if (t_close(sd) < 0)
		error("serwer->serwer_wrok->t_close", fd, sd);
}
Exemplo n.º 2
0
enum proto_accept_error
proto_accept_connection(int listener_fd, int *read_fd, int *write_fd,
			const char **name)
{
    int timeout = server_int_option("name_lookup_timeout", 5);
    int fd;
    struct sockaddr_in *addr = (struct sockaddr_in *) call->addr.buf;
    static Stream *s = 0;

    if (!s)
	s = new_stream(100);

    fd = t_open((void *) "/dev/tcp", O_RDWR, 0);
    if (fd < 0) {
	if (t_errno == TSYSERR && errno == EMFILE)
	    return PA_FULL;
	else {
	    log_ti_error("Opening endpoint for new connection");
	    return PA_OTHER;
	}
    }
    if (t_bind(fd, 0, 0) < 0) {
	log_ti_error("Binding endpoint for new connection");
	t_close(fd);
	return PA_OTHER;
    }
    if (t_listen(listener_fd, call) < 0) {
	log_ti_error("Accepting new network connection");
	t_close(fd);
	return PA_OTHER;
    }
    if (t_accept(listener_fd, fd, call) < 0) {
	log_ti_error("Accepting new network connection");
	t_close(fd);
	return PA_OTHER;
    }
    if (!set_rw_able(fd)) {
	t_close(fd);
	return PA_OTHER;
    }
    *read_fd = *write_fd = fd;
    stream_printf(s, "%s, port %d",
		  lookup_name_from_addr(addr, timeout),
		  (int) ntohs(addr->sin_port));
    *name = reset_stream(s);
    return PA_OKAY;
}
Exemplo n.º 3
0
Arquivo: tlx.c Projeto: alhazred/onarm
/*
 * Takes a connection request off 'fd' in the form of a t_call structure
 * and returns a pointer to it.
 * Returns NULL on failure, else pointer to t_call structure on success.
 */
static struct t_call *
get_new_conind(int fd)
{
	struct t_call *call;

	debug_msg("Entering get_new_conind");

	/* LINTED E_BAD_PTR_CAST_ALIGN */
	if ((call = (struct t_call *)t_alloc(fd, T_CALL, T_ALL)) == NULL) {
		error_msg("t_alloc: %s", t_strerror(t_errno));
		return (NULL);
	}
	if (t_listen(fd, call) < 0) {
		error_msg("t_listen: %s", t_strerror(t_errno));
		(void) t_free((char *)call, T_CALL);
		return (NULL);
	}

	return (call);
}
Exemplo n.º 4
0
static XtransConnInfo
TRANS(TLIAccept)(XtransConnInfo ciptr, int *status)

{
    struct t_call	*call;
    XtransConnInfo	newciptr;
    int	i;

    prmsg(2,"TLIAccept(%x->%d)\n", ciptr, ciptr->fd);

    if( (call=(struct t_call *)t_alloc(ciptr->fd,T_CALL,T_ALL)) == NULL )
    {
	prmsg(1, "TLIAccept() failed to allocate a t_call\n");
	*status = TRANS_ACCEPT_BAD_MALLOC;
	return NULL;
    }

    if( t_listen(ciptr->fd,call) < 0 )
    {
	extern char *t_errlist[];
	extern int t_errno;
	prmsg(1, "TLIAccept() t_listen() failed\n");
	prmsg(1, "TLIAccept: %s\n", t_errlist[t_errno]);
	t_free((char *)call,T_CALL);
	*status = TRANS_ACCEPT_MISC_ERROR;
	return NULL;
    }

    /*
     * Now we need to set up the new endpoint for the incoming connection.
     */

    i=ciptr->index; /* Makes the next line more readable */

    if( (newciptr=TRANS(TLIOpen)(TLItrans2devtab[i].devcotsname)) == NULL )
    {
	prmsg(1, "TLIAccept() failed to open a new endpoint\n");
	t_free((char *)call,T_CALL);
	*status = TRANS_ACCEPT_MISC_ERROR;
	return NULL;
    }

    if( TRANS(TLITLIBindLocal)(newciptr->fd,TLItrans2devtab[i].family,"") < 0 )
    {
	prmsg(1,
	      "TLIAccept: TRANS(TLITLIBindLocal)() failed: %d\n",
	      errno);
	t_free((char *)call,T_CALL);
	t_close(newciptr->fd);
	free(newciptr);
	*status = TRANS_ACCEPT_MISC_ERROR;
	return NULL;
    }


    if( t_accept(ciptr->fd,newciptr->fd,call) < 0 )
    {
	extern char *t_errlist[];
	extern int t_errno;
	prmsg(1, "TLIAccept() t_accept() failed\n");
	prmsg(1, "TLIAccept: %s\n", t_errlist[t_errno]);
	if( t_errno == TLOOK )
	{
	    int evtype = t_look(ciptr->fd);
	    prmsg(1, "TLIAccept() t_look() returned %d\n", evtype);
	    switch( evtype )
	    {
		case T_DISCONNECT:
		    if( t_rcvdis(ciptr->fd, NULL) < 0 )
		    {
			prmsg(1, "TLIAccept() t_rcvdis() failed\n");
			prmsg(1, "TLIAccept: %s\n", t_errlist[t_errno]);
		    }
		    break;
		default:
		    break;
	    }
	}
	t_free((char *)call,T_CALL);
	t_close(newciptr->fd);
	free(newciptr);
	*status = TRANS_ACCEPT_FAILED;
	return NULL;
    }

    t_free((char *)call,T_CALL);

    if( TRANS(TLIGetAddr)(newciptr) < 0 )
    {
	prmsg(1,
	      "TLIAccept: TRANS(TLIGetPeerAddr)() failed: %d\n",
	      errno);
	t_close(newciptr->fd);
	free(newciptr);
	*status = TRANS_ACCEPT_MISC_ERROR;
	return NULL;
    }

    if( TRANS(TLIGetPeerAddr)(newciptr) < 0 )
    {
	prmsg(1,
	      "TLIAccept: TRANS(TLIGetPeerAddr)() failed: %d\n",
	      errno);
	t_close(newciptr->fd);
	free(newciptr->addr);
	free(newciptr);
	*status = TRANS_ACCEPT_MISC_ERROR;
	return NULL;
    }

    if( ioctl(newciptr->fd, I_POP,"timod") < 0 )
    {
	prmsg(1, "TLIAccept() ioctl(I_POP, \"timod\") failed %d\n",
	      errno);
	t_close(newciptr->fd);
	free(newciptr->addr);
	free(newciptr);
	*status = TRANS_ACCEPT_MISC_ERROR;
	return NULL;
    }

    if( ioctl(newciptr->fd, I_PUSH,"tirdwr") < 0 )
    {
	prmsg(1, "TLIAccept() ioctl(I_PUSH,\"tirdwr\") failed %d\n",
	      errno);
	t_close(newciptr->fd);
	free(newciptr->addr);
	free(newciptr);
	*status = TRANS_ACCEPT_MISC_ERROR;
	return NULL;
    }

    *status = 0;

    return newciptr;
}
Exemplo n.º 5
0
int main(int argc, char **argv) {

    int opt;
    char *dir = NULL;
    char *puerto = NULL;
    int iPuerto = 0;
    int flagServidor=0;
    int resultado_primitiva;
    t_direccion tsap_destino, tsap_origen;

    memset(&tsap_origen, 0, sizeof (tsap_origen));
    memset(&tsap_destino, 0, sizeof (tsap_destino));

    while ((opt = getopt(argc, argv, "scp:r:")) != -1) {
        switch (opt) {
            case 's':
                fprintf(stderr,"\naplicacion servidor\n");
                flagServidor = 1;
                break;
            case 'c':
                fprintf(stderr,"\naplicacion cliente\n");
                flagServidor = 0;
                break;
            case 'p':
                puerto = optarg;
                iPuerto = atoi(puerto);
                fprintf(stderr,"\nEl puerto es: %d\n", iPuerto);
                //tsap_origen.puerto = iPuerto;
                break;
            case 'r':
                dir = optarg;
                inet_pton(AF_INET, dir, &tsap_destino.ip);
                fprintf(stderr,"\nla direccion es: %s\n", dir);
                break;
        }
    }

    //miramos si la aplicacion funciona como cliente o como servidor
    if(flagServidor == 0){
        tsap_destino.puerto=iPuerto;
        resultado_primitiva = t_connect (&tsap_destino, &tsap_origen);
    }else{
        tsap_origen.puerto = iPuerto;
        resultado_primitiva = t_listen (&tsap_origen, &tsap_destino);
    }

    //resultados de la primitiva
    switch(resultado_primitiva){
        case 0:
            fprintf(stderr,"\nPrimitiva ejecutada con exito\n");
            break;
        case -1:
            fprintf(stderr,"\nLa conexion referida no esta en la tabla\n");
            break;
        case -2:
            fprintf(stderr,"\nColision con una conexion ya presente en la tabla\n");
            break;
        case -3:
            fprintf(stderr,"\nError de (sub)red.Agotado el numero de retransmisiones de un paquete\n");
            break;
        case -4:
            fprintf(stderr,"\nError al entrar en el kernel\n");
            break;
        case -5:
            fprintf(stderr,"\nMensaje demasiado grande\n");
            break;
        case -6:
            fprintf(stderr,"\nNo existe el TSAP especificado\n");
            break;
        case -7:
            fprintf(stderr,"\nDatos no disponibles\n");
            break;
        case -8:
            fprintf(stderr,"\nFlujo de datos ya cerrado\n");
            break;
        case -9:
            fprintf(stderr,"\nArgumento invalido\n");
            break;
        case -10:
            fprintf(stderr,"\nNumero maximo de conexiones alcanzado\n");
            break;
        case -11:
            fprintf(stderr,"\nError indefinido\n");
            break;
        case -12:
            fprintf(stderr,"\nRecibida desconexion\n");
            break;
    }
   
    return 0;
}
Exemplo n.º 6
0
/*
 ****************************************************************
 *	Aguarda pedidos de conexão (servidor)			*
 ****************************************************************
 */
int
tcp_listen (int fd, char *name)
{
	T_CALL		call;
	T_BIND		bind;
	INADDR		client_addr, addr[2];
	int		newfd;
	char		*cp;

	/*
	 *	Preenche a estrutura T_CALL: só o membro addr é relevante.
	 */
	FILL_NETBUF (call.addr,  &client_addr, sizeof (client_addr));
	FILL_NETBUF (call.opt,   NULL,         0);
	FILL_NETBUF (call.udata, NULL,         0);

	/*
	 *	Espera chegar um pedido de conexão.
	 */
	if (t_listen (fd, &call) < 0)
		return (-1);

	/*
	 *	Abre outro "endpoint".
	 */
	if ((newfd = t_open (tcpdevname, O_RDWR, (T_INFO *)NULL)) < 0)
		return (-1);

	/*
	 *	Preenche a estrutura T_BIND.
	 */
	if (client_addr.a_addr == LOCAL_IP_ADDR)
	{	/*
		 *	A conexão é local: deixa o kernel gerar outro port.
		 */
		bind.addr.len    = 0;
		bind.addr.buf    = addr;
		bind.addr.maxlen = sizeof (INADDR);
	}
	else
	{	/*
		 *	A conexão é remota: respeita o port do "t_listen".
		 */
		FILL_INADDR (addr[0], 0, listen_port);
		FILL_INADDR (addr[1], client_addr.a_addr, client_addr.a_port);
		FILL_NETBUF (bind.addr, addr, sizeof (addr));
	}

	bind.qlen = 0;		/* Não vamos dar "t_listen" nesta conexão */

	if (t_bind (newfd, &bind, (T_BIND *)NULL) < 0)
		goto bad;

	if (set_param (newfd) < 0)
		goto bad;

	/*
	 *	Aceita a conexão no novo "endpoint".
	 */
	if (t_accept (fd, newfd, &call) < 0)
		goto bad;

	if
	(	name != NOSTR &&
		(cp = t_addr_to_node (fd, client_addr.a_addr)) != NOSTR
	)
		strcpy (name, cp);

	return (newfd);

bad:	t_close (newfd);
	return (-1);

}	/* end tcp_listen */
Exemplo n.º 7
0
HTCPCONN tcp_ipv6_server_init(unsigned short p_usPort, void * arg )
{
	gFunctions = (T_PFN_functions *)arg;

	SOCKTYPE sock = t_socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	if (sock == INVALID_SOCKET)
	{	C_NLOG_ERR("Iniche:tcp_ipv6_server_init: couldn't create ip6 socket");
			return NULL;
	}

	//add sock in array
	int i = 0;
	int isSett = 0;
	for(;i < MAX_SOCK_NO; i++)
	{
		if(g_sockets[i].sock == INVALID_SOCKET)
		{
			g_sockets[i].sock = sock;
			g_sockets[i].indxToListen = -1;
			isSett = 1;
			break;
		}
	}
	if(isSett == 0)
	{
		t_socketclose(sock);
		C_NLOG_ERR("Iniche:tcp_ipv6_server_init: couldn't create ip6 socket.");
		return NULL;
	}

	struct sockaddr_in6   tcpsin6;
	int addrlen = sizeof(tcpsin6);
	IP6CPY(&tcpsin6.sin6_addr, &in6addr_any);
	tcpsin6.sin6_port = htons(p_usPort);
	tcpsin6.sin6_family = AF_INET6;

	int e = t_bind(g_sockets[i].sock, (struct sockaddr *)&tcpsin6, addrlen);
	if (e != 0)
	{
		e = t_errno(g_sockets[i].sock);
		t_socketclose(g_sockets[i].sock);
		g_sockets[i].sock = INVALID_SOCKET;
		C_NLOG_ERR("Iniche:tcp_ipv6_server_init: error %d binding tcp listen on port %d",e, p_usPort);
		return NULL;
	}

	int iTmp = 1;

	e = t_setsockopt(g_sockets[i].sock, SOL_SOCKET, SO_NONBLOCK, (void *)&iTmp, sizeof(iTmp));
	if (e == SOCKET_ERROR)
	{
		e = t_errno(g_sockets[i].sock);
		t_socketclose(g_sockets[i].sock);
		g_sockets[i].sock = INVALID_SOCKET;
		C_NLOG_ERR("Iniche:tcp_ipv6_server_init: t_setsockopt() SO_NONBLOCK failed, Err: %d", e);
		return NULL;
	}

	e = t_listen(g_sockets[i].sock,MAX_SOCK_NO);
	if (e != 0)
	{
		e = t_errno(g_sockets[i].sock);
		t_socketclose(g_sockets[i].sock);
		g_sockets[i].sock = INVALID_SOCKET;
		C_NLOG_ERR("Iniche:tcp_ipv6_server_init: error %d  tcp can't listen on port %d",e, p_usPort);
		return NULL;
	}

	 C_NLOG_INFO( "Iniche:tcp_ipv6_server_init: TCP server initialized successfully." );

	 return (HTCPCONN)(g_sockets+i);
}