1 /*
2  * Copyright (C) 2020 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 com.android.networkstack.tethering;
18 
19 import android.net.MacAddress;
20 
21 import androidx.annotation.NonNull;
22 
23 import com.android.net.module.util.Struct;
24 import com.android.net.module.util.Struct.Field;
25 import com.android.net.module.util.Struct.Type;
26 
27 import java.net.Inet4Address;
28 import java.net.UnknownHostException;
29 import java.util.Objects;
30 
31 /** Key type for downstream & upstream IPv4 forwarding maps. */
32 public class Tether4Key extends Struct {
33     @Field(order = 0, type = Type.U32)
34     public final long iif;
35 
36     @Field(order = 1, type = Type.EUI48)
37     public final MacAddress dstMac;
38 
39     @Field(order = 2, type = Type.U8, padding = 1)
40     public final short l4proto;
41 
42     @Field(order = 3, type = Type.ByteArray, arraysize = 4)
43     public final byte[] src4;
44 
45     @Field(order = 4, type = Type.ByteArray, arraysize = 4)
46     public final byte[] dst4;
47 
48     @Field(order = 5, type = Type.UBE16)
49     public final int srcPort;
50 
51     @Field(order = 6, type = Type.UBE16)
52     public final int dstPort;
53 
Tether4Key(final long iif, @NonNull final MacAddress dstMac, final short l4proto, final byte[] src4, final byte[] dst4, final int srcPort, final int dstPort)54     public Tether4Key(final long iif, @NonNull final MacAddress dstMac, final short l4proto,
55             final byte[] src4, final byte[] dst4, final int srcPort,
56             final int dstPort) {
57         Objects.requireNonNull(dstMac);
58 
59         this.iif = iif;
60         this.dstMac = dstMac;
61         this.l4proto = l4proto;
62         this.src4 = src4;
63         this.dst4 = dst4;
64         this.srcPort = srcPort;
65         this.dstPort = dstPort;
66     }
67 
68     @Override
toString()69     public String toString() {
70         try {
71             return String.format(
72                     "iif: %d, dstMac: %s, l4proto: %d, src4: %s, dst4: %s, "
73                             + "srcPort: %d, dstPort: %d",
74                     iif, dstMac, l4proto,
75                     Inet4Address.getByAddress(src4), Inet4Address.getByAddress(dst4),
76                     Short.toUnsignedInt((short) srcPort), Short.toUnsignedInt((short) dstPort));
77         } catch (UnknownHostException | IllegalArgumentException e) {
78             return String.format("Invalid IP address", e);
79         }
80     }
81 }
82