1 /*
2  * Copyright (C) 2021 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.server.connectivity.mdns;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.net.Network;
22 import android.os.Looper;
23 
24 import java.io.IOException;
25 import java.net.DatagramPacket;
26 import java.util.List;
27 
28 /**
29  * Base class for multicast socket client.
30  *
31  * @hide
32  */
33 public interface MdnsSocketClientBase {
34     /*** Start mDns discovery on given network. */
startDiscovery()35     default void startDiscovery() throws IOException { }
36 
37     /*** Stop mDns discovery. */
stopDiscovery()38     default void stopDiscovery() { }
39 
40     /*** Set callback for receiving mDns response */
setCallback(@ullable Callback callback)41     void setCallback(@Nullable Callback callback);
42 
43     /**
44      * Send mDNS request packets via given network that asks for multicast response.
45      */
sendPacketRequestingMulticastResponse(@onNull List<DatagramPacket> packets, boolean onlyUseIpv6OnIpv6OnlyNetworks)46     void sendPacketRequestingMulticastResponse(@NonNull List<DatagramPacket> packets,
47             boolean onlyUseIpv6OnIpv6OnlyNetworks);
48 
49     /**
50      * Send mDNS request packets via given network that asks for unicast response.
51      */
sendPacketRequestingUnicastResponse(@onNull List<DatagramPacket> packets, boolean onlyUseIpv6OnIpv6OnlyNetworks)52     void sendPacketRequestingUnicastResponse(@NonNull List<DatagramPacket> packets,
53             boolean onlyUseIpv6OnIpv6OnlyNetworks);
54 
55     /*** Notify that the given network is requested for mdns discovery / resolution */
notifyNetworkRequested(@onNull MdnsServiceBrowserListener listener, @Nullable Network network, @NonNull SocketCreationCallback socketCreationCallback)56     void notifyNetworkRequested(@NonNull MdnsServiceBrowserListener listener,
57             @Nullable Network network, @NonNull SocketCreationCallback socketCreationCallback);
58 
59     /*** Notify that the network is unrequested */
notifyNetworkUnrequested(@onNull MdnsServiceBrowserListener listener)60     default void notifyNetworkUnrequested(@NonNull MdnsServiceBrowserListener listener) { }
61 
62     /*** Gets looper that used by the socket client */
getLooper()63     default Looper getLooper() {
64         return null;
65     }
66 
67     /** Returns whether the socket client support requesting per network */
supportsRequestingSpecificNetworks()68     boolean supportsRequestingSpecificNetworks();
69 
70     /*** Callback for mdns response  */
71     interface Callback {
72         /*** Receive a mdns response */
onResponseReceived(@onNull MdnsPacket packet, @NonNull SocketKey socketKey)73         void onResponseReceived(@NonNull MdnsPacket packet, @NonNull SocketKey socketKey);
74 
75         /*** Parse a mdns response failed */
onFailedToParseMdnsResponse(int receivedPacketNumber, int errorCode, @NonNull SocketKey socketKey)76         void onFailedToParseMdnsResponse(int receivedPacketNumber, int errorCode,
77                 @NonNull SocketKey socketKey);
78     }
79 
80     /*** Callback for requested socket creation  */
81     interface SocketCreationCallback {
82         /*** Notify requested socket is created */
onSocketCreated(@onNull SocketKey socketKey)83         void onSocketCreated(@NonNull SocketKey socketKey);
84 
85         /*** Notify requested socket is destroyed */
onSocketDestroyed(@onNull SocketKey socketKey)86         void onSocketDestroyed(@NonNull SocketKey socketKey);
87     }
88 }