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 27 /** 28 * Base class for multicast socket client. 29 * 30 * @hide 31 */ 32 public interface MdnsSocketClientBase { 33 /*** Start mDns discovery on given network. */ startDiscovery()34 default void startDiscovery() throws IOException { } 35 36 /*** Stop mDns discovery. */ stopDiscovery()37 default void stopDiscovery() { } 38 39 /*** Set callback for receiving mDns response */ setCallback(@ullable Callback callback)40 void setCallback(@Nullable Callback callback); 41 42 /** 43 * Send a mDNS request packet via given network that asks for multicast response. 44 * 45 * <p>The socket client may use a null network to identify some or all interfaces, in which case 46 * passing null sends the packet to these. 47 */ sendMulticastPacket(@onNull DatagramPacket packet, @Nullable Network network)48 void sendMulticastPacket(@NonNull DatagramPacket packet, @Nullable Network network); 49 50 /** 51 * Send a mDNS request packet via given network that asks for unicast response. 52 * 53 * <p>The socket client may use a null network to identify some or all interfaces, in which case 54 * passing null sends the packet to these. 55 */ sendUnicastPacket(@onNull DatagramPacket packet, @Nullable Network network)56 void sendUnicastPacket(@NonNull DatagramPacket packet, @Nullable Network network); 57 58 /*** Notify that the given network is requested for mdns discovery / resolution */ notifyNetworkRequested(@onNull MdnsServiceBrowserListener listener, @Nullable Network network, @NonNull SocketCreationCallback socketCreationCallback)59 void notifyNetworkRequested(@NonNull MdnsServiceBrowserListener listener, 60 @Nullable Network network, @NonNull SocketCreationCallback socketCreationCallback); 61 62 /*** Notify that the network is unrequested */ notifyNetworkUnrequested(@onNull MdnsServiceBrowserListener listener)63 default void notifyNetworkUnrequested(@NonNull MdnsServiceBrowserListener listener) { } 64 65 /*** Gets looper that used by the socket client */ getLooper()66 default Looper getLooper() { 67 return null; 68 } 69 70 /*** Callback for mdns response */ 71 interface Callback { 72 /*** Receive a mdns response */ onResponseReceived(@onNull MdnsPacket packet, int interfaceIndex, @Nullable Network network)73 void onResponseReceived(@NonNull MdnsPacket packet, int interfaceIndex, 74 @Nullable Network network); 75 76 /*** Parse a mdns response failed */ onFailedToParseMdnsResponse(int receivedPacketNumber, int errorCode, @Nullable Network network)77 void onFailedToParseMdnsResponse(int receivedPacketNumber, int errorCode, 78 @Nullable Network network); 79 } 80 81 /*** Callback for requested socket creation */ 82 interface SocketCreationCallback { 83 /*** Notify requested socket is created */ onSocketCreated(@ullable Network network)84 void onSocketCreated(@Nullable Network network); 85 86 /*** Notify requested socket is destroyed */ onAllSocketsDestroyed(@ullable Network network)87 void onAllSocketsDestroyed(@Nullable Network network); 88 } 89 } 90