]> git.meshlink.io Git - catta/blob - avahi-ui/zssh.c
12b78bfe9c596970e17cce5ac7685de72c486c7f
[catta] / avahi-ui / zssh.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <unistd.h>
28 #include <errno.h>
29
30 #include <gtk/gtk.h>
31
32 #include <avahi-client/client.h>
33 #include <avahi-common/strlst.h>
34 #include <avahi-common/malloc.h>
35
36 #include "avahi-ui.h"
37
38 int main(int argc, char*argv[]) {
39     GtkWidget *d;
40
41     gtk_init(&argc, &argv);
42     
43     d = aui_service_dialog_new("Choose SSH server");
44     aui_service_dialog_set_browse_service_types(AUI_SERVICE_DIALOG(d), "_ssh._tcp",  /*"_ftp._tcp", "_http._tcp",*/ NULL);
45     aui_service_dialog_set_resolve_service(AUI_SERVICE_DIALOG(d), TRUE);
46     aui_service_dialog_set_resolve_host_name(AUI_SERVICE_DIALOG(d), !avahi_nss_support());
47
48     gtk_window_present(GTK_WINDOW(d));
49
50     if (gtk_dialog_run(GTK_DIALOG(d)) == GTK_RESPONSE_OK) {
51         char p[16], a[AVAHI_ADDRESS_STR_MAX], *u = NULL;
52         char *h = NULL;
53         const AvahiStringList *txt;
54         
55         snprintf(p, sizeof(p), "%u", aui_service_dialog_get_port(AUI_SERVICE_DIALOG(d)));
56
57         if (avahi_nss_support())
58             h = g_strdup(aui_service_dialog_get_host_name(AUI_SERVICE_DIALOG(d)));
59         else
60             h = g_strdup(avahi_address_snprint(a, sizeof(a), aui_service_dialog_get_address(AUI_SERVICE_DIALOG(d))));
61
62         for (txt = aui_service_dialog_get_txt_data(AUI_SERVICE_DIALOG(d)); txt; txt = txt->next) {
63             char *key, *value;
64             
65             if (avahi_string_list_get_pair((AvahiStringList*) txt, &key, &value, NULL) < 0)
66                 break;
67
68             if (strcmp(key, "u") == 0)
69                 u = g_strdup(value);
70             
71             avahi_free(key);
72             avahi_free(value);
73         }
74
75         gtk_widget_destroy(d);
76
77         if (u) {
78             g_print("ssh -p %s -l %s %s\n", p, u, h);
79             execlp("ssh", "ssh", "-p", p, "-l", u, h, NULL); 
80         } else {
81             g_print("ssh -p %s %s\n", p, h);
82             execlp("ssh", "ssh", "-p", p, h, NULL); 
83         }
84
85         g_warning("execlp() failed: %s", strerror(errno));
86
87         g_free(h);
88         g_free(u);
89         
90     } else {
91         gtk_widget_destroy(d);
92
93         g_print("Canceled.\n");
94     }
95     
96     return 1;
97     
98 }