From 1bcce7eed7a41aea705523063a947ec2c494b6d8 Mon Sep 17 00:00:00 2001 From: leitner Date: Mon, 5 Feb 2001 19:06:12 +0000 Subject: [PATCH] even more stuff --- .cvsignore | 1 + haven2i.h | 1 + socket/socket_getifidx.c | 13 ++++++++++ socket/socket_getifname.c | 21 ++++++++++++++++ socket/socket_local4.c | 18 ++++++++++++++ socket/socket_local6.c | 40 +++++++++++++++++++++++++++++++ socket/socket_remote4.c | 18 ++++++++++++++ socket/socket_remote6.c | 40 +++++++++++++++++++++++++++++++ socket/socket_send4.c | 16 +++++++++++++ socket/socket_send6.c | 39 ++++++++++++++++++++++++++++++ socket/socket_sendfile.c | 50 +++++++++++++++++++++++++++++++++++++++ 11 files changed, 257 insertions(+) create mode 100644 .cvsignore create mode 100644 haven2i.h create mode 100644 socket/socket_getifidx.c create mode 100644 socket/socket_getifname.c create mode 100644 socket/socket_local4.c create mode 100644 socket/socket_local6.c create mode 100644 socket/socket_remote4.c create mode 100644 socket/socket_remote6.c create mode 100644 socket/socket_send4.c create mode 100644 socket/socket_send6.c create mode 100644 socket/socket_sendfile.c diff --git a/.cvsignore b/.cvsignore new file mode 100644 index 0000000..718f4d2 --- /dev/null +++ b/.cvsignore @@ -0,0 +1 @@ +t diff --git a/haven2i.h b/haven2i.h new file mode 100644 index 0000000..fd50644 --- /dev/null +++ b/haven2i.h @@ -0,0 +1 @@ +#define HAVE_N2I diff --git a/socket/socket_getifidx.c b/socket/socket_getifidx.c new file mode 100644 index 0000000..c71fc48 --- /dev/null +++ b/socket/socket_getifidx.c @@ -0,0 +1,13 @@ +#include +#include +#include +#include "socket.h" +#include "haven2i.h" + +uint32 socket_getifidx(const char* ifname) { +#ifdef HAVE_N2I + return if_nametoindex(ifname); +#else + return 0; +#endif +} diff --git a/socket/socket_getifname.c b/socket/socket_getifname.c new file mode 100644 index 0000000..72b2d0c --- /dev/null +++ b/socket/socket_getifname.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include "socket.h" +#include "haven2i.h" + +#ifdef HAVE_N2I +static char ifname[IFNAMSIZ]; + +const char* socket_getifname(uint32 interface) { + char *tmp=if_indextoname(interface,ifname); + if (tmp) + return tmp; + else + return "[unknown]"; +} +#else +const char* socket_getifname(uint32 interface) { + return "[unknown]"; +} +#endif diff --git a/socket/socket_local4.c b/socket/socket_local4.c new file mode 100644 index 0000000..0a82496 --- /dev/null +++ b/socket/socket_local4.c @@ -0,0 +1,18 @@ +#include +#include +#include +#include +#include "byte.h" +#include "socket.h" + +int socket_local4(int s,char ip[4],uint16 *port) +{ + struct sockaddr_in si; + unsigned int len = sizeof si; + + if (getsockname(s,(struct sockaddr *) &si,&len) == -1) return -1; + *(uint32*)ip = *(uint32*)&si.sin_addr; + uint16_unpack_big((char *) &si.sin_port,port); + return 0; +} + diff --git a/socket/socket_local6.c b/socket/socket_local6.c new file mode 100644 index 0000000..ba1e01f --- /dev/null +++ b/socket/socket_local6.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include "byte.h" +#include "socket.h" +#include "ip6.h" +#include "haveip6.h" +#include "error.h" +#include "uint32.h" + +int socket_local6(int s,char ip[16],uint16 *port,uint32 *scope_id) +{ +#ifdef LIBC_HAS_IP6 + struct sockaddr_in6 si; +#else + struct sockaddr_in si; +#endif + unsigned int len = sizeof si; + + if (getsockname(s,(struct sockaddr *) &si,&len) == -1) return -1; +#ifdef LIBC_HAS_IP6 + if (si.sin6_family==AF_INET) { + struct sockaddr_in *si4=(struct sockaddr_in*)&si; + byte_copy(ip,12,V4mappedprefix); + byte_copy(ip+12,4,(char *) &si4->sin_addr); + uint16_unpack_big((char *) &si4->sin_port,port); + return 0; + } + byte_copy(ip,16,(char *) &si.sin6_addr); + uint16_unpack_big((char *) &si.sin6_port,port); + if (scope_id) *scope_id=si.sin6_scope_id; +#else + byte_copy(ip,12,V4mappedprefix); + byte_copy(ip+12,4,(char *) &si.sin_addr); + uint16_unpack_big((char *) &si.sin_port,port); + if (scope_id) *scope_id=0; +#endif + return 0; +} diff --git a/socket/socket_remote4.c b/socket/socket_remote4.c new file mode 100644 index 0000000..b3c99c8 --- /dev/null +++ b/socket/socket_remote4.c @@ -0,0 +1,18 @@ +#include +#include +#include +#include +#include "byte.h" +#include "socket.h" + +int socket_remote4(int s,char ip[4],uint16 *port) +{ + struct sockaddr_in si; + unsigned int len = sizeof si; + + if (getpeername(s,(struct sockaddr *) &si,&len) == -1) return -1; + *(uint32*)ip = *(uint32*)&si.sin_addr; + uint16_unpack_big((char *) &si.sin_port,port); + return 0; +} + diff --git a/socket/socket_remote6.c b/socket/socket_remote6.c new file mode 100644 index 0000000..6d80aff --- /dev/null +++ b/socket/socket_remote6.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include "byte.h" +#include "socket.h" +#include "ip6.h" +#include "haveip6.h" +#include "error.h" +#include "uint32.h" + +int socket_remote6(int s,char ip[16],uint16 *port,uint32 *scope_id) +{ +#ifdef LIBC_HAS_IP6 + struct sockaddr_in6 si; +#else + struct sockaddr_in si; +#endif + unsigned int len = sizeof si; + + if (getpeername(s,(struct sockaddr *) &si,&len) == -1) return -1; +#ifdef LIBC_HAS_IP6 + if (si.sin6_family==AF_INET) { + struct sockaddr_in *si4=(struct sockaddr_in*)&si; + byte_copy(ip,12,V4mappedprefix); + byte_copy(ip+12,4,(char *) &si4->sin_addr); + uint16_unpack_big((char *) &si4->sin_port,port); + return 0; + } + byte_copy(ip,16,(char *) &si.sin6_addr); + uint16_unpack_big((char *) &si.sin6_port,port); + if (scope_id) *scope_id=si.sin6_scope_id; +#else + byte_copy(ip,12,V4mappedprefix); + byte_copy(ip+12,4,(char *) &si.sin_addr); + uint16_unpack_big((char *) &si.sin_port,port); + if (scope_id) *scope_id=0; +#endif + return 0; +} diff --git a/socket/socket_send4.c b/socket/socket_send4.c new file mode 100644 index 0000000..e76a603 --- /dev/null +++ b/socket/socket_send4.c @@ -0,0 +1,16 @@ +#include +#include +#include +#include +#include "byte.h" +#include "socket.h" + +int socket_send4(int s,const char *buf,unsigned int len,const char ip[4],uint16 port) { + struct sockaddr_in si; + + byte_zero(&si,sizeof si); + si.sin_family = AF_INET; + uint16_pack_big((char*) &si.sin_port,port); + *((uint32*)&si.sin_addr) = *((uint32*)ip); + return sendto(s,buf,len,0,(struct sockaddr *) &si,sizeof si); +} diff --git a/socket/socket_send6.c b/socket/socket_send6.c new file mode 100644 index 0000000..75f23e8 --- /dev/null +++ b/socket/socket_send6.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include "byte.h" +#include "socket.h" +#include "ip6.h" +#include "haveip6.h" + +int socket_send6(int s,const char *buf,unsigned int len,const char ip[16],uint16 port,uint32 scope_id) +{ +#ifdef LIBC_HAS_IP6 + struct sockaddr_in6 si; +#else + struct sockaddr_in si; +#endif + + byte_zero(&si,sizeof si); +#ifdef LIBC_HAS_IP6 + if (noipv6) { +#endif + if (ip6_isv4mapped(ip)) + return socket_send4(s,buf,len,ip+12,port); + if (byte_equal(ip,16,V6loopback)) + return socket_send4(s,buf,len,ip4loopback,port); +#ifdef LIBC_HAS_IP6 + errno=EPROTO; + return -1; + } + si.sin6_family = AF_INET6; + uint16_pack_big((char *) &si.sin6_port,port); + byte_copy((char *) &si.sin6_addr,16,ip); + return sendto(s,buf,len,0,(struct sockaddr *) &si,sizeof si); +#else + errno=EPROTO; + return -1; +#endif +} diff --git a/socket/socket_sendfile.c b/socket/socket_sendfile.c new file mode 100644 index 0000000..8a674de --- /dev/null +++ b/socket/socket_sendfile.c @@ -0,0 +1,50 @@ +#include +#include "socket.h" + +#ifdef __linux__ + +#ifdef __GLIBC__ +#include +#else +#ifdef __dietlibc__ +#include +#else +#include +_syscall4(int,sendfile,int,out,int,in,long *,offset,unsigned long,count) +#endif +#endif + +int socket_sendfile(int out,int in,uint32 offset,uint32 bytes) { + return sendfile(out,in,&offset,bytes); +} + +#else + +#ifdef _HPUX_SOURCE + +/* http://www.devresource.hp.com/STK/man/10.30/sendfile_2.html */ +#include +int socket_sendfile(int out,int in,uint32 offset,uint32 bytes) { + return sendfile(out,in,offset,bytes,0,0); +} + +#else + +#define BUFSIZE 16384 + +int socket_sendfile(int out,int in,uint32 offset,uint32 bytes) { + char buf[BUFSIZE]; + int n,m; + int sent=0; + if (lseek(in,offset,SEEK_SET) == -1) + return -1; + if ((n=read(in,buf,(bytes