| | 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.Data.Tables.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 TableSasIPRange : IEquatable<TableSasIPRange> |
| | 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> |
| 80 | 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> |
| 0 | 26 | | public IPAddress End { get; } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Creates a new <see cref="TableSasIPRange"/>. |
| | 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 TableSasIPRange(IPAddress start, IPAddress end = null) |
| | 38 | | { |
| 0 | 39 | | Start = start ?? IPAddress.None; |
| 0 | 40 | | End = end ?? IPAddress.None; |
| 0 | 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) => |
| 80 | 49 | | address == null || address == IPAddress.None; |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Creates a string representation of an <see cref="TableSasIPRange"/>. |
| | 53 | | /// </summary> |
| | 54 | | /// <returns> |
| | 55 | | /// A string representation of an <see cref="TableSasIPRange"/>. |
| | 56 | | /// </returns> |
| | 57 | | public override string ToString() => |
| 80 | 58 | | IsEmpty(Start) ? string.Empty : |
| 80 | 59 | | IsEmpty(End) ? Start.ToString() : |
| 80 | 60 | | Start.ToString() + "-" + End.ToString(); |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Parse an IP range string into a new <see cref="TableSasIPRange"/>. |
| | 64 | | /// </summary> |
| | 65 | | /// <param name="s">IP range string to parse.</param> |
| | 66 | | /// <returns>The parsed <see cref="TableSasIPRange"/>.</returns> |
| | 67 | | public static TableSasIPRange Parse(string s) |
| | 68 | | { |
| 0 | 69 | | var dashIndex = s.IndexOf('-'); |
| 0 | 70 | | return dashIndex == -1 ? |
| 0 | 71 | | new TableSasIPRange(IPAddress.Parse(s)) : |
| 0 | 72 | | new TableSasIPRange( |
| 0 | 73 | | IPAddress.Parse(s.Substring(0, dashIndex)), |
| 0 | 74 | | IPAddress.Parse(s.Substring(dashIndex + 1))); |
| | 75 | | } |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Check if two <see cref="TableSasIPRange"/> 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 TableSasIPRange other && Equals(other); |
| | 85 | |
|
| | 86 | | /// <summary> |
| | 87 | | /// Get a hash code for the <see cref="TableSasIPRange"/>. |
| | 88 | | /// </summary> |
| | 89 | | /// <returns>Hash code for the <see cref="TableSasIPRange"/>.</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="TableSasIPRange"/> 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 ==(TableSasIPRange left, TableSasIPRange right) => |
| 0 | 101 | | left.Equals(right); |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// Check if two <see cref="TableSasIPRange"/> 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 !=(TableSasIPRange left, TableSasIPRange right) => |
| 0 | 110 | | !(left == right); |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// Check if two <see cref="TableSasIPRange"/> 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(TableSasIPRange other) => |
| 0 | 118 | | ((IsEmpty(Start) && IsEmpty(other.Start)) || |
| 0 | 119 | | (Start != null && Start.Equals(other.Start))) && |
| 0 | 120 | | ((IsEmpty(End) && IsEmpty(other.End)) || |
| 0 | 121 | | (End != null && End.Equals(other.End))); |
| | 122 | | } |
| | 123 | | } |