projects
/
tinc
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Simplify fake getname/addrinfo() functions, possibly fixing freeing a NULL pointer.
[tinc]
/
lib
/
fake-getnameinfo.c
diff --git
a/lib/fake-getnameinfo.c
b/lib/fake-getnameinfo.c
index
0541648
..
6630dec
100644
(file)
--- a/
lib/fake-getnameinfo.c
+++ b/
lib/fake-getnameinfo.c
@@
-9,57
+9,38
@@
* that ai_family is AF_INET. Don't use it for another purpose.
*/
* that ai_family is AF_INET. Don't use it for another purpose.
*/
-#include "config.h"
-
-#include <stdio.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <netdb.h>
-#include <string.h>
-
-#include <system.h>
+#include "system.h"
#include "fake-getnameinfo.h"
#include "fake-getnameinfo.h"
+#include "fake-getaddrinfo.h"
#ifndef HAVE_GETNAMEINFO
#ifndef HAVE_GETNAMEINFO
-int getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
- size_t hostlen, char *serv, size_t servlen, int flags)
+int getnameinfo(const struct sockaddr *sa, size_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags)
{
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
struct hostent *hp;
{
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
struct hostent *hp;
- char tmpserv[16];
-
- if (serv) {
- snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port));
- if (strlen(tmpserv) >= servlen)
- return EAI_MEMORY;
- else
- strcpy(serv, tmpserv);
- }
- if (host) {
- if (flags & NI_NUMERICHOST) {
- if (strlen(inet_ntoa(sin->sin_addr)) >= hostlen)
- return EAI_MEMORY;
+ if(serv)
+ snprintf(serv, sizeof(tmpserv), "%d", ntohs(sin->sin_port));
- strcpy(host, inet_ntoa(sin->sin_addr));
- return 0;
- } else {
- hp = gethostbyaddr((char *)&sin->sin_addr,
- sizeof(struct in_addr), AF_INET);
- if (hp == NULL)
- return EAI_NODATA;
-
- if (strlen(hp->h_name) >= hostlen)
- return EAI_MEMORY;
+ if(!host)
+ return 0;
- strcpy(host, hp->h_name);
-
return 0
;
- }
+ if(flags & NI_NUMERICHOST) {
+
strncpy(host, inet_ntoa(sin->sin_addr), sizeof(host))
;
+ return 0;
}
}
+
+ hp = gethostbyaddr((char *)&sin->sin_addr, sizeof(struct in_addr), AF_INET);
+
+ if(!hp || !hp->h_name)
+ return EAI_NODATA;
+
+ if(strlen(hp->h_name) >= hostlen)
+ return EAI_MEMORY;
+
+ strncpy(host, hp->h_name, hostlen);
return 0;
}
#endif /* !HAVE_GETNAMEINFO */
return 0;
}
#endif /* !HAVE_GETNAMEINFO */