Browse Source

even more stuff

master
Felix von Leitner 21 years ago
parent
commit
d406554aa0
  1. 1
      .cvsignore
  2. 1
      haven2i.h
  3. 13
      socket/socket_getifidx.c
  4. 21
      socket/socket_getifname.c
  5. 18
      socket/socket_local4.c
  6. 40
      socket/socket_local6.c
  7. 18
      socket/socket_remote4.c
  8. 40
      socket/socket_remote6.c
  9. 16
      socket/socket_send4.c
  10. 39
      socket/socket_send6.c
  11. 50
      socket/socket_sendfile.c

1
.cvsignore

@ -0,0 +1 @@
t

1
haven2i.h

@ -0,0 +1 @@
#define HAVE_N2I

13
socket/socket_getifidx.c

@ -0,0 +1,13 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include "socket.h"
#include "haven2i.h"
uint32 socket_getifidx(const char* ifname) {
#ifdef HAVE_N2I
return if_nametoindex(ifname);
#else
return 0;
#endif
}

21
socket/socket_getifname.c

@ -0,0 +1,21 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#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

18
socket/socket_local4.c

@ -0,0 +1,18 @@
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#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;
}

40
socket/socket_local6.c

@ -0,0 +1,40 @@
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#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;
}

18
socket/socket_remote4.c

@ -0,0 +1,18 @@
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#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;
}

40
socket/socket_remote6.c

@ -0,0 +1,40 @@
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#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;
}

16
socket/socket_send4.c

@ -0,0 +1,16 @@
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#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);
}

39
socket/socket_send6.c

@ -0,0 +1,39 @@
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#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
}

50
socket/socket_sendfile.c

@ -0,0 +1,50 @@
#include <unistd.h>
#include "socket.h"
#ifdef __linux__
#ifdef __GLIBC__
#include <sys/sendfile.h>
#else
#ifdef __dietlibc__
#include <sys/sendfile.h>
#else
#include <linux/unistd.h>
_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 <sys/socket.h>
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<BUFSIZE)?bytes:BUFSIZE))<0)
return (sent?sent:-1);
if ((m=write(out,buf,n))<0)
return -1;
return n;
}
#endif
#endif
Loading…
Cancel
Save