aboutsummaryrefslogtreecommitdiffstats
path: root/camel/camel-service.c
diff options
context:
space:
mode:
authorDan Winship <danw@src.gnome.org>2000-03-29 04:51:58 +0800
committerDan Winship <danw@src.gnome.org>2000-03-29 04:51:58 +0800
commit207652677441264ca1e51d06898b37ea4164c8e2 (patch)
treeded681c259297bf4b25a83fb9cd1bccc57fe4baf /camel/camel-service.c
parentc3b6d117ee155a571d8c4da01b81b6855e26e6f6 (diff)
downloadgsoc2013-evolution-207652677441264ca1e51d06898b37ea4164c8e2.tar.gz
gsoc2013-evolution-207652677441264ca1e51d06898b37ea4164c8e2.tar.zst
gsoc2013-evolution-207652677441264ca1e51d06898b37ea4164c8e2.zip
convenience functions to canonicalize the host and port values of a
* camel-service.c (camel_service_gethost, camel_service_getport): convenience functions to canonicalize the host and port values of a service's URL. * providers/pop3/camel-pop3-store.c: use them svn path=/trunk/; revision=2216
Diffstat (limited to 'camel/camel-service.c')
-rw-r--r--camel/camel-service.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/camel/camel-service.c b/camel/camel-service.c
index 394a163ae0..01b72e1a70 100644
--- a/camel/camel-service.c
+++ b/camel/camel-service.c
@@ -28,6 +28,9 @@
#include "camel-log.h"
#include "camel-exception.h"
+#include <ctype.h>
+#include <stdlib.h>
+
static GtkObjectClass *parent_class=NULL;
/* Returns the class for a CamelService */
@@ -418,3 +421,88 @@ camel_service_free_auth_types (CamelService *service, GList *authtypes)
{
CSERV_CLASS (service)->free_auth_types (service, authtypes);
}
+
+
+
+/* URL utility routines */
+
+/**
+ * camel_service_gethost: get a hostent for a CamelService's host
+ * @service: a CamelService
+ * @ex: a CamelException
+ *
+ * This is a convenience function to do a gethostbyname on the host
+ * for the service's URL.
+ *
+ * Return value: a (statically-allocated) hostent.
+ **/
+struct hostent *
+camel_service_gethost (CamelService *service, CamelException *ex)
+{
+ struct hostent *h;
+ char *hostname;
+
+ if (service->url->host)
+ hostname = service->url->host;
+ else
+ hostname = "localhost";
+ h = gethostbyname (hostname);
+ if (!h) {
+ extern int h_errno;
+
+ if (h_errno == HOST_NOT_FOUND || h_errno == NO_DATA) {
+ camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_URL_INVALID,
+ "No such host %s.", hostname);
+ } else {
+ camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
+ "Temporarily unable to look up "
+ "hostname %s.", hostname);
+ }
+ return NULL;
+ }
+
+ return h;
+}
+
+/**
+ * camel_service_getport: get the port number for a service's URL
+ * @service: a CamelService
+ * @default_name: the default service name if none is provided by the URL
+ * @default_number: the default port number if the @default_name lookup fails
+ * @proto: the protocol (eg, "tcp") to be used
+ * @ex: a CamelExcption
+ *
+ * This is a convenience function to get the port number for a service
+ * based on the service's URL (which may contain either a numeric
+ * or symbolic port name) and the provided defaults.
+ *
+ * Return value: a port number, or -1 if the user specified a port name
+ * that could not be resolved.
+ **/
+int
+camel_service_getport (CamelService *service, char *default_port,
+ int default_number, char *proto, CamelException *ex)
+{
+ struct servent *s;
+ char *port;
+
+ if (service->url->port)
+ port = service->url->port;
+ else
+ port = default_port;
+
+ if (isdigit (*port))
+ return htons (atoi (port));
+
+ s = getservbyname (port, proto);
+ if (s)
+ return s->s_port;
+
+
+ if (port == service->url->port) {
+ camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_URL_INVALID,
+ "Unknown port `%s'", port);
+ return -1;
+ } else
+ return default_number;
+}