Mercurial > masqmail
comparison src/libident/id_parse.c @ 10:26e34ae9a3e3
changed indention and line wrapping to a more consistent style
author | meillo@marmaro.de |
---|---|
date | Mon, 27 Oct 2008 16:23:10 +0100 |
parents | 08114f7dcc23 |
children |
comparison
equal
deleted
inserted
replaced
9:31cc8a89cb74 | 10:26e34ae9a3e3 |
---|---|
30 | 30 |
31 #ifdef _AIX | 31 #ifdef _AIX |
32 # include <sys/select.h> | 32 # include <sys/select.h> |
33 #endif | 33 #endif |
34 #ifdef VMS | 34 #ifdef VMS |
35 # include <sys/socket.h> /* for fd_set */ | 35 # include <sys/socket.h> /* for fd_set */ |
36 #endif | 36 #endif |
37 #define IN_LIBIDENT_SRC | 37 #define IN_LIBIDENT_SRC |
38 #include "ident.h" | 38 #include "ident.h" |
39 | 39 |
40 | 40 |
41 /* | 41 /* |
42 int id_parse __P7(ident_t *, id, | 42 int |
43 struct timeval *, timeout, | 43 id_parse __P7(ident_t *, id, |
44 int *, lport, | 44 struct timeval *, timeout, |
45 int *, fport, | 45 int *, lport, |
46 char **, identifier, | 46 int *, fport, |
47 char **, opsys, | 47 char **, identifier, |
48 char **, charset) | 48 char **, opsys, |
49 char **, charset) | |
49 */ | 50 */ |
50 | 51 |
51 int id_parse __P(( ident_t *id, | 52 int |
52 __STRUCT_TIMEVAL_P timeout, | 53 id_parse __P((ident_t * id, |
53 int *lport, | 54 __STRUCT_TIMEVAL_P timeout, |
54 int *fport, | 55 int *lport, |
55 char **identifier, | 56 int *fport, |
56 char **opsys, | 57 char **identifier, |
57 char **charset)) | 58 char **opsys, |
59 char **charset)) | |
58 { | 60 { |
59 char c, *cp, *tmp_charset; | 61 char c, *cp, *tmp_charset; |
60 fd_set rs; | 62 fd_set rs; |
61 int pos, res=0, lp, fp; | 63 int pos, res = 0, lp, fp; |
62 | 64 |
63 errno = 0; | 65 errno = 0; |
64 | 66 |
65 tmp_charset = 0; | 67 tmp_charset = 0; |
66 | 68 |
67 if (!id) | 69 if (!id) |
68 return -1; | 70 return -1; |
69 if (lport) | 71 if (lport) |
70 *lport = 0; | 72 *lport = 0; |
71 if (fport) | 73 if (fport) |
72 *fport = 0; | 74 *fport = 0; |
73 if (identifier) | 75 if (identifier) |
74 *identifier = 0; | 76 *identifier = 0; |
75 if (opsys) | 77 if (opsys) |
76 *opsys = 0; | 78 *opsys = 0; |
77 if (charset) | 79 if (charset) |
78 *charset = 0; | 80 *charset = 0; |
79 | 81 |
80 pos = strlen(id->buf); | 82 pos = strlen(id->buf); |
81 | 83 |
82 if (timeout) | 84 if (timeout) { |
83 { | 85 FD_ZERO(&rs); |
84 FD_ZERO(&rs); | 86 FD_SET(id->fd, &rs); |
85 FD_SET(id->fd, &rs); | |
86 | 87 |
87 #ifdef __hpux | 88 #ifdef __hpux |
88 if ((res = select(FD_SETSIZE, (int *) &rs, (int *)0, (int *)0, timeout)) < 0) | 89 if ((res = select(FD_SETSIZE, (int *) &rs, (int *) 0, (int *) 0, timeout)) < 0) |
89 #else | 90 #else |
90 if ((res = select(FD_SETSIZE, &rs, (fd_set *)0, (fd_set *)0, timeout)) < 0) | 91 if ((res = select(FD_SETSIZE, &rs, (fd_set *) 0, (fd_set *) 0, timeout)) < 0) |
91 #endif | 92 #endif |
92 return -1; | 93 return -1; |
93 | 94 |
94 if (res == 0) | 95 if (res == 0) { |
95 { | 96 errno = ETIMEDOUT; |
96 errno = ETIMEDOUT; | 97 return -1; |
97 return -1; | 98 } |
98 } | 99 } |
99 } | 100 |
100 | 101 /* Every octal value is allowed except 0, \n and \r */ |
101 /* Every octal value is allowed except 0, \n and \r */ | 102 while (pos < sizeof(id->buf) |
102 while (pos < sizeof(id->buf) && | 103 && (res = read(id->fd, id->buf + pos, 1)) == 1 |
103 (res = read(id->fd, id->buf + pos, 1)) == 1 && | 104 && id->buf[pos] != '\n' && id->buf[pos] != '\r') |
104 id->buf[pos] != '\n' && id->buf[pos] != '\r') | 105 pos++; |
105 pos++; | 106 |
106 | 107 if (res < 0) |
107 if (res < 0) | 108 return -1; |
108 return -1; | 109 |
109 | 110 if (res == 0) { |
110 if (res == 0) | 111 errno = ENOTCONN; |
111 { | 112 return -1; |
112 errno = ENOTCONN; | 113 } |
113 return -1; | 114 |
114 } | 115 if (id->buf[pos] != '\n' && id->buf[pos] != '\r') |
115 | 116 return 0; /* Not properly terminated string */ |
116 if (id->buf[pos] != '\n' && id->buf[pos] != '\r') | 117 |
117 return 0; /* Not properly terminated string */ | 118 id->buf[pos++] = '\0'; |
118 | 119 |
119 id->buf[pos++] = '\0'; | 120 /* |
120 | 121 ** Get first field (<lport> , <fport>) |
121 /* | 122 */ |
122 ** Get first field (<lport> , <fport>) | 123 cp = id_strtok(id->buf, ":", &c); |
123 */ | |
124 cp = id_strtok(id->buf, ":", &c); | |
125 if (!cp) | |
126 return -2; | |
127 | |
128 if (sscanf(cp, " %d , %d", &lp, &fp) != 2) | |
129 { | |
130 if (identifier) | |
131 { | |
132 *identifier = id_strdup(cp); | |
133 if (*identifier == NULL) | |
134 return -4; | |
135 } | |
136 return -2; | |
137 } | |
138 | |
139 if (lport) | |
140 *lport = lp; | |
141 if (fport) | |
142 *fport = fp; | |
143 | |
144 /* | |
145 ** Get second field (USERID or ERROR) | |
146 */ | |
147 cp = id_strtok((char *)0, ":", &c); | |
148 if (!cp) | |
149 return -2; | |
150 | |
151 if (strcmp(cp, "ERROR") == 0) | |
152 { | |
153 cp = id_strtok((char *)0, "\n\r", &c); | |
154 if (!cp) | 124 if (!cp) |
155 return -2; | 125 return -2; |
156 | 126 |
157 if (identifier) | 127 if (sscanf(cp, " %d , %d", &lp, &fp) != 2) { |
158 { | 128 if (identifier) { |
159 *identifier = id_strdup(cp); | 129 *identifier = id_strdup(cp); |
160 if (*identifier == NULL) | 130 if (*identifier == NULL) |
161 return -4; | 131 return -4; |
162 } | 132 } |
163 | 133 return -2; |
164 return 2; | 134 } |
165 } | 135 |
166 else if (strcmp(cp, "USERID") == 0) | 136 if (lport) |
167 { | 137 *lport = lp; |
138 if (fport) | |
139 *fport = fp; | |
140 | |
168 /* | 141 /* |
169 ** Get first subfield of third field <opsys> | 142 ** Get second field (USERID or ERROR) |
170 */ | 143 */ |
171 cp = id_strtok((char *) 0, ",:", &c); | 144 cp = id_strtok((char *) 0, ":", &c); |
172 if (!cp) | 145 if (!cp) |
173 return -2; | |
174 | |
175 if (opsys) | |
176 { | |
177 *opsys = id_strdup(cp); | |
178 if (*opsys == NULL) | |
179 return -4; | |
180 } | |
181 | |
182 /* | |
183 ** We have a second subfield (<charset>) | |
184 */ | |
185 if (c == ',') | |
186 { | |
187 cp = id_strtok((char *)0, ":", &c); | |
188 if (!cp) | |
189 return -2; | 146 return -2; |
190 | 147 |
191 tmp_charset = cp; | 148 if (strcmp(cp, "ERROR") == 0) { |
192 if (charset) | 149 cp = id_strtok((char *) 0, "\n\r", &c); |
193 { | 150 if (!cp) |
194 *charset = id_strdup(cp); | 151 return -2; |
195 if (*charset == NULL) | 152 |
196 return -4; | 153 if (identifier) { |
197 } | 154 *identifier = id_strdup(cp); |
198 | 155 if (*identifier == NULL) |
199 /* | 156 return -4; |
200 ** We have even more subfields - ignore them | 157 } |
201 */ | 158 |
202 if (c == ',') | 159 return 2; |
203 id_strtok((char *)0, ":", &c); | 160 } else if (strcmp(cp, "USERID") == 0) { |
204 } | 161 /* |
205 | 162 ** Get first subfield of third field <opsys> |
206 if (tmp_charset && strcmp(tmp_charset, "OCTET") == 0) | 163 */ |
207 cp = id_strtok((char *)0, (char *)0, &c); | 164 cp = id_strtok((char *) 0, ",:", &c); |
208 else | 165 if (!cp) |
209 cp = id_strtok((char *)0, "\n\r", &c); | 166 return -2; |
210 | 167 |
211 if (identifier) | 168 if (opsys) { |
212 { | 169 *opsys = id_strdup(cp); |
213 *identifier = id_strdup(cp); | 170 if (*opsys == NULL) |
214 if (*identifier == NULL) | 171 return -4; |
215 return -4; | 172 } |
216 } | 173 |
217 return 1; | 174 /* |
218 } | 175 ** We have a second subfield (<charset>) |
219 else | 176 */ |
220 { | 177 if (c == ',') { |
221 if (identifier) | 178 cp = id_strtok((char *) 0, ":", &c); |
222 { | 179 if (!cp) |
223 *identifier = id_strdup(cp); | 180 return -2; |
224 if (*identifier == NULL) | 181 |
225 return -4; | 182 tmp_charset = cp; |
226 } | 183 if (charset) { |
227 return -3; | 184 *charset = id_strdup(cp); |
228 } | 185 if (*charset == NULL) |
186 return -4; | |
187 } | |
188 | |
189 /* | |
190 ** We have even more subfields - ignore them | |
191 */ | |
192 if (c == ',') | |
193 id_strtok((char *) 0, ":", &c); | |
194 } | |
195 | |
196 if (tmp_charset && strcmp(tmp_charset, "OCTET") == 0) | |
197 cp = id_strtok((char *) 0, (char *) 0, &c); | |
198 else | |
199 cp = id_strtok((char *) 0, "\n\r", &c); | |
200 | |
201 if (identifier) { | |
202 *identifier = id_strdup(cp); | |
203 if (*identifier == NULL) | |
204 return -4; | |
205 } | |
206 return 1; | |
207 } else { | |
208 if (identifier) { | |
209 *identifier = id_strdup(cp); | |
210 if (*identifier == NULL) | |
211 return -4; | |
212 } | |
213 return -3; | |
214 } | |
229 } | 215 } |