2005-08-08 16:50:13 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <netdb.h>
|
2012-08-03 23:30:17 +04:00
|
|
|
#include <arpa/inet.h>
|
2005-08-08 16:50:13 +04:00
|
|
|
|
|
|
|
#include "u.h"
|
|
|
|
#include "lib.h"
|
|
|
|
#include "dat.h"
|
|
|
|
#include "fns.h"
|
|
|
|
#include "error.h"
|
2012-08-03 23:30:17 +04:00
|
|
|
#include "ip.h"
|
2005-08-08 16:50:13 +04:00
|
|
|
|
|
|
|
#include "devip.h"
|
|
|
|
|
|
|
|
#undef listen
|
|
|
|
#undef accept
|
|
|
|
#undef bind
|
|
|
|
|
2012-08-03 23:30:17 +04:00
|
|
|
static int
|
|
|
|
family(unsigned char *addr)
|
|
|
|
{
|
|
|
|
if(isv4(addr))
|
|
|
|
return AF_INET;
|
|
|
|
return AF_INET6;
|
|
|
|
}
|
|
|
|
|
2012-09-10 21:51:39 +04:00
|
|
|
static int
|
|
|
|
addrlen(struct sockaddr_storage *ss)
|
|
|
|
{
|
|
|
|
switch(ss->ss_family){
|
|
|
|
case AF_INET:
|
|
|
|
return sizeof(struct sockaddr_in);
|
|
|
|
case AF_INET6:
|
|
|
|
return sizeof(struct sockaddr_in6);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-08 16:50:13 +04:00
|
|
|
void
|
|
|
|
osipinit(void)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
gethostname(buf, sizeof(buf));
|
|
|
|
kstrdup(&sysname, buf);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-08-03 23:30:17 +04:00
|
|
|
so_socket(int type, unsigned char *addr)
|
2005-08-08 16:50:13 +04:00
|
|
|
{
|
|
|
|
int fd, one;
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
default:
|
|
|
|
error("bad protocol type");
|
|
|
|
case S_TCP:
|
|
|
|
type = SOCK_STREAM;
|
|
|
|
break;
|
|
|
|
case S_UDP:
|
|
|
|
type = SOCK_DGRAM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-08-03 23:30:17 +04:00
|
|
|
fd = socket(family(addr), type, 0);
|
2005-08-08 16:50:13 +04:00
|
|
|
if(fd < 0)
|
|
|
|
oserror();
|
|
|
|
|
|
|
|
one = 1;
|
|
|
|
if(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof(one)) > 0){
|
|
|
|
oserrstr();
|
|
|
|
print("setsockopt: %r");
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-03 23:30:17 +04:00
|
|
|
so_connect(int fd, unsigned char *raddr, unsigned short rport)
|
2005-08-08 16:50:13 +04:00
|
|
|
{
|
2012-08-03 23:30:17 +04:00
|
|
|
struct sockaddr_storage ss;
|
2005-08-08 16:50:13 +04:00
|
|
|
|
2012-08-03 23:30:17 +04:00
|
|
|
memset(&ss, 0, sizeof(ss));
|
|
|
|
|
|
|
|
ss.ss_family = family(raddr);
|
|
|
|
|
|
|
|
switch(ss.ss_family){
|
|
|
|
case AF_INET:
|
|
|
|
hnputs(&((struct sockaddr_in*)&ss)->sin_port, rport);
|
|
|
|
v6tov4((unsigned char*)&((struct sockaddr_in*)&ss)->sin_addr.s_addr, raddr);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
hnputs(&((struct sockaddr_in6*)&ss)->sin6_port, rport);
|
|
|
|
memcpy(&((struct sockaddr_in6*)&ss)->sin6_addr.s6_addr, raddr, sizeof(struct in6_addr));
|
|
|
|
break;
|
|
|
|
}
|
2005-08-08 16:50:13 +04:00
|
|
|
|
2012-09-10 21:51:39 +04:00
|
|
|
if(connect(fd, (struct sockaddr*)&ss, addrlen(&ss)) < 0)
|
2005-08-08 16:50:13 +04:00
|
|
|
oserror();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-03 23:30:17 +04:00
|
|
|
so_getsockname(int fd, unsigned char *laddr, unsigned short *lport)
|
2005-08-08 16:50:13 +04:00
|
|
|
{
|
2005-12-30 02:15:58 +03:00
|
|
|
socklen_t len;
|
2012-08-03 23:30:17 +04:00
|
|
|
struct sockaddr_storage ss;
|
2005-08-08 16:50:13 +04:00
|
|
|
|
2012-08-03 23:30:17 +04:00
|
|
|
len = sizeof(ss);
|
|
|
|
if(getsockname(fd, (struct sockaddr*)&ss, &len) < 0)
|
2005-08-08 16:50:13 +04:00
|
|
|
oserror();
|
|
|
|
|
2012-08-03 23:30:17 +04:00
|
|
|
switch(ss.ss_family){
|
|
|
|
case AF_INET:
|
|
|
|
v4tov6(laddr, (unsigned char*)&((struct sockaddr_in*)&ss)->sin_addr.s_addr);
|
|
|
|
*lport = nhgets(&((struct sockaddr_in*)&ss)->sin_port);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
memcpy(laddr, &((struct sockaddr_in6*)&ss)->sin6_addr.s6_addr, sizeof(struct in6_addr));
|
|
|
|
*lport = nhgets(&((struct sockaddr_in6*)&ss)->sin6_port);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("not AF_INET or AF_INET6");
|
|
|
|
}
|
2005-08-08 16:50:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
so_listen(int fd)
|
|
|
|
{
|
|
|
|
if(listen(fd, 5) < 0)
|
|
|
|
oserror();
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-08-03 23:30:17 +04:00
|
|
|
so_accept(int fd, unsigned char *raddr, unsigned short *rport)
|
2005-08-08 16:50:13 +04:00
|
|
|
{
|
2005-11-07 20:34:39 +03:00
|
|
|
int nfd;
|
2005-12-30 02:15:58 +03:00
|
|
|
socklen_t len;
|
2012-08-03 23:30:17 +04:00
|
|
|
struct sockaddr_storage ss;
|
2005-08-08 16:50:13 +04:00
|
|
|
|
2012-08-03 23:30:17 +04:00
|
|
|
len = sizeof(ss);
|
|
|
|
nfd = accept(fd, (struct sockaddr*)&ss, &len);
|
2005-08-08 16:50:13 +04:00
|
|
|
if(nfd < 0)
|
|
|
|
oserror();
|
|
|
|
|
2012-08-03 23:30:17 +04:00
|
|
|
switch(ss.ss_family){
|
|
|
|
case AF_INET:
|
|
|
|
v4tov6(raddr, (unsigned char*)&((struct sockaddr_in*)&ss)->sin_addr.s_addr);
|
|
|
|
*rport = nhgets(&((struct sockaddr_in*)&ss)->sin_port);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
memcpy(raddr, &((struct sockaddr_in6*)&ss)->sin6_addr.s6_addr, sizeof(struct in6_addr));
|
|
|
|
*rport = nhgets(&((struct sockaddr_in6*)&ss)->sin6_port);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("not AF_INET or AF_INET6");
|
|
|
|
}
|
2005-08-08 16:50:13 +04:00
|
|
|
return nfd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-03 23:30:17 +04:00
|
|
|
so_bind(int fd, int su, unsigned short port, unsigned char *addr)
|
2005-08-08 16:50:13 +04:00
|
|
|
{
|
|
|
|
int i, one;
|
2012-08-03 23:30:17 +04:00
|
|
|
struct sockaddr_storage ss;
|
2005-08-08 16:50:13 +04:00
|
|
|
|
|
|
|
one = 1;
|
|
|
|
if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&one, sizeof(one)) < 0){
|
|
|
|
oserrstr();
|
|
|
|
print("setsockopt: %r");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(su) {
|
|
|
|
for(i = 600; i < 1024; i++) {
|
2012-08-03 23:30:17 +04:00
|
|
|
memset(&ss, 0, sizeof(ss));
|
|
|
|
ss.ss_family = family(addr);
|
|
|
|
|
|
|
|
switch(ss.ss_family){
|
|
|
|
case AF_INET:
|
|
|
|
((struct sockaddr_in*)&ss)->sin_port = i;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
((struct sockaddr_in6*)&ss)->sin6_port = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-09-10 21:51:39 +04:00
|
|
|
if(bind(fd, (struct sockaddr*)&ss, addrlen(&ss)) >= 0)
|
2005-08-08 16:50:13 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
oserror();
|
|
|
|
}
|
|
|
|
|
2012-08-03 23:30:17 +04:00
|
|
|
memset(&ss, 0, sizeof(ss));
|
|
|
|
ss.ss_family = family(addr);
|
2005-08-08 16:50:13 +04:00
|
|
|
|
2012-08-03 23:30:17 +04:00
|
|
|
switch(ss.ss_family){
|
|
|
|
case AF_INET:
|
|
|
|
hnputs(&((struct sockaddr_in*)&ss)->sin_port, port);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
hnputs(&((struct sockaddr_in6*)&ss)->sin6_port, port);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-09-10 21:51:39 +04:00
|
|
|
if(bind(fd, (struct sockaddr*)&ss, addrlen(&ss)) < 0)
|
2005-08-08 16:50:13 +04:00
|
|
|
oserror();
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
so_gethostbyname(char *host, char**hostv, int n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char buf[32];
|
|
|
|
unsigned char *p;
|
|
|
|
struct hostent *hp;
|
|
|
|
|
|
|
|
hp = gethostbyname(host);
|
|
|
|
if(hp == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for(i = 0; hp->h_addr_list[i] && i < n; i++) {
|
|
|
|
p = (unsigned char*)hp->h_addr_list[i];
|
|
|
|
sprint(buf, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
|
|
|
|
hostv[i] = strdup(buf);
|
|
|
|
if(hostv[i] == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
hostlookup(char *host)
|
|
|
|
{
|
2012-08-03 23:30:17 +04:00
|
|
|
char buf[INET6_ADDRSTRLEN];
|
2005-08-08 16:50:13 +04:00
|
|
|
uchar *p;
|
|
|
|
struct hostent *he;
|
2012-08-03 23:30:17 +04:00
|
|
|
struct addrinfo *result;
|
2005-08-08 16:50:13 +04:00
|
|
|
|
|
|
|
he = gethostbyname(host);
|
|
|
|
if(he != 0 && he->h_addr_list[0]) {
|
|
|
|
p = (uchar*)he->h_addr_list[0];
|
|
|
|
sprint(buf, "%ud.%ud.%ud.%ud", p[0], p[1], p[2], p[3]);
|
2012-08-03 23:30:17 +04:00
|
|
|
} else if(getaddrinfo(host, NULL, NULL, &result) == 0) {
|
|
|
|
switch (result->ai_family) {
|
|
|
|
case AF_INET:
|
|
|
|
inet_ntop(AF_INET, &((struct sockaddr_in*)result->ai_addr)->sin_addr, buf, sizeof(buf));
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
inet_ntop(AF_INET6, &((struct sockaddr_in6*)result->ai_addr)->sin6_addr, buf, sizeof(buf));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return nil;
|
|
|
|
}
|
2005-08-08 16:50:13 +04:00
|
|
|
} else
|
2012-08-03 23:30:17 +04:00
|
|
|
return nil;
|
2005-08-08 16:50:13 +04:00
|
|
|
|
|
|
|
return strdup(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
so_getservbyname(char *service, char *net, char *port)
|
|
|
|
{
|
|
|
|
struct servent *s;
|
|
|
|
|
|
|
|
s = getservbyname(service, net);
|
|
|
|
if(s == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
sprint(port, "%d", nhgets(&s->s_port));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
so_send(int fd, void *d, int n, int f)
|
|
|
|
{
|
2005-11-07 20:13:41 +03:00
|
|
|
return send(fd, d, n, f);
|
2005-08-08 16:50:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
so_recv(int fd, void *d, int n, int f)
|
|
|
|
{
|
|
|
|
return recv(fd, d, n, f);
|
|
|
|
}
|