mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-06 09:50:28 +00:00
changes after review of #204
* use calloc instead malloc for s_aliases * use strcasecmp instead of strcmp * look through aliases when comparing for LookupServicesByName * provide buffer for name in LookupServicesByName also * provide a filepath param in LookupServicesBy* for testing * use static buffers and DNS_NAME_MAX for s_name
This commit is contained in:
parent
58fb2fb3d3
commit
b73edacaa9
5 changed files with 66 additions and 58 deletions
|
@ -31,9 +31,9 @@
|
||||||
|
|
||||||
struct hostent *gethostbyaddr(const void *s_addr, socklen_t len, int type) {
|
struct hostent *gethostbyaddr(const void *s_addr, socklen_t len, int type) {
|
||||||
static struct hostent *ptr1, he1;
|
static struct hostent *ptr1, he1;
|
||||||
static char h_name[DNS_NAME_MAX+1];
|
static char h_name[DNS_NAME_MAX + 1];
|
||||||
static char* h_aliases[1];
|
static char *h_aliases[1];
|
||||||
static char* h_addr_list[2];
|
static char *h_addr_list[2];
|
||||||
static char h_addr_list0[4];
|
static char h_addr_list0[4];
|
||||||
|
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
|
|
|
@ -31,21 +31,19 @@
|
||||||
|
|
||||||
struct servent *getservbyname(const char *name, const char *proto) {
|
struct servent *getservbyname(const char *name, const char *proto) {
|
||||||
static struct servent *ptr0, se0;
|
static struct servent *ptr0, se0;
|
||||||
|
static char s_name[DNS_NAME_MAX + 1];
|
||||||
char *localproto = proto;
|
char *localproto = proto;
|
||||||
int p;
|
int p;
|
||||||
|
|
||||||
if (!ptr0) {
|
if (!ptr0) {
|
||||||
se0.s_name = NULL;
|
se0.s_name = s_name;
|
||||||
se0.s_aliases = (char **)malloc(sizeof(char *) * 1);
|
if (!(se0.s_aliases = calloc(1, sizeof(char *)))) return NULL;
|
||||||
if (!se0.s_aliases) return NULL;
|
|
||||||
se0.s_aliases[0] = NULL;
|
|
||||||
|
|
||||||
se0.s_port = 0;
|
se0.s_port = 0;
|
||||||
se0.s_proto = NULL;
|
se0.s_proto = NULL;
|
||||||
ptr0 = &se0;
|
ptr0 = &se0;
|
||||||
}
|
}
|
||||||
|
|
||||||
p = LookupServicesByName(name, &localproto);
|
p = LookupServicesByName(name, &localproto, ptr0->s_name, DNS_NAME_MAX, NULL);
|
||||||
if (p == -1) {
|
if (p == -1) {
|
||||||
// localproto got alloc'd during the lookup?
|
// localproto got alloc'd during the lookup?
|
||||||
if (!proto && localproto != proto) free(localproto);
|
if (!proto && localproto != proto) free(localproto);
|
||||||
|
@ -53,9 +51,6 @@ struct servent *getservbyname(const char *name, const char *proto) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ptr0->s_port = p;
|
ptr0->s_port = p;
|
||||||
if (ptr0->s_name) free(ptr0->s_name);
|
|
||||||
ptr0->s_name = strdup(name);
|
|
||||||
|
|
||||||
if (ptr0->s_proto) free(ptr0->s_proto);
|
if (ptr0->s_proto) free(ptr0->s_proto);
|
||||||
ptr0->s_proto = strdup(localproto);
|
ptr0->s_proto = strdup(localproto);
|
||||||
|
|
||||||
|
|
|
@ -30,30 +30,25 @@
|
||||||
|
|
||||||
struct servent *getservbyport(int port, const char *proto) {
|
struct servent *getservbyport(int port, const char *proto) {
|
||||||
static struct servent *ptr1, se1;
|
static struct servent *ptr1, se1;
|
||||||
char name[DNS_NAME_MAX];
|
static char s_name[DNS_NAME_MAX + 1];
|
||||||
char *localproto = proto;
|
char *localproto = proto;
|
||||||
|
|
||||||
if (!ptr1) {
|
if (!ptr1) {
|
||||||
se1.s_name = NULL;
|
se1.s_name = s_name;
|
||||||
se1.s_aliases = (char **)malloc(sizeof(char *) * 1);
|
if (!(se1.s_aliases = calloc(1, sizeof(char *)))) return NULL;
|
||||||
if (!se1.s_aliases) return NULL;
|
|
||||||
se1.s_aliases[0] = NULL;
|
|
||||||
|
|
||||||
se1.s_port = 0;
|
se1.s_port = 0;
|
||||||
se1.s_proto = NULL;
|
se1.s_proto = NULL;
|
||||||
ptr1 = &se1;
|
ptr1 = &se1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (LookupServicesByPort(port, &localproto, name, sizeof(name)) == -1) {
|
if (LookupServicesByPort(port, &localproto, ptr1->s_name, DNS_NAME_MAX,
|
||||||
|
NULL) == -1) {
|
||||||
// localproto got alloc'd during the lookup?
|
// localproto got alloc'd during the lookup?
|
||||||
if (!proto && localproto != proto) free(localproto);
|
if (!proto && localproto != proto) free(localproto);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ptr1->s_port = port;
|
ptr1->s_port = port;
|
||||||
if (ptr1->s_name) free(ptr1->s_name);
|
|
||||||
ptr1->s_name = strdup(name);
|
|
||||||
|
|
||||||
if (ptr1->s_proto) free(ptr1->s_proto);
|
if (ptr1->s_proto) free(ptr1->s_proto);
|
||||||
ptr1->s_proto = strdup(localproto);
|
ptr1->s_proto = strdup(localproto);
|
||||||
|
|
||||||
|
|
|
@ -65,51 +65,54 @@ static textwindows noinline char *GetNtServicesTxtPath(char *pathbuf,
|
||||||
*
|
*
|
||||||
* @param servport is the port number (in network byte order)
|
* @param servport is the port number (in network byte order)
|
||||||
* @param servproto is a pointer to a string (*servproto can be NULL)
|
* @param servproto is a pointer to a string (*servproto can be NULL)
|
||||||
* @param buf is a buffer to store the resulting name
|
* @param buf is a buffer to store the official name of the service
|
||||||
* @param bufsize is the size of buf
|
* @param bufsize is the size of buf
|
||||||
|
* @param filepath is the location of the services file
|
||||||
|
* (if NULL, uses /etc/services)
|
||||||
* @returns 0 on success, -1 on error
|
* @returns 0 on success, -1 on error
|
||||||
*
|
*
|
||||||
* @note aliases are not read from the file.
|
* @note aliases are not read from the file.
|
||||||
*/
|
*/
|
||||||
int LookupServicesByPort(const int servport, char **servproto, char *buf,
|
int LookupServicesByPort(const int servport, char **servproto, char *buf,
|
||||||
size_t bufsize) {
|
size_t bufsize, const char *filepath) {
|
||||||
FILE *f;
|
FILE *f;
|
||||||
char *line;
|
char *line;
|
||||||
char pathbuf[PATH_MAX];
|
char pathbuf[PATH_MAX];
|
||||||
const char *path;
|
const char *path;
|
||||||
size_t linesize;
|
size_t linesize;
|
||||||
int count, found;
|
int found;
|
||||||
char *name, *port, *proto, *comment, *tok;
|
char *name, *port, *proto, *comment, *tok;
|
||||||
|
|
||||||
path = "/etc/services";
|
if (!(path = filepath)) {
|
||||||
if (IsWindows()) {
|
path = "/etc/services";
|
||||||
path = firstnonnull(GetNtServicesTxtPath(pathbuf, ARRAYLEN(pathbuf)), path);
|
if (IsWindows()) {
|
||||||
|
path =
|
||||||
|
firstnonnull(GetNtServicesTxtPath(pathbuf, ARRAYLEN(pathbuf)), path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bufsize == 0 || !(f = fopen(path, "r"))) {
|
if (bufsize == 0 || !(f = fopen(path, "r"))) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
line = NULL;
|
line = NULL;
|
||||||
linesize = 0;
|
linesize = 0;
|
||||||
count = 0;
|
|
||||||
found = 0;
|
found = 0;
|
||||||
|
|
||||||
while (found == 0 && (getline(&line, &linesize, f)) != -1) {
|
while (found == 0 && (getline(&line, &linesize, f)) != -1) {
|
||||||
if ((comment = strchr(line, '#'))) *comment = '\0';
|
if ((comment = strchr(line, '#'))) *comment = '\0';
|
||||||
name = strtok_r(line, " \t\r\n\v", &tok);
|
name = strtok_r(line, " \t\r\n\v", &tok);
|
||||||
port = strtok_r(NULL, "/ \t\r\n\v", &tok);
|
port = strtok_r(NULL, "/ \t\r\n\v", &tok);
|
||||||
if (name && port && servport == htons(atoi(port))) {
|
proto = strtok_r(NULL, " \t\r\n\v", &tok);
|
||||||
if (!(proto = strtok_r(NULL, " \t\r\n\v", &tok)))
|
if (name && port && proto && servport == htons(atoi(port))) {
|
||||||
continue;
|
if (!servproto[0]) {
|
||||||
else if (!servproto[0]) {
|
|
||||||
servproto[0] = strdup(proto);
|
servproto[0] = strdup(proto);
|
||||||
strncpy(buf, name, bufsize);
|
strncpy(buf, name, bufsize);
|
||||||
found = 1;
|
found = 1;
|
||||||
} else if (strcmp(proto, servproto[0]) == 0) {
|
} else if (strcasecmp(proto, servproto[0]) == 0) {
|
||||||
strncpy(buf, name, bufsize);
|
strncpy(buf, name, bufsize);
|
||||||
found = 1;
|
found = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
count++;
|
|
||||||
}
|
}
|
||||||
free(line);
|
free(line);
|
||||||
|
|
||||||
|
@ -129,32 +132,39 @@ int LookupServicesByPort(const int servport, char **servproto, char *buf,
|
||||||
*
|
*
|
||||||
* @param servname is a NULL-terminated string
|
* @param servname is a NULL-terminated string
|
||||||
* @param servproto is a pointer to a string (*servproto can be NULL)
|
* @param servproto is a pointer to a string (*servproto can be NULL)
|
||||||
|
* @param buf is a buffer to store the official name of the service
|
||||||
|
* @param bufsize is the size of buf
|
||||||
|
* @param filepath is the location of services file
|
||||||
|
* (if NULL, uses /etc/services)
|
||||||
* @returns -1 on error, or
|
* @returns -1 on error, or
|
||||||
* positive port number (in network byte order)
|
* positive port number (in network byte order)
|
||||||
*
|
*
|
||||||
* @note aliases are not read from the file.
|
* @note aliases are read from file for comparison, but not returned.
|
||||||
* @see LookupServicesByPort
|
* @see LookupServicesByPort
|
||||||
*/
|
*/
|
||||||
int LookupServicesByName(const char *servname, char **servproto) {
|
int LookupServicesByName(const char *servname, char **servproto, char *buf,
|
||||||
|
size_t bufsize, const char *filepath) {
|
||||||
FILE *f;
|
FILE *f;
|
||||||
char *line;
|
char *line;
|
||||||
char pathbuf[PATH_MAX];
|
char pathbuf[PATH_MAX];
|
||||||
const char *path;
|
const char *path;
|
||||||
size_t linesize;
|
size_t linesize;
|
||||||
int count, found, result;
|
int found, result;
|
||||||
char *name, *port, *proto, *comment, *tok;
|
char *name, *port, *proto, *alias, *comment, *tok;
|
||||||
|
|
||||||
path = "/etc/services";
|
if (!(path = filepath)) {
|
||||||
|
path = "/etc/services";
|
||||||
if (IsWindows()) {
|
if (IsWindows()) {
|
||||||
path = firstnonnull(GetNtServicesTxtPath(pathbuf, ARRAYLEN(pathbuf)), path);
|
path =
|
||||||
|
firstnonnull(GetNtServicesTxtPath(pathbuf, ARRAYLEN(pathbuf)), path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!(f = fopen(path, "r"))) {
|
|
||||||
|
if (bufsize == 0 || !(f = fopen(path, "r"))) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
line = NULL;
|
line = NULL;
|
||||||
linesize = 0;
|
linesize = 0;
|
||||||
count = 0;
|
|
||||||
found = 0;
|
found = 0;
|
||||||
result = -1;
|
result = -1;
|
||||||
|
|
||||||
|
@ -162,19 +172,26 @@ int LookupServicesByName(const char *servname, char **servproto) {
|
||||||
if ((comment = strchr(line, '#'))) *comment = '\0';
|
if ((comment = strchr(line, '#'))) *comment = '\0';
|
||||||
name = strtok_r(line, " \t\r\n\v", &tok);
|
name = strtok_r(line, " \t\r\n\v", &tok);
|
||||||
port = strtok_r(NULL, "/ \t\r\n\v", &tok);
|
port = strtok_r(NULL, "/ \t\r\n\v", &tok);
|
||||||
if (name && port && strcmp(name, servname) == 0) {
|
proto = strtok_r(NULL, " \t\r\n\v", &tok);
|
||||||
if (!(proto = strtok_r(NULL, " \t\r\n\v", &tok)))
|
if (name && port && proto) {
|
||||||
continue;
|
alias = name;
|
||||||
else if (!servproto[0]) {
|
while (alias && strcasecmp(alias, servname) != 0)
|
||||||
servproto[0] = strdup(proto);
|
alias = strtok_r(NULL, " \t\r\n\v", &tok);
|
||||||
result = htons(atoi(port));
|
|
||||||
found = 1;
|
if (alias) /* alias matched with servname */
|
||||||
} else if (strcmp(proto, servproto[0]) == 0) {
|
{
|
||||||
result = htons(atoi(port));
|
if (!servproto[0]) {
|
||||||
found = 1;
|
servproto[0] = strdup(proto);
|
||||||
|
result = htons(atoi(port));
|
||||||
|
strncpy(buf, name, bufsize);
|
||||||
|
found = 1;
|
||||||
|
} else if (strcasecmp(proto, servproto[0]) == 0) {
|
||||||
|
result = htons(atoi(port));
|
||||||
|
strncpy(buf, name, bufsize);
|
||||||
|
found = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
count++;
|
|
||||||
}
|
}
|
||||||
free(line);
|
free(line);
|
||||||
|
|
||||||
|
|
|
@ -6,9 +6,10 @@
|
||||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||||
COSMOPOLITAN_C_START_
|
COSMOPOLITAN_C_START_
|
||||||
|
|
||||||
int LookupServicesByPort(const int, char **, char *, size_t)
|
int LookupServicesByPort(const int, char **, char *, size_t, const char *)
|
||||||
paramsnonnull((2, 3));
|
paramsnonnull((2, 3));
|
||||||
int LookupServicesByName(const char *, char **) paramsnonnull((1, 2));
|
int LookupServicesByName(const char *, char **, char *, size_t, const char *)
|
||||||
|
paramsnonnull((1, 2, 3));
|
||||||
|
|
||||||
/* TODO: implement like struct HostsTxt? */
|
/* TODO: implement like struct HostsTxt? */
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue