masqmail

view 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 source
1 /*
2 ** ident.c High-level calls to the ident lib
3 **
4 ** Author: Pär Emanuelsson <pell@lysator.liu.se>
5 ** Hacked by: Peter Eriksson <pen@lysator.liu.se>
6 */
8 #ifdef NeXT3
9 # include <libc.h>
10 #endif
12 #include <stdio.h>
14 #ifdef HAVE_ANSIHEADERS
15 # include <stdlib.h>
16 # include <string.h>
17 #endif
19 #include <errno.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
24 #define IN_LIBIDENT_SRC
25 #include "ident.h"
27 #include <arpa/inet.h>
32 /* Do a complete ident query and return result */
34 IDENT *ident_lookup __P2(int, fd,
35 int, timeout)
36 {
37 struct sockaddr_in localaddr, remoteaddr;
38 int len;
40 len = sizeof(remoteaddr);
41 if (getpeername(fd, (struct sockaddr*) &remoteaddr, &len) < 0)
42 return 0;
44 len = sizeof(localaddr);
45 if (getsockname(fd, (struct sockaddr *) &localaddr, &len) < 0)
46 return 0;
48 return ident_query( &localaddr.sin_addr, &remoteaddr.sin_addr,
49 ntohs(localaddr.sin_port), ntohs(remoteaddr.sin_port),
50 timeout);
51 }
54 IDENT *ident_query __P5(struct in_addr *, laddr,
55 struct in_addr *, raddr,
56 int, lport,
57 int, rport,
58 int, timeout)
59 {
60 int res;
61 ident_t *id;
62 struct timeval timout;
63 IDENT *ident=0;
66 timout.tv_sec = timeout;
67 timout.tv_usec = 0;
69 if (timeout)
70 id = id_open( laddr, raddr, &timout);
71 else
72 id = id_open( laddr, raddr, (struct timeval *)0);
74 if (!id)
75 {
76 errno = EINVAL;
77 return 0;
78 }
80 if (timeout)
81 res = id_query(id, rport, lport, &timout);
82 else
83 res = id_query(id, rport, lport, (struct timeval *) 0);
85 if (res < 0)
86 {
87 id_close(id);
88 return 0;
89 }
91 ident = (IDENT *) malloc(sizeof(IDENT));
92 if (!ident) {
93 id_close(id);
94 return 0;
95 }
97 if (timeout)
98 res = id_parse(id, &timout,
99 &ident->lport,
100 &ident->fport,
101 &ident->identifier,
102 &ident->opsys,
103 &ident->charset);
104 else
105 res = id_parse(id, (struct timeval *) 0,
106 &ident->lport,
107 &ident->fport,
108 &ident->identifier,
109 &ident->opsys,
110 &ident->charset);
112 if (res != 1)
113 {
114 free(ident);
115 id_close(id);
116 return 0;
117 }
119 id_close(id);
120 return ident; /* At last! */
121 }
124 char *ident_id __P2(int, fd,
125 int, timeout)
126 {
127 IDENT *ident;
128 char *id=0;
130 ident = ident_lookup(fd, timeout);
131 if (ident && ident->identifier && *ident->identifier)
132 {
133 id = id_strdup(ident->identifier);
134 if (id == NULL)
135 return NULL;
136 }
138 ident_free(ident);
139 return id;
140 }
143 void ident_free __P1(IDENT *, id)
144 {
145 if (!id)
146 return;
147 if (id->identifier)
148 free(id->identifier);
149 if (id->opsys)
150 free(id->opsys);
151 if (id->charset)
152 free(id->charset);
153 free(id);
154 }