extended socket API; you can now pass NULL to ignore results you don't

need
master
leitner 22 years ago
parent f828d295ce
commit 53b201046e

@ -17,6 +17,7 @@
fix typo breaking fmt_long for dest==NULL fix typo breaking fmt_long for dest==NULL
add fmt_*longlong() add fmt_*longlong()
add range check to scan_ulong, scan_ulonglong add range check to scan_ulong, scan_ulonglong
extended socket API; you can now pass NULL for results you don't want
0.14: 0.14:
avoid bus errors in byte_copy avoid bus errors in byte_copy

@ -8,7 +8,7 @@
#include "haveip6.h" #include "haveip6.h"
#include "havesl.h" #include "havesl.h"
int socket_accept6(int s,char ip[16],uint16 *port,uint32 *scope_id) int socket_accept6(int s,char* ip,uint16* port,uint32* scope_id)
{ {
#ifdef LIBC_HAS_IP6 #ifdef LIBC_HAS_IP6
struct sockaddr_in6 sa; struct sockaddr_in6 sa;
@ -24,20 +24,24 @@ int socket_accept6(int s,char ip[16],uint16 *port,uint32 *scope_id)
#ifdef LIBC_HAS_IP6 #ifdef LIBC_HAS_IP6
if (sa.sin6_family==AF_INET) { if (sa.sin6_family==AF_INET) {
struct sockaddr_in *sa4=(struct sockaddr_in*)&sa; struct sockaddr_in *sa4=(struct sockaddr_in*)&sa;
if (ip) {
byte_copy(ip,12,V4mappedprefix); byte_copy(ip,12,V4mappedprefix);
byte_copy(ip+12,4,(char *) &sa4->sin_addr); byte_copy(ip+12,4,(char *) &sa4->sin_addr);
uint16_unpack_big((char *) &sa4->sin_port,port); }
if (port) uint16_unpack_big((char *) &sa4->sin_port,port);
return fd; return fd;
} }
byte_copy(ip,16,(char *) &sa.sin6_addr); if (ip) byte_copy(ip,16,(char *) &sa.sin6_addr);
uint16_unpack_big((char *) &sa.sin6_port,port); if (port) uint16_unpack_big((char *) &sa.sin6_port,port);
if (scope_id) *scope_id=sa.sin6_scope_id; if (scope_id) *scope_id=sa.sin6_scope_id;
return fd; return fd;
#else #else
if (ip) {
byte_copy(ip,12,V4mappedprefix); byte_copy(ip,12,V4mappedprefix);
byte_copy(ip+12,4,(char *) &sa.sin_addr); byte_copy(ip+12,4,(char *) &sa.sin_addr);
uint16_unpack_big((char *) &sa.sin_port,port); }
if (port) uint16_unpack_big((char *) &sa.sin_port,port);
if (scope_id) *scope_id=0; if (scope_id) *scope_id=0;
return fd; return fd;
#endif #endif

@ -11,6 +11,9 @@ int socket_bind4(int s,const char *ip,uint16 port) {
byte_zero(&si,sizeof si); byte_zero(&si,sizeof si);
si.sin_family = AF_INET; si.sin_family = AF_INET;
uint16_pack_big((char*) &si.sin_port,port); uint16_pack_big((char*) &si.sin_port,port);
if (ip)
*(uint32*)&si.sin_addr = *(uint32*)ip; *(uint32*)&si.sin_addr = *(uint32*)ip;
else
si.sin_addr.s_addr=INADDR_ANY;
return bind(s,(struct sockaddr*)&si,sizeof si); return bind(s,(struct sockaddr*)&si,sizeof si);
} }

@ -11,7 +11,11 @@ int socket_bind6(int s,const char ip[16],uint16 port,uint32 scope_id)
{ {
#ifdef LIBC_HAS_IP6 #ifdef LIBC_HAS_IP6
struct sockaddr_in6 sa; struct sockaddr_in6 sa;
#endif
if (!ip) ip=V6any;
#ifdef LIBC_HAS_IP6
if (noipv6) { if (noipv6) {
#endif #endif
int i; int i;

@ -12,8 +12,8 @@ int socket_local4(int s,char ip[4],uint16 *port)
socklen_t len = sizeof si; socklen_t len = sizeof si;
if (getsockname(s,(struct sockaddr *) &si,&len) == -1) return -1; if (getsockname(s,(struct sockaddr *) &si,&len) == -1) return -1;
*(uint32*)ip = *(uint32*)&si.sin_addr; if (ip) *(uint32*)ip = *(uint32*)&si.sin_addr;
uint16_unpack_big((char *) &si.sin_port,port); if (port) uint16_unpack_big((char *) &si.sin_port,port);
return 0; return 0;
} }

@ -22,18 +22,22 @@ int socket_local6(int s,char ip[16],uint16 *port,uint32 *scope_id)
#ifdef LIBC_HAS_IP6 #ifdef LIBC_HAS_IP6
if (si.sin6_family==AF_INET) { if (si.sin6_family==AF_INET) {
struct sockaddr_in *si4=(struct sockaddr_in*)&si; struct sockaddr_in *si4=(struct sockaddr_in*)&si;
if (ip) {
byte_copy(ip,12,V4mappedprefix); byte_copy(ip,12,V4mappedprefix);
byte_copy(ip+12,4,(char *) &si4->sin_addr); byte_copy(ip+12,4,(char *) &si4->sin_addr);
uint16_unpack_big((char *) &si4->sin_port,port); }
if (port) uint16_unpack_big((char *) &si4->sin_port,port);
return 0; return 0;
} }
byte_copy(ip,16,(char *) &si.sin6_addr); if (ip) byte_copy(ip,16,(char *) &si.sin6_addr);
uint16_unpack_big((char *) &si.sin6_port,port); if (port) uint16_unpack_big((char *) &si.sin6_port,port);
if (scope_id) *scope_id=si.sin6_scope_id; if (scope_id) *scope_id=si.sin6_scope_id;
#else #else
if (ip) {
byte_copy(ip,12,V4mappedprefix); byte_copy(ip,12,V4mappedprefix);
byte_copy(ip+12,4,(char *) &si.sin_addr); byte_copy(ip+12,4,(char *) &si.sin_addr);
uint16_unpack_big((char *) &si.sin_port,port); }
if (port) uint16_unpack_big((char *) &si.sin_port,port);
if (scope_id) *scope_id=0; if (scope_id) *scope_id=0;
#endif #endif
return 0; return 0;

@ -11,7 +11,7 @@ int socket_recv4(int s,char *buf,unsigned int len,char ip[4],uint16 *port) {
int r; int r;
if ((r = recvfrom(s,buf,len,0,(struct sockaddr *) &si,&Len))<0) return -1; if ((r = recvfrom(s,buf,len,0,(struct sockaddr *) &si,&Len))<0) return -1;
*(uint32*)ip = *(uint32*)&si.sin_addr; if (ip) *(uint32*)ip = *(uint32*)&si.sin_addr;
uint16_unpack_big((char *) &si.sin_port,port); if (port) uint16_unpack_big((char *) &si.sin_port,port);
return r; return r;
} }

@ -24,18 +24,22 @@ int socket_recv6(int s,char *buf,unsigned int len,char ip[16],uint16 *port,uint3
#ifdef LIBC_HAS_IP6 #ifdef LIBC_HAS_IP6
if (noipv6) { if (noipv6) {
struct sockaddr_in *si4=(struct sockaddr_in *)&si; struct sockaddr_in *si4=(struct sockaddr_in *)&si;
if (ip) {
byte_copy(ip,12,V4mappedprefix); byte_copy(ip,12,V4mappedprefix);
byte_copy(ip+12,4,(char *) &si4->sin_addr); byte_copy(ip+12,4,(char *) &si4->sin_addr);
uint16_unpack_big((char *) &si4->sin_port,port); }
if (port) uint16_unpack_big((char *) &si4->sin_port,port);
return r; return r;
} }
byte_copy(ip,16,(char *) &si.sin6_addr); if (ip) byte_copy(ip,16,(char *) &si.sin6_addr);
uint16_unpack_big((char *) &si.sin6_port,port); if (port) uint16_unpack_big((char *) &si.sin6_port,port);
if (scope_id) *scope_id=si.sin6_scope_id; if (scope_id) *scope_id=si.sin6_scope_id;
#else #else
if (ip) {
byte_copy(ip,12,(char *)V4mappedprefix); byte_copy(ip,12,(char *)V4mappedprefix);
byte_copy(ip+12,4,(char *) &si.sin_addr); byte_copy(ip+12,4,(char *) &si.sin_addr);
uint16_unpack_big((char *) &si.sin_port,port); }
if (port) uint16_unpack_big((char *) &si.sin_port,port);
if (scope_id) *scope_id=0; if (scope_id) *scope_id=0;
#endif #endif

@ -12,8 +12,8 @@ int socket_remote4(int s,char ip[4],uint16 *port)
socklen_t len = sizeof si; socklen_t len = sizeof si;
if (getpeername(s,(struct sockaddr *) &si,&len) == -1) return -1; if (getpeername(s,(struct sockaddr *) &si,&len) == -1) return -1;
*(uint32*)ip = *(uint32*)&si.sin_addr; if (ip) *(uint32*)ip = *(uint32*)&si.sin_addr;
uint16_unpack_big((char *) &si.sin_port,port); if (port) uint16_unpack_big((char *) &si.sin_port,port);
return 0; return 0;
} }

@ -22,18 +22,22 @@ int socket_remote6(int s,char ip[16],uint16 *port,uint32 *scope_id)
#ifdef LIBC_HAS_IP6 #ifdef LIBC_HAS_IP6
if (si.sin6_family==AF_INET) { if (si.sin6_family==AF_INET) {
struct sockaddr_in *si4=(struct sockaddr_in*)&si; struct sockaddr_in *si4=(struct sockaddr_in*)&si;
if (ip) {
byte_copy(ip,12,V4mappedprefix); byte_copy(ip,12,V4mappedprefix);
byte_copy(ip+12,4,(char *) &si4->sin_addr); byte_copy(ip+12,4,(char *) &si4->sin_addr);
uint16_unpack_big((char *) &si4->sin_port,port); }
if (port) uint16_unpack_big((char *) &si4->sin_port,port);
return 0; return 0;
} }
byte_copy(ip,16,(char *) &si.sin6_addr); if (ip) byte_copy(ip,16,(char *) &si.sin6_addr);
uint16_unpack_big((char *) &si.sin6_port,port); if (port) uint16_unpack_big((char *) &si.sin6_port,port);
if (scope_id) *scope_id=si.sin6_scope_id; if (scope_id) *scope_id=si.sin6_scope_id;
#else #else
if (ip) {
byte_copy(ip,12,V4mappedprefix); byte_copy(ip,12,V4mappedprefix);
byte_copy(ip+12,4,(char *) &si.sin_addr); byte_copy(ip+12,4,(char *) &si.sin_addr);
uint16_unpack_big((char *) &si.sin_port,port); }
if (port) uint16_unpack_big((char *) &si.sin_port,port);
if (scope_id) *scope_id=0; if (scope_id) *scope_id=0;
#endif #endif
return 0; return 0;

Loading…
Cancel
Save