--- l2_ch_socket.c 2001/11/02 18:42:51 1.31
+++ l2_ch_socket.c 2001/11/03 20:49:25 1.32
@@ -38,19 +38,14 @@
#include "l2.h"
#include "l2_p.h"
-#define L2_SOCKET_SABUFREAD 4096
-#define L2_SOCKET_SABUFWRITE 4096
-
/* declare private channel configuration */
typedef struct {
- sa_addr_t *saaServer;
- sa_t *saServer;
- long lTimeout;
-
- char *szHost; /* IP Address or name of host to connect to */
- int iProto; /* Protocol to use, IPPROTO_TCP or IPPROTO_UDP */
- int iPort; /* TCP Port to connect to */
- int iSocket; /* Socket descriptor used during writing */
+ char *szProto;
+ char *szHost;
+ char *szPort;
+ long nTimeout;
+ sa_addr_t *saaRemote;
+ sa_t *saRemote;
} l2_ch_socket_t;
/* create channel */
@@ -63,14 +58,12 @@
return L2_ERR_MEM;
/* initialize configuration with reasonable defaults */
- cfg->saaServer = NULL;
- cfg->saServer = NULL;
- cfg->lTimeout = 30;
-
- cfg->szHost = NULL;
- cfg->iProto = -1;
- cfg->iPort = 0;
- cfg->iSocket = -1;
+ cfg->szProto = strdup("tcp");
+ cfg->szHost = NULL;
+ cfg->szPort = NULL;
+ cfg->nTimeout = 30;
+ cfg->saaRemote = NULL;
+ cfg->saRemote = NULL;
/* link private channel configuration into channel context */
ctx->vp = cfg;
@@ -82,57 +75,53 @@
static l2_result_t hook_configure(l2_context_t *ctx, l2_channel_t *ch, const char *fmt, va_list ap)
{
l2_ch_socket_t *cfg = (l2_ch_socket_t *)ctx->vp;
- char *szProtocol = NULL;
l2_param_t pa[5];
l2_result_t rv;
/* feed and call generic parameter parsing engine */
- L2_PARAM_SET(pa[0], protocol, STRING, &szProtocol);
- L2_PARAM_SET(pa[1], timeout, INT, &cfg->lTimeout);
- L2_PARAM_SET(pa[2], host, CHARPTR, &cfg->szHost);
- L2_PARAM_SET(pa[3], port, INT, &cfg->iPort);
+ L2_PARAM_SET(pa[0], proto, STRING, &cfg->szProto);
+ L2_PARAM_SET(pa[1], host, STRING, &cfg->szHost);
+ L2_PARAM_SET(pa[2], port, STRING, &cfg->szPort);
+ L2_PARAM_SET(pa[3], timeout, INT, &cfg->nTimeout);
L2_PARAM_END(pa[4]);
rv = l2_util_setparams(pa, fmt, ap);
- /* translate incoming configuration parameters */
- if (szProtocol != NULL) {
- if (strcmp(szProtocol, "IPPROTO_UDP") == 0)
- cfg->iProto = IPPROTO_UDP;
- else if (strcmp(szProtocol, "IPPROTO_TCP") == 0)
- cfg->iProto = IPPROTO_TCP;
- else
- return L2_ERR_ARG;
- }
+ /* sanity check configuration parameters */
+ if ( cfg->szProto != NULL
+ && !( strcmp(cfg->szProto, "udp") == 0
+ || strcmp(cfg->szProto, "tcp") == 0))
+ return L2_ERR_ARG;
- free(szProtocol);
return rv;
}
/* open channel */
static l2_result_t hook_open(l2_context_t *ctx, l2_channel_t *ch)
{
- sa_rc_t rc;
l2_ch_socket_t *cfg = (l2_ch_socket_t *)ctx->vp;
+ sa_rc_t rc;
/* make sure a path was set */
- if (cfg->szHost == NULL)
+ if (cfg->szHost == NULL || cfg->szPort == NULL)
return L2_ERR_USE;
/* create socket address */
- if ((rc = sa_addr_create(&cfg->saaServer)) != SA_OK)
+ if ((rc = sa_addr_create(&cfg->saaRemote)) != SA_OK)
return (rc == SA_ERR_SYS ? L2_ERR_SYS : L2_ERR_INT);
- if ((rc = sa_addr_u2a(cfg->saaServer, "inet://%s:%d",
- cfg->szHost, cfg->iPort)) != SA_OK)
+ if ((rc = sa_addr_u2a(cfg->saaRemote, "inet://%s:%s",
+ cfg->szHost, cfg->szPort)) != SA_OK)
return (rc == SA_ERR_SYS ? L2_ERR_SYS : L2_ERR_INT);
/* create socket */
- if ((rc = sa_create(&cfg->saServer)) != SA_OK)
+ if ((rc = sa_create(&cfg->saRemote)) != SA_OK)
return (rc == SA_ERR_SYS ? L2_ERR_SYS : L2_ERR_INT);
/* configure socket parameters */
- sa_timeout(cfg->saServer, SA_TIMEOUT_ALL, cfg->lTimeout, 0);
- sa_buffer (cfg->saServer, SA_BUFFER_READ, L2_SOCKET_SABUFREAD);
- sa_buffer (cfg->saServer, SA_BUFFER_WRITE, L2_SOCKET_SABUFWRITE);
+ sa_timeout(cfg->saRemote, SA_TIMEOUT_ALL, cfg->nTimeout, 0);
+ if (strcmp(cfg->szProto, "tcp") == 0) {
+ sa_buffer(cfg->saRemote, SA_BUFFER_READ, 4096);
+ sa_buffer(cfg->saRemote, SA_BUFFER_WRITE, 4096);
+ }
return L2_OK;
}
@@ -142,33 +131,34 @@
l2_level_t level, const char *buf, size_t buf_size)
{
l2_ch_socket_t *cfg = (l2_ch_socket_t *)ctx->vp;
- size_t sizeWrite;
- size_t sizeRemain;
- sa_t *sa;
- sa_rc_t rc;
- sa_addr_t *saa;
-
- /* parameter checks */
- assert(cfg->saServer != NULL);
-
- /* establish connection to server */
- saa = cfg->saaServer;
- sa = cfg->saServer;
- if ((rc = sa_connect(sa, saa)) != SA_OK) {
- sa_shutdown(sa, "rw"); /* shutdown connection to server */
- return (rc == SA_ERR_SYS ? L2_ERR_SYS : L2_ERR_INT);
+ size_t sizeWrite;
+ size_t sizeRemain;
+ sa_rc_t rc;
+
+ /* establish connection to server (TCP only) */
+ if (strcmp(cfg->szProto, "tcp") == 0) {
+ if ((rc = sa_connect(cfg->saRemote, cfg->saaRemote)) != SA_OK)
+ return (rc == SA_ERR_SYS ? L2_ERR_SYS : L2_ERR_INT);
}
- if (cfg->iProto == IPPROTO_TCP) {
- /* write message to channel socket, but check to make */
- /* sure that the whole message was successfully written */
- sizeWrite = 0;
- sizeRemain = buf_size;
- while(sizeRemain) {
- if ((rc = sa_write(sa, buf, sizeRemain, &sizeWrite)) != SA_OK)
- return (rc == SA_ERR_SYS ? L2_ERR_SYS : L2_ERR_INT);
- sizeRemain = sizeRemain - sizeWrite; /* how much is left? */
- }
+ /* write message to channel socket, but check to make
+ sure that the whole message was successfully written */
+ sizeWrite = 0;
+ sizeRemain = buf_size;
+ while (sizeRemain > 0) {
+ if (strcmp(cfg->szProto, "tcp") == 0)
+ rc = sa_write(cfg->saRemote, buf, sizeRemain, &sizeWrite);
+ else
+ rc = sa_send(cfg->saRemote, buf, sizeRemain, &sizeWrite, cfg->saaRemote);
+ if (rc != SA_OK)
+ return (rc == SA_ERR_SYS ? L2_ERR_SYS : L2_ERR_INT);
+ sizeRemain = sizeRemain - sizeWrite; /* how much is left? */
+ }
+
+ /* shutdown connection to server (TCP only) */
+ if (strcmp(cfg->szProto, "tcp") == 0) {
+ if ((rc = sa_shutdown(cfg->saRemote, "rw")) != SA_OK)
+ return (rc == SA_ERR_SYS ? L2_ERR_SYS : L2_ERR_INT);
}
return L2_OK;
@@ -179,17 +169,14 @@
{
l2_ch_socket_t *cfg = (l2_ch_socket_t *)ctx->vp;
- /* parameter checks */
-/* FIXME! assert(cfg->saServer->fdSocket != -1); */
-
/* destroy remote address */
- if (cfg->saServer != NULL) {
- sa_destroy(cfg->saServer);
- cfg->saServer = NULL;
- }
- if (cfg->saaServer != NULL) {
- sa_addr_destroy(cfg->saaServer);
- cfg->saaServer = NULL;
+ if (cfg->saRemote != NULL) {
+ sa_destroy(cfg->saRemote);
+ cfg->saRemote = NULL;
+ }
+ if (cfg->saaRemote != NULL) {
+ sa_addr_destroy(cfg->saaRemote);
+ cfg->saaRemote = NULL;
}
return L2_OK;
@@ -201,6 +188,12 @@
l2_ch_socket_t *cfg = (l2_ch_socket_t *)ctx->vp;
/* destroy channel configuration */
+ if (cfg->szProto != NULL)
+ free(cfg->szProto);
+ if (cfg->szHost != NULL)
+ free(cfg->szHost);
+ if (cfg->szPort != NULL)
+ free(cfg->szPort);
free(cfg);
return L2_OK;
|