version 1.23, 2003/10/28 09:08:06 |
version 1.23.2.2, 2005/03/10 17:15:04 |
|
|
} |
} |
|
|
/* set/unset filedescriptor to non-blocking */ |
/* set/unset filedescriptor to non-blocking */ |
void |
int |
set_nonblock(int fd) |
set_nonblock(int fd) |
{ |
{ |
int val; |
int val; |
|
|
val = fcntl(fd, F_GETFL, 0); |
val = fcntl(fd, F_GETFL, 0); |
if (val < 0) { |
if (val < 0) { |
error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno)); |
error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno)); |
return; |
return (-1); |
} |
} |
if (val & O_NONBLOCK) { |
if (val & O_NONBLOCK) { |
debug2("fd %d is O_NONBLOCK", fd); |
debug3("fd %d is O_NONBLOCK", fd); |
return; |
return (0); |
} |
} |
debug2("fd %d setting O_NONBLOCK", fd); |
debug2("fd %d setting O_NONBLOCK", fd); |
val |= O_NONBLOCK; |
val |= O_NONBLOCK; |
if (fcntl(fd, F_SETFL, val) == -1) |
if (fcntl(fd, F_SETFL, val) == -1) { |
debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", |
debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd, |
fd, strerror(errno)); |
strerror(errno)); |
|
return (-1); |
|
} |
|
return (0); |
} |
} |
|
|
void |
int |
unset_nonblock(int fd) |
unset_nonblock(int fd) |
{ |
{ |
int val; |
int val; |
|
|
val = fcntl(fd, F_GETFL, 0); |
val = fcntl(fd, F_GETFL, 0); |
if (val < 0) { |
if (val < 0) { |
error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno)); |
error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno)); |
return; |
return (-1); |
} |
} |
if (!(val & O_NONBLOCK)) { |
if (!(val & O_NONBLOCK)) { |
debug2("fd %d is not O_NONBLOCK", fd); |
debug3("fd %d is not O_NONBLOCK", fd); |
return; |
return (0); |
} |
} |
debug("fd %d clearing O_NONBLOCK", fd); |
debug("fd %d clearing O_NONBLOCK", fd); |
val &= ~O_NONBLOCK; |
val &= ~O_NONBLOCK; |
if (fcntl(fd, F_SETFL, val) == -1) |
if (fcntl(fd, F_SETFL, val) == -1) { |
debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", |
debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s", |
fd, strerror(errno)); |
fd, strerror(errno)); |
|
return (-1); |
|
} |
|
return (0); |
} |
} |
|
|
/* disable nagle on socket */ |
/* disable nagle on socket */ |
|
|
return total; |
return total; |
} |
} |
|
|
|
/* |
|
* Search for next delimiter between hostnames/addresses and ports. |
|
* Argument may be modified (for termination). |
|
* Returns *cp if parsing succeeds. |
|
* *cp is set to the start of the next delimiter, if one was found. |
|
* If this is the last field, *cp is set to NULL. |
|
*/ |
char * |
char * |
|
hpdelim(char **cp) |
|
{ |
|
char *s, *old; |
|
|
|
if (cp == NULL || *cp == NULL) |
|
return NULL; |
|
|
|
old = s = *cp; |
|
if (*s == '[') { |
|
if ((s = strchr(s, ']')) == NULL) |
|
return NULL; |
|
else |
|
s++; |
|
} else if ((s = strpbrk(s, ":/")) == NULL) |
|
s = *cp + strlen(*cp); /* skip to end (see first case below) */ |
|
|
|
switch (*s) { |
|
case '\0': |
|
*cp = NULL; /* no more fields*/ |
|
break; |
|
|
|
case ':': |
|
case '/': |
|
*s = '\0'; /* terminate */ |
|
*cp = s + 1; |
|
break; |
|
|
|
default: |
|
return NULL; |
|
} |
|
|
|
return old; |
|
} |
|
|
|
char * |
cleanhostname(char *host) |
cleanhostname(char *host) |
{ |
{ |
if (*host == '[' && host[strlen(host) - 1] == ']') { |
if (*host == '[' && host[strlen(host) - 1] == ']') { |
|
|
{ |
{ |
va_list ap; |
va_list ap; |
char buf[1024]; |
char buf[1024]; |
int nalloc; |
u_int nalloc; |
|
|
va_start(ap, fmt); |
va_start(ap, fmt); |
vsnprintf(buf, sizeof(buf), fmt, ap); |
vsnprintf(buf, sizeof(buf), fmt, ap); |
|
|
args->nalloc = nalloc; |
args->nalloc = nalloc; |
args->list[args->num++] = xstrdup(buf); |
args->list[args->num++] = xstrdup(buf); |
args->list[args->num] = NULL; |
args->list[args->num] = NULL; |
|
} |
|
|
|
/* |
|
* Read an entire line from a public key file into a static buffer, discarding |
|
* lines that exceed the buffer size. Returns 0 on success, -1 on failure. |
|
*/ |
|
int |
|
read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz, |
|
u_long *lineno) |
|
{ |
|
while (fgets(buf, bufsz, f) != NULL) { |
|
(*lineno)++; |
|
if (buf[strlen(buf) - 1] == '\n' || feof(f)) { |
|
return 0; |
|
} else { |
|
debug("%s: %s line %lu exceeds size limit", __func__, |
|
filename, *lineno); |
|
/* discard remainder of line */ |
|
while(fgetc(f) != '\n' && !feof(f)) |
|
; /* nothing */ |
|
} |
|
} |
|
return -1; |
} |
} |