From 5aa1bda888a0440a6d6ef150f46cbc2573c3c2d1 Mon Sep 17 00:00:00 2001
From: Felix Zhang <fezhang@suse.com>
Date: Fri, 25 Apr 2014 18:06:29 +0800
Subject: [PATCH] avahi

---
 configure.ac       |   1 +
 xrdp/Makefile.am   |   9 +++--
 xrdp/xrdp.h        |   8 ++++
 xrdp/xrdp_avahi.c  | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 xrdp/xrdp_listen.c |   2 +-
 5 files changed, 133 insertions(+), 4 deletions(-)
 create mode 100644 xrdp/xrdp_avahi.c

diff --git a/configure.ac b/configure.ac
index b48aa74..f014d5c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -7,6 +7,7 @@ AM_INIT_AUTOMAKE([1.6 foreign])
 AC_PROG_CC
 AC_C_CONST
 AC_PROG_LIBTOOL
+PKG_CHECK_MODULES(AVAHI, avahi-client >= 0.6.4)
 AC_ARG_ENABLE(nopam, AS_HELP_STRING([--enable-nopam],
               [Build no PAM support (default: no)]),
               [nopam=true], [nopam=false])
diff --git a/xrdp/Makefile.am b/xrdp/Makefile.am
index 4fe6f6a..c5c9d0d 100644
--- a/xrdp/Makefile.am
+++ b/xrdp/Makefile.am
@@ -15,7 +15,8 @@ AM_CFLAGS = \
 
 INCLUDES = \
   -I$(top_srcdir)/common \
-  -I$(top_srcdir)/libxrdp
+  -I$(top_srcdir)/libxrdp \
+  $(AVAHI_CFLAGS)
 
 sbin_PROGRAMS = \
   xrdp
@@ -33,11 +34,13 @@ xrdp_SOURCES = \
   xrdp_painter.c \
   xrdp_process.c \
   xrdp_region.c \
-  xrdp_wm.c
+  xrdp_wm.c \
+  xrdp_avahi.c
 
 xrdp_LDADD = \
   $(top_srcdir)/common/libcommon.la \
-  $(top_srcdir)/libxrdp/libxrdp.la
+  $(top_srcdir)/libxrdp/libxrdp.la \
+  $(AVAHI_LIBS)
 
 xrdpsysconfdir=$(sysconfdir)/xrdp
 
diff --git a/xrdp/xrdp.h b/xrdp/xrdp.h
index d602e7d..14e7aaa 100644
--- a/xrdp/xrdp.h
+++ b/xrdp/xrdp.h
@@ -150,6 +150,8 @@ xrdp_listen_create(void);
 void APP_CC
 xrdp_listen_delete(struct xrdp_listen* self);
 int APP_CC
+xrdp_listen_get_port(char* port, int port_bytes);
+int APP_CC
 xrdp_listen_main_loop(struct xrdp_listen* self,
                       struct xrdp_startup_params* startup_param);
 
@@ -414,3 +416,9 @@ int DEFAULT_CC
 server_send_to_channel(struct xrdp_mod* mod, int channel_id,
                        char* data, int data_len,
                        int total_data_len, int flags);
+
+/* xrdp_avahi.c */
+int APP_CC
+xrdp_avahi_init(void);
+void APP_CC
+xrdp_avahi_fini(void);
diff --git a/xrdp/xrdp_avahi.c b/xrdp/xrdp_avahi.c
new file mode 100644
index 0000000..d760264
--- /dev/null
+++ b/xrdp/xrdp_avahi.c
@@ -0,0 +1,117 @@
+/*
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+   xrdp: A Remote Desktop Protocol server.
+   Copyright (C) Novell, Inc. 2008
+
+   avahi integration
+
+*/
+
+#include "xrdp.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <avahi-client/client.h>
+#include <avahi-client/publish.h>
+#include <avahi-common/thread-watch.h>
+
+static AvahiClient       *client = NULL;
+static AvahiThreadedPoll *threaded_poll = NULL;
+static AvahiEntryGroup   *avahi_group = NULL;
+
+static const char *_service_name = "RDP service on %s";
+
+static void
+avahi_client_callback (AvahiClient      *c,
+		       AvahiClientState state,
+		       void             *userdata)
+{
+    switch (state) {
+    case AVAHI_CLIENT_S_RUNNING:
+	avahi_group = avahi_entry_group_new (c, 0, 0);
+	if (avahi_group)
+	{
+	    char hname[512];
+	    char name[576];
+	    char port[8];
+	    /* dummy parameters */
+	    char address[256];
+	    struct xrdp_startup_params* startup_param = {"", 0, 0, 0, 0};
+
+	    if (gethostname (hname, sizeof (hname)))
+		break;
+
+	    sprintf (name, _service_name, hname);
+
+	    xrdp_listen_get_port_address (port, sizeof (port),
+                                          address, sizeof (address),
+                                          startup_param);
+
+	    avahi_entry_group_add_service (avahi_group,
+					   AVAHI_IF_UNSPEC,
+					   AVAHI_PROTO_UNSPEC,
+					   0, 
+					   name,
+					   "_rdp._tcp",
+					   0,
+					   0,
+					   atoi (port),
+					   NULL);
+
+	    avahi_entry_group_commit (avahi_group);
+	}
+	break;
+    case AVAHI_CLIENT_FAILURE:
+    case AVAHI_CLIENT_S_COLLISION:
+    case AVAHI_CLIENT_CONNECTING:
+	break;
+    case AVAHI_CLIENT_S_REGISTERING:
+	if (avahi_group)
+	    avahi_entry_group_reset (avahi_group);
+    default:
+	break;
+    }
+}
+
+int APP_CC
+xrdp_avahi_init (void)
+{
+    if (!(threaded_poll = avahi_threaded_poll_new ()))
+	return 1;
+
+    if (!(client = avahi_client_new (avahi_threaded_poll_get (threaded_poll),
+				    0, 
+				    avahi_client_callback,
+				    NULL,
+				    NULL)))
+       return 1;
+
+   if (avahi_threaded_poll_start (threaded_poll) < 0)
+       return 1;
+
+   return 0;
+}
+
+void APP_CC
+xrdp_avahi_fini (void)
+{
+    avahi_threaded_poll_stop (threaded_poll);
+    if (avahi_group)
+	avahi_entry_group_free (avahi_group);
+    avahi_client_free (client);
+    avahi_threaded_poll_free (threaded_poll);
+}
diff --git a/xrdp/xrdp_listen.c b/xrdp/xrdp_listen.c
index 23f532a..b9749b2 100644
--- a/xrdp/xrdp_listen.c
+++ b/xrdp/xrdp_listen.c
@@ -118,7 +118,7 @@ xrdp_process_run(void* in_val)
 }
 
 /*****************************************************************************/
-static int
+int
 xrdp_listen_get_port_address(char* port, int port_bytes,
                              char* address, int address_bytes,
                              struct xrdp_startup_params* startup_param)
-- 
1.7.12.4

