345 lines · 7.2 KB · Text
RawDownload
.TH IP 2.SH NAMEeipfmt, parseip, parseipmask, v4parseip, v4parsecidr, parseether, myipaddr, myetheraddr, maskip, equivip4, equivip6, defmask, isv4, v4tov6, v6tov4, nhgetv, nhgetl, nhgets, hnputv, hnputl, hnputs, ptclbsum, freeipifc, readipifc \- Internet Protocol addressing.SH SYNOPSIS.de PB.PP.ft L.nf...PB#include <u.h>#include <libc.h>#include <ip.h>.PBint		eipfmt(Fmt*).PBvlong	parseip(uchar *ipaddr, char *str).PBvlong	parseipmask(uchar *ipaddr, char *str).PBchar*	v4parseip(uchar *ipaddr, char *str).PBulong	v4parsecidr(uchar *addr, uchar *mask, char *str).PBint		parseether(uchar *eaddr, char *str).PBint		myetheraddr(uchar *eaddr, char *dev).PBint		myipaddr(uchar *ipaddr, char *net).PBvoid		maskip(uchar *from, uchar *mask, uchar *to).PBint		equivip4(uchar *ipaddr1, uchar *ipaddr2).PBint		equivip6(uchar *ipaddr1, uchar *ipaddr2).PBuchar*	defmask(uchar *ipaddr).PBint		isv4(uchar *ipaddr).PBvoid		v4tov6(uchar *ipv6, uchar *ipv4).PBvoid		v6tov4(uchar *ipv4, uchar *ipv6).PBushort	nhgets(void *p).PBuint		nhgetl(void *p).PBuvlong	nhgetv(void *p).PBvoid		hnputs(void *p, ushort v).PBvoid		hnputl(void *p, uint v).PBvoid		hnputv(void *p, uvlong v).PBushort	ptclbsum(uchar *a, int n).PBIpifc*	freeipifc(Ipifc *ifc).PBIpifc*	readipifc(char *net, Ipifc *ifc, int index).PBuchar	IPv4bcast[IPaddrlen];.PBuchar	IPv4allsys[IPaddrlen];.PBuchar	IPv4allrouter[IPaddrlen];.PBuchar	IPallbits[IPaddrlen];.PBuchar	IPnoaddr[IPaddrlen];.PBuchar	v4prefix[IPaddrlen];.SH DESCRIPTIONThese routines are used by Internet Protocol (IP) programs tomanipulate IP and Ethernet addresses.Plan 9, by default, uses V6 format IP addresses.  Since V4addresses fit into the V6 space, all IP addresses can be represented.IP addresses are stored as a string of 16.B unsigned.BR chars ,Ethernetaddresses as 6.B unsigned.BR chars .Either V4 or V6 string representation can be used for IP addresses.For V4 addresses, the representation can be (up to) 4 decimalintegers from 0 to 255 separated by periods.For V6 addresses, the representation is (up to) 8 hex integersfrom 0x0 to 0xFFFF separated by colons.Strings of 0 integers can be elided using two colons.For example,.B FFFF::1111is equivalent to.BR FFFF:0:0:0:0:0:0:1111 .The string representation for IP masks is a superset of theaddress representation.  It includes slash notation that indicatesthe number of leading 1 bits in the mask.  Thus, aV4 class C mask can be represented as.BR FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FF00 ,.BR 255.255.255.0 ,or.BR /120.The string representation of Ethernet addresses is exactly12 hexadecimal digits..PP.I Eipfmtis a.IR print (2)formatter for Ethernet (verb.BR E )addresses,IP V6 (verb.BR I )addresses,IP V4 (verb.BR V )addresses,and IP V6 (verb.BR M )masks..PP.I Parseipconverts a string pointed to by.I strto a 16-byte IP address starting at.IR ipaddr .As a concession to backwards compatibility,if the string is a V4 address, the return valueis an unsigned long integer containing the big-endian V4 address.If not, the return value is 6..I Parseipmaskconverts a string pointed to by.I strto a 6-byte IP mask starting at.IR ipaddr .It too returns an unsigned long big-endian V4 address or 6.Both routines return -1 on errors..PP.I V4parseipconverts a string pointed to by.I strto a 4-byte V4 IP address starting at.IR ipaddr ..PP.I V4parsecidrconverts a string of the formaddr/mask, pointed to by.IR str ,to a 4-byte V4 IP address starting at.I ipaddrand a 4-byte V4 IP mask starting at.IR mask ..PP.I Myipaddrreturns the first valid IP address inthe IP stack rooted at.IR net ..PP.I Parseetherconverts a string pointed to by.I strto a 6-byte Ethernet address starting at.IR eaddr ..I Myetheraddrreads the Ethernet address string from file.IB dev /addrand parses it into.IR eaddr .Both routines return a negative number on errors..PP.I Maskipplaces the bit-wise AND of the IP addresses pointedto by its first two arguments into the buffer pointedto by the third..PP.I Equivipreturns non-zero if the IP addresses pointed to by its twoarguments are equal..I Equivip4operates on v4 addresses,.I equivip6operates on v6 addresses..PP.I Defmaskreturns the standard class A, B, or C mask for.IR ipaddr ..PP.I Isv4returns non-zero if the V6 address is in the V4 space, that is,if it starts with.BR 0:0:0:0:0:0:FFFF ..I V4tov6converts the 4-byte V4 address,.IR v4ip ,to a V6 address and puts the result in.IR v6ip ..I V6tov4converts the V6 address,.IR v6ip ,to a 4-byte V4 address and puts the result in.IR v4ip ..PP.IR Hnputs ,.I hnputland.I hnputvare used to store 16-bit, 32-bit, and 64-bit integers, respectively, into IP big-endian form..IR Nhgets ,.I nhgetland.I nhgetvconvert big-endian 2, 4 and 8 byte quantities into integers (or.IR uvlong s)..PP.I Pctlbsumreturns the one's complement checksum used in IP protocols, typically invoked as.IP.EXhnputs(hdr->cksum, ~ptclbsum(data, len) & 0xffff);.EE.PPA number of standard IP addresses in V6 format are also defined.  They are:.TF IPv4allrouter.TP.B IPv4bcastthe V4 broadcast address.TP.B IPv4allsysthe V4 all systems multicast address.TP.B IPv4allrouterthe V4 all routers multicast address.TP.B IPallbitsthe V6 all bits on address.TP.B IPnoaddrthe V6 null address, all zeros.TP.B v4prefixthe IP V6 prefix to all embedded V4 addresses.PD.PP.I Readipifcreturns information abouta particular interface.RI ( index>= 0)or all IP interfaces.RI ( index< 0)configured under a mount point.IR net ,default.BR /net .Each interface is described by one.I Ipifcstructure which in turn points to a linked list of.IR Iplifcstructures describing the addresses assignedto this interface.If the list.IR ifcis supplied,that list is freed.Thus, subsequent calls can be usedto free the list returned by the previous call..I Ipifcis:.PP.EXtypedef struct Ipifc{	Ipifc	*next;	Iplifc	*lifc;		/* local addressses */ 	/* per ip interface */	int	index;			/* number of interface in ipifc dir */	char	dev[64];			/* associated physical device */ 	int	mtu;				/* max transfer unit */ 	uchar	sendra6;		/* on == send router adv */	uchar	recvra6;		/* on == rcv router adv */ 	uvlong	pktin;		/* packets read */	uvlong	pktout;		/* packets written */	uvlong	errin;		/* read errors */	uvlong	errout;		/* write errors */	Ipv6rp	rp;			/* route advertisement params */} Ipifc;.EE.PP.I Iplifcis:.PP.EXstruct Iplifc{	Iplifc	*next; 	uchar	ip[IPaddrlen];	uchar	mask[IPaddrlen];	uchar	net[IPaddrlen];	/* ip & mask */	ulong	preflt;		/* preferred lifetime */	ulong	validlt;		/* valid lifetime */};.EE.PP.I Ipv6rpis:.PP.EXstruct Ipv6rp{	int	mflag;	int	oflag;	int 	maxraint;			/* max route adv interval */	int	minraint;			/* min route adv interval */	int	linkmtu;	int	reachtime;	int	rxmitra;	int	ttl;	int	routerlt;	};.EE.PP.I Devcontains the first 64 bytes of the device configured with thisinterface..I Netis.IB ip & maskif the network is multipoint orthe remote address if the network ispoint to point..PP.I Freeifcfrees memory allocated by.IR readipifc ..SH SOURCE.B /sys/src/libip.SH SEE ALSO.IR print (2),.IR ip (3)