3 * $Id: ident.c,v 1.5 2002/01/13 13:33:15 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.5 2002/01/13 13:33:15 mdw
34 * Track interface change for @lbuf@.
36 * Revision 1.4 2001/06/22 19:35:39 mdw
37 * New @conn_init@ interface.
39 * Revision 1.3 2000/10/08 11:17:45 mdw
40 * (ident_socket): change sizes to be @size_t@.
42 * Revision 1.2 2000/06/17 10:38:35 mdw
43 * Track changes to selbuf interface.
45 * Revision 1.1 1999/10/04 21:41:58 mdw
46 * Added ident client from `fw'.
50 /*----- Header files ------------------------------------------------------*/
57 #include <sys/types.h>
60 #include <sys/socket.h>
61 #include <netinet/in.h>
62 #include <arpa/inet.h>
71 /*----- Main code ---------------------------------------------------------*/
75 * Arguments: @char **pp@ = address of string pointer
77 * Returns: Address of next token.
79 * Use: Reads the next token from the result string. Tokens are
80 * terminated by whitespace, or `:' or `,' characters. The
81 * result string has had `\' escapes removed; it's stored in
82 * the same memory as the input string. The actual content
83 * of the delimiter doesn't seem to be interesting, so it
87 static char *next(char **pp)
91 /* --- Deal with reads past the end of the string --- */
96 /* --- Initialize various pointers into the string --- */
100 /* --- Skip past any leading whitespace --- */
102 while (isspace((unsigned char)*p))
105 /* --- Now start work on the string itself --- */
108 if (*p == 0 || *p == ':' || *p == ',' || isspace((unsigned char)*p))
110 else if (*p == '\\') {
120 /* --- Tidy up afterwards --- */
122 while (isspace((unsigned char)*p))
126 else if (*p == ':' || *p == ',')
137 * Arguments: @char *p@ = pointer to input string from identd
138 * @ident_reply *i@ = pointer to output block
142 * Use: Parses a result string from an RFC931 (identd) server.
145 static void parse(char *p, ident_reply *i)
149 /* --- Read the source and destination port numbers --- */
151 i->sport = atoi(next(&p));
152 i->dport = atoi(next(&p));
154 /* --- Find out what sort of a reply this is --- */
157 if (strcmp(q, "USERID") == 0) {
158 i->type = IDENT_USERID;
159 i->u.userid.os = next(&p);
160 i->u.userid.user = next(&p);
161 } else if (strcmp(q, "ERROR") == 0) {
162 i->type = IDENT_ERROR;
163 i->u.error = next(&p);
170 * Arguments: @char *s@ = pointer to string from ident server
171 * @size_t len@ = length of the line
172 * @void *p@ = pointer to my request block
176 * Use: Handles a string from an ident server.
179 static void line(char *s, size_t len, void *p)
181 ident_request *rq = p;
183 rq->state = IDENT_DONE;
184 close(rq->b.reader.fd);
192 selbuf_destroy(&rq->b);
195 /* --- @connected@ --- *
197 * Arguments: @int fd@ = file descriptor
198 * @void *p@ = pointer to request block
202 * Use: Handles a connection to a remote ident server.
205 static void connected(int fd, void *p)
207 ident_request *rq = p;
210 /* --- Handle an error during the connect --- */
215 /* --- Initialize the string to send to the remote host --- */
218 dstr_putf(&d, "%u, %u\r\n",
219 ntohs(rq->remote.sin_port), ntohs(rq->local.sin_port));
220 } CATCH switch (exc_type) {
228 /* --- Do the rest of the work --- */
230 if (write(fd, d.buf, d.len) < d.len)
233 rq->state = IDENT_READ;
234 selbuf_init(&rq->b, rq->s, fd, line, rq);
237 /* --- Tidy up after misfortunes --- */
243 rq->state = IDENT_DONE;
247 /* --- @ident_abort@ --- *
249 * Arguments: @ident_request *rq@ = pointer to request block
253 * Use: Cancels an ident request in progress.
256 void ident_abort(ident_request *rq)
263 close(rq->b.reader.fd);
264 selbuf_destroy(&rq->b);
271 * Arguments: @ident_request *rq@ = pointer to request block
275 * Use: Starts a connection to the remote ident server.
278 static void go(ident_request *rq)
281 struct sockaddr_in sin;
284 /* --- Create the socket I'll use --- */
286 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
288 memset(&sin, 0, sizeof(sin));
289 sin.sin_family = AF_INET;
291 sin.sin_addr = rq->local.sin_addr;
292 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)))
295 /* --- Out-of-band data would confuse us --- */
298 setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
300 /* --- Start a connection to the remote server --- */
302 sin.sin_family = AF_INET;
303 sin.sin_port = htons(113);
304 sin.sin_addr = rq->remote.sin_addr;
305 if (conn_init(&rq->c, rq->s, fd, (struct sockaddr *)&sin, sizeof(sin),
309 /* --- Finish off initializing the block --- */
311 rq->state = IDENT_CONN;
314 /* --- Tidy up after lossage --- */
319 rq->state = IDENT_DONE;
325 * Arguments: @ident_request *rq@ = pointer to request block
326 * @sel_state *s@ = I/O multiplexor
327 * @const struct sockaddr_in *local, *remote@ = addresses
328 * @void (*func)(ident_reply *i, void *p)@ = handler function
329 * @void *p@ = argument for handler
333 * Use: Initializes an ident request.
336 void ident(ident_request *rq, sel_state *s,
337 const struct sockaddr_in *local,
338 const struct sockaddr_in *remote,
339 void (*func)(ident_reply */*i*/, void */*p*/),
342 memcpy(&rq->local, local, sizeof(rq->local));
343 memcpy(&rq->remote, remote, sizeof(rq->remote));
350 /* --- @ident_socket@ --- *
352 * Arguments: @ident_request *rq@ = pointer to request block
353 * @sel_state *s@ = I/O multiplexor
354 * @int sk@ = connected socket file descriptor
355 * @void (*func)(ident_reply *i, void *p)@ = handler function
356 * @void *p@ = argument for handler
360 * Use: An alternative interface to @ident@. Initializes an ident
361 * request from a connected socket, rather than from an explicit
362 * address. This will call @getsockname@ and @getpeername@ to
363 * find out what the socket is actually connected to, which adds
364 * convenience but wastes time.
367 void ident_socket(ident_request *rq, sel_state *s, int sk,
368 void (*func)(ident_reply */*i*/, void */*p*/),
372 if ((sinsz = sizeof(struct sockaddr_in),
373 getsockname(sk, (struct sockaddr *)&rq->local, &sinsz)) ||
374 (sinsz = sizeof(struct sockaddr_in),
375 getpeername(sk, (struct sockaddr *)&rq->remote, &sinsz))) {
376 rq->state = IDENT_DONE;
387 /*----- That's all, folks -------------------------------------------------*/