masqmail-0.2

diff src/libident/ident.c @ 0:08114f7dcc23

this is masqmail-0.2.21 from oliver kurth
author meillo@marmaro.de
date Fri, 26 Sep 2008 17:05:23 +0200
parents
children 26e34ae9a3e3
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/libident/ident.c	Fri Sep 26 17:05:23 2008 +0200
     1.3 @@ -0,0 +1,155 @@
     1.4 +/*
     1.5 +** ident.c	High-level calls to the ident lib
     1.6 +**
     1.7 +** Author: Pär Emanuelsson <pell@lysator.liu.se>
     1.8 +** Hacked by: Peter Eriksson <pen@lysator.liu.se>
     1.9 +*/
    1.10 +
    1.11 +#ifdef NeXT3
    1.12 +#  include <libc.h>
    1.13 +#endif
    1.14 +
    1.15 +#include <stdio.h>
    1.16 +
    1.17 +#ifdef HAVE_ANSIHEADERS
    1.18 +#  include <stdlib.h>
    1.19 +#  include <string.h>
    1.20 +#endif
    1.21 +
    1.22 +#include <errno.h>
    1.23 +
    1.24 +#include <sys/types.h>
    1.25 +#include <sys/socket.h>
    1.26 +
    1.27 +#define IN_LIBIDENT_SRC
    1.28 +#include "ident.h"
    1.29 +
    1.30 +#include <arpa/inet.h>
    1.31 +
    1.32 +
    1.33 +
    1.34 +
    1.35 +/* Do a complete ident query and return result */
    1.36 +
    1.37 +IDENT *ident_lookup __P2(int, fd,
    1.38 +			 int, timeout)
    1.39 +{
    1.40 +    struct sockaddr_in localaddr, remoteaddr;
    1.41 +    int len;
    1.42 +    
    1.43 +    len = sizeof(remoteaddr);
    1.44 +    if (getpeername(fd, (struct sockaddr*) &remoteaddr, &len) < 0)
    1.45 +	return 0;
    1.46 +    
    1.47 +    len = sizeof(localaddr);
    1.48 +    if (getsockname(fd, (struct sockaddr *) &localaddr, &len) < 0)
    1.49 +	return 0;
    1.50 +
    1.51 +    return ident_query( &localaddr.sin_addr, &remoteaddr.sin_addr,
    1.52 +		       ntohs(localaddr.sin_port), ntohs(remoteaddr.sin_port),
    1.53 +		       timeout);
    1.54 +}
    1.55 +
    1.56 +
    1.57 +IDENT *ident_query __P5(struct in_addr *, laddr,
    1.58 +			struct in_addr *, raddr,
    1.59 +			int, lport,
    1.60 +			int, rport,
    1.61 +			int, timeout)
    1.62 +{
    1.63 +    int res;
    1.64 +    ident_t *id;
    1.65 +    struct timeval timout;
    1.66 +    IDENT *ident=0;
    1.67 +
    1.68 +    
    1.69 +    timout.tv_sec = timeout;
    1.70 +    timout.tv_usec = 0;
    1.71 +    
    1.72 +    if (timeout)
    1.73 +	id = id_open( laddr, raddr, &timout);
    1.74 +    else
    1.75 +	id = id_open( laddr, raddr, (struct timeval *)0);
    1.76 +    
    1.77 +    if (!id)
    1.78 +    {
    1.79 +	errno = EINVAL;
    1.80 +	return 0;
    1.81 +    }
    1.82 +  
    1.83 +    if (timeout)
    1.84 +	res = id_query(id, rport, lport, &timout);
    1.85 +    else
    1.86 +	res = id_query(id, rport, lport, (struct timeval *) 0);
    1.87 +    
    1.88 +    if (res < 0)
    1.89 +    {
    1.90 +	id_close(id);
    1.91 +	return 0;
    1.92 +    }
    1.93 +    
    1.94 +    ident = (IDENT *) malloc(sizeof(IDENT));
    1.95 +    if (!ident) {
    1.96 +	id_close(id);
    1.97 +	return 0;
    1.98 +    }
    1.99 +    
   1.100 +    if (timeout)
   1.101 +	res = id_parse(id, &timout,
   1.102 +		       &ident->lport,
   1.103 +		       &ident->fport,
   1.104 +		       &ident->identifier,
   1.105 +		       &ident->opsys,
   1.106 +		       &ident->charset);
   1.107 +    else
   1.108 +	res = id_parse(id, (struct timeval *) 0,
   1.109 +		       &ident->lport,
   1.110 +		       &ident->fport,
   1.111 +		       &ident->identifier,
   1.112 +		       &ident->opsys,
   1.113 +		       &ident->charset);
   1.114 +    
   1.115 +    if (res != 1)
   1.116 +    {
   1.117 +	free(ident);
   1.118 +	id_close(id);
   1.119 +	return 0;
   1.120 +    }
   1.121 +    
   1.122 +    id_close(id);
   1.123 +    return ident;			/* At last! */
   1.124 +}
   1.125 +
   1.126 +
   1.127 +char *ident_id __P2(int, fd,
   1.128 +		    int, timeout)
   1.129 +{
   1.130 +    IDENT *ident;
   1.131 +    char *id=0;
   1.132 +    
   1.133 +    ident = ident_lookup(fd, timeout);
   1.134 +    if (ident && ident->identifier && *ident->identifier)
   1.135 +    {
   1.136 +	id = id_strdup(ident->identifier);
   1.137 +	if (id == NULL)
   1.138 +	    return NULL;
   1.139 +    }
   1.140 +
   1.141 +    ident_free(ident);
   1.142 +    return id;
   1.143 +}
   1.144 +
   1.145 +
   1.146 +void ident_free __P1(IDENT *, id)
   1.147 +{
   1.148 +    if (!id)
   1.149 +	return;
   1.150 +    if (id->identifier)
   1.151 +	free(id->identifier);
   1.152 +    if (id->opsys)
   1.153 +	free(id->opsys);
   1.154 +    if (id->charset)
   1.155 +	free(id->charset);
   1.156 +    free(id);
   1.157 +}
   1.158 +