3 * $Id: ident.c,v 1.1 1999/10/04 21:41:58 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.1 1999/10/04 21:41:58 mdw
34 * Added ident client from `fw'.
38 /*----- Header files ------------------------------------------------------*/
45 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
59 /*----- Main code ---------------------------------------------------------*/
63 * Arguments: @char **pp@ = address of string pointer
65 * Returns: Address of next token.
67 * Use: Reads the next token from the result string. Tokens are
68 * terminated by whitespace, or `:' or `,' characters. The
69 * result string has had `\' escapes removed; it's stored in
70 * the same memory as the input string. The actual content
71 * of the delimiter doesn't seem to be interesting, so it
75 static char *next(char **pp
)
79 /* --- Deal with reads past the end of the string --- */
84 /* --- Initialize various pointers into the string --- */
88 /* --- Skip past any leading whitespace --- */
90 while (isspace((unsigned char)*p
))
93 /* --- Now start work on the string itself --- */
96 if (*p
== 0 || *p
== ':' || *p
== ',' || isspace((unsigned char)*p
))
98 else if (*p
== '\\') {
108 /* --- Tidy up afterwards --- */
110 while (isspace((unsigned char)*p
))
114 else if (*p
== ':' || *p
== ',')
125 * Arguments: @char *p@ = pointer to input string from identd
126 * @ident_reply *i@ = pointer to output block
130 * Use: Parses a result string from an RFC931 (identd) server.
133 static void parse(char *p
, ident_reply
*i
)
137 /* --- Read the source and destination port numbers --- */
139 i
->sport
= atoi(next(&p
));
140 i
->dport
= atoi(next(&p
));
142 /* --- Find out what sort of a reply this is --- */
145 if (strcmp(q
, "USERID") == 0) {
146 i
->type
= IDENT_USERID
;
147 i
->u
.userid
.os
= next(&p
);
148 i
->u
.userid
.user
= next(&p
);
149 } else if (strcmp(q
, "ERROR") == 0) {
150 i
->type
= IDENT_ERROR
;
151 i
->u
.error
= next(&p
);
158 * Arguments: @char *s@ = pointer to string from ident server
159 * @void *p@ = pointer to my request block
163 * Use: Handles a string from an ident server.
166 static void line(char *s
, void *p
)
168 ident_request
*rq
= p
;
170 rq
->state
= IDENT_DONE
;
171 selbuf_disable(&rq
->b
);
172 close(rq
->b
.reader
.fd
);
182 /* --- @connected@ --- *
184 * Arguments: @int fd@ = file descriptor
185 * @void *p@ = pointer to request block
189 * Use: Handles a connection to a remote ident server.
192 static void connected(int fd
, void *p
)
194 ident_request
*rq
= p
;
197 /* --- Handle an error during the connect --- */
202 /* --- Initialize the string to send to the remote host --- */
205 dstr_putf(&d
, "%u, %u\r\n",
206 ntohs(rq
->remote
.sin_port
), ntohs(rq
->local
.sin_port
));
207 } CATCH
switch (exc_type
) {
215 /* --- Do the rest of the work --- */
217 if (write(fd
, d
.buf
, d
.len
) < d
.len
)
220 rq
->state
= IDENT_READ
;
221 selbuf_init(&rq
->b
, rq
->s
, fd
, line
, rq
);
224 /* --- Tidy up after misfortunes --- */
230 rq
->state
= IDENT_DONE
;
234 /* --- @ident_abort@ --- *
236 * Arguments: @ident_request *rq@ = pointer to request block
240 * Use: Cancels an ident request in progress.
243 void ident_abort(ident_request
*rq
)
250 selbuf_disable(&rq
->b
);
251 close(rq
->b
.reader
.fd
);
258 * Arguments: @ident_request *rq@ = pointer to request block
262 * Use: Starts a connection to the remote ident server.
265 static void go(ident_request
*rq
)
268 struct sockaddr_in sin
;
271 /* --- Create the socket I'll use --- */
273 if ((fd
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0)
275 memset(&sin
, 0, sizeof(sin
));
276 sin
.sin_family
= AF_INET
;
278 sin
.sin_addr
= rq
->local
.sin_addr
;
279 if (bind(fd
, (struct sockaddr
*)&sin
, sizeof(sin
)))
282 /* --- Out-of-band data would confuse us --- */
285 setsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(opt
));
287 /* --- Start a connection to the remote server --- */
289 sin
.sin_family
= AF_INET
;
290 sin
.sin_port
= htons(113);
291 sin
.sin_addr
= rq
->remote
.sin_addr
;
292 conn_init(&rq
->c
, rq
->s
, fd
, (struct sockaddr
*)&sin
, sizeof(sin
),
295 /* --- Finish off initializing the block --- */
297 rq
->state
= IDENT_CONN
;
300 /* --- Tidy up after lossage --- */
305 rq
->state
= IDENT_DONE
;
311 * Arguments: @ident_request *rq@ = pointer to request block
312 * @sel_state *s@ = I/O multiplexor
313 * @const struct sockaddr_in *local, *remote@ = addresses
314 * @void (*func)(ident_reply *i, void *p)@ = handler function
315 * @void *p@ = argument for handler
319 * Use: Initializes an ident request.
322 void ident(ident_request
*rq
, sel_state
*s
,
323 const struct sockaddr_in
*local
,
324 const struct sockaddr_in
*remote
,
325 void (*func
)(ident_reply */
*i*/
, void */
*p*/
),
328 memcpy(&rq
->local
, local
, sizeof(rq
->local
));
329 memcpy(&rq
->remote
, remote
, sizeof(rq
->remote
));
336 /* --- @ident_socket@ --- *
338 * Arguments: @ident_request *rq@ = pointer to request block
339 * @sel_state *s@ = I/O multiplexor
340 * @int sk@ = connected socket file descriptor
341 * @void (*func)(ident_reply *i, void *p)@ = handler function
342 * @void *p@ = argument for handler
346 * Use: An alternative interface to @ident@. Initializes an ident
347 * request from a connected socket, rather than from an explicit
348 * address. This will call @getsockname@ and @getpeername@ to
349 * find out what the socket is actually connected to, which adds
350 * convenience but wastes time.
353 void ident_socket(ident_request
*rq
, sel_state
*s
, int sk
,
354 void (*func
)(ident_reply */
*i*/
, void */
*p*/
),
358 if ((sinsz
= sizeof(struct sockaddr_in
),
359 getsockname(sk
, (struct sockaddr
*)&rq
->local
, &sinsz
)) ||
360 (sinsz
= sizeof(struct sockaddr_in
),
361 getpeername(sk
, (struct sockaddr
*)&rq
->remote
, &sinsz
))) {
362 rq
->state
= IDENT_DONE
;
373 /*----- That's all, folks -------------------------------------------------*/