From fc4193e1cef96b204a9647d5cf0ab123f438562a Mon Sep 17 00:00:00 2001 From: leitner Date: Mon, 5 Feb 2001 21:55:25 +0000 Subject: [PATCH] buffer stuff from Olaf and ip[46]_fmt stuff from me. --- buffer/buffer_get_token.c | 7 +++--- buffer/buffer_getn.c | 3 ++- ip4.h | 9 ++++++++ socket/ip4_fmt.3 | 27 ++++++++++++++++++++++ socket/ip4_fmt.c | 16 +++++++++++++ socket/ip6_fmt.3 | 32 ++++++++++++++++++++++++++ socket/ip6_fmt.c | 47 +++++++++++++++++++++++++++++++++++++++ socket/ip6_fmt_flat.c | 21 +++++++++++++++++ 8 files changed, 158 insertions(+), 4 deletions(-) create mode 100644 ip4.h create mode 100644 socket/ip4_fmt.3 create mode 100644 socket/ip4_fmt.c create mode 100644 socket/ip6_fmt.3 create mode 100644 socket/ip6_fmt.c create mode 100644 socket/ip6_fmt_flat.c diff --git a/buffer/buffer_get_token.c b/buffer/buffer_get_token.c index 954679b..f58e496 100644 --- a/buffer/buffer_get_token.c +++ b/buffer/buffer_get_token.c @@ -6,9 +6,10 @@ int buffer_get_token(buffer* b,char* x,unsigned int len,const char* charset,unsi int blen; for (blen=0;blen + +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/ip4_fmt.c b/socket/ip4_fmt.c new file mode 100644 index 0000000..1c4932c --- /dev/null +++ b/socket/ip4_fmt.c @@ -0,0 +1,16 @@ +#include "fmt.h" +#include "ip4.h" + +unsigned int ip4_fmt(char *s,const char ip[4]) +{ + unsigned int len; + int i; + + len = 0; + 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++='.'; } + } + return len; +} diff --git a/socket/ip6_fmt.3 b/socket/ip6_fmt.3 new file mode 100644 index 0000000..ef5c129 --- /dev/null +++ b/socket/ip6_fmt.3 @@ -0,0 +1,32 @@ +.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/ip6_fmt.c b/socket/ip6_fmt.c new file mode 100644 index 0000000..a759a8b --- /dev/null +++ b/socket/ip6_fmt.c @@ -0,0 +1,47 @@ +#include "fmt.h" +#include "byte.h" +#include "ip4.h" +#include "ip6.h" + +unsigned int ip6_fmt(char *s,const char ip[16]) +{ + unsigned int len; + unsigned int i; + unsigned int temp; + unsigned int compressing; + int j; + + len = 0; compressing = 0; + for (j=0; j<16; j+=2) { + if (j==12 && ip6_isv4mapped(ip)) { + temp=ip4_fmt(s,ip+12); + len+=temp; + if (s) s+=temp; + break; + } + temp = ((unsigned long) (unsigned char) ip[j] << 8) + + (unsigned long) (unsigned char) ip[j+1]; + if (temp == 0) { + if (!compressing) { + compressing=1; + if (j==0) { + if (s) *s++=':'; ++len; + } + } + } else { + if (compressing) { + compressing=0; + if (s) *s++=':'; ++len; + } + i = fmt_xlong(s,temp); len += i; if (s) s += i; + if (j<14) { + if (s) *s++ = ':'; + ++len; + } + } + } + +/* if (s) *s=0; */ + return len; +} + diff --git a/socket/ip6_fmt_flat.c b/socket/ip6_fmt_flat.c new file mode 100644 index 0000000..ec3d959 --- /dev/null +++ b/socket/ip6_fmt_flat.c @@ -0,0 +1,21 @@ +#include "ip6.h" + +static char tohex(char num) { + if (num<10) + return num+'0'; + else if (num<16) + return num-10+'a'; + else + return -1; +} + +unsigned int ip6_fmt_flat(char *s,const char ip[16]) +{ + int i; + if (!s) return 32; + for (i=0; i<16; i++) { + *s++=tohex((unsigned char)ip[i] >> 4); + *s++=tohex((unsigned char)ip[i] & 15); + } + return 32; +}