3 * $Id: ident.c,v 1.3 2000/10/08 11:17:45 mdw Exp $
5 * Nonblocking RFC931 client
7 * (c) 1999 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.3 2000/10/08 11:17:45 mdw
34 * (ident_socket): change sizes to be @size_t@.
36 * Revision 1.2 2000/06/17 10:38:35 mdw
37 * Track changes to selbuf interface.
39 * Revision 1.1 1999/10/04 21:41:58 mdw
40 * Added ident client from `fw'.
44 /*----- Header files ------------------------------------------------------*/
51 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
65 /*----- Main code ---------------------------------------------------------*/
69 * Arguments: @char **pp@ = address of string pointer
71 * Returns: Address of next token.
73 * Use: Reads the next token from the result string. Tokens are
74 * terminated by whitespace, or `:' or `,' characters. The
75 * result string has had `\' escapes removed; it's stored in
76 * the same memory as the input string. The actual content
77 * of the delimiter doesn't seem to be interesting, so it
81 static char *next(char **pp
)
85 /* --- Deal with reads past the end of the string --- */
90 /* --- Initialize various pointers into the string --- */
94 /* --- Skip past any leading whitespace --- */
96 while (isspace((unsigned char)*p
))
99 /* --- Now start work on the string itself --- */
102 if (*p
== 0 || *p
== ':' || *p
== ',' || isspace((unsigned char)*p
))
104 else if (*p
== '\\') {
114 /* --- Tidy up afterwards --- */
116 while (isspace((unsigned char)*p
))
120 else if (*p
== ':' || *p
== ',')
131 * Arguments: @char *p@ = pointer to input string from identd
132 * @ident_reply *i@ = pointer to output block
136 * Use: Parses a result string from an RFC931 (identd) server.
139 static void parse(char *p
, ident_reply
*i
)
143 /* --- Read the source and destination port numbers --- */
145 i
->sport
= atoi(next(&p
));
146 i
->dport
= atoi(next(&p
));
148 /* --- Find out what sort of a reply this is --- */
151 if (strcmp(q
, "USERID") == 0) {
152 i
->type
= IDENT_USERID
;
153 i
->u
.userid
.os
= next(&p
);
154 i
->u
.userid
.user
= next(&p
);
155 } else if (strcmp(q
, "ERROR") == 0) {
156 i
->type
= IDENT_ERROR
;
157 i
->u
.error
= next(&p
);
164 * Arguments: @char *s@ = pointer to string from ident server
165 * @void *p@ = pointer to my request block
169 * Use: Handles a string from an ident server.
172 static void line(char *s
, void *p
)
174 ident_request
*rq
= p
;
176 rq
->state
= IDENT_DONE
;
177 close(rq
->b
.reader
.fd
);
185 selbuf_destroy(&rq
->b
);
188 /* --- @connected@ --- *
190 * Arguments: @int fd@ = file descriptor
191 * @void *p@ = pointer to request block
195 * Use: Handles a connection to a remote ident server.
198 static void connected(int fd
, void *p
)
200 ident_request
*rq
= p
;
203 /* --- Handle an error during the connect --- */
208 /* --- Initialize the string to send to the remote host --- */
211 dstr_putf(&d
, "%u, %u\r\n",
212 ntohs(rq
->remote
.sin_port
), ntohs(rq
->local
.sin_port
));
213 } CATCH
switch (exc_type
) {
221 /* --- Do the rest of the work --- */
223 if (write(fd
, d
.buf
, d
.len
) < d
.len
)
226 rq
->state
= IDENT_READ
;
227 selbuf_init(&rq
->b
, rq
->s
, fd
, line
, rq
);
230 /* --- Tidy up after misfortunes --- */
236 rq
->state
= IDENT_DONE
;
240 /* --- @ident_abort@ --- *
242 * Arguments: @ident_request *rq@ = pointer to request block
246 * Use: Cancels an ident request in progress.
249 void ident_abort(ident_request
*rq
)
256 close(rq
->b
.reader
.fd
);
257 selbuf_destroy(&rq
->b
);
264 * Arguments: @ident_request *rq@ = pointer to request block
268 * Use: Starts a connection to the remote ident server.
271 static void go(ident_request
*rq
)
274 struct sockaddr_in sin
;
277 /* --- Create the socket I'll use --- */
279 if ((fd
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0)
281 memset(&sin
, 0, sizeof(sin
));
282 sin
.sin_family
= AF_INET
;
284 sin
.sin_addr
= rq
->local
.sin_addr
;
285 if (bind(fd
, (struct sockaddr
*)&sin
, sizeof(sin
)))
288 /* --- Out-of-band data would confuse us --- */
291 setsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(opt
));
293 /* --- Start a connection to the remote server --- */
295 sin
.sin_family
= AF_INET
;
296 sin
.sin_port
= htons(113);
297 sin
.sin_addr
= rq
->remote
.sin_addr
;
298 conn_init(&rq
->c
, rq
->s
, fd
, (struct sockaddr
*)&sin
, sizeof(sin
),
301 /* --- Finish off initializing the block --- */
303 rq
->state
= IDENT_CONN
;
306 /* --- Tidy up after lossage --- */
311 rq
->state
= IDENT_DONE
;
317 * Arguments: @ident_request *rq@ = pointer to request block
318 * @sel_state *s@ = I/O multiplexor
319 * @const struct sockaddr_in *local, *remote@ = addresses
320 * @void (*func)(ident_reply *i, void *p)@ = handler function
321 * @void *p@ = argument for handler
325 * Use: Initializes an ident request.
328 void ident(ident_request
*rq
, sel_state
*s
,
329 const struct sockaddr_in
*local
,
330 const struct sockaddr_in
*remote
,
331 void (*func
)(ident_reply */
*i*/
, void */
*p*/
),
334 memcpy(&rq
->local
, local
, sizeof(rq
->local
));
335 memcpy(&rq
->remote
, remote
, sizeof(rq
->remote
));
342 /* --- @ident_socket@ --- *
344 * Arguments: @ident_request *rq@ = pointer to request block
345 * @sel_state *s@ = I/O multiplexor
346 * @int sk@ = connected socket file descriptor
347 * @void (*func)(ident_reply *i, void *p)@ = handler function
348 * @void *p@ = argument for handler
352 * Use: An alternative interface to @ident@. Initializes an ident
353 * request from a connected socket, rather than from an explicit
354 * address. This will call @getsockname@ and @getpeername@ to
355 * find out what the socket is actually connected to, which adds
356 * convenience but wastes time.
359 void ident_socket(ident_request
*rq
, sel_state
*s
, int sk
,
360 void (*func
)(ident_reply */
*i*/
, void */
*p*/
),
364 if ((sinsz
= sizeof(struct sockaddr_in
),
365 getsockname(sk
, (struct sockaddr
*)&rq
->local
, &sinsz
)) ||
366 (sinsz
= sizeof(struct sockaddr_in
),
367 getpeername(sk
, (struct sockaddr
*)&rq
->remote
, &sinsz
))) {
368 rq
->state
= IDENT_DONE
;
379 /*----- That's all, folks -------------------------------------------------*/