masqmail

view src/libident/ident.c @ 172:cd38f10532e7

merge
author meillo@marmaro.de
date Wed, 14 Jul 2010 00:55:22 +0200
parents 08114f7dcc23
children
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*
35 ident_lookup __P2(int, fd, 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, ntohs(localaddr.sin_port), ntohs(remoteaddr.sin_port), timeout);
49 }
52 IDENT*
53 ident_query __P5(struct in_addr *, laddr, struct in_addr *, raddr, int, lport, int, rport, int, timeout)
54 {
55 int res;
56 ident_t *id;
57 struct timeval timout;
58 IDENT *ident = 0;
61 timout.tv_sec = timeout;
62 timout.tv_usec = 0;
64 if (timeout)
65 id = id_open(laddr, raddr, &timout);
66 else
67 id = id_open(laddr, raddr, (struct timeval *) 0);
69 if (!id) {
70 errno = EINVAL;
71 return 0;
72 }
74 if (timeout)
75 res = id_query(id, rport, lport, &timout);
76 else
77 res = id_query(id, rport, lport, (struct timeval *) 0);
79 if (res < 0) {
80 id_close(id);
81 return 0;
82 }
84 ident = (IDENT *) malloc(sizeof(IDENT));
85 if (!ident) {
86 id_close(id);
87 return 0;
88 }
90 if (timeout)
91 res = id_parse(id, &timout, &ident->lport, &ident->fport, &ident->identifier, &ident->opsys, &ident->charset);
92 else
93 res = id_parse(id, (struct timeval *) 0, &ident->lport, &ident->fport, &ident->identifier, &ident->opsys, &ident->charset);
95 if (res != 1) {
96 free(ident);
97 id_close(id);
98 return 0;
99 }
101 id_close(id);
102 return ident; /* At last! */
103 }
106 char*
107 ident_id __P2(int, fd, int, timeout)
108 {
109 IDENT *ident;
110 char *id = 0;
112 ident = ident_lookup(fd, timeout);
113 if (ident && ident->identifier && *ident->identifier) {
114 id = id_strdup(ident->identifier);
115 if (id == NULL)
116 return NULL;
117 }
119 ident_free(ident);
120 return id;
121 }
124 void
125 ident_free __P1(IDENT *, id)
126 {
127 if (!id)
128 return;
129 if (id->identifier)
130 free(id->identifier);
131 if (id->opsys)
132 free(id->opsys);
133 if (id->charset)
134 free(id->charset);
135 free(id);
136 }