diff --git a/ip4.h b/ip4.h index b561a4c..83c18ea 100644 --- a/ip4.h +++ b/ip4.h @@ -1,9 +1,15 @@ #ifndef IP4_H #define IP4_H -extern unsigned int ip4_scan(const char *src,char *ip); -extern unsigned int ip4_fmt(char *dest,const char *ip); +extern unsigned int scan_ip4(const char *src,char *ip); +extern unsigned int fmt_ip4(char *dest,const char *ip); + +/* for djb backwards compatibility */ +#define ip4_scan scan_ip4 +#define ip4_fmt fmt_ip4 #define IP4_FMT 20 +extern const char ip4loopback[4]; /* = {127,0,0,1};*/ + #endif diff --git a/ip6.h b/ip6.h index 97f0697..0314138 100644 --- a/ip6.h +++ b/ip6.h @@ -1,11 +1,11 @@ #ifndef IP6_H #define IP6_H -extern unsigned int ip6_scan(const char *src,char *ip); -extern unsigned int ip6_fmt(char *dest,const char *ip); +extern unsigned int scan_ip6(const char *src,char *ip); +extern unsigned int fmt_ip6(char *dest,const char *ip); -extern unsigned int ip6_scan_flat(const char *src,char *); -extern unsigned int ip6_fmt_flat(char *dest,const char *); +extern unsigned int scan_ip6_flat(const char *src,char *); +extern unsigned int fmt_ip6_flat(char *dest,const char *); /* ip6 address syntax: (h = hex digit), no leading '0' required @@ -23,6 +23,4 @@ extern const unsigned char V6any[16]; /*={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; */ #define ip6_isv4mapped(ip) (byte_equal(ip,12,V4mappedprefix)) -extern const char ip4loopback[4]; /* = {127,0,0,1};*/ - #endif diff --git a/socket/fmt_ip4.3 b/socket/fmt_ip4.3 new file mode 100644 index 0000000..9e32ffc --- /dev/null +++ b/socket/fmt_ip4.3 @@ -0,0 +1,27 @@ +.TH fmt_ip4 3 +.SH NAME +fmt_ip4 \- write a formatted ASCII representation of an IPv4 number +.SH SYNTAX +.B #include + +unsigned int \fBfmt_ip4\fP(char *\fIdest\fR,const char \fIip\fR[4]); +.SH DESCRIPTION +fmt_ip4 formats an IPv4 number in dotted-decimal ASCII representation +from \fIip\fR and writes the result into \fIdest\fR. It returns the +number of bytes written. + +If \fIdest\fR equals FMT_LEN (i.e. is zero), fmt_ip4 returns the number +of bytes it would have written. + +fmt_ip4 does not append \\0. + +For convenience, ip4.h defines the integer IP4_FMT to be big enough to +contain every possible fmt_ip4 output plus \\0. +.SH EXAMPLE +#include + + char buf[IP4_FMT]; + char ip[4]; + buf[fmt_ip4(buf,ip)]=0; +.SH "SEE ALSO" +scan_ip4(3), ip6_fmt(3) diff --git a/socket/ip4_fmt.c b/socket/fmt_ip4.c similarity index 66% rename from socket/ip4_fmt.c rename to socket/fmt_ip4.c index 1c4932c..78e4327 100644 --- a/socket/ip4_fmt.c +++ b/socket/fmt_ip4.c @@ -1,7 +1,7 @@ #include "fmt.h" #include "ip4.h" -unsigned int ip4_fmt(char *s,const char ip[4]) +unsigned int fmt_ip4(char *s,const char ip[4]) { unsigned int len; int i; @@ -10,7 +10,7 @@ unsigned int ip4_fmt(char *s,const char ip[4]) for (i=0; i<4; ++i) { register unsigned int j; len+=(j=fmt_ulong(s,(unsigned long) (unsigned char) ip[i]))+1; - if (s && i<3) { s+=i; *s++='.'; } + if (s && i<3) { s+=j; *s++='.'; } } - return len; + return len-1; } diff --git a/socket/fmt_ip6.3 b/socket/fmt_ip6.3 new file mode 100644 index 0000000..76c3aac --- /dev/null +++ b/socket/fmt_ip6.3 @@ -0,0 +1,32 @@ +.TH fmt_ip6 3 +.SH NAME +fmt_ip6 \- write a formatted ASCII representation of an IPv6 number +.SH SYNTAX +.B #include + +unsigned int \fBfmt_ip6\fP(char *\fIdest\fR,const char \fIip\fR[16]); +.SH DESCRIPTION +fmt_ip6 formats an IPv6 number in ASCII representation from \fIip\fR and +writes the result into \fIdest\fR. It returns the number of bytes +written. + +fmt_ip6 will apply "::" compression to the output. + +If \fIip\fR is an IPv4-mapped IPv6 address, fmt_ip6 will output the last +4 bytes as IPv4 number in dotted-decimal notation. + +If \fIdest\fR equals FMT_LEN (i.e. is zero), fmt_ip6 returns the number +of bytes it would have written. + +fmt_ip6 does not append \\0. + +For convenience, ip6.h defines the integer IP6_FMT to be big enough to +contain every possible fmt_ip6 output plus \\0. +.SH EXAMPLE +#include + + char buf[IP6_FMT]; + char ip[16]; + buf[fmt_ip6(buf,ip)]=0; +.SH "SEE ALSO" +scan_ip6(3), ip4_fmt(3) diff --git a/socket/ip6_fmt.c b/socket/fmt_ip6.c similarity index 94% rename from socket/ip6_fmt.c rename to socket/fmt_ip6.c index a759a8b..9c224d6 100644 --- a/socket/ip6_fmt.c +++ b/socket/fmt_ip6.c @@ -3,7 +3,7 @@ #include "ip4.h" #include "ip6.h" -unsigned int ip6_fmt(char *s,const char ip[16]) +unsigned int fmt_ip6(char *s,const char ip[16]) { unsigned int len; unsigned int i; diff --git a/socket/ip6_fmt_flat.c b/socket/fmt_ip6_flat.c similarity index 85% rename from socket/ip6_fmt_flat.c rename to socket/fmt_ip6_flat.c index ec3d959..7473666 100644 --- a/socket/ip6_fmt_flat.c +++ b/socket/fmt_ip6_flat.c @@ -9,7 +9,7 @@ static char tohex(char num) { return -1; } -unsigned int ip6_fmt_flat(char *s,const char ip[16]) +unsigned int fmt_ip6_flat(char *s,const char ip[16]) { int i; if (!s) return 32; diff --git a/socket/ip4_fmt.3 b/socket/ip4_fmt.3 deleted file mode 100644 index 95482f7..0000000 --- a/socket/ip4_fmt.3 +++ /dev/null @@ -1,27 +0,0 @@ -.TH ip4_fmt 3 -.SH NAME -ip4_fmt \- write a formatted ASCII representation of an IPv4 number -.SH SYNTAX -.B #include - -unsigned int \fBip4_fmt\fP(char *\fIdest\fR,const char \fIip\fR[4]); -.SH DESCRIPTION -ip4_fmt formats an IPv4 number in dotted-decimal ASCII representation -from \fIip\fR and writes the result into \fIdest\fR. It returns the -number of bytes written. - -If \fIdest\fR equals FMT_LEN (i.e. is zero), ip4_fmt returns the number -of bytes it would have written. - -ip4_fmt does not append \\0. - -For convenience, ip4.h defines the integer IP4_FMT to be big enough to -contain every possible ip4_fmt output plus \\0. -.SH EXAMPLE -#include - - char buf[IP4_FMT]; - char ip[4]; - buf[ip4_fmt(buf,ip)]=0; -.SH "SEE ALSO" -ip4_scan(3), ip6_fmt(3) diff --git a/socket/ip6_fmt.3 b/socket/ip6_fmt.3 deleted file mode 100644 index ef5c129..0000000 --- a/socket/ip6_fmt.3 +++ /dev/null @@ -1,32 +0,0 @@ -.TH ip6_fmt 3 -.SH NAME -ip6_fmt \- write a formatted ASCII representation of an IPv6 number -.SH SYNTAX -.B #include - -unsigned int \fBip6_fmt\fP(char *\fIdest\fR,const char \fIip\fR[16]); -.SH DESCRIPTION -ip6_fmt formats an IPv6 number in ASCII representation from \fIip\fR and -writes the result into \fIdest\fR. It returns the number of bytes -written. - -ip6_fmt will apply "::" compression to the output. - -If \fIip\fR is an IPv4-mapped IPv6 address, ip6_fmt will output the last -4 bytes as IPv4 number in dotted-decimal notation. - -If \fIdest\fR equals FMT_LEN (i.e. is zero), ip6_fmt returns the number -of bytes it would have written. - -ip6_fmt does not append \\0. - -For convenience, ip6.h defines the integer IP6_FMT to be big enough to -contain every possible ip6_fmt output plus \\0. -.SH EXAMPLE -#include - - char buf[IP6_FMT]; - char ip[16]; - buf[ip6_fmt(buf,ip)]=0; -.SH "SEE ALSO" -ip6_scan(3), ip4_fmt(3) diff --git a/socket/socket_connect6.c b/socket/socket_connect6.c index df428e7..bea5358 100644 --- a/socket/socket_connect6.c +++ b/socket/socket_connect6.c @@ -9,6 +9,7 @@ #include "haveip6.h" #include "error.h" #include "uint32.h" +#include "ip4.h" int socket_connect6(int s,const char ip[16],uint16 port,uint32 scope_id) { diff --git a/socket/socket_send6.c b/socket/socket_send6.c index 75f23e8..5634686 100644 --- a/socket/socket_send6.c +++ b/socket/socket_send6.c @@ -7,6 +7,7 @@ #include "socket.h" #include "ip6.h" #include "haveip6.h" +#include "ip4.h" int socket_send6(int s,const char *buf,unsigned int len,const char ip[16],uint16 port,uint32 scope_id) { diff --git a/t.c b/t.c index 5113775..c179067 100644 --- a/t.c +++ b/t.c @@ -6,13 +6,20 @@ #include "stralloc.h" #include "socket.h" #include "buffer.h" +#include "ip4.h" #define rdtscl(low) \ __asm__ __volatile__ ("rdtsc" : "=a" (low) : : "edx") int main(int argc,char* argv[]) { + char buf[100]; + buf[ip4_fmt(buf,ip4loopback)]=0; + buffer_puts(buffer_1small,buf); + buffer_flush(buffer_1small); +#if 0 buffer_puts(buffer_1small,"hello, world\n"); buffer_flush(buffer_1small); +#endif #if 0 int s=socket_tcp4(); char ip[4]={127,0,0,1};