File: tcpremoteinfo.c

package info (click to toggle)
courier 0.60.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 52,288 kB
  • ctags: 12,677
  • sloc: ansic: 165,348; cpp: 24,820; sh: 16,410; perl: 6,839; makefile: 3,621; yacc: 289; sed: 16
file content (164 lines) | stat: -rw-r--r-- 2,808 bytes parent folder | download | duplicates (7)
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
/*
** Copyright 1998 - 2001 Double Precision, Inc.
** See COPYING for distribution information.
*/

#if	HAVE_CONFIG_H
#include	"config.h"
#endif
#include	"tcpremoteinfo.h"
#include	"soxwrap/sconnect.h"

#if HAVE_UNISTD_H
#include	<unistd.h>
#endif
#if HAVE_FCNTL_H
#include	<fcntl.h>
#endif
#include	<stdio.h>
#include	<errno.h>
#include	<string.h>

#include	"soxwrap/soxwrap.h"

static const char rcsid[]="$Id: tcpremoteinfo.c,v 1.7 2002/02/21 00:37:01 mrsam Exp $";

const char *tcpremoteinfo(const RFC1035_ADDR *laddr, int lport,
	const RFC1035_ADDR *raddr, int rport, const char **ostype)
{
int	fd;
time_t	current_time, max_time;
fd_set	fds;
struct	timeval	tv;
static char buf[512];
char	*bufptr;
int	bufleft, n;
char	*p;
char	*q;
RFC1035_NETADDR	sin;
const struct sockaddr *addr;
int	addrlen;

	fd=rfc1035_mksocket(SOCK_STREAM, 0, &n);
	if (fd < 0)	return (0);

	if (rfc1035_mkaddress(n, &sin, laddr, 0, &addr, &addrlen) < 0)
	{
		close(fd);
		return (0);
	}

	if (sox_bind(fd, addr, addrlen) < 0)
	{
		sox_close(fd);
		return (0);
	}

	time (&current_time);
	max_time=current_time+30;

	if (rfc1035_mkaddress(n, &sin, raddr, htons(113), &addr, &addrlen) < 0)
	{
		sox_close(fd);
		return (0);
	}

	if (s_connect(fd, addr, addrlen, max_time - current_time) < 0)
	{
		sox_close(fd);
		return (0);
	}

	sprintf(buf, "%d,%d\r\n", ntohs(rport), ntohs(lport));
	bufptr=buf;
	bufleft=strlen(buf);
	while (bufleft)
	{
		time(&current_time);
		if (current_time >= max_time)
		{
			sox_close(fd);
			return (0);
		}

		FD_ZERO(&fds);
		FD_SET(fd, &fds);
		tv.tv_sec=max_time-current_time;
		tv.tv_usec=0;
		if (sox_select(fd+1, 0, &fds, 0, &tv) != 1 ||
			!FD_ISSET(fd, &fds))
		{
			sox_close(fd);
			return (0);
		}
		n=sox_write(fd, bufptr, bufleft);
		if (n <= 0)
		{
			sox_close(fd);
			return (0);
		}
		bufptr += n;
		bufleft -= n;
	}

	bufptr=buf;
	bufleft=sizeof(buf);
	do
	{
		if (bufleft == 0)
		{
			sox_close(fd);
			return (0);
		}

		time(&current_time);
		if (current_time >= max_time)
		{
			sox_close(fd);
			return (0);
		}

		FD_ZERO(&fds);
		FD_SET(fd, &fds);
		tv.tv_sec=max_time-current_time;
		tv.tv_usec=0;
		if (sox_select(fd+1, &fds, 0, 0, &tv) != 1 ||
			!FD_ISSET(fd, &fds))
		{
			sox_close(fd);
			return (0);
		}

		n=sox_read(fd, bufptr, bufleft);
		if (n <= 0)
		{
			sox_close(fd);
			return (0);
		}
		bufptr += n;
		bufleft -= n;
	} while (bufptr[-1] != '\n');
	sox_close(fd);
	bufptr[-1]=0;
	--bufptr;
	if (bufptr > buf && bufptr[-1] == '\r')
		bufptr[-1]=0;

	if ((p=strchr(buf, ':')) == 0)
		return (0);

	q=++p;
	if ((p=strchr(p, ':')) == 0)
		return (0);

	*p++=0;
	q=strtok(q, " \t");
	if (!q || strcmp(q, "USERID"))	return (0);
	if (ostype)	*ostype=p;
	if ((p=strchr(p, ':')) == 0)
		return (0);
	*p++=0;
	while (*p && (*p == ' ' || *p == '\t'))	p++;
	return (p);
}