1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.net.metrics; 18 19 import android.annotation.NonNull; 20 import android.annotation.SystemApi; 21 import android.annotation.TestApi; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 25 /** 26 * An event logged when the APF packet socket receives an RA packet. 27 * {@hide} 28 */ 29 @SystemApi 30 @TestApi 31 public final class RaEvent implements IpConnectivityLog.Event { 32 33 private static final long NO_LIFETIME = -1L; 34 35 // Lifetime in seconds of options found in a single RA packet. 36 // When an option is not set, the value of the associated field is -1; 37 /** @hide */ 38 public final long routerLifetime; 39 /** @hide */ 40 public final long prefixValidLifetime; 41 /** @hide */ 42 public final long prefixPreferredLifetime; 43 /** @hide */ 44 public final long routeInfoLifetime; 45 /** @hide */ 46 public final long rdnssLifetime; 47 /** @hide */ 48 public final long dnsslLifetime; 49 50 /** @hide */ RaEvent(long routerLifetime, long prefixValidLifetime, long prefixPreferredLifetime, long routeInfoLifetime, long rdnssLifetime, long dnsslLifetime)51 public RaEvent(long routerLifetime, long prefixValidLifetime, long prefixPreferredLifetime, 52 long routeInfoLifetime, long rdnssLifetime, long dnsslLifetime) { 53 this.routerLifetime = routerLifetime; 54 this.prefixValidLifetime = prefixValidLifetime; 55 this.prefixPreferredLifetime = prefixPreferredLifetime; 56 this.routeInfoLifetime = routeInfoLifetime; 57 this.rdnssLifetime = rdnssLifetime; 58 this.dnsslLifetime = dnsslLifetime; 59 } 60 61 /** @hide */ RaEvent(Parcel in)62 private RaEvent(Parcel in) { 63 routerLifetime = in.readLong(); 64 prefixValidLifetime = in.readLong(); 65 prefixPreferredLifetime = in.readLong(); 66 routeInfoLifetime = in.readLong(); 67 rdnssLifetime = in.readLong(); 68 dnsslLifetime = in.readLong(); 69 } 70 71 /** @hide */ 72 @Override writeToParcel(Parcel out, int flags)73 public void writeToParcel(Parcel out, int flags) { 74 out.writeLong(routerLifetime); 75 out.writeLong(prefixValidLifetime); 76 out.writeLong(prefixPreferredLifetime); 77 out.writeLong(routeInfoLifetime); 78 out.writeLong(rdnssLifetime); 79 out.writeLong(dnsslLifetime); 80 } 81 82 /** @hide */ 83 @Override describeContents()84 public int describeContents() { 85 return 0; 86 } 87 88 @Override toString()89 public String toString() { 90 return new StringBuilder("RaEvent(lifetimes: ") 91 .append(String.format("router=%ds, ", routerLifetime)) 92 .append(String.format("prefix_valid=%ds, ", prefixValidLifetime)) 93 .append(String.format("prefix_preferred=%ds, ", prefixPreferredLifetime)) 94 .append(String.format("route_info=%ds, ", routeInfoLifetime)) 95 .append(String.format("rdnss=%ds, ", rdnssLifetime)) 96 .append(String.format("dnssl=%ds)", dnsslLifetime)) 97 .toString(); 98 } 99 100 @Override equals(Object obj)101 public boolean equals(Object obj) { 102 if (obj == null || !(obj.getClass().equals(RaEvent.class))) return false; 103 final RaEvent other = (RaEvent) obj; 104 return routerLifetime == other.routerLifetime 105 && prefixValidLifetime == other.prefixValidLifetime 106 && prefixPreferredLifetime == other.prefixPreferredLifetime 107 && routeInfoLifetime == other.routeInfoLifetime 108 && rdnssLifetime == other.rdnssLifetime 109 && dnsslLifetime == other.dnsslLifetime; 110 } 111 112 /** @hide */ 113 public static final @android.annotation.NonNull Parcelable.Creator<RaEvent> CREATOR = new Parcelable.Creator<RaEvent>() { 114 public RaEvent createFromParcel(Parcel in) { 115 return new RaEvent(in); 116 } 117 118 public RaEvent[] newArray(int size) { 119 return new RaEvent[size]; 120 } 121 }; 122 123 public static final class Builder { 124 125 long routerLifetime = NO_LIFETIME; 126 long prefixValidLifetime = NO_LIFETIME; 127 long prefixPreferredLifetime = NO_LIFETIME; 128 long routeInfoLifetime = NO_LIFETIME; 129 long rdnssLifetime = NO_LIFETIME; 130 long dnsslLifetime = NO_LIFETIME; 131 Builder()132 public Builder() { 133 } 134 build()135 public @NonNull RaEvent build() { 136 return new RaEvent(routerLifetime, prefixValidLifetime, prefixPreferredLifetime, 137 routeInfoLifetime, rdnssLifetime, dnsslLifetime); 138 } 139 updateRouterLifetime(long lifetime)140 public @NonNull Builder updateRouterLifetime(long lifetime) { 141 routerLifetime = updateLifetime(routerLifetime, lifetime); 142 return this; 143 } 144 updatePrefixValidLifetime(long lifetime)145 public @NonNull Builder updatePrefixValidLifetime(long lifetime) { 146 prefixValidLifetime = updateLifetime(prefixValidLifetime, lifetime); 147 return this; 148 } 149 updatePrefixPreferredLifetime(long lifetime)150 public @NonNull Builder updatePrefixPreferredLifetime(long lifetime) { 151 prefixPreferredLifetime = updateLifetime(prefixPreferredLifetime, lifetime); 152 return this; 153 } 154 updateRouteInfoLifetime(long lifetime)155 public @NonNull Builder updateRouteInfoLifetime(long lifetime) { 156 routeInfoLifetime = updateLifetime(routeInfoLifetime, lifetime); 157 return this; 158 } 159 updateRdnssLifetime(long lifetime)160 public @NonNull Builder updateRdnssLifetime(long lifetime) { 161 rdnssLifetime = updateLifetime(rdnssLifetime, lifetime); 162 return this; 163 } 164 updateDnsslLifetime(long lifetime)165 public @NonNull Builder updateDnsslLifetime(long lifetime) { 166 dnsslLifetime = updateLifetime(dnsslLifetime, lifetime); 167 return this; 168 } 169 updateLifetime(long currentLifetime, long newLifetime)170 private long updateLifetime(long currentLifetime, long newLifetime) { 171 if (currentLifetime == RaEvent.NO_LIFETIME) { 172 return newLifetime; 173 } 174 return Math.min(currentLifetime, newLifetime); 175 } 176 } 177 } 178