|   |  | 1 |  | // Copyright (c) Microsoft Corporation. All rights reserved. | 
|   |  | 2 |  | // Licensed under the MIT License. | 
|   |  | 3 |  |  | 
|   |  | 4 |  | using System; | 
|   |  | 5 |  | using System.ComponentModel; | 
|   |  | 6 |  | using System.Net; | 
|   |  | 7 |  |  | 
|   |  | 8 |  | namespace Azure.Storage.Sas | 
|   |  | 9 |  | { | 
|   |  | 10 |  |     /// <summary> | 
|   |  | 11 |  |     /// Represents a range of allowed IP addresses for constructing a Shared | 
|   |  | 12 |  |     /// Access Signature. | 
|   |  | 13 |  |     /// </summary> | 
|   |  | 14 |  |     public readonly struct SasIPRange : IEquatable<SasIPRange> | 
|   |  | 15 |  |     { | 
|   |  | 16 |  |         /// <summary> | 
|   |  | 17 |  |         /// Gets the start of the IP range.  Not specified if equal to null or | 
|   |  | 18 |  |         /// <see cref="IPAddress.None"/>. | 
|   |  | 19 |  |         /// </summary> | 
|   | 2016 | 20 |  |         public IPAddress Start { get; } | 
|   |  | 21 |  |  | 
|   |  | 22 |  |         /// <summary> | 
|   |  | 23 |  |         /// Gets the optional end of the IP range.  Not specified if equal to | 
|   |  | 24 |  |         /// null or <see cref="IPAddress.None"/>. | 
|   |  | 25 |  |         /// </summary> | 
|   | 396 | 26 |  |         public IPAddress End { get; } | 
|   |  | 27 |  |  | 
|   |  | 28 |  |         /// <summary> | 
|   |  | 29 |  |         /// Creates a new <see cref="SasIPRange"/>. | 
|   |  | 30 |  |         /// </summary> | 
|   |  | 31 |  |         /// <param name="start"> | 
|   |  | 32 |  |         /// The range's start <see cref="IPAddress"/>. | 
|   |  | 33 |  |         /// </param> | 
|   |  | 34 |  |         /// <param name="end"> | 
|   |  | 35 |  |         /// The range's optional end <see cref="IPAddress"/>. | 
|   |  | 36 |  |         /// </param> | 
|   |  | 37 |  |         public SasIPRange(IPAddress start, IPAddress end = null) | 
|   |  | 38 |  |         { | 
|   | 474 | 39 |  |             Start = start ?? IPAddress.None; | 
|   | 474 | 40 |  |             End = end ?? IPAddress.None; | 
|   | 474 | 41 |  |         } | 
|   |  | 42 |  |  | 
|   |  | 43 |  |         /// <summary> | 
|   |  | 44 |  |         /// Check if an <see cref="IPAddress"/> was not provided. | 
|   |  | 45 |  |         /// </summary> | 
|   |  | 46 |  |         /// <param name="address">The address to check.</param> | 
|   |  | 47 |  |         /// <returns>True if it's empty, false otherwise.</returns> | 
|   |  | 48 |  |         private static bool IsEmpty(IPAddress address) => | 
|   | 1908 | 49 |  |             address == null || address == IPAddress.None; | 
|   |  | 50 |  |  | 
|   |  | 51 |  |         /// <summary> | 
|   |  | 52 |  |         /// Creates a string representation of an <see cref="SasIPRange"/>. | 
|   |  | 53 |  |         /// </summary> | 
|   |  | 54 |  |         /// <returns> | 
|   |  | 55 |  |         /// A string representation of an <see cref="SasIPRange"/>. | 
|   |  | 56 |  |         /// </returns> | 
|   |  | 57 |  |         public override string ToString() => | 
|   | 1710 | 58 |  |             IsEmpty(Start) ? string.Empty : | 
|   | 1710 | 59 |  |             IsEmpty(End) ? Start.ToString() : | 
|   | 1710 | 60 |  |             Start.ToString() + "-" + End.ToString(); | 
|   |  | 61 |  |  | 
|   |  | 62 |  |         /// <summary> | 
|   |  | 63 |  |         /// Parse an IP range string into a new <see cref="SasIPRange"/>. | 
|   |  | 64 |  |         /// </summary> | 
|   |  | 65 |  |         /// <param name="s">IP range string to parse.</param> | 
|   |  | 66 |  |         /// <returns>The parsed <see cref="SasIPRange"/>.</returns> | 
|   |  | 67 |  |         public static SasIPRange Parse(string s) | 
|   |  | 68 |  |         { | 
|   | 36 | 69 |  |             var dashIndex = s.IndexOf('-'); | 
|   | 36 | 70 |  |             return dashIndex == -1 ? | 
|   | 36 | 71 |  |                 new SasIPRange(IPAddress.Parse(s)) : | 
|   | 36 | 72 |  |                 new SasIPRange( | 
|   | 36 | 73 |  |                     IPAddress.Parse(s.Substring(0, dashIndex)), | 
|   | 36 | 74 |  |                     IPAddress.Parse(s.Substring(dashIndex + 1))); | 
|   |  | 75 |  |         } | 
|   |  | 76 |  |  | 
|   |  | 77 |  |         /// <summary> | 
|   |  | 78 |  |         /// Check if two <see cref="SasIPRange"/> instances are equal. | 
|   |  | 79 |  |         /// </summary> | 
|   |  | 80 |  |         /// <param name="obj">The instance to compare to.</param> | 
|   |  | 81 |  |         /// <returns>True if they're equal, false otherwise.</returns> | 
|   |  | 82 |  |         [EditorBrowsable(EditorBrowsableState.Never)] | 
|   |  | 83 |  |         public override bool Equals(object obj) => | 
|   | 0 | 84 |  |             obj is SasIPRange other && Equals(other); | 
|   |  | 85 |  |  | 
|   |  | 86 |  |         /// <summary> | 
|   |  | 87 |  |         /// Get a hash code for the <see cref="SasIPRange"/>. | 
|   |  | 88 |  |         /// </summary> | 
|   |  | 89 |  |         /// <returns>Hash code for the <see cref="SasIPRange"/>.</returns> | 
|   |  | 90 |  |         [EditorBrowsable(EditorBrowsableState.Never)] | 
|   |  | 91 |  |         public override int GetHashCode() => | 
|   | 0 | 92 |  |             (Start?.GetHashCode() ?? 0) ^ (End?.GetHashCode() ?? 0); | 
|   |  | 93 |  |  | 
|   |  | 94 |  |         /// <summary> | 
|   |  | 95 |  |         /// Check if two <see cref="SasIPRange"/> instances are equal. | 
|   |  | 96 |  |         /// </summary> | 
|   |  | 97 |  |         /// <param name="left">The first instance to compare.</param> | 
|   |  | 98 |  |         /// <param name="right">The second instance to compare.</param> | 
|   |  | 99 |  |         /// <returns>True if they're equal, false otherwise.</returns> | 
|   |  | 100 |  |         public static bool operator ==(SasIPRange left, SasIPRange right) => | 
|   | 0 | 101 |  |             left.Equals(right); | 
|   |  | 102 |  |  | 
|   |  | 103 |  |         /// <summary> | 
|   |  | 104 |  |         /// Check if two <see cref="SasIPRange"/> instances are not equal. | 
|   |  | 105 |  |         /// </summary> | 
|   |  | 106 |  |         /// <param name="left">The first instance to compare.</param> | 
|   |  | 107 |  |         /// <param name="right">The second instance to compare.</param> | 
|   |  | 108 |  |         /// <returns>True if they're not equal, false otherwise.</returns> | 
|   |  | 109 |  |         public static bool operator !=(SasIPRange left, SasIPRange right) => | 
|   | 0 | 110 |  |             !(left == right); | 
|   |  | 111 |  |  | 
|   |  | 112 |  |         /// <summary> | 
|   |  | 113 |  |         /// Check if two <see cref="SasIPRange"/> instances are equal. | 
|   |  | 114 |  |         /// </summary> | 
|   |  | 115 |  |         /// <param name="other">The instance to compare to.</param> | 
|   |  | 116 |  |         /// <returns>True if they're equal, false otherwise.</returns> | 
|   |  | 117 |  |         public bool Equals(SasIPRange other) => | 
|   | 54 | 118 |  |             ((IsEmpty(Start) && IsEmpty(other.Start)) || | 
|   | 54 | 119 |  |              (Start != null && Start.Equals(other.Start))) && | 
|   | 54 | 120 |  |             ((IsEmpty(End) && IsEmpty(other.End)) || | 
|   | 54 | 121 |  |              (End != null && End.Equals(other.End))); | 
|   |  | 122 |  |     } | 
|   |  | 123 |  | } |