3 * Nonblocking RFC931 client
5 * (c) 1999 Mark Wooding
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
35 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
49 /*----- Main code ---------------------------------------------------------*/
53 * Arguments: @char **pp@ = address of string pointer
55 * Returns: Address of next token.
57 * Use: Reads the next token from the result string. Tokens are
58 * terminated by whitespace, or `:' or `,' characters. The
59 * result string has had `\' escapes removed; it's stored in
60 * the same memory as the input string. The actual content
61 * of the delimiter doesn't seem to be interesting, so it
65 static char *next(char **pp)
69 /* --- Deal with reads past the end of the string --- */
74 /* --- Initialize various pointers into the string --- */
78 /* --- Skip past any leading whitespace --- */
80 while (isspace((unsigned char)*p))
83 /* --- Now start work on the string itself --- */
86 if (*p == 0 || *p == ':' || *p == ',' || isspace((unsigned char)*p))
88 else if (*p == '\\') {
98 /* --- Tidy up afterwards --- */
100 while (isspace((unsigned char)*p))
104 else if (*p == ':' || *p == ',')
115 * Arguments: @char *p@ = pointer to input string from identd
116 * @ident_reply *i@ = pointer to output block
120 * Use: Parses a result string from an RFC931 (identd) server.
123 static void parse(char *p, ident_reply *i)
127 /* --- Read the source and destination port numbers --- */
129 i->sport = atoi(next(&p));
130 i->dport = atoi(next(&p));
132 /* --- Find out what sort of a reply this is --- */
135 if (strcmp(q, "USERID") == 0) {
136 i->type = IDENT_USERID;
137 i->u.userid.os = next(&p);
138 i->u.userid.user = next(&p);
139 } else if (strcmp(q, "ERROR") == 0) {
140 i->type = IDENT_ERROR;
141 i->u.error = next(&p);
148 * Arguments: @char *s@ = pointer to string from ident server
149 * @size_t len@ = length of the line
150 * @void *p@ = pointer to my request block
154 * Use: Handles a string from an ident server.
157 static void line(char *s, size_t len, void *p)
159 ident_request *rq = p;
161 rq->state = IDENT_DONE;
162 close(rq->b.reader.fd);
170 selbuf_destroy(&rq->b);
173 /* --- @connected@ --- *
175 * Arguments: @int fd@ = file descriptor
176 * @void *p@ = pointer to request block
180 * Use: Handles a connection to a remote ident server.
183 static void connected(int fd, void *p)
185 ident_request *rq = p;
188 /* --- Handle an error during the connect --- */
193 /* --- Initialize the string to send to the remote host --- */
196 dstr_putf(&d, "%u, %u\r\n",
197 ntohs(rq->remote.sin_port), ntohs(rq->local.sin_port));
198 } CATCH switch (exc_type) {
206 /* --- Do the rest of the work --- */
208 if (write(fd, d.buf, d.len) < d.len)
211 rq->state = IDENT_READ;
212 selbuf_init(&rq->b, rq->s, fd, line, rq);
215 /* --- Tidy up after misfortunes --- */
221 rq->state = IDENT_DONE;
225 /* --- @ident_abort@ --- *
227 * Arguments: @ident_request *rq@ = pointer to request block
231 * Use: Cancels an ident request in progress.
234 void ident_abort(ident_request *rq)
241 close(rq->b.reader.fd);
242 selbuf_destroy(&rq->b);
249 * Arguments: @ident_request *rq@ = pointer to request block
253 * Use: Starts a connection to the remote ident server.
256 static void go(ident_request *rq)
259 struct sockaddr_in sin;
262 /* --- Create the socket I'll use --- */
264 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
266 memset(&sin, 0, sizeof(sin));
267 sin.sin_family = AF_INET;
269 sin.sin_addr = rq->local.sin_addr;
270 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)))
273 /* --- Out-of-band data would confuse us --- */
276 setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
278 /* --- Start a connection to the remote server --- */
280 sin.sin_family = AF_INET;
281 sin.sin_port = htons(113);
282 sin.sin_addr = rq->remote.sin_addr;
283 if (conn_init(&rq->c, rq->s, fd, (struct sockaddr *)&sin, sizeof(sin),
287 /* --- Finish off initializing the block --- */
289 rq->state = IDENT_CONN;
292 /* --- Tidy up after lossage --- */
297 rq->state = IDENT_DONE;
303 * Arguments: @ident_request *rq@ = pointer to request block
304 * @sel_state *s@ = I/O multiplexor
305 * @const struct sockaddr_in *local, *remote@ = addresses
306 * @void (*func)(ident_reply *i, void *p)@ = handler function
307 * @void *p@ = argument for handler
311 * Use: Initializes an ident request.
314 void ident(ident_request *rq, sel_state *s,
315 const struct sockaddr_in *local,
316 const struct sockaddr_in *remote,
317 void (*func)(ident_reply */*i*/, void */*p*/),
320 memcpy(&rq->local, local, sizeof(rq->local));
321 memcpy(&rq->remote, remote, sizeof(rq->remote));
328 /* --- @ident_socket@ --- *
330 * Arguments: @ident_request *rq@ = pointer to request block
331 * @sel_state *s@ = I/O multiplexor
332 * @int sk@ = connected socket file descriptor
333 * @void (*func)(ident_reply *i, void *p)@ = handler function
334 * @void *p@ = argument for handler
338 * Use: An alternative interface to @ident@. Initializes an ident
339 * request from a connected socket, rather than from an explicit
340 * address. This will call @getsockname@ and @getpeername@ to
341 * find out what the socket is actually connected to, which adds
342 * convenience but wastes time.
345 void ident_socket(ident_request *rq, sel_state *s, int sk,
346 void (*func)(ident_reply */*i*/, void */*p*/),
350 if ((sinsz = sizeof(struct sockaddr_in),
351 getsockname(sk, (struct sockaddr *)&rq->local, &sinsz)) ||
352 (sinsz = sizeof(struct sockaddr_in),
353 getpeername(sk, (struct sockaddr *)&rq->remote, &sinsz))) {
354 rq->state = IDENT_DONE;
365 /*----- That's all, folks -------------------------------------------------*/