diff options
author | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
---|---|---|
committer | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
commit | e5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch) | |
tree | d8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/ape/lib/bsd/getservbyname.c |
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/ape/lib/bsd/getservbyname.c')
-rwxr-xr-x | sys/src/ape/lib/bsd/getservbyname.c | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/sys/src/ape/lib/bsd/getservbyname.c b/sys/src/ape/lib/bsd/getservbyname.c new file mode 100755 index 000000000..0212d5403 --- /dev/null +++ b/sys/src/ape/lib/bsd/getservbyname.c @@ -0,0 +1,100 @@ +/* posix */ +#include <sys/types.h> +#include <unistd.h> +#include <stdlib.h> +#include <stdio.h> +#include <fcntl.h> +#include <string.h> +#include <errno.h> +#include <ctype.h> + +/* bsd extensions */ +#include <sys/uio.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <netdb.h> + +#include "priv.h" + +enum +{ + Nname= 6, +}; + +/* + * for inet addresses only + */ +struct servent* +getservbyname(char *name, char *proto) +{ + int i, fd, m, num; + char *p, *bp; + int nn, na; + static struct servent s; + static char buf[1024]; + static char *nptr[Nname+1]; + + num = 1; + for(p = name; *p; p++) + if(!isdigit(*p)) + num = 0; + + s.s_name = 0; + + /* connect to server */ + fd = open("/net/cs", O_RDWR); + if(fd < 0){ + _syserrno(); + return 0; + } + + /* construct the query, always expect an ip# back */ + if(num) + snprintf(buf, sizeof buf, "!port=%s %s=*", name, proto); + else + snprintf(buf, sizeof buf, "!%s=%s port=*", proto, name); + + /* query the server */ + if(write(fd, buf, strlen(buf)) < 0){ + _syserrno(); + return 0; + } + lseek(fd, 0, 0); + for(i = 0; i < sizeof(buf)-1; i += m){ + m = read(fd, buf+i, sizeof(buf) - 1 - i); + if(m <= 0) + break; + buf[i+m++] = ' '; + } + close(fd); + buf[i] = 0; + + /* parse the reply */ + nn = na = 0; + for(bp = buf;;){ + p = strchr(bp, '='); + if(p == 0) + break; + *p++ = 0; + if(strcmp(bp, proto) == 0){ + if(nn < Nname) + nptr[nn++] = p; + } else if(strcmp(bp, "port") == 0){ + s.s_port = htons(atoi(p)); + } + while(*p && *p != ' ') + p++; + if(*p) + *p++ = 0; + bp = p; + } + if(nn+na == 0) + return 0; + + nptr[nn] = 0; + s.s_aliases = nptr; + if(s.s_name == 0) + s.s_name = nptr[0]; + + return &s; +} |