File: ident.c

package info (click to toggle)
libident 0.32-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,464 kB
  • sloc: sh: 8,260; ansic: 840; makefile: 27
file content (179 lines) | stat: -rw-r--r-- 3,723 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
** ident.c	High-level calls to the ident lib
**
** Author: Pr Emanuelsson <pell@lysator.liu.se>
** Hacked by: Peter Eriksson <pen@lysator.liu.se>
** Updated by: Rmi Denis-Courmont <rdenis@simphalempin.com>
*/

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#if HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#if HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#if HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif

#define IN_LIBIDENT_SRC
#include "ident.h"





/* Do a complete ident query and return result */

IDENT *ident_lookup (int fd, int timeout)
{
    struct sockaddr_storage localaddr, remoteaddr;
    int len;
    
    len = sizeof(remoteaddr);
    if (getpeername(fd, (struct sockaddr*)&remoteaddr, &len) < 0)
	return 0;
    
    len = sizeof(localaddr);
    if (getsockname(fd, (struct sockaddr *)&localaddr, &len) < 0)
	return 0;

    return ident_query_addr((struct sockaddr *)&localaddr,
				(struct sockaddr *)&remoteaddr, timeout);
}


IDENT *ident_query (const struct in_addr *laddr, const struct in_addr *faddr,
			int lport, int rport, int timeout)
{
    struct sockaddr_in lsockaddr, fsockaddr;

    memset(&lsockaddr, 0, sizeof(lsockaddr));
    lsockaddr.sin_family = AF_INET;
#if HAVE_SA_LEN
    lsockaddr.sin_len = sizeof(lsockaddr);
#endif
    lsockaddr.sin_addr.s_addr = laddr->s_addr;
    lsockaddr.sin_port = lport;

    memcpy(&fsockaddr, &lsockaddr, sizeof(fsockaddr));
    fsockaddr.sin_addr.s_addr = faddr->s_addr;
    fsockaddr.sin_port = rport;

    return ident_query_addr((struct sockaddr *)&lsockaddr,
				(struct sockaddr *)&fsockaddr, timeout);
}

IDENT *ident_query_addr (const struct sockaddr *laddr,
				const struct sockaddr *raddr, int timeout)
{
    int res, lport, rport;
    ident_t *id;
    struct timeval timout;
    IDENT *ident = NULL;

    switch (laddr->sa_family)
    {
      case AF_INET:
        /* We assume that both addresses are in the same family. */
        lport = ntohs(((struct sockaddr_in *)laddr)->sin_port);
        rport = ntohs(((struct sockaddr_in *)raddr)->sin_port);
        break;

#ifdef AF_INET6
      case AF_INET6:
        lport = ntohs(((struct sockaddr_in6 *)laddr)->sin6_port);
        rport = ntohs(((struct sockaddr_in6 *)raddr)->sin6_port);
        break;
#endif

      default:
        return NULL;
    }
    
    timout.tv_sec = timeout;
    timout.tv_usec = 0;
    
    id = id_open_addr(laddr, raddr, (timeout) ? &timout : NULL);
    if (id == NULL)
    {
	errno = EINVAL;
	return NULL;
    }
  
    if (timeout)
	res = id_query(id, rport, lport, &timout);
    else
	res = id_query(id, rport, lport, (struct timeval *) 0);
    
    if (res < 0)
    {
	id_close(id);
	return NULL;
    }
    
    ident = (IDENT *) malloc(sizeof(IDENT));
    if (ident == NULL) {
	id_close(id);
	return NULL;
    }
    
    res = id_parse(id, (timeout) ? &timout : NULL, &ident->lport,
		   &ident->fport, &ident->identifier, &ident->opsys,
		   &ident->charset);
    
    if (res != 1)
    {
	free(ident);
	id_close(id);
	return NULL;
    }
    
    id_close(id);
    return ident;			/* At last! */
}


char *ident_id (int fd, int timeout)
{
    IDENT *ident;
    char *id = NULL;
    
    ident = ident_lookup(fd, timeout);
    if (ident && ident->identifier && *ident->identifier)
    {
	id = id_strdup(ident->identifier);
	if (id == NULL)
	    return NULL;
    }

    ident_free(ident);
    return id;
}


void ident_free (IDENT * id)
{
    if (id != NULL)
    {
        if (id->identifier)
	    free(id->identifier);
	if (id->opsys)
	    free(id->opsys);
	if (id->charset)
	    free(id->charset);
	free(id);
    }
}

const char *id_version = PACKAGE_VERSION;