IP(2)
NAME
eipfmt, parseip, parseipmask, v4parseip, v4parsecidr, parseether, myipaddr, myetheraddr, maskip, equivip4, equivip6, defmask, isv4, v4tov6, v6tov4, nhgetv, nhgetl, nhgets, hnputv, hnputl, hnputs, ptclbsum, readipifc − Internet protocol
SYNOPSIS
#include <u.h>
#include <libc.h>
#include <ip.h>
inteipfmt(Fmt∗)
ulongparseip(uchar ∗ipaddr, char ∗str)
ulongparseipmask(uchar ∗ipaddr, char ∗str)
char∗v4parseip(uchar ∗ipaddr, char ∗str)
ulongv4parsecidr(uchar ∗addr, uchar ∗mask, char ∗str)
intparseether(uchar ∗eaddr, char ∗str)
intmyetheraddr(uchar ∗eaddr, char ∗dev)
intmyipaddr(uchar ∗ipaddr, char ∗net)
voidmaskip(uchar ∗from, uchar ∗mask, uchar ∗to)
intequivip4(uchar ∗ipaddr1, uchar ∗ipaddr2)
intequivip6(uchar ∗ipaddr1, uchar ∗ipaddr2)
uchar∗defmask(uchar ∗ipaddr)
intisv4(uchar ∗ipaddr)
voidv4tov6(uchar ∗ipv6, uchar ∗ipv4)
voidv6tov4(uchar ∗ipv4, uchar ∗ipv6)
ushortnhgets(void ∗p)
uintnhgetl(void ∗p)
uvlongnhgetv(void ∗p)
voidhnputs(void ∗p, ushort v)
voidhnputl(void ∗p, uint v)
voidhnputv(void ∗p, uvlong v)
ushortptclbsum(uchar ∗a, int n)
Ipifc∗readipifc(char ∗net, Ipifc ∗ifc, int index)
ucharIPv4bcast[IPaddrlen];
ucharIPv4allsys[IPaddrlen];
ucharIPv4allrouter[IPaddrlen];
ucharIPallbits[IPaddrlen];
ucharIPnoaddr[IPaddrlen];
ucharv4prefix[IPaddrlen];
DESCRIPTION
These routines are used by Internet Protocol (IP) programs to manipulate IP and Ethernet addresses. Plan 9, by default, uses V6 format IP addresses. Since V4 addresses fit into the V6 space, all IP addresses can be represented. IP addresses are stored as a string of 16 unsigned chars, Ethernet addresses as 6 unsigned chars. Either V4 or V6 string representation can be used for IP addresses. For V4 addresses, the representation can be (up to) 4 decimal integers from 0 to 255 separated by periods. For V6 addresses, the representation is (up to) 8 hex integers from 0x0 to 0xFFFF separated by colons. Strings of 0 integers can be elided using two colons. For example, FFFF::1111 is equivalent to FFFF:0:0:0:0:0:0:1111. The string representation for IP masks is a superset of the address representation. It includes slash notation that indicates the number of leading 1 bits in the mask. Thus, a V4 class C mask can be represented as FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FF00, 255.255.255.0, or /120. The string representation of Ethernet addresses is exactly 12 hexadecimal digits.
Eipfmt is a print(2) formatter for Ethernet (verb E) addresses, IP V6 (verb I) addresses, IP V4 (verb V) addresses, and IP V6 (verb M) masks.
Parseip converts a string pointed to by str to a 16-byte IP address starting at ipaddr. As a concession to backwards compatibility, if the string is a V4 address, the return value is an unsigned long integer containing the big-endian V4 address. If not, the return value is 6. Parseipmask converts a string pointed to by str to a 6-byte IP mask starting at ipaddr. It too returns an unsigned long big-endian V4 address or 6. Both routines return -1 on errors.
V4parseip converts a string pointed to by str to a 4-byte V4 IP address starting at ipaddr.
V4parsecidr converts a string of the form addr/mask, pointed to by str, to a 4-byte V4 IP address starting at ipaddr and a 4-byte V4 IP mask starting at mask.
Myipaddr returns the first valid IP address in the IP stack rooted at net.
Parseether converts a string pointed to by str to a 6-byte Ethernet address starting at eaddr. Myetheraddr reads the Ethernet address string from file dev/1/stats and parses it into eaddr. Both routines return a negative number on errors.
Maskip places the bit-wise AND of the IP addresses pointed to by its first two arguments into the buffer pointed to by the third.
Equivip returns non-zero if the IP addresses pointed to by its two arguments are equal. Equivip4 operates on v4 addresses, equivip6 operates on v6 addresses.
Defmask returns the standard class A, B, or C mask for ipaddr.
Isv4 returns non-zero if the V6 address is in the V4 space, that is, if it starts with 0:0:0:0:0:0:FFFF. V4tov6 converts the V4 address, v4ip, to a V6 address and puts the result in v6ip. V6tov4 converts the V6 address, v6ip, to a V4 address and puts the result in v4ip.
Hnputs, hnputl and hnputv are used to store 16-bit, 32-bit, and 64-bit integers, respectively, into IP big-endian form. Nhgets, nhgetl and nhgetv convert big-endian 2, 4 and 8 byte quantities into integers (or uvlongs).
Pctlbsum returns the one’s complement checksum used in IP protocols, typically invoked as
hnputs(hdr->cksum, ~ptclbsum(data, len) & 0xffff);
A number of standard IP addresses in V6 format are also defined. They are:
IPv4bcast
the V4 broadcast address
IPv4allsys
the V4 all systems multicast address
IPv4allrouter
the V4 all routers multicast address
IPallbits
the V6 all bits on address
IPnoaddr
the V6 null address, all zeros
v4prefix
the IP V6 prefix to all embedded V4 addresses
Readipifc returns information about a particular interface (index >= 0) or all IP interfaces (index < 0) configured under a mount point net, default /net. Each interface is described by one Ipifc structure which in turn points to a linked list of Iplifc structures describing the addresses assigned to this interface. If the list ifc is supplied, that list is freed. Thus, subsequent calls can be used to free the list returned by the previous call. Ipifc is:
typedef struct Ipifc
{
Ipifc∗next;
Iplifc∗lifc;/∗ local addressses ∗/
/∗ per ip interface ∗/
intindex;/∗ number of interface in ipifc dir ∗/
chardev[64];/∗ associated physical device ∗/
intmtu;/∗ max transfer unit ∗/
longvalidlt;/∗ valid life time ∗/
longpreflt;/∗ preferred life time ∗/
ucharsendra6;/∗ on == send router adv ∗/
ucharrecvra6;/∗ on == rcv router adv ∗/
ulongpktin;/∗ packets read ∗/
ulongpktout;/∗ packets written ∗/
ulongerrin;/∗ read errors ∗/
ulongerrout;/∗ write errors ∗/
Ipv6rprp;/∗ route advertisement params ∗/
} Ipifc;
Iplifc is:
struct Iplifc
{
Iplifc∗next;
ucharip[IPaddrlen];
ucharmask[IPaddrlen];
ucharnet[IPaddrlen];/∗ ip & mask ∗/
ulongpreflt;/∗ preferred lifetime ∗/
ulongvalidlt;/∗ valid lifetime ∗/
};
Ipv6rp is: struct Ipv6rp { intmflag; intoflag; int maxraint;/∗ max route adv interval ∗/ int minraint; /∗ min route adv interval ∗/ int linkmtu; int reachtime; int rxmitra; int ttl; int routerlt; };
Dev contains the first 64 bytes of the device configured with this interface. Net is ip&mask if the network is multipoint or the remote address if the network is point to point.
SOURCE
/sys/src/libip
SEE ALSO
Plan 9 — December 20, 2004