comparison dbutil.c @ 165:0cfba3034be5

Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
author Matt Johnston <matt@ucc.asn.au>
date Sun, 02 Jan 2005 20:25:56 +0000
parents b9d3f725e00b
children 3e49d8d18005
comparison
equal deleted inserted replaced
161:b9d3f725e00b 165:0cfba3034be5
162 unsigned int nsock; 162 unsigned int nsock;
163 struct linger linger; 163 struct linger linger;
164 int val; 164 int val;
165 int sock; 165 int sock;
166 166
167 TRACE(("enter dropbear_listen")); 167 TRACE(("enter dropbear_listen"))
168 168
169 memset(&hints, 0, sizeof(hints)); 169 memset(&hints, 0, sizeof(hints));
170 hints.ai_family = AF_UNSPEC; /* TODO: let them flag v4 only etc */ 170 hints.ai_family = AF_UNSPEC; /* TODO: let them flag v4 only etc */
171 hints.ai_socktype = SOCK_STREAM; 171 hints.ai_socktype = SOCK_STREAM;
172 172
173 if (address && address[0] == '\0') { 173 if (address && address[0] == '\0') {
174 TRACE(("dropbear_listen: local loopback")); 174 TRACE(("dropbear_listen: local loopback"))
175 address = NULL; 175 address = NULL;
176 } else { 176 } else {
177 TRACE(("dropbear_listen: not local loopback")); 177 TRACE(("dropbear_listen: not local loopback"))
178 hints.ai_flags = AI_PASSIVE; 178 hints.ai_flags = AI_PASSIVE;
179 } 179 }
180 err = getaddrinfo(address, port, &hints, &res0); 180 err = getaddrinfo(address, port, &hints, &res0);
181 181
182 if (err) { 182 if (err) {
184 int len; 184 int len;
185 len = 20 + strlen(gai_strerror(err)); 185 len = 20 + strlen(gai_strerror(err));
186 *errstring = (char*)m_malloc(len); 186 *errstring = (char*)m_malloc(len);
187 snprintf(*errstring, len, "Error resolving: %s", gai_strerror(err)); 187 snprintf(*errstring, len, "Error resolving: %s", gai_strerror(err));
188 } 188 }
189 TRACE(("leave dropbear_listen: failed resolving")); 189 TRACE(("leave dropbear_listen: failed resolving"))
190 return -1; 190 return -1;
191 } 191 }
192 192
193 193
194 nsock = 0; 194 nsock = 0;
201 201
202 sock = socks[nsock]; /* For clarity */ 202 sock = socks[nsock]; /* For clarity */
203 203
204 if (sock < 0) { 204 if (sock < 0) {
205 err = errno; 205 err = errno;
206 TRACE(("socket() failed")); 206 TRACE(("socket() failed"))
207 continue; 207 continue;
208 } 208 }
209 209
210 /* Various useful socket options */ 210 /* Various useful socket options */
211 val = 1; 211 val = 1;
219 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&val, sizeof(val)); 219 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&val, sizeof(val));
220 220
221 if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) { 221 if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
222 err = errno; 222 err = errno;
223 close(sock); 223 close(sock);
224 TRACE(("bind(%s) failed", port)); 224 TRACE(("bind(%s) failed", port))
225 continue; 225 continue;
226 } 226 }
227 227
228 if (listen(sock, 20) < 0) { 228 if (listen(sock, 20) < 0) {
229 err = errno; 229 err = errno;
230 close(sock); 230 close(sock);
231 TRACE(("listen() failed")); 231 TRACE(("listen() failed"))
232 continue; 232 continue;
233 } 233 }
234 234
235 *maxfd = MAX(*maxfd, sock); 235 *maxfd = MAX(*maxfd, sock);
236 236
241 if (errstring != NULL && *errstring == NULL) { 241 if (errstring != NULL && *errstring == NULL) {
242 int len; 242 int len;
243 len = 20 + strlen(strerror(err)); 243 len = 20 + strlen(strerror(err));
244 *errstring = (char*)m_malloc(len); 244 *errstring = (char*)m_malloc(len);
245 snprintf(*errstring, len, "Error listening: %s", strerror(err)); 245 snprintf(*errstring, len, "Error listening: %s", strerror(err));
246 TRACE(("leave dropbear_listen: failure, %s", strerror(err))); 246 TRACE(("leave dropbear_listen: failure, %s", strerror(err)))
247 return -1; 247 return -1;
248 } 248 }
249 } 249 }
250 250
251 TRACE(("leave dropbear_listen: success, %d socks bound", nsock)); 251 TRACE(("leave dropbear_listen: success, %d socks bound", nsock))
252 return nsock; 252 return nsock;
253 } 253 }
254 254
255 /* Connect via TCP to a host. Connection will try ipv4 or ipv6, will 255 /* Connect via TCP to a host. Connection will try ipv4 or ipv6, will
256 * return immediately if nonblocking is set. On failure, if errstring 256 * return immediately if nonblocking is set. On failure, if errstring
262 262
263 struct addrinfo *res0 = NULL, *res = NULL, hints; 263 struct addrinfo *res0 = NULL, *res = NULL, hints;
264 int sock; 264 int sock;
265 int err; 265 int err;
266 266
267 TRACE(("enter connect_remote")); 267 TRACE(("enter connect_remote"))
268 268
269 if (errstring != NULL) { 269 if (errstring != NULL) {
270 *errstring = NULL; 270 *errstring = NULL;
271 } 271 }
272 272
280 int len; 280 int len;
281 len = 20 + strlen(gai_strerror(err)); 281 len = 20 + strlen(gai_strerror(err));
282 *errstring = (char*)m_malloc(len); 282 *errstring = (char*)m_malloc(len);
283 snprintf(*errstring, len, "Error resolving: %s", gai_strerror(err)); 283 snprintf(*errstring, len, "Error resolving: %s", gai_strerror(err));
284 } 284 }
285 TRACE(("Error resolving: %s", gai_strerror(err))); 285 TRACE(("Error resolving: %s", gai_strerror(err)))
286 return -1; 286 return -1;
287 } 287 }
288 288
289 sock = -1; 289 sock = -1;
290 err = EADDRNOTAVAIL; 290 err = EADDRNOTAVAIL;
301 close(sock); 301 close(sock);
302 sock = -1; 302 sock = -1;
303 if (errstring != NULL && *errstring == NULL) { 303 if (errstring != NULL && *errstring == NULL) {
304 *errstring = m_strdup("Failed non-blocking"); 304 *errstring = m_strdup("Failed non-blocking");
305 } 305 }
306 TRACE(("Failed non-blocking: %s", strerror(errno))); 306 TRACE(("Failed non-blocking: %s", strerror(errno)))
307 continue; 307 continue;
308 } 308 }
309 } 309 }
310 310
311 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) { 311 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
312 if (errno == EINPROGRESS && nonblocking) { 312 if (errno == EINPROGRESS && nonblocking) {
313 TRACE(("Connect in progress")); 313 TRACE(("Connect in progress"))
314 break; 314 break;
315 } else { 315 } else {
316 err = errno; 316 err = errno;
317 close(sock); 317 close(sock);
318 sock = -1; 318 sock = -1;
329 int len; 329 int len;
330 len = 20 + strlen(strerror(err)); 330 len = 20 + strlen(strerror(err));
331 *errstring = (char*)m_malloc(len); 331 *errstring = (char*)m_malloc(len);
332 snprintf(*errstring, len, "Error connecting: %s", strerror(err)); 332 snprintf(*errstring, len, "Error connecting: %s", strerror(err));
333 } 333 }
334 TRACE(("Error connecting: %s", strerror(err))); 334 TRACE(("Error connecting: %s", strerror(err)))
335 } else { 335 } else {
336 /* Success */ 336 /* Success */
337 /* (err is used as a dummy var here) */ 337 /* (err is used as a dummy var here) */
338 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&err, sizeof(err)); 338 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&err, sizeof(err));
339 } 339 }
341 freeaddrinfo(res0); 341 freeaddrinfo(res0);
342 if (sock > 0 && errstring != NULL && *errstring != NULL) { 342 if (sock > 0 && errstring != NULL && *errstring != NULL) {
343 m_free(*errstring); 343 m_free(*errstring);
344 } 344 }
345 345
346 TRACE(("leave connect_remote: sock %d\n", sock)); 346 TRACE(("leave connect_remote: sock %d\n", sock))
347 return sock; 347 return sock;
348 } 348 }
349 349
350 /* Return a string representation of the socket address passed. The return 350 /* Return a string representation of the socket address passed. The return
351 * value is allocated with malloc() */ 351 * value is allocated with malloc() */
501 #if defined(DROPBEAR_CLIENT) || defined(ENABLE_SVR_PUBKEY_AUTH) 501 #if defined(DROPBEAR_CLIENT) || defined(ENABLE_SVR_PUBKEY_AUTH)
502 int buf_getline(buffer * line, FILE * authfile) { 502 int buf_getline(buffer * line, FILE * authfile) {
503 503
504 int c = EOF; 504 int c = EOF;
505 505
506 TRACE(("enter buf_getline")); 506 TRACE(("enter buf_getline"))
507 507
508 buf_setpos(line, 0); 508 buf_setpos(line, 0);
509 buf_setlen(line, 0); 509 buf_setlen(line, 0);
510 510
511 while (line->pos < line->size) { 511 while (line->pos < line->size) {
516 } 516 }
517 517
518 buf_putbyte(line, (unsigned char)c); 518 buf_putbyte(line, (unsigned char)c);
519 } 519 }
520 520
521 TRACE(("leave getauthline: line too long")); 521 TRACE(("leave getauthline: line too long"))
522 /* We return success, but the line length will be zeroed - ie we just 522 /* We return success, but the line length will be zeroed - ie we just
523 * ignore that line */ 523 * ignore that line */
524 buf_setlen(line, 0); 524 buf_setlen(line, 0);
525 525
526 out: 526 out:
527 527
528 528
529 /* if we didn't read anything before EOF or error, exit */ 529 /* if we didn't read anything before EOF or error, exit */
530 if (c == EOF && line->pos == 0) { 530 if (c == EOF && line->pos == 0) {
531 TRACE(("leave buf_getline: failure")); 531 TRACE(("leave buf_getline: failure"))
532 return DROPBEAR_FAILURE; 532 return DROPBEAR_FAILURE;
533 } else { 533 } else {
534 TRACE(("leave buf_getline: success")); 534 TRACE(("leave buf_getline: success"))
535 buf_setpos(line, 0); 535 buf_setpos(line, 0);
536 return DROPBEAR_SUCCESS; 536 return DROPBEAR_SUCCESS;
537 } 537 }
538 538
539 } 539 }
616 } 616 }
617 617
618 618
619 void setnonblocking(int fd) { 619 void setnonblocking(int fd) {
620 620
621 TRACE(("setnonblocking: %d", fd)); 621 TRACE(("setnonblocking: %d", fd))
622 622
623 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { 623 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
624 dropbear_exit("Couldn't set nonblocking"); 624 dropbear_exit("Couldn't set nonblocking");
625 } 625 }
626 TRACE(("leave setnonblocking")); 626 TRACE(("leave setnonblocking"))
627 } 627 }