3 * $Id: ident.c,v 1.2 2000/06/17 10:38:35 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.2 2000/06/17 10:38:35 mdw
34 * Track changes to selbuf interface.
36 * Revision 1.1 1999/10/04 21:41:58 mdw
37 * Added ident client from `fw'.
41 /*----- Header files ------------------------------------------------------*/
48 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
62 /*----- Main code ---------------------------------------------------------*/
66 * Arguments: @char **pp@ = address of string pointer
68 * Returns: Address of next token.
70 * Use: Reads the next token from the result string. Tokens are
71 * terminated by whitespace, or `:' or `,' characters. The
72 * result string has had `\' escapes removed; it's stored in
73 * the same memory as the input string. The actual content
74 * of the delimiter doesn't seem to be interesting, so it
78 static char *next(char **pp
)
82 /* --- Deal with reads past the end of the string --- */
87 /* --- Initialize various pointers into the string --- */
91 /* --- Skip past any leading whitespace --- */
93 while (isspace((unsigned char)*p
))
96 /* --- Now start work on the string itself --- */
99 if (*p
== 0 || *p
== ':' || *p
== ',' || isspace((unsigned char)*p
))
101 else if (*p
== '\\') {
111 /* --- Tidy up afterwards --- */
113 while (isspace((unsigned char)*p
))
117 else if (*p
== ':' || *p
== ',')
128 * Arguments: @char *p@ = pointer to input string from identd
129 * @ident_reply *i@ = pointer to output block
133 * Use: Parses a result string from an RFC931 (identd) server.
136 static void parse(char *p
, ident_reply
*i
)
140 /* --- Read the source and destination port numbers --- */
142 i
->sport
= atoi(next(&p
));
143 i
->dport
= atoi(next(&p
));
145 /* --- Find out what sort of a reply this is --- */
148 if (strcmp(q
, "USERID") == 0) {
149 i
->type
= IDENT_USERID
;
150 i
->u
.userid
.os
= next(&p
);
151 i
->u
.userid
.user
= next(&p
);
152 } else if (strcmp(q
, "ERROR") == 0) {
153 i
->type
= IDENT_ERROR
;
154 i
->u
.error
= next(&p
);
161 * Arguments: @char *s@ = pointer to string from ident server
162 * @void *p@ = pointer to my request block
166 * Use: Handles a string from an ident server.
169 static void line(char *s
, void *p
)
171 ident_request
*rq
= p
;
173 rq
->state
= IDENT_DONE
;
174 close(rq
->b
.reader
.fd
);
182 selbuf_destroy(&rq
->b
);
185 /* --- @connected@ --- *
187 * Arguments: @int fd@ = file descriptor
188 * @void *p@ = pointer to request block
192 * Use: Handles a connection to a remote ident server.
195 static void connected(int fd
, void *p
)
197 ident_request
*rq
= p
;
200 /* --- Handle an error during the connect --- */
205 /* --- Initialize the string to send to the remote host --- */
208 dstr_putf(&d
, "%u, %u\r\n",
209 ntohs(rq
->remote
.sin_port
), ntohs(rq
->local
.sin_port
));
210 } CATCH
switch (exc_type
) {
218 /* --- Do the rest of the work --- */
220 if (write(fd
, d
.buf
, d
.len
) < d
.len
)
223 rq
->state
= IDENT_READ
;
224 selbuf_init(&rq
->b
, rq
->s
, fd
, line
, rq
);
227 /* --- Tidy up after misfortunes --- */
233 rq
->state
= IDENT_DONE
;
237 /* --- @ident_abort@ --- *
239 * Arguments: @ident_request *rq@ = pointer to request block
243 * Use: Cancels an ident request in progress.
246 void ident_abort(ident_request
*rq
)
253 close(rq
->b
.reader
.fd
);
254 selbuf_destroy(&rq
->b
);
261 * Arguments: @ident_request *rq@ = pointer to request block
265 * Use: Starts a connection to the remote ident server.
268 static void go(ident_request
*rq
)
271 struct sockaddr_in sin
;
274 /* --- Create the socket I'll use --- */
276 if ((fd
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0)
278 memset(&sin
, 0, sizeof(sin
));
279 sin
.sin_family
= AF_INET
;
281 sin
.sin_addr
= rq
->local
.sin_addr
;
282 if (bind(fd
, (struct sockaddr
*)&sin
, sizeof(sin
)))
285 /* --- Out-of-band data would confuse us --- */
288 setsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(opt
));
290 /* --- Start a connection to the remote server --- */
292 sin
.sin_family
= AF_INET
;
293 sin
.sin_port
= htons(113);
294 sin
.sin_addr
= rq
->remote
.sin_addr
;
295 conn_init(&rq
->c
, rq
->s
, fd
, (struct sockaddr
*)&sin
, sizeof(sin
),
298 /* --- Finish off initializing the block --- */
300 rq
->state
= IDENT_CONN
;
303 /* --- Tidy up after lossage --- */
308 rq
->state
= IDENT_DONE
;
314 * Arguments: @ident_request *rq@ = pointer to request block
315 * @sel_state *s@ = I/O multiplexor
316 * @const struct sockaddr_in *local, *remote@ = addresses
317 * @void (*func)(ident_reply *i, void *p)@ = handler function
318 * @void *p@ = argument for handler
322 * Use: Initializes an ident request.
325 void ident(ident_request
*rq
, sel_state
*s
,
326 const struct sockaddr_in
*local
,
327 const struct sockaddr_in
*remote
,
328 void (*func
)(ident_reply */
*i*/
, void */
*p*/
),
331 memcpy(&rq
->local
, local
, sizeof(rq
->local
));
332 memcpy(&rq
->remote
, remote
, sizeof(rq
->remote
));
339 /* --- @ident_socket@ --- *
341 * Arguments: @ident_request *rq@ = pointer to request block
342 * @sel_state *s@ = I/O multiplexor
343 * @int sk@ = connected socket file descriptor
344 * @void (*func)(ident_reply *i, void *p)@ = handler function
345 * @void *p@ = argument for handler
349 * Use: An alternative interface to @ident@. Initializes an ident
350 * request from a connected socket, rather than from an explicit
351 * address. This will call @getsockname@ and @getpeername@ to
352 * find out what the socket is actually connected to, which adds
353 * convenience but wastes time.
356 void ident_socket(ident_request
*rq
, sel_state
*s
, int sk
,
357 void (*func
)(ident_reply */
*i*/
, void */
*p*/
),
361 if ((sinsz
= sizeof(struct sockaddr_in
),
362 getsockname(sk
, (struct sockaddr
*)&rq
->local
, &sinsz
)) ||
363 (sinsz
= sizeof(struct sockaddr_in
),
364 getpeername(sk
, (struct sockaddr
*)&rq
->remote
, &sinsz
))) {
365 rq
->state
= IDENT_DONE
;
376 /*----- That's all, folks -------------------------------------------------*/