ip 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. .TH IP 2
  2. .SH NAME
  3. 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
  4. .SH SYNOPSIS
  5. .B #include <u.h>
  6. .br
  7. .B #include <libc.h>
  8. .br
  9. .B #include <ip.h>
  10. .PP
  11. .B
  12. int eipfmt(Fmt*)
  13. .PP
  14. .B
  15. ulong parseip(uchar *ipaddr, char *str)
  16. .PP
  17. .B
  18. ulong parseipmask(uchar *ipaddr, char *str)
  19. .PP
  20. .B
  21. char* v4parseip(uchar *ipaddr, char *str)
  22. .PP
  23. .B
  24. ulong v4parsecidr(uchar *addr, uchar *mask, char *str)
  25. .PP
  26. .B
  27. int parseether(uchar *eaddr, char *str)
  28. .PP
  29. .B
  30. int myetheraddr(uchar *eaddr, char *dev)
  31. .PP
  32. .B
  33. int myipaddr(uchar *ipaddr, char *net)
  34. .PP
  35. .B
  36. void maskip(uchar *from, uchar *mask, uchar *to)
  37. .PP
  38. .B
  39. int equivip4(uchar *ipaddr1, uchar *ipaddr2)
  40. .PP
  41. .B
  42. int equivip6(uchar *ipaddr1, uchar *ipaddr2)
  43. .PP
  44. .B
  45. uchar* defmask(uchar *ipaddr)
  46. .PP
  47. .B
  48. int isv4(uchar *ipaddr)
  49. .PP
  50. .B
  51. void v4tov6(uchar *ipv6, uchar *ipv4)
  52. .PP
  53. .B
  54. void v6tov4(uchar *ipv4, uchar *ipv6)
  55. .PP
  56. .B
  57. ushort nhgets(void *p)
  58. .PP
  59. .B
  60. uint nhgetl(void *p)
  61. .PP
  62. .B
  63. uvlong nhgetv(void *p)
  64. .PP
  65. .B
  66. void hnputs(void *p, ushort v)
  67. .PP
  68. .B
  69. void hnputl(void *p, uint v)
  70. .PP
  71. .B
  72. void hnputv(void *p, uvlong v)
  73. .PP
  74. .B
  75. ushort ptclbsum(uchar *a, int n)
  76. .PP
  77. .B
  78. Ipifc* readipifc(char *net, Ipifc *ifc, int index)
  79. .PP
  80. .B
  81. uchar IPv4bcast[IPaddrlen];
  82. .PP
  83. .B
  84. uchar IPv4allsys[IPaddrlen];
  85. .PP
  86. .B
  87. uchar IPv4allrouter[IPaddrlen];
  88. .PP
  89. .B
  90. uchar IPallbits[IPaddrlen];
  91. .PP
  92. .B
  93. uchar IPnoaddr[IPaddrlen];
  94. .PP
  95. .B
  96. uchar v4prefix[IPaddrlen];
  97. .SH DESCRIPTION
  98. These routines are used by Internet Protocol (IP) programs to
  99. manipulate IP and Ethernet addresses.
  100. Plan 9, by default, uses V6 format IP addresses. Since V4
  101. addresses fit into the V6 space, all IP addresses can be represented.
  102. IP addresses are stored as a string of 16
  103. .B unsigned
  104. .BR chars ,
  105. Ethernet
  106. addresses as 6
  107. .B unsigned
  108. .BR chars .
  109. Either V4 or V6 string representation can be used for IP addresses.
  110. For V4 addresses, the representation can be (up to) 4 decimal
  111. integers from 0 to 255 separated by periods.
  112. For V6 addresses, the representation is (up to) 8 hex integers
  113. from 0x0 to 0xFFFF separated by colons.
  114. Strings of 0 integers can be elided using two colons.
  115. For example,
  116. .B FFFF::1111
  117. is equivalent to
  118. .BR FFFF:0:0:0:0:0:0:1111 .
  119. The string representation for IP masks is a superset of the
  120. address representation. It includes slash notation that indicates
  121. the number of leading 1 bits in the mask. Thus, a
  122. V4 class C mask can be represented as
  123. .BR FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FF00 ,
  124. .BR 255.255.255.0 ,
  125. or
  126. .BR /120.
  127. The string representation of Ethernet addresses is exactly
  128. 12 hexadecimal digits.
  129. .PP
  130. .I Eipfmt
  131. is a
  132. .IR print (2)
  133. formatter for Ethernet (verb
  134. .BR E )
  135. addresses,
  136. IP V6 (verb
  137. .BR I )
  138. addresses,
  139. IP V4 (verb
  140. .BR V )
  141. addresses,
  142. and IP V6 (verb
  143. .BR M )
  144. masks.
  145. .PP
  146. .I Parseip
  147. converts a string pointed to by
  148. .I str
  149. to a 16-byte IP address starting at
  150. .IR ipaddr .
  151. As a concession to backwards compatibility,
  152. if the string is a V4 address, the return value
  153. is an unsigned long integer containing the big-endian V4 address.
  154. If not, the return value is 6.
  155. .I Parseipmask
  156. converts a string pointed to by
  157. .I str
  158. to a 6-byte IP mask starting at
  159. .IR ipaddr .
  160. It too returns an unsigned long big-endian V4 address or 6.
  161. Both routines return -1 on errors.
  162. .PP
  163. .I V4parseip
  164. converts a string pointed to by
  165. .I str
  166. to a 4-byte V4 IP address starting at
  167. .IR ipaddr .
  168. .PP
  169. .I V4parsecidr
  170. converts a string of the form
  171. addr/mask, pointed to by
  172. .IR str ,
  173. to a 4-byte V4 IP address starting at
  174. .I ipaddr
  175. and a 4-byte V4 IP mask starting at
  176. .IR mask .
  177. .PP
  178. .I Myipaddr
  179. returns the first valid IP address in
  180. the IP stack rooted at
  181. .IR net .
  182. .PP
  183. .I Parseether
  184. converts a string pointed to by
  185. .I str
  186. to a 6-byte Ethernet address starting at
  187. .IR eaddr .
  188. .I Myetheraddr
  189. reads the Ethernet address string from file
  190. .IB dev /1/stats
  191. and parses it into
  192. .IR eaddr .
  193. Both routines return a negative number on errors.
  194. .PP
  195. .I Maskip
  196. places the bit-wise AND of the IP addresses pointed
  197. to by its first two arguments into the buffer pointed
  198. to by the third.
  199. .PP
  200. .I Equivip
  201. returns non-zero if the IP addresses pointed to by its two
  202. arguments are equal.
  203. .I Equivip4
  204. operates on v4 addresses,
  205. .I equivip6
  206. operates on v6 addresses.
  207. .PP
  208. .I Defmask
  209. returns the standard class A, B, or C mask for
  210. .IR ipaddr .
  211. .PP
  212. .I Isv4
  213. returns non-zero if the V6 address is in the V4 space, that is,
  214. if it starts with
  215. .BR 0:0:0:0:0:0:FFFF .
  216. .I V4tov6
  217. converts the V4 address,
  218. .IR v4ip ,
  219. to a V6 address and puts the result in
  220. .IR v6ip .
  221. .I V6tov4
  222. converts the V6 address,
  223. .IR v6ip ,
  224. to a V4 address and puts the result in
  225. .IR v4ip .
  226. .PP
  227. .IR Hnputs ,
  228. .I hnputl
  229. and
  230. .I hnputv
  231. are used to store 16-bit, 32-bit, and 64-bit integers, respectively, into IP big-endian form.
  232. .IR Nhgets ,
  233. .I nhgetl
  234. and
  235. .I nhgetv
  236. convert big-endian 2, 4 and 8 byte quantities into integers (or uvlongs).
  237. .PP
  238. .I Pctlbsum
  239. returns the one's complement checksum used in IP protocols, typically invoked as
  240. .EX
  241. hnputs(hdr->cksum, ~ptclbsum(data, len) & 0xffff);
  242. .EE
  243. .PP
  244. A number of standard IP addresses in V6 format are also defined. They
  245. are:
  246. .IP \f5IPv4bcast
  247. the V4 broadcast address
  248. .IP \f5IPv4allsys
  249. the V4 all systems multicast address
  250. .IP \f5IPv4allrouter
  251. the V4 all routers multicast address
  252. .IP \f5IPallbits
  253. the V6 all bits on address
  254. .IP \f5IPnoaddr
  255. the V6 null address, all zeros
  256. .IP \f5v4prefix
  257. the IP V6 prefix to all embedded V4 addresses
  258. .PP
  259. .I Readipifc
  260. returns information about
  261. a particular interface (\fIindex\fP >= 0)
  262. or all IP interfaces (\fIindex\fP < 0)
  263. configured under a
  264. mount point
  265. .IR net ,
  266. default
  267. .BR /net .
  268. Each interface is described by one
  269. .I Ipifc
  270. structure which in turn points to a linked list of
  271. .IR Iplifc
  272. structures describing the addresses assigned
  273. to this interface.
  274. If the list
  275. .IR ifc
  276. is supplied,
  277. that list is freed.
  278. Thus, subsequent calls can be used
  279. to free the list returned by the previous call.
  280. .I Ipifc
  281. is:
  282. .PP
  283. .EX
  284. typedef struct Ipifc
  285. {
  286. Ipifc *next;
  287. Iplifc *lifc; /* local addressses */
  288. /* per ip interface */
  289. int index; /* number of interface in ipifc dir */
  290. char dev[64]; /* associated physical device */
  291. int mtu; /* max transfer unit */
  292. long validlt; /* valid life time */
  293. long preflt; /* preferred life time */
  294. uchar sendra6; /* on == send router adv */
  295. uchar recvra6; /* on == rcv router adv */
  296. ulong pktin; /* packets read */
  297. ulong pktout; /* packets written */
  298. ulong errin; /* read errors */
  299. ulong errout; /* write errors */
  300. Ipv6rp rp; /* route advertisement params */
  301. } Ipifc;
  302. .EE
  303. .PP
  304. .I Iplifc
  305. is:
  306. .PP
  307. .EX
  308. struct Iplifc
  309. {
  310. Iplifc *next;
  311. uchar ip[IPaddrlen];
  312. uchar mask[IPaddrlen];
  313. uchar net[IPaddrlen]; /* ip & mask */
  314. ulong preflt; /* preferred lifetime */
  315. ulong validlt; /* valid lifetime */
  316. };
  317. .EE
  318. .PP
  319. .I Ipv6rp
  320. is:
  321. struct Ipv6rp
  322. {
  323. int mflag;
  324. int oflag;
  325. int maxraint; /* max route adv interval */
  326. int minraint; /* min route adv interval */
  327. int linkmtu;
  328. int reachtime;
  329. int rxmitra;
  330. int ttl;
  331. int routerlt;
  332. };
  333. .PP
  334. .I Dev
  335. contains the first 64 bytes of the device configured with this
  336. interface.
  337. .I Net
  338. is
  339. .IB ip & mask
  340. if the network is multipoint or
  341. the remote address if the network is
  342. point to point.
  343. .SH SOURCE
  344. .B /sys/src/libip
  345. .SH SEE ALSO
  346. .IR print (2)