1
0

ip 6.8 KB

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