Mapping.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Authors:
  3. // Alan McGovern alan.mcgovern@gmail.com
  4. // Ben Motmans <ben.motmans@gmail.com>
  5. //
  6. // Copyright (C) 2006 Alan McGovern
  7. // Copyright (C) 2007 Ben Motmans
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. namespace Mono.Nat
  30. {
  31. public class Mapping
  32. {
  33. private string description;
  34. private DateTime expiration;
  35. private int lifetime;
  36. private int privatePort;
  37. private Protocol protocol;
  38. private int publicPort;
  39. public Mapping(Protocol protocol, int privatePort, int publicPort)
  40. : this (protocol, privatePort, publicPort, 0)
  41. {
  42. }
  43. public Mapping(Protocol protocol, int privatePort, int publicPort, int lifetime)
  44. {
  45. this.protocol = protocol;
  46. this.privatePort = privatePort;
  47. this.publicPort = publicPort;
  48. this.lifetime = lifetime;
  49. if (lifetime == int.MaxValue)
  50. this.expiration = DateTime.MaxValue;
  51. else if (lifetime == 0)
  52. this.expiration = DateTime.Now;
  53. else
  54. this.expiration = DateTime.Now.AddSeconds (lifetime);
  55. }
  56. public string Description
  57. {
  58. get { return description; }
  59. set { description = value; }
  60. }
  61. public Protocol Protocol
  62. {
  63. get { return protocol; }
  64. internal set { protocol = value; }
  65. }
  66. public int PrivatePort
  67. {
  68. get { return privatePort; }
  69. internal set { privatePort = value; }
  70. }
  71. public int PublicPort
  72. {
  73. get { return publicPort; }
  74. internal set { publicPort = value; }
  75. }
  76. public int Lifetime
  77. {
  78. get { return lifetime; }
  79. internal set { lifetime = value; }
  80. }
  81. public DateTime Expiration
  82. {
  83. get { return expiration; }
  84. internal set { expiration = value; }
  85. }
  86. public bool IsExpired()
  87. {
  88. return expiration < DateTime.Now;
  89. }
  90. public override bool Equals(object obj)
  91. {
  92. var other = obj as Mapping;
  93. return other == null ? false : this.protocol == other.protocol &&
  94. this.privatePort == other.privatePort && this.publicPort == other.publicPort;
  95. }
  96. public override int GetHashCode()
  97. {
  98. return this.protocol.GetHashCode() ^ this.privatePort.GetHashCode() ^ this.publicPort.GetHashCode();
  99. }
  100. public override string ToString()
  101. {
  102. return String.Format( "Protocol: {0}, Public Port: {1}, Private Port: {2}, Description: {3}, Expiration: {4}, Lifetime: {5}",
  103. this.protocol, this.publicPort, this.privatePort, this.description, this.expiration, this.lifetime );
  104. }
  105. }
  106. }