~ubuntu-branches/ubuntu/warty/ncbi-tools6/warty

« back to all changes in this revision

Viewing changes to network/socks/socks.cstc.4.2/libident/id_parse.c

  • Committer: Bazaar Package Importer
  • Author(s): Aaron M. Ucko
  • Date: 2002-04-04 22:13:09 UTC
  • Revision ID: james.westby@ubuntu.com-20020404221309-vfze028rfnlrldct
Tags: upstream-6.1.20011220a
Import upstream version 6.1.20011220a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
** id_parse.c                    Receive and parse a reply from an IDENT server
 
3
**
 
4
** Author: Peter Eriksson <pen@lysator.liu.se>
 
5
** Fiddling: P�r Emanuelsson <pell@lysator.liu.se>
 
6
*/
 
7
 
 
8
#include <stdio.h>
 
9
#include <errno.h>
 
10
#include <ctype.h>
 
11
#include <string.h>
 
12
 
 
13
#include <sys/types.h>
 
14
#include <sys/wait.h>
 
15
#include <sys/time.h>
 
16
 
 
17
#include "ident.h"
 
18
 
 
19
/* >>> Ian Dunkin  */
 
20
#if defined(ultrix) || defined(AIX_PS2)
 
21
char *strdup(s)
 
22
char *s;
 
23
{
 
24
        char *malloc();
 
25
        static char *p;
 
26
 
 
27
        if ((p = malloc(strlen(s) + 1)) != NULL)
 
28
                strcpy(p, s);
 
29
        return(p);
 
30
}
 
31
#endif
 
32
/* <<< Ian Dunkin */
 
33
 
 
34
static char *xstrtok
 
35
#ifdef __STDC__
 
36
  (char *cp, char *cs, char *dc)
 
37
#else
 
38
  (cp, cs, dc)
 
39
char *cp;
 
40
char *cs;
 
41
char *dc;
 
42
#endif
 
43
{
 
44
  static char *bp = 0;
 
45
 
 
46
  if (cp)
 
47
    bp = cp;
 
48
 
 
49
  /*
 
50
  ** No delimitor cs - return whole buffer and point at end
 
51
  */
 
52
  if (!cs)
 
53
  {
 
54
    while (*bp)
 
55
      bp++;
 
56
    return cs;
 
57
  }
 
58
  
 
59
  /*
 
60
  ** Skip leading spaces
 
61
  */
 
62
  while (isspace(*bp))
 
63
    bp++;
 
64
 
 
65
  /*
 
66
  ** No token found?
 
67
  */
 
68
  if (!*bp)
 
69
    return 0;
 
70
 
 
71
  cp = bp;
 
72
  while (*bp && !index(cs, *bp))
 
73
    bp++;
 
74
 
 
75
  /*
 
76
  ** Remove trailing spaces
 
77
  */
 
78
  *dc = *bp;
 
79
  for (dc = bp-1; dc > cp && isspace(*dc); dc--)
 
80
    ;
 
81
  *++dc = '\0';
 
82
  
 
83
  bp++;
 
84
 
 
85
  return cp;
 
86
}
 
87
 
 
88
 
 
89
int id_parse
 
90
#ifdef __STDC__
 
91
  (ident_t *id,
 
92
   struct timeval *timeout,
 
93
   int *lport,
 
94
   int *fport,
 
95
   char **identifier,
 
96
   char **opsys,
 
97
   char **charset)
 
98
#else
 
99
  (id, timeout, lport, fport, identifier, opsys, charset)
 
100
ident_t *id;
 
101
struct timeval *timeout;
 
102
int *lport;
 
103
int *fport;
 
104
char **identifier;
 
105
char **opsys;
 
106
char **charset;
 
107
#endif
 
