File: id_open.c

package info (click to toggle)
socks4-server 4.3.beta2-13
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,512 kB
  • ctags: 1,778
  • sloc: ansic: 19,305; makefile: 404; sh: 52
file content (177 lines) | stat: -rw-r--r-- 3,870 bytes parent folder | download | duplicates (9)
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
/*
** id_open.c                 Establish/initiate a connection to an IDENT server
**
** Author: Peter Eriksson <pen@lysator.liu.se>
** Fixes: Pdr Emanuelsson <pell@lysator.liu.se>
*/


#include <stdio.h>
#include <errno.h>
#if defined(ISC)
#include <net/errno.h>
#endif /* #if defined(ISC) */
#include <memory.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/file.h>
#if defined(ISC)
#include <fcntl.h>
#endif /* #if defined(ISC) */
#include <netinet/in.h>
#include <arpa/inet.h>

/* SOLARIS */
#if defined(__svr4__) || defined (SOLARIS) || defined(SCO)
#include <fcntl.h>
#endif

/* LINUX */
#ifdef LINUX
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#ifdef FNDLEAY                /*watch out for a ?typo? in fcntl.h !? */
#define FNDELAY FNDLEAY
#endif
#endif /* LINUX */

/* The following struct linger declaration seemed to be
 * missing from older versions of LINUX but is present in
 * current.  If you need it, you must uncomment NEED_STRUCT_LINGER
 * in the top level Makefile.
 */

#ifdef NEED_STRUCT_LINGER
/*
 * Structure used for manipulating linger option.
 */
struct  linger {
        int     l_onoff;                /* option on/off */
        int     l_linger;               /* linger time */
};
#endif /* #ifdef NEED_STRUCT_LINGER */

#include "ident.h"

/*extern void *malloc __P((int size));*/
  

ident_t *id_open
#ifdef __STDC__
  (struct in_addr *laddr, struct in_addr *faddr, struct timeval *timeout)
#else
  (laddr, faddr, timeout)
struct in_addr *laddr;
struct in_addr *faddr;
struct timeval *timeout;
#endif
{
  ident_t *id;
  int res, tmperrno;
  struct sockaddr_in sin_laddr, sin_faddr;
  fd_set rs, ws, es;
#if !defined(SO_DONTLINGER) || defined(SOLARIS)
  struct linger linger;
#endif

#if defined(sgi) || defined (SOLARIS) || defined(__bsdi__)
    int on = 1;
#endif    

  if ((id = (ident_t *) malloc(sizeof(*id))) == 0)
    return 0;

  if ((id->fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  {
    free(id);
    return 0;
  }

  if (timeout)
  {
    if ((res = fcntl(id->fd, F_GETFL, 0)) < 0)
      goto ERROR;
  
    if (fcntl(id->fd, F_SETFL, res | FNDELAY) < 0)
      goto ERROR;
  }

#if defined(SO_DONTLINGER) && !defined(SOLARIS)
  if (setsockopt(id->fd, SOL_SOCKET, SO_DONTLINGER, 0, 0) < 0)
    goto ERROR;
#else
  linger.l_onoff = 0;
  linger.l_linger = 0;
  
  if (setsockopt(id->fd, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger)) < 0)
      goto ERROR;
#endif
  
#if defined(sgi) || defined(SOLARIS) || defined(__bsdi__)
    if (setsockopt(id->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
            sizeof(on)) < 0)
#else 
# if !defined(LINUX)
  if (setsockopt(id->fd, SOL_SOCKET, SO_REUSEADDR, 0, 0) < 0)
# endif
#endif
    goto ERROR;

  id->buf[0] = '\0';

  memset(&sin_laddr, 0, sizeof(sin_laddr));
  sin_laddr.sin_family = AF_INET;
  sin_laddr.sin_addr = *laddr;
  sin_laddr.sin_port = 0;

  if (bind(id->fd, (struct sockaddr *)&sin_laddr, sizeof(sin_laddr)) < 0)
    goto ERROR;
  
  memset(&sin_faddr, 0, sizeof(sin_faddr));
  sin_faddr.sin_family = AF_INET;
  sin_faddr.sin_addr = *faddr;
  sin_faddr.sin_port = htons(IDPORT);
  
  res = connect(id->fd, (struct sockaddr *)&sin_faddr, sizeof(sin_faddr));
  if (res < 0 && errno != EINPROGRESS)
    goto ERROR;

  if (timeout)
  {
    FD_ZERO(&rs);
    FD_ZERO(&ws);
    FD_ZERO(&es);
    FD_SET(id->fd, &rs);
    FD_SET(id->fd, &ws);
    FD_SET(id->fd, &es);

    if ((res = select(FD_SETSIZE, &rs, &ws, &es, timeout)) < 0)
      goto ERROR;

    if (res == 0)
    {
      errno = ETIMEDOUT;
      goto ERROR;
    }

    if (FD_ISSET(id->fd, &es))
      goto ERROR;

    if (!FD_ISSET(id->fd, &rs) && !FD_ISSET(id->fd, &ws))
      goto ERROR;
  }

  return id;

ERROR:
  tmperrno = errno;		/* Save, so close() won't erase it */
  close(id->fd);
  free(id);
  errno = tmperrno;
  return 0;
}