提交 e40f48a4 编写于 作者: R Rich Felker

implement inet_lnaof, inet_netof, and inet_makeaddr

also move all legacy inet_* functions into a single file to avoid
wasting object file and compile time overhead on them.

the added functions are legacy interfaces for working with classful
ipv4 network addresses. they have no modern usefulness whatsoever, but
some programs unconditionally use them anyway, and they're tiny.
上级 83966b36
......@@ -29,7 +29,10 @@ char *inet_ntoa (struct in_addr);
int inet_pton (int, const char *__restrict, void *__restrict);
const char *inet_ntop (int, const void *__restrict, char *__restrict, socklen_t);
int inet_aton (const char *, struct in_addr *); /* nonstandard but widely used */
int inet_aton (const char *, struct in_addr *);
struct in_addr inet_makeaddr(int, int);
in_addr_t inet_lnaof(struct in_addr);
in_addr_t inet_netof(struct in_addr);
#undef INET_ADDRSTRLEN
#undef INET6_ADDRSTRLEN
......
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "__dns.h"
in_addr_t inet_addr(const char *p)
{
struct sockaddr_in sin;
if (__ipparse(&sin, AF_INET, p)) return -1;
return sin.sin_addr.s_addr;
}
#include <sys/socket.h>
#include <arpa/inet.h>
int inet_aton(const char *cp, struct in_addr *inp)
{
return inet_pton(AF_INET, cp, (void *)inp) > 0;
}
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include "__dns.h"
in_addr_t inet_addr(const char *p)
{
struct sockaddr_in sin;
if (__ipparse(&sin, AF_INET, p)) return -1;
return sin.sin_addr.s_addr;
}
in_addr_t inet_network(const char *p)
{
return ntohl(inet_addr(p));
}
int inet_aton(const char *cp, struct in_addr *inp)
{
return inet_pton(AF_INET, cp, (void *)inp) > 0;
}
char *inet_ntoa(struct in_addr in)
{
static char buf[16];
unsigned char *a = (void *)&in;
snprintf(buf, sizeof buf, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
return buf;
}
struct in_addr inet_makeaddr(int net, int host)
{
uint32_t n = net, h = host;
if (n < 256) h |= n<<24;
else if (n < 65536) h |= n<<16;
else h |= n<<8;
return (struct in_addr){ h };
}
in_addr_t inet_lnaof(struct in_addr in)
{
uint32_t h = in.s_addr;
if (h>>24 < 128) return h & 0xffffff;
if (h>>24 < 192) return h & 0xffff;
return h & 0xff;
}
in_addr_t inet_netof(struct in_addr in)
{
uint32_t h = in.s_addr;
if (h>>24 < 128) return h >> 24;
if (h>>24 < 192) return h >> 16;
return h >> 8;
}
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "__dns.h"
in_addr_t inet_network(const char *p)
{
struct sockaddr_in sin;
if (__ipparse(&sin, AF_INET, p)) return -1;
return ntohl(sin.sin_addr.s_addr);
}
#include <arpa/inet.h>
#include <stdio.h>
char *inet_ntoa(struct in_addr in)
{
static char buf[16];
unsigned char *a = (void *)&in;
snprintf(buf, sizeof buf, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
return buf;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册