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 ------------------------------------------------------*/
33 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
48 /*----- Main code ---------------------------------------------------------*/
52 * Arguments: @char **pp@ = address of string pointer
54 * Returns: Address of next token.
56 * Use: Reads the next token from the result string. Tokens are
57 * terminated by whitespace, or `:' or `,' characters. The
58 * result string has had `\' escapes removed; it's stored in
59 * the same memory as the input string. The actual content
60 * of the delimiter doesn't seem to be interesting, so it
64 static char *next(char **pp)
68 /* --- Deal with reads past the end of the string --- */
73 /* --- Initialize various pointers into the string --- */
77 /* --- Skip past any leading whitespace --- */
82 /* --- Now start work on the string itself --- */
85 if (*p == 0 || *p == ':' || *p == ',' || ISSPACE(*p))
87 else if (*p == '\\') {
97 /* --- Tidy up afterwards --- */
103 else if (*p == ':' || *p == ',')
114 * Arguments: @char *p@ = pointer to input string from identd
115 * @ident_reply *i@ = pointer to output block
119 * Use: Parses a result string from an RFC931 (identd) server.
122 static void parse(char *p, ident_reply *i)
126 /* --- Read the source and destination port numbers --- */
128 i->sport = atoi(next(&p));
129 i->dport = atoi(next(&p));
131 /* --- Find out what sort of a reply this is --- */
134 if (STRCMP(q, ==, "USERID")) {
135 i->type = IDENT_USERID;
136 i->u.userid.os = next(&p);
137 i->u.userid.user = next(&p);
138 } else if (STRCMP(q, ==, "ERROR")) {
139 i->type = IDENT_ERROR;
140 i->u.error = next(&p);
147 * Arguments: @char *s@ = pointer to string from ident server
148 * @size_t len@ = length of the line
149 * @void *p@ = pointer to my request block
153 * Use: Handles a string from an ident server.
156 static void line(char *s, size_t len, void *p)
158 ident_request *rq = p;
160 rq->state = IDENT_DONE;
161 close(rq->b.reader.fd);
169 selbuf_destroy(&rq->b);
172 /* --- @connected@ --- *
174 * Arguments: @int fd@ = file descriptor
175 * @void *p@ = pointer to request block
179 * Use: Handles a connection to a remote ident server.
182 static void connected(int fd, void *p)
184 ident_request *rq = p;
187 /* --- Handle an error during the connect --- */
192 /* --- Initialize the string to send to the remote host --- */
195 dstr_putf(&d, "%u, %u\r\n",
196 ntohs(rq->remote.sin_port), ntohs(rq->local.sin_port));
197 } CATCH switch (exc_type) {
205 /* --- Do the rest of the work --- */
207 if (write(fd, d.buf, d.len) < d.len)
210 rq->state = IDENT_READ;
211 selbuf_init(&rq->b, rq->s, fd, line, rq);
214 /* --- Tidy up after misfortunes --- */
220 rq->state = IDENT_DONE;
224 /* --- @ident_abort@ --- *
226 * Arguments: @ident_request *rq@ = pointer to request block
230 * Use: Cancels an ident request in progress.
233 void ident_abort(ident_request *rq)
240 close(rq->b.reader.fd);
241 selbuf_destroy(&rq->b);
248 * Arguments: @ident_request *rq@ = pointer to request block
252 * Use: Starts a connection to the remote ident server.
255 static void go(ident_request *rq)
258 struct sockaddr_in sin;
261 /* --- Create the socket I'll use --- */
263 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
265 memset(&sin, 0, sizeof(sin));
266 sin.sin_family = AF_INET;
268 sin.sin_addr = rq->local.sin_addr;
269 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)))
272 /* --- Out-of-band data would confuse us --- */
275 setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
277 /* --- Start a connection to the remote server --- */
279 sin.sin_family = AF_INET;
280 sin.sin_port = htons(113);
281 sin.sin_addr = rq->remote.sin_addr;
282 if (conn_init(&rq->c, rq->s, fd, (struct sockaddr *)&sin, sizeof(sin),
286 /* --- Finish off initializing the block --- */
288 rq->state = IDENT_CONN;
291 /* --- Tidy up after lossage --- */
296 rq->state = IDENT_DONE;
302 * Arguments: @ident_request *rq@ = pointer to request block
303 * @sel_state *s@ = I/O multiplexor
304 * @const struct sockaddr_in *local, *remote@ = addresses
305 * @void (*func)(ident_reply *i, void *p)@ = handler function
306 * @void *p@ = argument for handler
310 * Use: Initializes an ident request.
313 void ident(ident_request *rq, sel_state *s,
314 const struct sockaddr_in *local,
315 const struct sockaddr_in *remote,
316 void (*func)(ident_reply */*i*/, void */*p*/),
319 memcpy(&rq->local, local, sizeof(rq->local));
320 memcpy(&rq->remote, remote, sizeof(rq->remote));
327 /* --- @ident_socket@ --- *
329 * Arguments: @ident_request *rq@ = pointer to request block
330 * @sel_state *s@ = I/O multiplexor
331 * @int sk@ = connected socket file descriptor
332 * @void (*func)(ident_reply *i, void *p)@ = handler function
333 * @void *p@ = argument for handler
337 * Use: An alternative interface to @ident@. Initializes an ident
338 * request from a connected socket, rather than from an explicit
339 * address. This will call @getsockname@ and @getpeername@ to
340 * find out what the socket is actually connected to, which adds
341 * convenience but wastes time.
344 void ident_socket(ident_request *rq, sel_state *s, int sk,
345 void (*func)(ident_reply */*i*/, void */*p*/),
349 if ((sinsz = sizeof(struct sockaddr_in),
350 getsockname(sk, (struct sockaddr *)&rq->local, &sinsz)) ||
351 (sinsz = sizeof(struct sockaddr_in),
352 getpeername(sk, (struct sockaddr *)&rq->remote, &sinsz))) {
353 rq->state = IDENT_DONE;
364 /*----- That's all, folks -------------------------------------------------*/