File xrdp-avahi.diff of Package xrdp

From 71ebbe34d841d3f29ae2d21fbae25889de5d4a57 Mon Sep 17 00:00:00 2001
From: Felix Zhang <fezhang@suse.com>
Date: Mon, 1 Aug 2016 17:04:31 +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 e1a150e..2cfe026 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)
 PKG_PROG_PKG_CONFIG
 AC_ARG_WITH([systemdsystemunitdir],
         AS_HELP_STRING([--with-systemdsystemunitdir=DIR], [Directory for systemd service files]),
diff --git a/xrdp/Makefile.am b/xrdp/Makefile.am
index 4fd9a68..e00bc2f 100644
--- a/xrdp/Makefile.am
+++ b/xrdp/Makefile.am
@@ -33,7 +33,8 @@ INCLUDES = \
   -I$(top_builddir) \
   -I$(top_srcdir)/common \
   -I$(top_srcdir)/libxrdp \
-  $(EXTRA_INCLUDES)
+  $(EXTRA_INCLUDES) \
+  $(AVAHI_CFLAGS)
 
 sbin_PROGRAMS = \
   xrdp
@@ -52,12 +53,14 @@ xrdp_SOURCES = \
   xrdp_process.c \
   xrdp_region.c \
   xrdp_wm.c \
-  xrdp_encoder.c
+  xrdp_encoder.c \
+  xrdp_avahi.c
 
 xrdp_LDADD = \
   $(top_builddir)/common/libcommon.la \
   $(top_builddir)/libxrdp/libxrdp.la \
-  $(EXTRA_LIBS)
+  $(EXTRA_LIBS) \
+  $(AVAHI_LIBS)
 
 xrdp_LDFLAGS = \
   $(EXTRA_FLAGS)
diff --git a/xrdp/xrdp.h b/xrdp/xrdp.h
index 67488a6..e71fb9b 100644
--- a/xrdp/xrdp.h
+++ b/xrdp/xrdp.h
@@ -164,6 +164,8 @@ void APP_CC
 xrdp_listen_delete(struct xrdp_listen* self);
 int APP_CC
 xrdp_listen_main_loop(struct xrdp_listen* self);
+int APP_CC
+xrdp_listen_get_port(char* port, int port_bytes);
 
 /* xrdp_region.c */
 struct xrdp_region* APP_CC
@@ -510,3 +512,9 @@ int DEFAULT_CC
 server_add_char_alpha(struct xrdp_mod* mod, int font, int charactor,
                       int offset, int baseline,
                       int width, int height, char* data);
+
+/* 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..7fa1656
--- /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 b2b19ca..c1ae4c5 100644
--- a/xrdp/xrdp_listen.c
+++ b/xrdp/xrdp_listen.c
@@ -145,7 +145,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,
                              int *tcp_nodelay, int *tcp_keepalive,
-- 
2.6.6
openSUSE Build Service is sponsored by