Author: Pekka Riikonen <priikone@silcnet.org>
- Copyright (C) 2006 - 2007 Pekka Riikonen
+ Copyright (C) 2006 - 2008 Pekka Riikonen
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
*/
-#include "silc.h"
+#include "silcruntime.h"
#include "silcsymbiansocketstream.h"
/************************ Static utility functions **************************/
class SilcSymbianTCPListener;
+extern "C" {
+
/* Deliver new stream to upper layer */
-static void silc_net_accept_stream(SilcSocketStreamStatus status,
+static void silc_net_accept_stream(SilcResult status,
SilcStream stream, void *context)
{
SilcNetListener listener = (SilcNetListener)context;
/* In case of error, the socket has been destroyed already via
silc_stream_destroy. */
- if (status != SILC_SOCKET_OK)
+ if (status != SILC_OK)
return;
- listener->callback(SILC_NET_OK, stream, listener->context);
+ listener->callback(SILC_OK, stream, listener->context);
}
+} /* extern "C" */
+
/* TCP Listener class */
class SilcSymbianTCPListener : public CActive {
SILC_LOG_DEBUG(("Creating TCP listener"));
- if (port < 0 || !schedule || !callback)
+ if (!schedule) {
+ schedule = silc_schedule_get_global();
+ if (!schedule) {
+ silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
+ goto err;
+ }
+ }
+
+ if (port < 0 || !callback) {
+ silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
goto err;
+ }
listener = (SilcNetListener)silc_calloc(1, sizeof(*listener));
if (!listener) {
- callback(SILC_NET_NO_MEMORY, NULL, context);
+ callback(SILC_ERR_OUT_OF_MEMORY, NULL, context);
return NULL;
}
listener->schedule = schedule;
listener->socks = (SilcSocket *)silc_calloc(local_ip_count,
sizeof(*listener->socks));
if (!listener->socks) {
- callback(SILC_NET_NO_MEMORY, NULL, context);
+ callback(SILC_ERR_OUT_OF_MEMORY, NULL, context);
return NULL;
}
} else {
listener->socks = (SilcSocket *)silc_calloc(1, sizeof(*listener->socks));
if (!listener->socks) {
- callback(SILC_NET_NO_MEMORY, NULL, context);
+ callback(SILC_ERR_OUT_OF_MEMORY, NULL, context);
return NULL;
}
if (l)
delete l;
if (callback)
- callback(SILC_NET_ERROR, NULL, context);
+ callback(SILC_ERR, NULL, context);
+ if (listener)
+ silc_net_close_listener(listener);
+ return NULL;
+}
+
+/* Create TCP listener, multiple ports */
+
+SilcNetListener
+silc_net_tcp_create_listener2(const char *local_ip_addr, int *ports,
+ SilcUInt32 port_count,
+ SilcBool ignore_port_error,
+ SilcBool lookup, SilcBool require_fqdn,
+ SilcSchedule schedule,
+ SilcNetCallback callback, void *context)
+{
+ SilcNetListener listener = NULL;
+ SilcSymbianTCPListener *l = NULL;
+ TInetAddr server;
+ TInt ret;
+ int i;
+
+ SILC_LOG_DEBUG(("Creating TCP listener"));
+
+ if (!schedule) {
+ schedule = silc_schedule_get_global();
+ if (!schedule) {
+ silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
+ goto err;
+ }
+ }
+
+ if (!callback) {
+ silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
+ goto err;
+ }
+
+ listener = (SilcNetListener)silc_calloc(1, sizeof(*listener));
+ if (!listener) {
+ callback(SILC_ERR_OUT_OF_MEMORY, NULL, context);
+ return NULL;
+ }
+ listener->schedule = schedule;
+ listener->callback = callback;
+ listener->context = context;
+ listener->require_fqdn = require_fqdn;
+ listener->lookup = lookup;
+
+ if (port_count > 0) {
+ listener->socks = (SilcSocket *)silc_calloc(port_count,
+ sizeof(*listener->socks));
+ if (!listener->socks) {
+ callback(SILC_ERR_OUT_OF_MEMORY, NULL, context);
+ return NULL;
+ }
+ } else {
+ listener->socks = (SilcSocket *)silc_calloc(1, sizeof(*listener->socks));
+ if (!listener->socks) {
+ callback(SILC_ERR_OUT_OF_MEMORY, NULL, context);
+ return NULL;
+ }
+
+ port_count = 1;
+ }
+
+ /* Bind to ports */
+ for (i = 0; i < port_count; i++) {
+ SILC_LOG_DEBUG(("Binding to local address %s:%d",
+ local_ip_addr ? local_ip_addr : "0.0.0.0",
+ ports ? ports[i] : 0));
+
+ l = new SilcSymbianTCPListener;
+ if (!l)
+ goto err;
+
+ /* Connect to socket server */
+ ret = l->ss.Connect();
+ if (ret != KErrNone)
+ goto err;
+
+#ifdef SILC_THREADS
+ /* Make our socket shareable between threads */
+ l->ss.ShareAuto();
+#endif /* SILC_THREADS */
+
+ /* Set listener address */
+ if (!silc_net_set_sockaddr(&server, local_ip_addr, ports ? ports[i] : 0)) {
+ if (ignore_port_error) {
+ delete l;
+ continue;
+ }
+ goto err;
+ }
+
+ /* Create the socket */
+ ret = l->sock.Open(l->ss, KAfInet, KSockStream, KProtocolInetTcp);
+ if (ret != KErrNone) {
+ if (ignore_port_error) {
+ delete l;
+ continue;
+ }
+ SILC_LOG_ERROR(("Cannot create socket, error %d", ret));
+ goto err;
+ }
+
+ /* Set the socket options */
+ ret = l->sock.SetOpt(KSoReuseAddr, KSolInetIp, 1);
+ if (ret != KErrNone) {
+ if (ignore_port_error) {
+ delete l;
+ continue;
+ }
+ SILC_LOG_ERROR(("Cannot set socket options, error %d", ret));
+ goto err;
+ }
+
+ /* Bind the listener socket */
+ ret = l->sock.Bind(server);
+ if (ret != KErrNone) {
+ if (ignore_port_error) {
+ delete l;
+ continue;
+ }
+ SILC_LOG_DEBUG(("Cannot bind socket, error %d", ret));
+ goto err;
+ }
+
+ /* Specify that we are listenning */
+ ret = l->sock.Listen(5);
+ if (ret != KErrNone) {
+ if (ignore_port_error) {
+ delete l;
+ continue;
+ }
+ SILC_LOG_ERROR(("Cannot set socket listenning, error %d", ret));
+ goto err;
+ }
+ l->Listen();
+
+ l->listener = listener;
+ listener->socks[i] = (SilcSocket)l;
+ listener->socks_count++;
+ }
+
+ if (ignore_port_error && !listener->socks_count) {
+ l = NULL;
+ goto err;
+ }
+
+ SILC_LOG_DEBUG(("TCP listener created"));
+
+ return listener;
+
+ err:
+ if (l)
+ delete l;
+ if (callback)
+ callback(SILC_ERR, NULL, context);
if (listener)
silc_net_close_listener(listener);
return NULL;
silc_free(listener);
}
-} /* extern "C" */
/**************************** TCP/IP connecting *****************************/
-static void silc_net_connect_stream(SilcSocketStreamStatus status,
- SilcStream stream, void *context);
+static void silc_net_connect_stream(SilcResult status,
+ SilcStream stream, void *context);
+
+} /* extern "C" */
/* TCP connecting class */
if (iStatus != KErrNone) {
if (callback)
- callback(SILC_NET_ERROR, NULL, context);
+ callback(SILC_ERR, NULL, context);
sock->CancelConnect();
delete sock;
ss->Close();
/* TCP stream creation callback */
-static void silc_net_connect_stream(SilcSocketStreamStatus status,
+static void silc_net_connect_stream(SilcResult status,
SilcStream stream, void *context)
{
SilcSymbianTCPConnect *conn = (SilcSymbianTCPConnect *)context;
- SilcNetStatus net_status = SILC_NET_OK;
SILC_LOG_DEBUG(("Socket stream creation status %d", status));
- if (status != SILC_SOCKET_OK) {
- /* In case of error, the socket has been destroyed already via
- silc_stream_destroy. */
- if (status == SILC_SOCKET_UNKNOWN_IP)
- net_status = SILC_NET_UNKNOWN_IP;
- else if (status == SILC_SOCKET_UNKNOWN_HOST)
- net_status = SILC_NET_UNKNOWN_HOST;
- else
- net_status = SILC_NET_ERROR;
- }
-
/* Call connection callback */
if (conn->callback)
- conn->callback(net_status, stream, conn->context);
+ conn->callback(status, stream, conn->context);
else if (stream)
silc_stream_destroy(stream);
{
SilcSymbianTCPConnect *conn;
TInetAddr local, remote;
- SilcNetStatus status;
+ SilcResult status;
TInt ret;
- if (!remote_ip_addr || remote_port < 1 || !schedule || !callback)
+ if (!schedule) {
+ schedule = silc_schedule_get_global();
+ if (!schedule) {
+ silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
+ return NULL;
+ }
+ }
+
+ if (!remote_ip_addr || remote_port < 1 || !callback) {
+ silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
return NULL;
+ }
SILC_LOG_DEBUG(("Creating connection to host %s port %d",
remote_ip_addr, remote_port));
conn = new SilcSymbianTCPConnect;
if (!conn) {
- callback(SILC_NET_NO_MEMORY, NULL, context);
+ callback(SILC_ERR_OUT_OF_MEMORY, NULL, context);
return NULL;
}
conn->schedule = schedule;
conn->port = remote_port;
conn->remote = strdup(remote_ip_addr);
if (!conn->remote) {
- status = SILC_NET_NO_MEMORY;
+ status = SILC_ERR_OUT_OF_MEMORY;
goto err;
}
/* Allocate socket */
conn->sock = new RSocket;
if (!conn->sock) {
- status = SILC_NET_NO_MEMORY;
+ status = SILC_ERR_OUT_OF_MEMORY;
goto err;
}
/* Allocate socket server */
conn->ss = new RSocketServ;
if (!conn->ss) {
- status = SILC_NET_NO_MEMORY;
+ status = SILC_ERR_OUT_OF_MEMORY;
goto err;
}
ret = conn->ss->Connect();
if (ret != KErrNone) {
SILC_LOG_ERROR(("Error connecting to socket server, error %d", ret));
- status = SILC_NET_ERROR;
+ status = SILC_ERR;
goto err;
}
/* Start async operation */
conn->op = silc_async_alloc(silc_net_connect_abort, NULL, (void *)conn);
if (!conn->op) {
- status = SILC_NET_NO_MEMORY;
+ status = SILC_ERR_OUT_OF_MEMORY;
goto err;
}
sizeof(conn->remote_ip))) {
SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the "
"host", conn->remote));
- status = SILC_NET_HOST_UNREACHABLE;
+ status = SILC_ERR_UNREACHABLE;
goto err;
}
ret = conn->sock->Open(*conn->ss, KAfInet, KSockStream, KProtocolInetTcp);
if (ret != KErrNone) {
SILC_LOG_ERROR(("Cannot create socket, error %d", ret));
- status = SILC_NET_ERROR;
+ status = SILC_ERR;
goto err;
}
/* Connect to the host */
if (!silc_net_set_sockaddr(&remote, conn->remote_ip, remote_port)) {
SILC_LOG_ERROR(("Cannot connect (cannot set address)"));
- status = SILC_NET_ERROR;
+ status = SILC_ERR;
goto err;
}
conn->Connect(remote);
SILC_LOG_DEBUG(("Creating UDP stream"));
- if (!schedule)
- goto err;
+ if (!schedule) {
+ schedule = silc_schedule_get_global();
+ if (!schedule) {
+ silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
+ goto err;
+ }
+ }
SILC_LOG_DEBUG(("Binding to local address %s",
local_ip_addr ? local_ip_addr : "0.0.0.0"));