network function (basic)
author"Luc Saillard <luc@saillard.org>"
Thu Sep 11 18:45:25 2008 +0200 (3 months ago)
changeset 78dd45aef9499d
parent 77c6866286354d
child 796bcb162461b0
network function (basic)
configure.ac
src/9dm.c
src/Makefile.am
src/network.c
src/network.h
--- a/configure.ac Thu Sep 11 17:50:11 2008 +0200
+++ b/configure.ac Thu Sep 11 18:45:25 2008 +0200
@@ -100,9 +100,20 @@ AC_SUBST(THINKFINGER_LIBS)
AC_SUBST(THINKFINGER_LIBS)
AC_SUBST(CRYPTO_LIBS)
+LIBCURL_CHECK_CONFIG
+
+if test "x$libcurl_feature_SSL" != "xyes"; then
+ AC_MSG_ERROR([SSL support in libcurl is required])
+fi
+
+
ALL_LINGUAS="fi fr sv"
-AM_GNU_GETTEXT_VERSION([0.17])
+GETTEXT_PACKAGE=9dm
AM_GNU_GETTEXT([external])
+AM_GNU_GETTEXT_VERSION([0.14.5])
+AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE, "$GETTEXT_PACKAGE",[The name of the gettext package name])
+AC_SUBST(GETTEXT_PACKAGE)
+
_9logindatadir="\$(datadir)/$PACKAGE/"
_9loginpixmapsdir="\$(datadir)/$PACKAGE/pixmaps"
--- a/src/9dm.c Thu Sep 11 17:50:11 2008 +0200
+++ b/src/9dm.c Thu Sep 11 18:45:25 2008 +0200
@@ -32,6 +32,7 @@
/* static functions */
static void usage(const char *programname);
+static gboolean network_check_available_cb(const gpointer user_data);
#if HAVE_THINKFINGER
static gint bio_sensor_message_timeout_cb(const gpointer data);
@@ -159,6 +160,7 @@ int main(int argc, char **argv)
#if HAVE_THINKFINGER
g_timeout_add(TIMEOUT_1*1000, bio_sensor_message_timeout_cb,(gpointer)main_window_height);
#endif
+ g_timeout_add(TIMEOUT_1*100, network_check_available_cb, NULL);
gtk_widget_show(main_window);
@@ -317,4 +319,16 @@ static gint bio_sensor_timeout_cb(const
#endif
-
+static gboolean
+network_check_available_cb(const gpointer user_data)
+{
+ trace("\n");
+
+
+
+
+ return FALSE;
+}
+
+
+
--- a/src/Makefile.am Thu Sep 11 17:50:11 2008 +0200
+++ b/src/Makefile.am Thu Sep 11 18:45:25 2008 +0200
@@ -5,8 +5,8 @@ EXTRA_DIST = 9dm_startx
EXTRA_DIST = 9dm_startx
9dm_SOURCES= \
- 9dm.c \
- 9dm.h \
+ 9dm.c \
+ 9dm.h \
bio_functions.c \
bio_functions.h \
desktop.c \
@@ -14,8 +14,10 @@ 9dm_SOURCES= \
effects.c \
effects.h \
gettext.h \
- minidesk_network.h \
- minidesk_network.c \
+ minidesk_network.h \
+ minidesk_network.c \
+ network.h \
+ network.c \
utils.c \
utils.h \
users.c \
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/network.c Thu Sep 11 18:45:25 2008 +0200
@@ -0,0 +1,233 @@
+/*
+ * 9dm: the easyneuf login diplay manager
+ *
+ * Copyright (C) 2007-2008 Luc Saillard <luc@saillard.org>
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <curl/curl.h>
+#include <ctype.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <net/if.h>
+#include <glib.h>
+#include <glib/gi18n-lib.h>
+#include <errno.h>
+
+#include "network.h"
+#include "utils.h"
+
+
+#define DISABLE_SSL_VERIFICATION 1
+#define EASYNEUF_GET_WANIP_URL "http://www.easyneuf.org/stun.php"
+
+
+GQuark
+network_error_quark(void)
+{
+ static GQuark quark = 0;
+ if (quark == 0)
+ quark = g_quark_from_static_string ("network-error-quark");
+ return quark;
+}
+
+
+
+/*
+ * Return the WAN ip address of te Easygate router
+ *
+ * This function works only if you are in 192.168.9.253.
+ *
+ * @return NULL if we cannot contact the router
+ * a string that must be free with g_free()
+ *
+ */
+char *
+get_wanip_from_http_server(void)
+{
+ char *content, *p;
+
+ content = network_http_request(EASYNEUF_GET_WANIP_URL, NULL, NULL, NULL);
+ if (content == NULL)
+ return NULL;
+
+ /* Only accept [0-9.]* */
+ p = content;
+ while (*p)
+ {
+ if (*p == '.')
+ p++;
+ else if (*p >= '0' && *p <= '9')
+ p++;
+ else
+ {
+ *p = 0;
+ break;
+ }
+ }
+
+ return content;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+static size_t
+curl_common_append_data(void *ptr, size_t size, size_t nmemb, GString *stream)
+{
+ g_string_append_len(stream, ptr, size * nmemb);
+ return size * nmemb;
+}
+
+
+
+/*
+ * Perform a HTTP GET/POST method
+ *
+ * @param url the url
+ * @param postdata set this to a non null pointer if you want to request a
+ * POST request. The string must be ended by a \0, and do not
+ * contains binary data.
+ * @param output_size
+ * set to non null if you want the size of the stream returned
+ * by the webserver.
+ *
+ * @return NULL if an error occured
+ * a string that contains the anwser from the webserver. This string
+ * must be free with g_free()
+ */
+char *
+network_http_request(const char *url, const char *postdata, unsigned int *output_size, GError **gerror)
+{
+ CURL *curl;
+ CURLcode curl_result;
+ long response_code;
+ GString *content_result;
+
+ if (output_size)
+ *output_size = 0;
+
+ curl = curl_easy_init();
+ if (curl == NULL)
+ {
+ g_set_error(gerror, NETWORK_ERROR, EIO, _("Could not initialize the curl library to communicate with the webserver"));
+ return NULL;
+ }
+
+ content_result = g_string_sized_new(256);
+ if (content_result == NULL)
+ {
+ g_set_error(gerror, NETWORK_ERROR, ENOMEM, _("Could not allocate memory to store the content of the HTTP request"));
+ goto error_cleanup_curl;
+ }
+
+ trace("url=%s post_data=%s\n", url, postdata?postdata:"");
+
+ curl_easy_setopt(curl, CURLOPT_URL, url);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, content_result);
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, (curl_write_callback)curl_common_append_data);
+ if (postdata)
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata);
+ if (strstr(url, "https://"))
+ {
+ curl_easy_setopt(curl, CURLOPT_CAINFO, "/usr/share/9firstboot/easyneuf.fr.crt");
+#if DISABLE_SSL_VERIFICATION
+ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
+ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, FALSE);
+#endif
+ }
+ curl_result = curl_easy_perform(curl);
+
+ if (curl_result != CURLE_OK)
+ {
+ g_set_error(gerror, NETWORK_ERROR, EIO, curl_easy_strerror(curl_result));
+ goto error_show_error;
+ }
+
+ curl_result = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
+ if (curl_result != CURLE_OK)
+ {
+ g_set_error(gerror, NETWORK_ERROR, EIO, curl_easy_strerror(curl_result));
+ goto error_show_error;
+ }
+
+ if ((int)(response_code/100) != 2)
+ {
+ /* All error code different than 200 is an error */
+ trace("Failed to download url (%s) (http_code = %ld)\n", url, response_code);
+ g_set_error(gerror, NETWORK_ERROR, EIO, _("Bad response from the webserver (http_code = %ld)"), response_code);
+ goto error_cleanup_string;
+ }
+
+ curl_easy_cleanup(curl);
+
+ if (output_size)
+ {
+ *output_size = content_result->len;
+ trace("return data len = %d\n", content_result->len);
+ }
+
+ return g_string_free(content_result, FALSE);
+
+error_show_error:
+ trace("curl error:%d: %s\n", curl_result, curl_easy_strerror(curl_result));
+ trace("content_result: %s\n", content_result->str);
+error_cleanup_string:
+ g_string_free(content_result, TRUE);
+error_cleanup_curl:
+ curl_easy_cleanup(curl);
+ return NULL;
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/network.h Thu Sep 11 18:45:25 2008 +0200
@@ -0,0 +1,30 @@
+/*
+ * 9dm: the easyneuf login diplay manager
+ *
+ * Copyright (C) 2007-2008 Luc Saillard <luc@saillard.org>
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#ifndef __NETWORK__H
+#define __NETWORK__H
+
+char *get_wanip_from_http_server(void);
+char *network_http_request(const char *url, const char *postdata, unsigned int *output_size, GError **gerror);
+
+
+#endif
+