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>
50 /*----- Main code ---------------------------------------------------------*/
54 * Arguments: @char **pp@ = address of string pointer
56 * Returns: Address of next token.
58 * Use: Reads the next token from the result string. Tokens are
59 * terminated by whitespace, or `:' or `,' characters. The
60 * result string has had `\' escapes removed; it's stored in
61 * the same memory as the input string. The actual content
62 * of the delimiter doesn't seem to be interesting, so it
66 static char *next(char **pp)
70 /* --- Deal with reads past the end of the string --- */
75 /* --- Initialize various pointers into the string --- */
79 /* --- Skip past any leading whitespace --- */
84 /* --- Now start work on the string itself --- */
87 if (*p == 0 || *p == ':' || *p == ',' || ISSPACE(*p))
89 else if (*p == '\\') {
99 /* --- Tidy up afterwards --- */
105 else if (*p == ':' || *p == ',')
116 * Arguments: @char *p@ = pointer to input string from identd
117 * @ident_reply *i@ = pointer to output block
121 * Use: Parses a result string from an RFC931 (identd) server.
124 static void parse(char *p, ident_reply *i)
128 /* --- Read the source and destination port numbers --- */
130 i->sport = atoi(next(&p));
131 i->dport = atoi(next(&p));
133 /* --- Find out what sort of a reply this is --- */
136 if (STRCMP(q, ==, "USERID")) {
137 i->type = IDENT_USERID;
138 i->u.userid.os = next(&p);
139 i->u.userid.user = next(&p);
140 } else if (STRCMP(q, ==, "ERROR")) {
141 i->type = IDENT_ERROR;
142 i->u.error = next(&p);
149 * Arguments: @char *s@ = pointer to string from ident server
150 * @size_t len@ = length of the line
151 * @void *p@ = pointer to my request block
155 * Use: Handles a string from an ident server.
158 static void line(char *s, size_t len, void *p)
160 ident_request *rq = p;
162 rq->state = IDENT_DONE;
163 close(rq->b.reader.fd);
171 selbuf_destroy(&rq->b);
174 /* --- @connected@ --- *
176 * Arguments: @int fd@ = file descriptor
177 * @void *p@ = pointer to request block
181 * Use: Handles a connection to a remote ident server.
184 static void connected(int fd, void *p)
186 ident_request *rq = p;
189 /* --- Handle an error during the connect --- */
194 /* --- Initialize the string to send to the remote host --- */
197 dstr_putf(&d, "%u, %u\r\n",
198 ntohs(rq->remote.sin_port), ntohs(rq->local.sin_port));
199 } CATCH switch (exc_type) {
207 /* --- Do the rest of the work --- */
209 if (write(fd, d.buf, d.len) < d.len)
212 rq->state = IDENT_READ;
213 selbuf_init(&rq->b, rq->s, fd, line, rq);
216 /* --- Tidy up after misfortunes --- */
222 rq->state = IDENT_DONE;
226 /* --- @ident_abort@ --- *
228 * Arguments: @ident_request *rq@ = pointer to request block
232 * Use: Cancels an ident request in progress.
235 void ident_abort(ident_request *rq)
242 close(rq->b.reader.fd);
243 selbuf_destroy(&rq->b);
250 * Arguments: @ident_request *rq@ = pointer to request block
254 * Use: Starts a connection to the remote ident server.
257 static void go(ident_request *rq)
260 struct sockaddr_in sin;
263 /* --- Create the socket I'll use --- */
265 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
267 memset(&sin, 0, sizeof(sin));
268 sin.sin_family = AF_INET;
270 sin.sin_addr = rq->local.sin_addr;
271 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)))
274 /* --- Out-of-band data would confuse us --- */
277 setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
279 /* --- Start a connection to the remote server --- */
281 sin.sin_family = AF_INET;
282 sin.sin_port = htons(113);
283 sin.sin_addr = rq->remote.sin_addr;
284 if (conn_init(&rq->c, rq->s, fd, (struct sockaddr *)&sin, sizeof(sin),
288 /* --- Finish off initializing the block --- */
290 rq->state = IDENT_CONN;
293 /* --- Tidy up after lossage --- */
298 rq->state = IDENT_DONE;
304 * Arguments: @ident_request *rq@ = pointer to request block
305 * @sel_state *s@ = I/O multiplexor
306 * @const struct sockaddr_in *local, *remote@ = addresses
307 * @void (*func)(ident_reply *i, void *p)@ = handler function
308 * @void *p@ = argument for handler
312 * Use: Initializes an ident request.
315 void ident(ident_request *rq, sel_state *s,
316 const struct sockaddr_in *local,
317 const struct sockaddr_in *remote,
318 void (*func)(ident_reply */*i*/, void */*p*/),
321 memcpy(&rq->local, local, sizeof(rq->local));
322 memcpy(&rq->remote, remote, sizeof(rq->remote));
329 /* --- @ident_socket@ --- *
331 * Arguments: @ident_request *rq@ = pointer to request block
332 * @sel_state *s@ = I/O multiplexor
333 * @int sk@ = connected socket file descriptor
334 * @void (*func)(ident_reply *i, void *p)@ = handler function
335 * @void *p@ = argument for handler
339 * Use: An alternative interface to @ident@. Initializes an ident
340 * request from a connected socket, rather than from an explicit
341 * address. This will call @getsockname@ and @getpeername@ to
342 * find out what the socket is actually connected to, which adds
343 * convenience but wastes time.
346 void ident_socket(ident_request *rq, sel_state *s, int sk,
347 void (*func)(ident_reply */*i*/, void */*p*/),
351 if ((sinsz = sizeof(struct sockaddr_in),
352 getsockname(sk, (struct sockaddr *)&rq->local, &sinsz)) ||
353 (sinsz = sizeof(struct sockaddr_in),
354 getpeername(sk, (struct sockaddr *)&rq->remote, &sinsz))) {
355 rq->state = IDENT_DONE;
366 /*----- That's all, folks -------------------------------------------------*/