--- l2_ut_sa.c 2001/09/14 19:06:40 1.12
+++ l2_ut_sa.c 2001/10/02 14:11:51 1.13
@@ -27,7 +27,10 @@
** sa.c: socket abstraction library
*/
-/* system headers */
+/* include optional Autoconf header */
+#include "l2_config.h"
+
+/* include system API headers */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
@@ -45,17 +48,19 @@
#include <sys/socket.h>
#include <arpa/inet.h>
-/* own headers */
-#include "l2_config.h"
+/* include own API header */
+#define SA_PREFIX l2_ut_
#include "l2_ut_sa.h"
+/* socket address abstraction object */
struct sa_addr_st {
- struct sockaddr *saa_buf;
- socklen_t saa_len;
- int saa_family;
- int saa_proto;
+ struct sockaddr *saBuf;
+ socklen_t slBuf;
+ int nFamily;
+ int nProto;
};
+/* socket abstraction object */
struct sa_st {
int sSocket;
int bTimeout;
@@ -68,10 +73,12 @@
char *cpWriteBuf;
};
+/* make sure AF_LOCAL define exists */
#if !defined(AF_LOCAL) && defined(AF_UNIX)
#define AF_LOCAL AF_UNIX
#endif
+/* make sure inet_pton() exists */
#if defined(HAVE_INET_PTON)
#define sa_inet_pton inet_pton
#elif defined(HAVE_INET_ATON)
@@ -93,6 +100,7 @@
#error "neither inet_pton nor inet_aton available"
#endif
+/* make sure inet_ntop() exists */
#if defined(HAVE_INET_NTOP)
#define sa_inet_ntop inet_ntop
#elif defined(HAVE_INET_NTOA)
@@ -120,6 +128,7 @@
#error "neither inet_ntop nor inet_ntoa available"
#endif
+/* make sure vsnprintf() exists */
#if defined(HAVE_VSNPRINTF)
#define sa_vsnprintf vsnprintf
#elif defined(HAVE_VSPRINTF)
@@ -137,6 +146,7 @@
#error "neither vsnprintf nor vsprintf available"
#endif
+/* make sure snprintf() exists */
#if defined(HAVE_SNPRINTF)
#define sa_snprintf snprintf
#else
@@ -175,10 +185,11 @@
char *cp;
int sf;
+ /* argument sanity check */
if (saa == NULL || uri == NULL)
return SA_ERR_ARG;
- /* create or just take over URI */
+ /* on-the-fly create or just take over URI */
va_start(ap, uri);
sa_vsnprintf(uribuf, sizeof(uribuf), uri, ap);
va_end(ap);
@@ -271,14 +282,14 @@
/* create result address structure */
if ((*saa = (sa_addr_t *)malloc(sizeof(sa_addr_t))) == NULL)
return SA_ERR_MEM;
- if (((*saa)->saa_buf = (struct sockaddr *)malloc(sl)) == NULL) {
+ if (((*saa)->saBuf = (struct sockaddr *)malloc(sl)) == NULL) {
free(*saa);
return SA_ERR_MEM;
}
- memcpy((*saa)->saa_buf, sa, sl);
- (*saa)->saa_len = sl;
- (*saa)->saa_family = sf;
- (*saa)->saa_proto = nProto;
+ memcpy((*saa)->saBuf, sa, sl);
+ (*saa)->slBuf = sl;
+ (*saa)->nFamily = sf;
+ (*saa)->nProto = nProto;
return SA_OK;
}
@@ -297,12 +308,12 @@
/* create result address structure */
if ((*saa = (sa_addr_t *)malloc(sizeof(sa_addr_t))) == NULL)
return SA_ERR_MEM;
- if (((*saa)->saa_buf = (struct sockaddr *)malloc(salen)) == NULL) {
+ if (((*saa)->saBuf = (struct sockaddr *)malloc(salen)) == NULL) {
free(*saa);
return SA_ERR_MEM;
}
- memcpy((*saa)->saa_buf, sabuf, salen);
- (*saa)->saa_len = salen;
+ memcpy((*saa)->saBuf, sabuf, salen);
+ (*saa)->slBuf = salen;
/* fill in family */
sf = 0;
@@ -318,13 +329,13 @@
sf = AF_INET6;
}
#endif
- (*saa)->saa_family = sf;
+ (*saa)->nFamily = sf;
/* fill in protocol */
if ((pe = getprotobyname("tcp")) != NULL)
- (*saa)->saa_proto = pe->p_proto;
+ (*saa)->nProto = pe->p_proto;
else
- (*saa)->saa_proto = 0;
+ (*saa)->nProto = 0;
return SA_OK;
}
@@ -341,16 +352,16 @@
if (saa == NULL || uri == NULL)
return SA_ERR_ARG;
- if ((pe = getprotobynumber(saa->saa_proto)) == NULL)
+ if ((pe = getprotobynumber(saa->nProto)) == NULL)
return SA_ERR_SYS;
- if (saa->saa_family == AF_INET) {
- sa4 = (struct sockaddr_in *)((void *)saa->saa_buf);
+ if (saa->nFamily == AF_INET) {
+ sa4 = (struct sockaddr_in *)((void *)saa->saBuf);
inet_ntop(AF_INET, &sa4->sin_addr.s_addr, caHost, sizeof(caHost));
nPort = ntohs(sa4->sin_port);
}
#ifdef AF_INET6
- else if (saa->saa_family == AF_INET6) {
- sa6 = (struct sockaddr_in6 *)((void *)saa->saa_buf);
+ else if (saa->nFamily == AF_INET6) {
+ sa6 = (struct sockaddr_in6 *)((void *)saa->saBuf);
inet_ntop(AF_INET6, &sa6->sin6_addr.s6_addr, caHost, sizeof(caHost));
nPort = ntohs(sa6->sin6_port);
}
@@ -367,10 +378,10 @@
if (saa == NULL || sabuf == NULL || salen == 0)
return SA_ERR_ARG;
- if ((*sabuf = (struct sockaddr *)malloc(saa->saa_len)) == NULL)
+ if ((*sabuf = (struct sockaddr *)malloc(saa->slBuf)) == NULL)
return SA_ERR_MEM;
- memmove(*sabuf, saa->saa_buf, saa->saa_len);
- *salen = saa->saa_len;
+ memmove(*sabuf, saa->saBuf, saa->slBuf);
+ *salen = saa->slBuf;
return SA_OK;
}
@@ -397,7 +408,7 @@
{
if (sa == NULL)
return SA_ERR_ARG;
- if (sa->sSocket == -1) {
+ if (sa->sSocket != -1) {
close(sa->sSocket);
sa->sSocket = -1;
}
@@ -485,9 +496,9 @@
if (sa == NULL || laddr == NULL)
return SA_ERR_ARG;
if (sa->sSocket == -1)
- if ((rv = sa_socket_init(sa, laddr->saa_family, laddr->saa_proto)) != SA_OK)
+ if ((rv = sa_socket_init(sa, laddr->nFamily, laddr->nProto)) != SA_OK)
return rv;
- if (bind(sa->sSocket, laddr->saa_buf, laddr->saa_len) == -1)
+ if (bind(sa->sSocket, laddr->saBuf, laddr->slBuf) == -1)
return SA_ERR_SYS;
return SA_OK;
}
@@ -503,11 +514,11 @@
return SA_ERR_ARG;
if (sa->sSocket == -1)
- if ((rv = sa_socket_init(sa, raddr->saa_family, raddr->saa_proto)) != SA_OK)
+ if ((rv = sa_socket_init(sa, raddr->nFamily, raddr->nProto)) != SA_OK)
return rv;
if (!sa->bTimeout)
- return connect(sa->sSocket, raddr->saa_buf, raddr->saa_len);
+ return connect(sa->sSocket, raddr->saBuf, raddr->slBuf);
error = 0;
rv = SA_OK;
@@ -519,7 +530,7 @@
fcntl(sa->sSocket, F_SETFL, flags|O_NONBLOCK);
/* perform the connect */
- if ((n = connect(sa->sSocket, raddr->saa_buf, raddr->saa_len)) < 0) {
+ if ((n = connect(sa->sSocket, raddr->saBuf, raddr->slBuf)) < 0) {
if (errno != EINPROGRESS) {
error = errno;
goto done;
@@ -634,6 +645,8 @@
} sa_buf;
socklen_t sa_len;
+ if (sa == NULL)
+ return SA_ERR_ARG;
sa_len = sizeof(sa_buf);
if (getpeername(sa->sSocket, (struct sockaddr *)&sa_buf, &sa_len) < 0)
return SA_ERR_SYS;
@@ -653,6 +666,8 @@
} sa_buf;
socklen_t sa_len;
+ if (sa == NULL)
+ return SA_ERR_ARG;
sa_len = sizeof(sa_buf);
if (getsockname(sa->sSocket, (struct sockaddr *)&sa_buf, &sa_len) < 0)
return SA_ERR_SYS;
@@ -661,6 +676,14 @@
return SA_OK;
}
+sa_rc_t sa_getfd(sa_t *sa, int *fd)
+{
+ if (sa == NULL || fd == NULL)
+ return SA_ERR_ARG;
+ *fd = sa->sSocket;
+ return SA_OK;
+}
+
static int sa_read_raw(sa_t *sa, char *cpBuf, int nBufLen)
{
int rv;
@@ -874,7 +897,7 @@
if (n <= 0)
return SA_ERR_SYS;
}
- if ((n = sendto(sa->sSocket, buf, buflen, 0, raddr->saa_buf, raddr->saa_len)) == -1)
+ if ((n = sendto(sa->sSocket, buf, buflen, 0, raddr->saBuf, raddr->slBuf)) == -1)
return SA_ERR_SYS;
if (bufdone != NULL)
*bufdone = n;
|