Browse Source

make socket_(tc|ud)p[46] actually return non-blocking sockets as

documented (Richard Lyons)
master
Felix von Leitner 16 years ago
parent
commit
071df9ee4a
  1. 2
      CHANGES
  2. 5
      cdb.h
  3. 28
      cdb/cdb.c
  4. 9
      cdb/cdb_make.c
  5. 5
      cdb_make.h
  6. 2
      io/iob_prefetch.c
  7. 3
      mmap.h
  8. 3
      mmap/mmap_private.c
  9. 3
      mmap/mmap_read.c
  10. 3
      mmap/mmap_shared.c
  11. 2
      scan/scan_httpdate.c
  12. 8
      socket/socket_tcp4.c
  13. 3
      socket/socket_tcp6.c
  14. 8
      socket/socket_udp4.c
  15. 1
      socket/socket_udp6.c
  16. 2
      test/client.c
  17. 2
      test/dllink.c
  18. 2
      test/httpd.c
  19. 2
      test/io5.c
  20. 2
      test/proxy.c
  21. 3
      test/server.c
  22. 1
      test/vd.c
  23. 18
      textcode.h
  24. 23
      textcode/fmt_ldapescape.c
  25. 24
      textcode/scan_ldapescape.c

2
CHANGES

@ -12,6 +12,8 @@
add cdb
add rangecheck.h
add io_block
make socket_(tc|ud)p[46] actually return non-blocking sockets as
documented (Richard Lyons)
0.24:
fix scan_to_sa (Tim Lorenz)

5
cdb.h

