UdpClient.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. //
  2. // System.Net.Sockets.UdpClient.cs
  3. //
  4. // Author:
  5. // Gonzalo Paniagua Javier <[email protected]>
  6. // Sridhar Kulkarni ([email protected])
  7. // Marek Safar ([email protected])
  8. //
  9. // Copyright (C) Ximian, Inc. http://www.ximian.com
  10. // Copyright 2011 Xamarin Inc.
  11. //
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Net;
  34. using System.Threading.Tasks;
  35. namespace System.Net.Sockets
  36. {
  37. public class UdpClient : IDisposable
  38. {
  39. private bool disposed = false;
  40. private bool active = false;
  41. private Socket socket;
  42. private AddressFamily family = AddressFamily.InterNetwork;
  43. private byte[] recvbuffer;
  44. public UdpClient () : this(AddressFamily.InterNetwork)
  45. {
  46. }
  47. public UdpClient(AddressFamily family)
  48. {
  49. if(family != AddressFamily.InterNetwork && family != AddressFamily.InterNetworkV6)
  50. throw new ArgumentException ("Family must be InterNetwork or InterNetworkV6", "family");
  51. this.family = family;
  52. InitSocket (null);
  53. }
  54. public UdpClient (int port)
  55. {
  56. if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
  57. throw new ArgumentOutOfRangeException ("port");
  58. this.family = AddressFamily.InterNetwork;
  59. IPEndPoint localEP = new IPEndPoint (IPAddress.Any, port);
  60. InitSocket (localEP);
  61. }
  62. public UdpClient (IPEndPoint localEP)
  63. {
  64. if (localEP == null)
  65. throw new ArgumentNullException ("localEP");
  66. this.family = localEP.AddressFamily;
  67. InitSocket (localEP);
  68. }
  69. public UdpClient (int port, AddressFamily family)
  70. {
  71. if (family != AddressFamily.InterNetwork && family != AddressFamily.InterNetworkV6)
  72. throw new ArgumentException ("Family must be InterNetwork or InterNetworkV6", "family");
  73. if (port < IPEndPoint.MinPort ||
  74. port > IPEndPoint.MaxPort) {
  75. throw new ArgumentOutOfRangeException ("port");
  76. }
  77. this.family = family;
  78. IPEndPoint localEP;
  79. if (family == AddressFamily.InterNetwork)
  80. localEP = new IPEndPoint (IPAddress.Any, port);
  81. else
  82. localEP = new IPEndPoint (IPAddress.IPv6Any, port);
  83. InitSocket (localEP);
  84. }
  85. public UdpClient (string hostname, int port)
  86. {
  87. if (hostname == null)
  88. throw new ArgumentNullException ("hostname");
  89. if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
  90. throw new ArgumentOutOfRangeException ("port");
  91. InitSocket (null);
  92. Connect (hostname, port);
  93. }
  94. private void InitSocket (EndPoint localEP)
  95. {
  96. if(socket != null) {
  97. socket.Close();
  98. socket = null;
  99. }
  100. socket = new Socket (family, SocketType.Dgram, ProtocolType.Udp);
  101. if (localEP != null)
  102. socket.Bind (localEP);
  103. }
  104. public void AllowNatTraversal (bool allowed)
  105. {
  106. if (allowed)
  107. socket.SetIPProtectionLevel (IPProtectionLevel.Unrestricted);
  108. else
  109. socket.SetIPProtectionLevel (IPProtectionLevel.EdgeRestricted);
  110. }
  111. public void Close ()
  112. {
  113. Dispose ();
  114. }
  115. #region Connect
  116. void DoConnect (IPEndPoint endPoint)
  117. {
  118. /* Catch EACCES and turn on SO_BROADCAST then,
  119. * as UDP sockets don't have it set by default
  120. */
  121. try {
  122. socket.Connect (endPoint);
  123. } catch (SocketException ex) {
  124. if (ex.ErrorCode == (int)SocketError.AccessDenied) {
  125. socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  126. socket.Connect (endPoint);
  127. } else {
  128. throw;
  129. }
  130. }
  131. }
  132. public void Connect (IPEndPoint endPoint)
  133. {
  134. CheckDisposed ();
  135. if (endPoint == null)
  136. throw new ArgumentNullException ("endPoint");
  137. DoConnect (endPoint);
  138. active = true;
  139. }
  140. public void Connect (IPAddress addr, int port)
  141. {
  142. if (addr == null)
  143. throw new ArgumentNullException ("addr");
  144. if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
  145. throw new ArgumentOutOfRangeException ("port");
  146. Connect (new IPEndPoint (addr, port));
  147. }
  148. public void Connect (string hostname, int port)
  149. {
  150. if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
  151. throw new ArgumentOutOfRangeException ("port");
  152. IPAddress[] addresses = Dns.GetHostAddresses (hostname);
  153. for(int i=0; i<addresses.Length; i++) {
  154. try {
  155. this.family = addresses[i].AddressFamily;
  156. Connect (new IPEndPoint (addresses[i], port));
  157. break;
  158. } catch(Exception e) {
  159. if(i == addresses.Length - 1){
  160. if(socket != null) {
  161. socket.Close();
  162. socket = null;
  163. }
  164. /// This is the last entry, re-throw the exception
  165. throw e;
  166. }
  167. }
  168. }
  169. }
  170. #endregion
  171. #region Multicast methods
  172. public void DropMulticastGroup (IPAddress multicastAddr)
  173. {
  174. CheckDisposed ();
  175. if (multicastAddr == null)
  176. throw new ArgumentNullException ("multicastAddr");
  177. if(family == AddressFamily.InterNetwork)
  178. socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
  179. new MulticastOption (multicastAddr));
  180. else
  181. socket.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
  182. new IPv6MulticastOption (multicastAddr));
  183. }
  184. public void DropMulticastGroup (IPAddress multicastAddr,
  185. int ifindex)
  186. {
  187. CheckDisposed ();
  188. /* LAMESPEC: exceptions haven't been specified
  189. * for this overload.
  190. */
  191. if (multicastAddr == null) {
  192. throw new ArgumentNullException ("multicastAddr");
  193. }
  194. /* Does this overload only apply to IPv6?
  195. * Only the IPv6MulticastOption has an
  196. * ifindex-using constructor. The MS docs
  197. * don't say.
  198. */
  199. if (family == AddressFamily.InterNetworkV6) {
  200. socket.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership, new IPv6MulticastOption (multicastAddr, ifindex));
  201. }
  202. }
  203. public void JoinMulticastGroup (IPAddress multicastAddr)
  204. {
  205. CheckDisposed ();
  206. if (multicastAddr == null)
  207. throw new ArgumentNullException ("multicastAddr");
  208. if(family == AddressFamily.InterNetwork)
  209. socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
  210. new MulticastOption (multicastAddr));
  211. else
  212. socket.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
  213. new IPv6MulticastOption (multicastAddr));
  214. }
  215. public void JoinMulticastGroup (int ifindex,
  216. IPAddress multicastAddr)
  217. {
  218. CheckDisposed ();
  219. if (multicastAddr == null)
  220. throw new ArgumentNullException ("multicastAddr");
  221. if (family == AddressFamily.InterNetworkV6)
  222. socket.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption (multicastAddr, ifindex));
  223. else
  224. throw new SocketException ((int) SocketError.OperationNotSupported);
  225. }
  226. public void JoinMulticastGroup (IPAddress multicastAddr, int timeToLive)
  227. {
  228. CheckDisposed ();
  229. if (multicastAddr == null)
  230. throw new ArgumentNullException ("multicastAddr");
  231. if (timeToLive < 0 || timeToLive > 255)
  232. throw new ArgumentOutOfRangeException ("timeToLive");
  233. JoinMulticastGroup (multicastAddr);
  234. if(family == AddressFamily.InterNetwork)
  235. socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive,
  236. timeToLive);
  237. else
  238. socket.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.MulticastTimeToLive,
  239. timeToLive);
  240. }
  241. public void JoinMulticastGroup (IPAddress multicastAddr,
  242. IPAddress localAddress)
  243. {
  244. CheckDisposed ();
  245. if (family == AddressFamily.InterNetwork)
  246. socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption (multicastAddr, localAddress));
  247. else
  248. throw new SocketException ((int) SocketError.OperationNotSupported);
  249. }
  250. #endregion
  251. #region Data I/O
  252. public byte [] Receive (ref IPEndPoint remoteEP)
  253. {
  254. CheckDisposed ();
  255. byte [] recBuffer = new byte [65536]; // Max. size
  256. EndPoint endPoint = (EndPoint) remoteEP;
  257. int dataRead = socket.ReceiveFrom (recBuffer, ref endPoint);
  258. if (dataRead < recBuffer.Length)
  259. recBuffer = CutArray (recBuffer, dataRead);
  260. remoteEP = (IPEndPoint) endPoint;
  261. return recBuffer;
  262. }
  263. int DoSend (byte[] dgram, int bytes, IPEndPoint endPoint)
  264. {
  265. /* Catch EACCES and turn on SO_BROADCAST then,
  266. * as UDP sockets don't have it set by default
  267. */
  268. try {
  269. if (endPoint == null) {
  270. return(socket.Send (dgram, 0, bytes,
  271. SocketFlags.None));
  272. } else {
  273. return(socket.SendTo (dgram, 0, bytes,
  274. SocketFlags.None,
  275. endPoint));
  276. }
  277. } catch (SocketException ex) {
  278. if (ex.ErrorCode == (int)SocketError.AccessDenied) {
  279. socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  280. if (endPoint == null) {
  281. return(socket.Send (dgram, 0, bytes, SocketFlags.None));
  282. } else {
  283. return(socket.SendTo (dgram, 0, bytes, SocketFlags.None, endPoint));
  284. }
  285. } else {
  286. throw;
  287. }
  288. }
  289. }
  290. public int Send (byte [] dgram, int bytes)
  291. {
  292. CheckDisposed ();
  293. if (dgram == null)
  294. throw new ArgumentNullException ("dgram");
  295. if (!active)
  296. throw new InvalidOperationException ("Operation not allowed on " +
  297. "non-connected sockets.");
  298. return(DoSend (dgram, bytes, null));
  299. }
  300. public int Send (byte [] dgram, int bytes, IPEndPoint endPoint)
  301. {
  302. CheckDisposed ();
  303. if (dgram == null)
  304. throw new ArgumentNullException ("dgram is null");
  305. if (active) {
  306. if (endPoint != null)
  307. throw new InvalidOperationException ("Cannot send packets to an " +
  308. "arbitrary host while connected.");
  309. return(DoSend (dgram, bytes, null));
  310. }
  311. return(DoSend (dgram, bytes, endPoint));
  312. }
  313. public int Send (byte [] dgram, int bytes, string hostname, int port)
  314. {
  315. return Send (dgram, bytes,
  316. new IPEndPoint (Dns.GetHostAddresses (hostname) [0], port));
  317. }
  318. private byte [] CutArray (byte [] orig, int length)
  319. {
  320. byte [] newArray = new byte [length];
  321. Buffer.BlockCopy (orig, 0, newArray, 0, length);
  322. return newArray;
  323. }
  324. #endregion
  325. IAsyncResult DoBeginSend (byte[] datagram, int bytes,
  326. IPEndPoint endPoint,
  327. AsyncCallback requestCallback,
  328. object state)
  329. {
  330. /* Catch EACCES and turn on SO_BROADCAST then,
  331. * as UDP sockets don't have it set by default
  332. */
  333. try {
  334. if (endPoint == null) {
  335. return(socket.BeginSend (datagram, 0, bytes, SocketFlags.None, requestCallback, state));
  336. } else {
  337. return(socket.BeginSendTo (datagram, 0, bytes, SocketFlags.None, endPoint, requestCallback, state));
  338. }
  339. } catch (SocketException ex) {
  340. if (ex.ErrorCode == (int)SocketError.AccessDenied) {
  341. socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  342. if (endPoint == null) {
  343. return(socket.BeginSend (datagram, 0, bytes, SocketFlags.None, requestCallback, state));
  344. } else {
  345. return(socket.BeginSendTo (datagram, 0, bytes, SocketFlags.None, endPoint, requestCallback, state));
  346. }
  347. } else {
  348. throw;
  349. }
  350. }
  351. }
  352. public IAsyncResult BeginSend (byte[] datagram, int bytes,
  353. AsyncCallback requestCallback,
  354. object state)
  355. {
  356. return(BeginSend (datagram, bytes, null,
  357. requestCallback, state));
  358. }
  359. public IAsyncResult BeginSend (byte[] datagram, int bytes,
  360. IPEndPoint endPoint,
  361. AsyncCallback requestCallback,
  362. object state)
  363. {
  364. CheckDisposed ();
  365. if (datagram == null) {
  366. throw new ArgumentNullException ("datagram");
  367. }
  368. return(DoBeginSend (datagram, bytes, endPoint,
  369. requestCallback, state));
  370. }
  371. public IAsyncResult BeginSend (byte[] datagram, int bytes,
  372. string hostname, int port,
  373. AsyncCallback requestCallback,
  374. object state)
  375. {
  376. return(BeginSend (datagram, bytes, new IPEndPoint (Dns.GetHostAddresses (hostname) [0], port), requestCallback, state));
  377. }
  378. public int EndSend (IAsyncResult asyncResult)
  379. {
  380. CheckDisposed ();
  381. if (asyncResult == null) {
  382. throw new ArgumentNullException ("asyncResult is a null reference");
  383. }
  384. return(socket.EndSend (asyncResult));
  385. }
  386. public IAsyncResult BeginReceive (AsyncCallback requestCallback, object state)
  387. {
  388. CheckDisposed ();
  389. recvbuffer = new byte[8192];
  390. EndPoint ep;
  391. if (family == AddressFamily.InterNetwork) {
  392. ep = new IPEndPoint (IPAddress.Any, 0);
  393. } else {
  394. ep = new IPEndPoint (IPAddress.IPv6Any, 0);
  395. }
  396. return(socket.BeginReceiveFrom (recvbuffer, 0, 8192,
  397. SocketFlags.None,
  398. ref ep,
  399. requestCallback, state));
  400. }
  401. public byte[] EndReceive (IAsyncResult asyncResult, ref IPEndPoint remoteEP)
  402. {
  403. CheckDisposed ();
  404. if (asyncResult == null) {
  405. throw new ArgumentNullException ("asyncResult is a null reference");
  406. }
  407. EndPoint ep;
  408. if (family == AddressFamily.InterNetwork) {
  409. ep = new IPEndPoint (IPAddress.Any, 0);
  410. } else {
  411. ep = new IPEndPoint (IPAddress.IPv6Any, 0);
  412. }
  413. int bytes = socket.EndReceiveFrom (asyncResult,
  414. ref ep);
  415. remoteEP = (IPEndPoint)ep;
  416. /* Need to copy into a new array here, because
  417. * otherwise the returned array length is not
  418. * 'bytes'
  419. */
  420. byte[] buf = new byte[bytes];
  421. Array.Copy (recvbuffer, buf, bytes);
  422. return(buf);
  423. }
  424. #region Properties
  425. protected bool Active {
  426. get { return active; }
  427. set { active = value; }
  428. }
  429. public Socket Client {
  430. get { return socket; }
  431. set { socket = value; }
  432. }
  433. public int Available
  434. {
  435. get {
  436. return(socket.Available);
  437. }
  438. }
  439. public bool DontFragment
  440. {
  441. get {
  442. return(socket.DontFragment);
  443. }
  444. set {
  445. socket.DontFragment = value;
  446. }
  447. }
  448. public bool EnableBroadcast
  449. {
  450. get {
  451. return(socket.EnableBroadcast);
  452. }
  453. set {
  454. socket.EnableBroadcast = value;
  455. }
  456. }
  457. public bool ExclusiveAddressUse
  458. {
  459. get {
  460. return(socket.ExclusiveAddressUse);
  461. }
  462. set {
  463. socket.ExclusiveAddressUse = value;
  464. }
  465. }
  466. public bool MulticastLoopback
  467. {
  468. get {
  469. return(socket.MulticastLoopback);
  470. }
  471. set {
  472. socket.MulticastLoopback = value;
  473. }
  474. }
  475. public short Ttl
  476. {
  477. get {
  478. return(socket.Ttl);
  479. }
  480. set {
  481. socket.Ttl = value;
  482. }
  483. }
  484. #endregion
  485. #region Disposing
  486. public void Dispose ()
  487. {
  488. Dispose (true);
  489. GC.SuppressFinalize (this);
  490. }
  491. protected virtual void Dispose (bool disposing)
  492. {
  493. if (disposed)
  494. return;
  495. disposed = true;
  496. if (disposing){
  497. if (socket != null)
  498. socket.Close ();
  499. socket = null;
  500. }
  501. }
  502. ~UdpClient ()
  503. {
  504. Dispose (false);
  505. }
  506. private void CheckDisposed ()
  507. {
  508. if (disposed)
  509. throw new ObjectDisposedException (GetType().FullName);
  510. }
  511. #endregion
  512. public Task<UdpReceiveResult> ReceiveAsync ()
  513. {
  514. return Task<UdpReceiveResult>.Factory.FromAsync (BeginReceive, r => {
  515. IPEndPoint remoteEndPoint = null;
  516. return new UdpReceiveResult (EndReceive (r, ref remoteEndPoint), remoteEndPoint);
  517. }, null);
  518. }
  519. public Task<int> SendAsync (byte[] datagram, int bytes)
  520. {
  521. return Task<int>.Factory.FromAsync (BeginSend, EndSend, datagram, bytes, null);
  522. }
  523. public Task<int> SendAsync (byte[] datagram, int bytes, IPEndPoint endPoint)
  524. {
  525. return Task<int>.Factory.FromAsync (BeginSend, EndSend, datagram, bytes, endPoint, null);
  526. }
  527. public Task<int> SendAsync (byte[] datagram, int bytes, string hostname, int port)
  528. {
  529. var t = Tuple.Create (datagram, bytes, hostname, port, this);
  530. return Task<int>.Factory.FromAsync ((callback, state) => {
  531. var d = (Tuple<byte[], int, string, int, UdpClient>) state;
  532. return d.Item5.BeginSend (d.Item1, d.Item2, d.Item3, d.Item4, callback, null);
  533. }, EndSend, t);
  534. }
  535. }
  536. }