3 * $Id: ident.c,v 1.6 2004/04/08 01:36:11 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 /*----- Header files ------------------------------------------------------*/
37 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
51 /*----- Main code ---------------------------------------------------------*/
55 * Arguments: @char **pp@ = address of string pointer
57 * Returns: Address of next token.
59 * Use: Reads the next token from the result string. Tokens are
60 * terminated by whitespace, or `:' or `,' characters. The
61 * result string has had `\' escapes removed; it's stored in
62 * the same memory as the input string. The actual content
63 * of the delimiter doesn't seem to be interesting, so it
67 static char *next(char **pp)
71 /* --- Deal with reads past the end of the string --- */
76 /* --- Initialize various pointers into the string --- */
80 /* --- Skip past any leading whitespace --- */
82 while (isspace((unsigned char)*p))
85 /* --- Now start work on the string itself --- */
88 if (*p == 0 || *p == ':' || *p == ',' || isspace((unsigned char)*p))
90 else if (*p == '\\') {
100 /* --- Tidy up afterwards --- */
102 while (isspace((unsigned char)*p))
106 else if (*p == ':' || *p == ',')
117 * Arguments: @char *p@ = pointer to input string from identd
118 * @ident_reply *i@ = pointer to output block
122 * Use: Parses a result string from an RFC931 (identd) server.
125 static void parse(char *p, ident_reply *i)
129 /* --- Read the source and destination port numbers --- */
131 i->sport = atoi(next(&p));
132 i->dport = atoi(next(&p));
134 /* --- Find out what sort of a reply this is --- */
137 if (strcmp(q, "USERID") == 0) {
138 i->type = IDENT_USERID;
139 i->u.userid.os = next(&p);
140 i->u.userid.user = next(&p);
141 } else if (strcmp(q, "ERROR") == 0) {
142 i->type = IDENT_ERROR;
143 i->u.error = next(&p);
150 * Arguments: @char *s@ = pointer to string from ident server
151 * @size_t len@ = length of the line
152 * @void *p@ = pointer to my request block
156 * Use: Handles a string from an ident server.
159 static void line(char *s, size_t len, void *p)
161 ident_request *rq = p;
163 rq->state = IDENT_DONE;
164 close(rq->b.reader.fd);
172 selbuf_destroy(&rq->b);
175 /* --- @connected@ --- *
177 * Arguments: @int fd@ = file descriptor
178 * @void *p@ = pointer to request block
182 * Use: Handles a connection to a remote ident server.
185 static void connected(int fd, void *p)
187 ident_request *rq = p;
190 /* --- Handle an error during the connect --- */
195 /* --- Initialize the string to send to the remote host --- */
198 dstr_putf(&d, "%u, %u\r\n",
199 ntohs(rq->remote.sin_port), ntohs(rq->local.sin_port));
200 } CATCH switch (exc_type) {
208 /* --- Do the rest of the work --- */
210 if (write(fd, d.buf, d.len) < d.len)
213 rq->state = IDENT_READ;
214 selbuf_init(&rq->b, rq->s, fd, line, rq);
217 /* --- Tidy up after misfortunes --- */
223 rq->state = IDENT_DONE;
227 /* --- @ident_abort@ --- *
229 * Arguments: @ident_request *rq@ = pointer to request block
233 * Use: Cancels an ident request in progress.
236 void ident_abort(ident_request *rq)
243 close(rq->b.reader.fd);
244 selbuf_destroy(&rq->b);
251 * Arguments: @ident_request *rq@ = pointer to request block
255 * Use: Starts a connection to the remote ident server.
258 static void go(ident_request *rq)
261 struct sockaddr_in sin;
264 /* --- Create the socket I'll use --- */
266 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
268 memset(&sin, 0, sizeof(sin));
269 sin.sin_family = AF_INET;
271 sin.sin_addr = rq->local.sin_addr;
272 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)))
275 /* --- Out-of-band data would confuse us --- */
278 setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
280 /* --- Start a connection to the remote server --- */
282 sin.sin_family = AF_INET;
283 sin.sin_port = htons(113);
284 sin.sin_addr = rq->remote.sin_addr;
285 if (conn_init(&rq->c, rq->s, fd, (struct sockaddr *)&sin, sizeof(sin),
289 /* --- Finish off initializing the block --- */
291 rq->state = IDENT_CONN;
294 /* --- Tidy up after lossage --- */
299 rq->state = IDENT_DONE;
305 * Arguments: @ident_request *rq@ = pointer to request block
306 * @sel_state *s@ = I/O multiplexor
307 * @const struct sockaddr_in *local, *remote@ = addresses
308 * @void (*func)(ident_reply *i, void *p)@ = handler function
309 * @void *p@ = argument for handler
313 * Use: Initializes an ident request.
316 void ident(ident_request *rq, sel_state *s,
317 const struct sockaddr_in *local,
318 const struct sockaddr_in *remote,
319 void (*func)(ident_reply */*i*/, void */*p*/),
322 memcpy(&rq->local, local, sizeof(rq->local));
323 memcpy(&rq->remote, remote, sizeof(rq->remote));
330 /* --- @ident_socket@ --- *
332 * Arguments: @ident_request *rq@ = pointer to request block
333 * @sel_state *s@ = I/O multiplexor
334 * @int sk@ = connected socket file descriptor
335 * @void (*func)(ident_reply *i, void *p)@ = handler function
336 * @void *p@ = argument for handler
340 * Use: An alternative interface to @ident@. Initializes an ident
341 * request from a connected socket, rather than from an explicit
342 * address. This will call @getsockname@ and @getpeername@ to
343 * find out what the socket is actually connected to, which adds
344 * convenience but wastes time.
347 void ident_socket(ident_request *rq, sel_state *s, int sk,
348 void (*func)(ident_reply */*i*/, void */*p*/),
352 if ((sinsz = sizeof(struct sockaddr_in),
353 getsockname(sk, (struct sockaddr *)&rq->local, &sinsz)) ||
354 (sinsz = sizeof(struct sockaddr_in),
355 getpeername(sk, (struct sockaddr *)&rq->remote, &sinsz))) {
356 rq->state = IDENT_DONE;
367 /*----- That's all, folks -------------------------------------------------*/