1 /*
2 ** id_query.c Transmit a query to an IDENT server
3 **
4 ** Author: Peter Eriksson <pen@lysator.liu.se>
5 */
6
7 #if HAVE_CONFIG_H
8 # include "config.h"
9 #endif
10
11 #include <stdio.h>
12 #include <errno.h>
13 #include <signal.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #if HAVE_SYS_TYPES_H
17 # include <sys/types.h>
18 #endif
19 #if HAVE_UNISTD_H
20 # include <unistd.h>
21 #endif
22 #if HAVE_SYS_SELECT_H
23 # include <sys/select.h>
24 #else
25 # if HAVE_SYS_TIME_H
26 # include <sys/time.h>
27 # endif
28 #endif
29
30 #define IN_LIBIDENT_SRC
31 #include "ident.h"
32
33
id_query(ident_t * id,int lport,int fport,struct timeval * timeout)34 int id_query (ident_t *id, int lport, int fport, struct timeval *timeout)
35 {
36 RETSIGTYPE (*old_sig)();
37 int res;
38 char buf[80];
39 fd_set ws;
40
41 sprintf(buf, "%d , %d\r\n", lport, fport);
42
43 if (timeout)
44 {
45 FD_ZERO(&ws);
46 FD_SET(id->fd, &ws);
47
48 res = select(FD_SETSIZE, NULL, &ws, NULL, timeout);
49 if (res < 0)
50 return -1;
51
52 if (res == 0)
53 {
54 errno = ETIMEDOUT;
55 return -1;
56 }
57 }
58
59 old_sig = signal(SIGPIPE, SIG_IGN);
60
61 res = write(id->fd, buf, strlen(buf));
62
63 signal(SIGPIPE, old_sig);
64
65 return res;
66 }
67