@ -2,6 +2,7 @@
#define CDB_H
#include "uint32.h"
#include "uint64.h"
#define CDB_HASHSTART 5381
extern uint32 cdb_hashadd(uint32 h,unsigned char c);
@ -9,7 +10,7 @@ extern uint32 cdb_hash(const unsigned char *buf,unsigned long int len);
struct cdb {
char *map; /* 0 if no map is available */
int fd;
int64 fd;
uint32 size; /* initialized if map is nonzero */
uint32 loop; /* number of hash slots searched under this key */
uint32 khash; /* initialized if loop is nonzero */
@ -21,7 +22,7 @@ struct cdb {
} ;
extern void cdb_free(struct cdb *);
extern void cdb_init(struct cdb *,int fd);
extern void cdb_init(struct cdb *,int64 fd);
extern int cdb_read(struct cdb *,unsigned char *,unsigned long int,uint32);

28
cdb/cdb.c

@ -2,15 +2,23 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <errno.h>
#include "byte.h"
#include "cdb.h"
#ifdef __MINGW32__
#include "windows.h"
#else
#include <sys/mman.h>
#endif
void cdb_free(struct cdb *c) {
if (c->map) {
#ifdef __MINGW32__
UnmapViewOfFile(c->map);
#else
munmap(c->map,c->size);
#endif
c->map = 0;
}
}
@ -19,14 +27,25 @@ void cdb_findstart(struct cdb *c) {
c->loop = 0;
}
void cdb_init(struct cdb *c,int fd) {
void cdb_init(struct cdb *c,int64 fd) {
#ifndef __MINGW32__
struct stat st;
char *x;
#endif
cdb_free(c);
cdb_findstart(c);
c->fd = fd;
#ifdef __MINGW32__
{
HANDLE m=CreateFileMapping((HANDLE)(uintptr_t)fd,0,PAGE_READONLY,0,0,NULL);
if (m)
if ((c->map=MapViewOfFile(m,FILE_MAP_READ,0,0,0)))
c->size=GetFileSize((HANDLE)(uintptr_t)fd,NULL);
CloseHandle(m);
}
#else
if (fstat(fd,&st) == 0)
if (st.st_size <= 0xffffffff) {
x = mmap(0,st.st_size,PROT_READ,MAP_SHARED,fd,0);
@ -35,6 +54,7 @@ void cdb_init(struct cdb *c,int fd) {
c->map = x;
}
}
#endif
}
int cdb_read(struct cdb *c,unsigned char *buf,unsigned long len,uint32 pos) {
@ -58,7 +78,11 @@ int cdb_read(struct cdb *c,unsigned char *buf,unsigned long len,uint32 pos) {
return 0;
FORMAT:
#ifdef EPROTO
errno = EPROTO;
#else
errno = EINVAL;
#endif
return -1;
}

9
cdb/cdb_make.c

@ -5,8 +5,11 @@
#include <errno.h>
#include "cdb.h"
#include "cdb_make.h"
#ifdef __MINGW32__
#include "windows.h"
#endif
int cdb_make_start(struct cdb_make *c,int fd) {
int cdb_make_start(struct cdb_make *c,int64 fd) {
c->head = 0;
c->split = 0;
c->hash = 0;
@ -14,7 +17,11 @@ int cdb_make_start(struct cdb_make *c,int fd) {
c->fd = fd;
c->pos = sizeof c->final;
buffer_init(&c->b,(void*)write,fd,c->bspace,sizeof c->bspace);
#ifdef __MINGW32__
return SetFilePointer((HANDLE)(uintptr_t)fd,c->pos,0,FILE_BEGIN);
#else
return lseek(fd,c->pos,SEEK_SET);
#endif
}
static int posplus(struct cdb_make *c,uint32 len) {

5
cdb_make.h

@ -2,6 +2,7 @@
#define CDB_MAKE_H
#include "buffer.h"
#include "uint64.h"
#include "uint32.h"
#define CDB_HPLIST 1000
@ -25,10 +26,10 @@ struct cdb_make {
uint32 numentries;
buffer b;
uint32 pos;
int fd;
int64 fd;
} ;
extern int cdb_make_start(struct cdb_make *,int);
extern int cdb_make_start(struct cdb_make *,int64);
extern int cdb_make_addbegin(struct cdb_make *,unsigned long int,unsigned long int);
extern int cdb_make_addend(struct cdb_make *,unsigned long int,unsigned long int,uint32);
extern int cdb_make_add(struct cdb_make *,const unsigned char *,unsigned long int,const unsigned char *,unsigned long int);

2
io/iob_prefetch.c

@ -3,7 +3,7 @@
#ifdef __MINGW32__
/* not supported */
void iob_internal(io_batch* b,uint64 bytes) {
void iob_prefetch(io_batch* b,uint64 bytes) {
(void)b;
(void)bytes;
}

3
mmap.h

@ -14,4 +14,7 @@ char* mmap_private(const char *filename,unsigned long* filesize);
* length of map in filesize and return pointer to map. */
char* mmap_shared(const char *filename,unsigned long* filesize);
/* unmap a mapped region */
int mmap_unmap(char* mapped,unsigned long maplen);
#endif

3
mmap/mmap_private.c

@ -18,7 +18,8 @@ char* mmap_private(const char* filename,unsigned long* filesize) {
m=CreateFileMapping(fd,0,PAGE_WRITECOPY,0,0,NULL);
map=0;
if (m)
map=MapViewOfFile(m,FILE_MAP_COPY,0,0,0);
if ((map=MapViewOfFile(m,FILE_MAP_COPY,0,0,0)))
*filesize=GetFileSize(fd,NULL);
CloseHandle(m);
CloseHandle(fd);
return map;

3
mmap/mmap_read.c

@ -18,7 +18,8 @@ extern char* mmap_read(const char* filename,unsigned long* filesize) {
m=CreateFileMapping(fd,0,PAGE_READONLY,0,0,NULL);
map=0;
if (m)
map=MapViewOfFile(m,FILE_MAP_READ,0,0,0);
if ((map=MapViewOfFile(m,FILE_MAP_READ,0,0,0)))
*filesize=GetFileSize(fd,NULL);
CloseHandle(m);
CloseHandle(fd);
return map;

3
mmap/mmap_shared.c

@ -18,7 +18,8 @@ extern char* mmap_shared(const char* filename,unsigned long* filesize) {
m=CreateFileMapping(fd,0,PAGE_READWRITE,0,0,NULL);
map=0;
if (m)
map=MapViewOfFile(m,FILE_MAP_WRITE,0,0,0);
if ((map=MapViewOfFile(m,FILE_MAP_WRITE,0,0,0)))
*filesize=GetFileSize(fd,NULL);
CloseHandle(m);
CloseHandle(fd);
return map;

2
scan/scan_httpdate.c

@ -63,6 +63,8 @@ done:
x.tm_wday=x.tm_yday=x.tm_isdst=0;
#if defined(__dietlibc__) || defined(__GLIBC__)
*t=timegm(&x);
#elif defined(__MINGW32__)
*t=mktime(&x);
#else
{
#ifdef sgi

8
socket/socket_tcp4.c

@ -1,12 +1,18 @@
#include <sys/types.h>
#ifndef __MINGW32__
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "socket.h"
#include "ndelay.h"
int socket_tcp4(void) {
int s;
__winsock_init();
return winsock2errno(socket(PF_INET,SOCK_STREAM,IPPROTO_TCP));
s = winsock2errno(socket(PF_INET,SOCK_STREAM,IPPROTO_TCP));
if (s == -1) return -1;
if (ndelay_on(s) == -1) { close(s); return -1; }
return s;
}

3
socket/socket_tcp6.c

@ -29,7 +29,7 @@ int socket_tcp6(void)
if (s == -1) {
if (errno == EINVAL || errno == EAFNOSUPPORT || errno == EPFNOSUPPORT || errno == EPROTONOSUPPORT) {
compat:
s=socket(AF_INET,SOCK_STREAM,0);
s=winsock2errno(socket(AF_INET,SOCK_STREAM,0));
noipv6=1;
if (s==-1) return -1;
} else
@ -41,6 +41,7 @@ compat:
winsock2errno(setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&zero,sizeof(zero)));
}
#endif
if (ndelay_on(s) == -1) { close(s); return -1; }
return s;
#else
return socket_tcp4();

8
socket/socket_udp4.c

@ -1,13 +1,19 @@
#include <sys/types.h>
#ifndef __MINGW32__
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "socket.h"
#include "ndelay.h"
int socket_udp4(void) {
int s;
__winsock_init();
return winsock2errno(socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP));
s = winsock2errno(socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP));
if (s == -1) return -1;
if (ndelay_on(s) == -1) { close(s); return -1; }
return s;
}

1
socket/socket_udp6.c

@ -41,6 +41,7 @@ compat:
winsock2errno(setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&zero,sizeof(zero)));
}
#endif
if (ndelay_on(s) == -1) { close(s); return -1; }
return s;
#else
return socket_udp();

2
test/client.c

@ -7,6 +7,7 @@
#include <unistd.h>
#include "socket.h"
#include <sys/socket.h>
#include "ndelay.h"
#ifdef __dietlibc__
#include <write12.h>
#else
@ -73,6 +74,7 @@ usage:
if (s==-1) panic("client: error: socket() failed");
if (socket_connect6(s,ip,port,scope_id)==-1) panic("client: error: connect() failed");
}
ndelay_off(s);
p[0].fd=0; p[0].events=POLLIN;
p[1].fd=s; p[1].events=POLLIN;
while (poll(p,2,5000)) {

2
test/dllink.c

@ -5,6 +5,7 @@
#include <unistd.h>
#include <stdio.h>
#include "case.h"
#include "ndelay.h"
int main(int argc,char* argv[]) {
int s=socket_tcp4();
@ -14,6 +15,7 @@ int main(int argc,char* argv[]) {
int header=1;
buffer filein;
ndelay_off(s);
if (argc<2 || strlen(argv[1])>900) {
buffer_putsflush(buffer_2,"usage: dllink ed2k://|file|<filename>|<filesize>|<MD4-sum|\n");
return 0;

2
test/httpd.c

@ -190,7 +190,6 @@ int main() {
panic("socket_bind6_reuse");
if (socket_listen(s,16)==-1)
panic("socket_listen");
io_nonblock(s);
if (!io_fd(s))
panic("io_fd");
io_wantread(s);
@ -209,7 +208,6 @@ int main() {
buffer_puts(buffer_2," (fd ");
buffer_putulong(buffer_2,n);
buffer_puts(buffer_2,")");
io_nonblock(n);
if (io_fd(n)) {
struct http_data* h=(struct http_data*)malloc(sizeof(struct http_data));
io_wantread(n);

2
test/io5.c

@ -27,7 +27,6 @@ int main() {
buffer_putnlflush(buffer_2);
return 111;
}
io_nonblock(s);
io_wantread(s);
buffer_puts(buffer_2,"listening on port 1234 (fd #");
buffer_putulong(buffer_2,s);
@ -47,7 +46,6 @@ int main() {
buffer_puts(buffer_2," (fd ");
buffer_putulong(buffer_2,n);
buffer_puts(buffer_2,")");
io_nonblock(n);
if (io_fd(n)) {
io_wantread(n);
} else {

2
test/proxy.c

@ -76,7 +76,6 @@ nomem:
buffer_putnlflush(buffer_2);
return 111;
}
io_nonblock(s);
if (!io_fd(s)) {
buffer_puts(buffer_2,"io_fd: ");
buffer_puterror(buffer_2);
@ -106,7 +105,6 @@ fail:
s->a=n; s->b=x; s->connected=0; s->done=s->todo=0;
s->dir=UNDECIDED;
io_nonblock(x);
io_nonblock(n);
socket_connect6(x,out.s,hisport,hisscope_id);
if (!io_fd(x) || !io_fd(n)) {
buffer_puts(buffer_2,"io_fd failed: ");

3
test/server.c

@ -6,6 +6,7 @@
#include <sys/poll.h>
#include <unistd.h>
#include "socket.h"
#include "ndelay.h"
#include <sys/socket.h>
#ifdef __dietlibc__
#include <write12.h>
@ -72,12 +73,14 @@ usage:
if (s==-1) panic("server: error: socket() failed");
if (socket_bind4_reuse(s,ip+12,port)==-1) panic("server: error: bind() failed");
if (socket_listen(s,1)==-1) panic("server: error: listen() failed");
ndelay_off(s);
if ((t=socket_accept4(s,0,0))==-1) panic("server: error: accept() failed");
} else {
s=socket_tcp6();
if (s==-1) panic("server: error: socket() failed");
if (socket_bind6_reuse(s,ip,port,scope_id)==-1) panic("server: error: bind() failed");
if (socket_listen(s,1)==-1) panic("server: error: listen() failed");
ndelay_off(s);
if ((t=socket_accept6(s,0,0,0))==-1) panic("server: error: accept() failed");
}
close(s);

1
test/vd.c

@ -3,6 +3,7 @@
#include <unistd.h>
#include <stdio.h>
#include <sys/uio.h>
#include "ndelay.h"
int main(int argc,char* argv[]) {
int s=socket_tcp4();

18
textcode.h

@ -3,22 +3,35 @@
/* These take len bytes from src and write them in encoded form to
* dest (if dest != NULL), returning the number of bytes written. */
/* needs len/3*4 bytes */
unsigned long fmt_uuencoded(char* dest,const char* src,unsigned long len);
/* needs len/3*4 bytes */
unsigned long fmt_base64(char* dest,const char* src,unsigned long len);
/* worst case: len*3 */
unsigned long fmt_quotedprintable(char* dest,const char* src,unsigned long len);
/* worst case: len*3 */
unsigned long fmt_quotedprintable2(char* dest,const char* src,unsigned long len,const char* escapeme);
/* worst case: len*3 */
unsigned long fmt_urlencoded(char* dest,const char* src,unsigned long len);
/* worst case: len*3 */
unsigned long fmt_urlencoded2(char* dest,const char* src,unsigned long len,const char* escapeme);
/* worst case: len*2 */
unsigned long fmt_yenc(char* dest,const char* src,unsigned long len);
/* needs len*2 bytes */
unsigned long fmt_hexdump(char* dest,const char* src,unsigned long len);
/* this changes '<' to '&lt;' and '&' to '&amp;' */
/* change '<' to '&lt;' and '&' to '&amp;'; worst case: len*5 */
unsigned long fmt_html(char* dest,const char* src,unsigned long len);
/* change '\' to "\\", '\n' to "\n", ^A to "\x01" etc */
/* change '\' to "\\", '\n' to "\n", ^A to "\x01" etc; worst case: len*4 */
unsigned long fmt_cescape(char* dest,const char* src,unsigned long len);
/* worst case: len*4 */
unsigned long fmt_cescape2(char* dest,const char* src,unsigned long len,const char* escapeme);
/* fold awk whitespace to '_'; this is great for writing fields with
* white spaces to a log file and still allow awk to do log analysis */
/* worst case: same size */
unsigned long fmt_foldwhitespace(char* dest,const char* src,unsigned long len);
/* worst case: len*3 */
unsigned long fmt_ldapescape(char* dest,const char* src,unsigned long len);
/* These read one line from src, decoded it, and write the result to
* dest. The number of decoded bytes is written to destlen. dest
@ -32,6 +45,7 @@ unsigned long scan_yenc(const char *src,char *dest,unsigned long *destlen);
unsigned long scan_hexdump(const char *src,char *dest,unsigned long *destlen);
unsigned long scan_html(const char *src,char *dest,unsigned long *destlen);
unsigned long scan_cescape(const char *src,char *dest,unsigned long *destlen);
unsigned long scan_ldapescape(const char* src,char* dest,unsigned long *destlen);
#ifdef STRALLOC_H
/* WARNING: these functions _append_ to the stralloc, not overwrite! */

23
textcode/fmt_ldapescape.c

@ -0,0 +1,23 @@
#include "fmt.h"
#include "textcode.h"
#include "haveinline.h"
#include "str.h"
unsigned long fmt_ldapescape(char* dest,const char* src,unsigned long len) {
register const unsigned char* s=(const unsigned char*) src;
unsigned long written=0,i;
for (i=0; i<len; ++i) {
if (s[i]=='*' || s[i]=='(' || s[i]==')' || s[i]==0 || s[i]=='\\') {
if (dest) {
dest[written]='\\';
dest[written+1]=fmt_tohex(s[i]>>4);
dest[written+2]=fmt_tohex(s[i]&15);
}
written+=3;
} else {
if (dest) dest[written]=s[i]; ++written;
}
}
return written;
}

24
textcode/scan_ldapescape.c

@ -0,0 +1,24 @@
#include "fmt.h"
#include "textcode.h"
#include "scan.h"
unsigned long scan_ldapescape(const char *src,char *dest,unsigned long *destlen) {
register const unsigned char* s=(const unsigned char*) src;
unsigned long written=0,i;
for (i=0; s[i]; ++i) {
if (s[i]=='\\') {
int j=scan_fromhex(s[i+1]);
if (j<0) break;
dest[written]=j<<4;
j=scan_fromhex(s[i+2]);
if (j<0) break;
dest[written]|=j;
i+=2;
} else {
dest[written]=s[i];
}
++written;
}
*destlen=written;
return i;
}
Loading…
Cancel
Save