108
{
 
109
  char c, *cp, *tmp_charset;
 
110
  fd_set rs;
 
111
  int pos, res, lp, fp;
 
112
 
 
113
  errno = 0;
 
114
  
 
115
  tmp_charset = 0;
 
116
 
 
117
  if (!id) return -1;
 
118
  if (lport) *lport = 0;
 
119
  if (fport) *fport = 0;
 
120
  if (identifier) *identifier = 0;
 
121
  if (opsys) *opsys = 0;
 
122
  if (charset) *charset = 0;
 
123
  
 
124
  pos = strlen(id->buf);
 
125
 
 
126
  if (timeout)
 
127
  {
 
128
    FD_ZERO(&rs);
 
129
    FD_SET(id->fd, &rs);
 
130
 
 
131
    if ((res = select(FD_SETSIZE, &rs, (fd_set *)0, (fd_set *)0, timeout)) < 0)
 
132
      return -1;
 
133
 
 
134
    if (res == 0)
 
135
    {
 
136
      errno = ETIMEDOUT;
 
137
      return -1;
 
138
    }
 
139
  }
 
140
 
 
141
  while (pos < sizeof(id->buf) &&
 
142
         (res = read(id->fd, id->buf + pos, 1)) == 1 &&
 
143
         id->buf[pos] != '\n')
 
144
    pos++;
 
145
 
 
146
  if (res < 0)
 
147
    return -1;
 
148
 
 
149
  if (res == 0)
 
150
  {
 
151
    errno = ENOTCONN;
 
152
    return -1;
 
153
  }
 
154
  
 
155
  if (id->buf[pos] != '\n')
 
156
    return 0;
 
157
 
 
158
  id->buf[pos++] = '\0';
 
159
 
 
160
  /*
 
161
  ** Get first field (<lport> , <fport>)
 
162
  */
 
163
  cp = xstrtok(id->buf, ":", &c);
 
164
  if (!cp)
 
165
    return -2;
 
166
 
 
167
  if (sscanf(cp, " %d , %d", &lp, &fp) != 2)
 
168
  {
 
169
    if (identifier) *identifier = strdup(cp);
 
170
    return -2;
 
171
  }
 
172
 
 
173
  if (lport) *lport = lp;
 
174
  if (fport) *fport = fp;
 
175
 
 
176
  /*
 
177
  ** Get second field (USERID or ERROR)
 
178
  */
 
179
  cp = xstrtok((char *)0, ":", &c);
 
180
  if (!cp)
 
181
    return -2;
 
182
 
 
183
  if (strcmp(cp, "ERROR") == 0)
 
184
  {
 
185
    cp = xstrtok((char *)0, "\n\r", &c);
 
186
    if (!cp)
 
187
      return -2;
 
188
 
 
189
    if (identifier) *identifier = strdup(cp);
 
190
    
 
191
    return 2;
 
192
  }
 
193
  else if (strcmp(cp, "USERID") == 0)
 
194
  {
 
195
    /*
 
196
    ** Get first subfield of third field <opsys>
 
197
    */
 
198
    cp = xstrtok((char *)0, ",:", &c);
 
199
    if (!cp)
 
200
      return -2;
 
201
 
 
202
    if (opsys) *opsys = strdup(cp);
 
203
 
 
204
    /*
 
205
    ** We have a second subfield (<charset>)
 
206
    */
 
207
    if (c == ',')
 
208
    {
 
209
      cp = xstrtok((char *)0, ":", &c);
 
210
      if (!cp)
 
211
        return -2;
 
212
 
 
213
      tmp_charset = cp;
 
214
      if (charset) *charset = strdup(cp);
 
215
 
 
216
      /*
 
217
      ** We have even more subfields - ignore them
 
218
      */
 
219
      if (c == ',')
 
220
        xstrtok((char *)0, ":", &c);
 
221
    }
 
222
 
 
223
    if (tmp_charset && strcmp(tmp_charset, "OCTET") == 0)
 
224
      cp = xstrtok((char *)0, (char *)0, &c);
 
225
    else
 
226
      cp = xstrtok((char *)0, "\n\r", &c);
 
227
 
 
228
    if (identifier) *identifier = strdup(cp);
 
229
    return 1;
 
230
  }
 
231
  else
 
232
  {
 
233
    if (identifier) *identifier = strdup(cp);
 
234
    return -3;
 
235
  }
 
236
}