3 * $Id: ident.c,v 1.4 2001/06/22 19:35:39 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.4 2001/06/22 19:35:39 mdw
34 * New @conn_init@ interface.
36 * Revision 1.3 2000/10/08 11:17:45 mdw
37 * (ident_socket): change sizes to be @size_t@.
39 * Revision 1.2 2000/06/17 10:38:35 mdw
40 * Track changes to selbuf interface.
42 * Revision 1.1 1999/10/04 21:41:58 mdw
43 * Added ident client from `fw'.
47 /*----- Header files ------------------------------------------------------*/
54 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
68 /*----- Main code ---------------------------------------------------------*/
72 * Arguments: @char **pp@ = address of string pointer
74 * Returns: Address of next token.
76 * Use: Reads the next token from the result string. Tokens are
77 * terminated by whitespace, or `:' or `,' characters. The
78 * result string has had `\' escapes removed; it's stored in
79 * the same memory as the input string. The actual content
80 * of the delimiter doesn't seem to be interesting, so it
84 static char *next(char **pp)
88 /* --- Deal with reads past the end of the string --- */
93 /* --- Initialize various pointers into the string --- */
97 /* --- Skip past any leading whitespace --- */
99 while (isspace((unsigned char)*p))
102 /* --- Now start work on the string itself --- */
105 if (*p == 0 || *p == ':' || *p == ',' || isspace((unsigned char)*p))
107 else if (*p == '\\') {
117 /* --- Tidy up afterwards --- */
119 while (isspace((unsigned char)*p))
123 else if (*p == ':' || *p == ',')
134 * Arguments: @char *p@ = pointer to input string from identd
135 * @ident_reply *i@ = pointer to output block
139 * Use: Parses a result string from an RFC931 (identd) server.
142 static void parse(char *p, ident_reply *i)
146 /* --- Read the source and destination port numbers --- */
148 i->sport = atoi(next(&p));
149 i->dport = atoi(next(&p));
151 /* --- Find out what sort of a reply this is --- */
154 if (strcmp(q, "USERID") == 0) {
155 i->type = IDENT_USERID;
156 i->u.userid.os = next(&p);
157 i->u.userid.user = next(&p);
158 } else if (strcmp(q, "ERROR") == 0) {
159 i->type = IDENT_ERROR;
160 i->u.error = next(&p);
167 * Arguments: @char *s@ = pointer to string from ident server
168 * @void *p@ = pointer to my request block
172 * Use: Handles a string from an ident server.
175 static void line(char *s, void *p)
177 ident_request *rq = p;
179 rq->state = IDENT_DONE;
180 close(rq->b.reader.fd);
188 selbuf_destroy(&rq->b);
191 /* --- @connected@ --- *
193 * Arguments: @int fd@ = file descriptor
194 * @void *p@ = pointer to request block
198 * Use: Handles a connection to a remote ident server.
201 static void connected(int fd, void *p)
203 ident_request *rq = p;
206 /* --- Handle an error during the connect --- */
211 /* --- Initialize the string to send to the remote host --- */
214 dstr_putf(&d, "%u, %u\r\n",
215 ntohs(rq->remote.sin_port), ntohs(rq->local.sin_port));
216 } CATCH switch (exc_type) {
224 /* --- Do the rest of the work --- */
226 if (write(fd, d.buf, d.len) < d.len)
229 rq->state = IDENT_READ;
230 selbuf_init(&rq->b, rq->s, fd, line, rq);
233 /* --- Tidy up after misfortunes --- */
239 rq->state = IDENT_DONE;
243 /* --- @ident_abort@ --- *
245 * Arguments: @ident_request *rq@ = pointer to request block
249 * Use: Cancels an ident request in progress.
252 void ident_abort(ident_request *rq)
259 close(rq->b.reader.fd);
260 selbuf_destroy(&rq->b);
267 * Arguments: @ident_request *rq@ = pointer to request block
271 * Use: Starts a connection to the remote ident server.
274 static void go(ident_request *rq)
277 struct sockaddr_in sin;
280 /* --- Create the socket I'll use --- */
282 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
284 memset(&sin, 0, sizeof(sin));
285 sin.sin_family = AF_INET;
287 sin.sin_addr = rq->local.sin_addr;
288 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)))
291 /* --- Out-of-band data would confuse us --- */
294 setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
296 /* --- Start a connection to the remote server --- */
298 sin.sin_family = AF_INET;
299 sin.sin_port = htons(113);
300 sin.sin_addr = rq->remote.sin_addr;
301 if (conn_init(&rq->c, rq->s, fd, (struct sockaddr *)&sin, sizeof(sin),
305 /* --- Finish off initializing the block --- */
307 rq->state = IDENT_CONN;
310 /* --- Tidy up after lossage --- */
315 rq->state = IDENT_DONE;
321 * Arguments: @ident_request *rq@ = pointer to request block
322 * @sel_state *s@ = I/O multiplexor
323 * @const struct sockaddr_in *local, *remote@ = addresses
324 * @void (*func)(ident_reply *i, void *p)@ = handler function
325 * @void *p@ = argument for handler
329 * Use: Initializes an ident request.
332 void ident(ident_request *rq, sel_state *s,
333 const struct sockaddr_in *local,
334 const struct sockaddr_in *remote,
335 void (*func)(ident_reply */*i*/, void */*p*/),
338 memcpy(&rq->local, local, sizeof(rq->local));
339 memcpy(&rq->remote, remote, sizeof(rq->remote));
346 /* --- @ident_socket@ --- *
348 * Arguments: @ident_request *rq@ = pointer to request block
349 * @sel_state *s@ = I/O multiplexor
350 * @int sk@ = connected socket file descriptor
351 * @void (*func)(ident_reply *i, void *p)@ = handler function
352 * @void *p@ = argument for handler
356 * Use: An alternative interface to @ident@. Initializes an ident
357 * request from a connected socket, rather than from an explicit
358 * address. This will call @getsockname@ and @getpeername@ to
359 * find out what the socket is actually connected to, which adds
360 * convenience but wastes time.
363 void ident_socket(ident_request *rq, sel_state *s, int sk,
364 void (*func)(ident_reply */*i*/, void */*p*/),
368 if ((sinsz = sizeof(struct sockaddr_in),
369 getsockname(sk, (struct sockaddr *)&rq->local, &sinsz)) ||
370 (sinsz = sizeof(struct sockaddr_in),
371 getpeername(sk, (struct sockaddr *)&rq->remote, &sinsz))) {
372 rq->state = IDENT_DONE;
383 /*----- That's all, folks -------------------------------------------------*/