chiark / gitweb /
Initial revision
[fwd] / ident.c
1 /* -*-c-*-
2  *
3  * $Id: ident.c,v 1.1 1999/07/01 08:56:23 mdw Exp $
4  *
5  * Parse replies from RFC931 servers
6  *
7  * (c) 1999 Mark Wooding
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of the `fw' port forwarder.
13  *
14  * `fw' is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  * 
19  * `fw' 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 General Public License for more details.
23  * 
24  * You should have received a copy of the GNU General Public License
25  * along with `fw'; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 /*----- Revision history --------------------------------------------------* 
30  *
31  * $Log: ident.c,v $
32  * Revision 1.1  1999/07/01 08:56:23  mdw
33  * Initial revision
34  *
35  * Revision 1.1.1.1  1999/05/21 22:22:27  mdw
36  * Initial import.  Code not really finished yet.
37  *
38  */
39
40 /*----- Header files ------------------------------------------------------*/
41
42 #include <ctype.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 #include "ident.h"
48
49 /*----- Main code ---------------------------------------------------------*/
50
51 /* --- @next@ --- *
52  *
53  * Arguments:   @char **pp@ = address of string pointer
54  *
55  * Returns:     Address of next token.
56  *
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
62  *              gets thrown away.
63  */
64
65 static char *next(char **pp)
66 {
67   char *p, *q, *r;
68
69   /* --- Deal with reads past the end of the string --- */
70
71   if (!*pp)
72     return ("");
73
74   /* --- Initialize various pointers into the string --- */
75
76   p = q = r = *pp;
77
78   /* --- Skip past any leading whitespace --- */
79
80   while (isspace((unsigned char)*p))
81     p++;
82
83   /* --- Now start work on the string itself --- */
84
85   for (;;) {
86     if (*p == 0 || *p == ':' || *p == ',' || isspace((unsigned char)*p))
87       break;
88     else if (*p == '\\') {
89       p++;
90       if (!*p) {
91         *q++ = '\\';
92         break;
93       }
94     }
95     *q++ = *p++;
96   }
97
98   /* --- Tidy up afterwards --- */
99
100   while (isspace((unsigned char)*p))
101     p++;
102   if (*p == 0)
103     *pp = 0;
104   else if (*p == ':' || *p == ',')
105     *pp = p + 1;
106   else
107     *pp = p;
108   *q = 0;
109
110   return (r);
111 }
112
113 /* --- @ident_parse@ --- *
114  *
115  * Arguments:   @char *p@ = pointer to input string from identd
116  *              @ident *i@ = pointer to output block
117  *
118  * Returns:     ---
119  *
120  * Use:         Parses a result string from an RFC931 (identd) server.
121  */
122
123 void ident_parse(char *p, ident *i)
124 {
125   char *q;
126
127   /* --- Read the source and destination port numbers --- */
128
129   i->sport = atoi(next(&p));
130   i->dport = atoi(next(&p));
131
132   /* --- Find out what sort of a reply this is --- */
133
134   q = next(&p);
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);
142   } else
143     i->type = ident_bad;
144 }
145
146 /*----- That's all, folks -------------------------------------------------*/