ip 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 /addr
  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
  237. .IR uvlong s).
  238. .PP
  239. .I Pctlbsum
  240. returns the one's complement checksum used in IP protocols, typically invoked as
  241. .IP
  242. .EX
  243. hnputs(hdr->cksum, ~ptclbsum(data, len) & 0xffff);
  244. .EE
  245. .PP
  246. A number of standard IP addresses in V6 format are also defined. They are:
  247. .TF IPv4allrouter
  248. .TP
  249. .B IPv4bcast
  250. the V4 broadcast address
  251. .TP
  252. .B IPv4allsys
  253. the V4 all systems multicast address
  254. .TP
  255. .B IPv4allrouter
  256. the V4 all routers multicast address
  257. .TP
  258. .B IPallbits
  259. the V6 all bits on address
  260. .TP
  261. .B IPnoaddr
  262. the V6 null address, all zeros
  263. .TP
  264. .B v4prefix
  265. the IP V6 prefix to all embedded V4 addresses
  266. .PD
  267. .PP
  268. .I Readipifc
  269. returns information about
  270. a particular interface
  271. .RI ( index
  272. >= 0)
  273. or all IP interfaces
  274. .RI ( index
  275. < 0)
  276. configured under a mount point
  277. .IR net ,
  278. default
  279. .BR /net .
  280. Each interface is described by one
  281. .I Ipifc
  282. structure which in turn points to a linked list of
  283. .IR Iplifc
  284. structures describing the addresses assigned
  285. to this interface.
  286. If the list
  287. .IR ifc
  288. is supplied,
  289. that list is freed.
  290. Thus, subsequent calls can be used
  291. to free the list returned by the previous call.
  292. .I Ipifc
  293. is:
  294. .PP
  295. .EX
  296. typedef struct Ipifc
  297. {
  298. Ipifc *next;
  299. Iplifc *lifc; /* local addressses */
  300. /* per ip interface */
  301. int index; /* number of interface in ipifc dir */
  302. char dev[64]; /* associated physical device */
  303. int mtu; /* max transfer unit */
  304. uchar sendra6; /* on == send router adv */
  305. uchar recvra6; /* on == rcv router adv */
  306. ulong pktin; /* packets read */
  307. ulong pktout; /* packets written */
  308. ulong errin; /* read errors */
  309. ulong errout; /* write errors */
  310. Ipv6rp rp; /* route advertisement params */
  311. } Ipifc;
  312. .EE
  313. .PP
  314. .I Iplifc
  315. is:
  316. .PP
  317. .EX
  318. struct Iplifc
  319. {
  320. Iplifc *next;
  321. uchar ip[IPaddrlen];
  322. uchar mask[IPaddrlen];
  323. uchar net[IPaddrlen]; /* ip & mask */
  324. ulong preflt; /* preferred lifetime */
  325. ulong validlt; /* valid lifetime */
  326. };
  327. .EE
  328. .PP
  329. .I Ipv6rp
  330. is:
  331. .PP
  332. .EX
  333. struct Ipv6rp
  334. {
  335. int mflag;
  336. int oflag;
  337. int maxraint; /* max route adv interval */
  338. int minraint; /* min route adv interval */
  339. int linkmtu;
  340. int reachtime;
  341. int rxmitra;
  342. int ttl;
  343. int routerlt;
  344. };
  345. .EE
  346. .PP
  347. .I Dev
  348. contains the first 64 bytes of the device configured with this
  349. interface.
  350. .I Net
  351. is
  352. .IB ip & mask
  353. if the network is multipoint or
  354. the remote address if the network is
  355. point to point.
  356. .SH SOURCE
  357. .B /sys/src/libip
  358. .SH SEE ALSO
  359. .IR print (2),
  360. .IR ip (3)