root/source3/smbd/avahi_register.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. avahi_entry_group_callback
  2. avahi_client_callback
  3. avahi_start_register

   1 /*
   2  * Unix SMB/CIFS implementation.
   3  * Register _smb._tcp with avahi
   4  *
   5  * Copyright (C) Volker Lendecke 2009
   6  *
   7  * This program is free software; you can redistribute it and/or modify
   8  * it under the terms of the GNU General Public License as published by
   9  * the Free Software Foundation; either version 3 of the License, or
  10  * (at your option) any later version.
  11  *
  12  * This program is distributed in the hope that it will be useful,
  13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  * GNU General Public License for more details.
  16  *
  17  * You should have received a copy of the GNU General Public License
  18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
  19  */
  20 
  21 #include "includes.h"
  22 
  23 #include <avahi-client/client.h>
  24 #include <avahi-client/publish.h>
  25 #include <avahi-common/error.h>
  26 
  27 struct avahi_state_struct {
  28         struct AvahiPoll *poll;
  29         AvahiClient *client;
  30         AvahiEntryGroup *entry_group;
  31         uint16_t port;
  32 };
  33 
  34 static void avahi_entry_group_callback(AvahiEntryGroup *g,
     /* [<][>][^][v][top][bottom][index][help] */
  35                                        AvahiEntryGroupState status,
  36                                        void *userdata)
  37 {
  38         struct avahi_state_struct *state = talloc_get_type_abort(
  39                 userdata, struct avahi_state_struct);
  40         int error;
  41 
  42         switch (status) {
  43         case AVAHI_ENTRY_GROUP_ESTABLISHED:
  44                 DEBUG(10, ("avahi_entry_group_callback: "
  45                            "AVAHI_ENTRY_GROUP_ESTABLISHED\n"));
  46                 break;
  47         case AVAHI_ENTRY_GROUP_FAILURE:
  48                 error = avahi_client_errno(state->client);
  49 
  50                 DEBUG(10, ("avahi_entry_group_callback: "
  51                            "AVAHI_ENTRY_GROUP_FAILURE: %s\n",
  52                            avahi_strerror(error)));
  53                 break;
  54         case AVAHI_ENTRY_GROUP_COLLISION:
  55                 DEBUG(10, ("avahi_entry_group_callback: "
  56                            "AVAHI_ENTRY_GROUP_COLLISION\n"));
  57                 break;
  58         case AVAHI_ENTRY_GROUP_UNCOMMITED:
  59                 DEBUG(10, ("avahi_entry_group_callback: "
  60                            "AVAHI_ENTRY_GROUP_UNCOMMITED\n"));
  61                 break;
  62         case AVAHI_ENTRY_GROUP_REGISTERING:
  63                 DEBUG(10, ("avahi_entry_group_callback: "
  64                            "AVAHI_ENTRY_GROUP_REGISTERING\n"));
  65                 break;
  66         }
  67 }
  68 
  69 static void avahi_client_callback(AvahiClient *c, AvahiClientState status,
     /* [<][>][^][v][top][bottom][index][help] */
  70                                   void *userdata)
  71 {
  72         struct avahi_state_struct *state = talloc_get_type_abort(
  73                 userdata, struct avahi_state_struct);
  74         int error;
  75 
  76         switch (status) {
  77         case AVAHI_CLIENT_S_RUNNING:
  78                 DEBUG(10, ("avahi_client_callback: AVAHI_CLIENT_S_RUNNING\n"));
  79 
  80                 state->entry_group = avahi_entry_group_new(
  81                         c, avahi_entry_group_callback, state);
  82                 if (state->entry_group == NULL) {
  83                         error = avahi_client_errno(c);
  84                         DEBUG(10, ("avahi_entry_group_new failed: %s\n",
  85                                    avahi_strerror(error)));
  86                         break;
  87                 }
  88                 if (avahi_entry_group_add_service(
  89                             state->entry_group, AVAHI_IF_UNSPEC,
  90                             AVAHI_PROTO_UNSPEC, 0, global_myname(),
  91                             "_smb._tcp", NULL, NULL, state->port, NULL) < 0) {
  92                         error = avahi_client_errno(c);
  93                         DEBUG(10, ("avahi_entry_group_add_service failed: "
  94                                    "%s\n", avahi_strerror(error)));
  95                         avahi_entry_group_free(state->entry_group);
  96                         state->entry_group = NULL;
  97                         break;
  98                 }
  99                 if (avahi_entry_group_commit(state->entry_group) < 0) {
 100                         error = avahi_client_errno(c);
 101                         DEBUG(10, ("avahi_entry_group_commit failed: "
 102                                    "%s\n", avahi_strerror(error)));
 103                         avahi_entry_group_free(state->entry_group);
 104                         state->entry_group = NULL;
 105                         break;
 106                 }
 107                 break;
 108         case AVAHI_CLIENT_FAILURE:
 109                 error = avahi_client_errno(c);
 110 
 111                 DEBUG(10, ("avahi_client_callback: AVAHI_CLIENT_FAILURE: %s\n",
 112                            avahi_strerror(error)));
 113 
 114                 if (error != AVAHI_ERR_DISCONNECTED) {
 115                         break;
 116                 }
 117                 avahi_client_free(c);
 118                 state->client = avahi_client_new(state->poll, AVAHI_CLIENT_NO_FAIL,
 119                                                  avahi_client_callback, state,
 120                                                  &error);
 121                 if (state->client == NULL) {
 122                         DEBUG(10, ("avahi_client_new failed: %s\n",
 123                                    avahi_strerror(error)));
 124                         break;
 125                 }
 126                 break;
 127         case AVAHI_CLIENT_S_COLLISION:
 128                 DEBUG(10, ("avahi_client_callback: "
 129                            "AVAHI_CLIENT_S_COLLISION\n"));
 130                 break;
 131         case AVAHI_CLIENT_S_REGISTERING:
 132                 DEBUG(10, ("avahi_client_callback: "
 133                            "AVAHI_CLIENT_S_REGISTERING\n"));
 134                 break;
 135         case AVAHI_CLIENT_CONNECTING:
 136                 DEBUG(10, ("avahi_client_callback: "
 137                            "AVAHI_CLIENT_CONNECTING\n"));
 138                 break;
 139         }
 140 }
 141 
 142 void *avahi_start_register(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
     /* [<][>][^][v][top][bottom][index][help] */
 143                            uint16_t port)
 144 {
 145         struct avahi_state_struct *state;
 146         int error;
 147 
 148         state = talloc(mem_ctx, struct avahi_state_struct);
 149         if (state == NULL) {
 150                 return state;
 151         }
 152         state->port = port;
 153         state->poll = tevent_avahi_poll(state, ev);
 154         if (state->poll == NULL) {
 155                 goto fail;
 156         }
 157         state->client = avahi_client_new(state->poll, AVAHI_CLIENT_NO_FAIL,
 158                                          avahi_client_callback, state,
 159                                          &error);
 160         if (state->client == NULL) {
 161                 DEBUG(10, ("avahi_client_new failed: %s\n",
 162                            avahi_strerror(error)));
 163                 goto fail;
 164         }
 165         return state;
 166 
 167  fail:
 168         TALLOC_FREE(state);
 169         return NULL;
 170 }

/* [<][>][^][v][top][bottom][index][help] */