ip 7.2 KB

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