1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "u.h"
#include "lib.h"
#include "dat.h"
#include "fns.h"
#include "error.h"
#include "ip.h"
#include "devip.h"
#undef listen
#undef accept
#undef bind
static int
family(unsigned char *addr)
{
if(isv4(addr))
return AF_INET;
return AF_INET6;
}
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;
}
void
osipinit(void)
{
char buf[1024];
gethostname(buf, sizeof(buf));
kstrdup(&sysname, buf);
}
int
so_socket(int type, unsigned char *addr)
{
int fd, one;
switch(type) {
default:
error("bad protocol type");
case S_TCP:
type = SOCK_STREAM;
break;
case S_UDP:
type = SOCK_DGRAM;
break;
}
fd = socket(family(addr), type, 0);
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
so_connect(int fd, unsigned char *raddr, unsigned short rport)
{
struct sockaddr_storage ss;
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;
}
if(connect(fd, (struct sockaddr*)&ss, addrlen(&ss)) < 0)
oserror();
}
void
so_getsockname(int fd, unsigned char *laddr, unsigned short *lport)
{
socklen_t len;
struct sockaddr_storage ss;
len = sizeof(ss);
if(getsockname(fd, (struct sockaddr*)&ss, &len) < 0)
oserror();
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");
}
}
void
so_listen(int fd)
{
if(listen(fd, 5) < 0)
oserror();
}
int
so_accept(int fd, unsigned char *raddr, unsigned short *rport)
{
int nfd;
socklen_t len;
struct sockaddr_storage ss;
len = sizeof(ss);
nfd = accept(fd, (struct sockaddr*)&ss, &len);
if(nfd < 0)
oserror();
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");
}
return nfd;
}
void
so_bind(int fd, int su, unsigned short port, unsigned char *addr)
{
int i, one;
struct sockaddr_storage ss;
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++) {
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;
}
if(bind(fd, (struct sockaddr*)&ss, addrlen(&ss)) >= 0)
return;
}
oserror();
}
memset(&ss, 0, sizeof(ss));
ss.ss_family = family(addr);
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;
}
if(bind(fd, (struct sockaddr*)&ss, addrlen(&ss)) < 0)
oserror();
}
int
so_gethostbyname(char *host, char **hostv, int n)
{
char buf[INET6_ADDRSTRLEN];
struct addrinfo *r, *p;
int i;
r = NULL;
if(getaddrinfo(host, NULL, NULL, &r))
return 0;
for(i = 0, p = r; i < n && p != NULL; p = p->ai_next){
switch (p->ai_family) {
default:
continue;
case AF_INET:
inet_ntop(AF_INET, &((struct sockaddr_in*)p->ai_addr)->sin_addr, buf, sizeof(buf));
break;
case AF_INET6:
inet_ntop(AF_INET6, &((struct sockaddr_in6*)p->ai_addr)->sin6_addr, buf, sizeof(buf));
break;
}
hostv[i++] = strdup(buf);
}
freeaddrinfo(r);
return i;
}
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)
{
return send(fd, d, n, f);
}
int
so_recv(int fd, void *d, int n, int f)
{
return recv(fd, d, n, f);
}
|