--- sa.c 2001/10/03 19:40:31 1.2
+++ sa.c 2001/10/04 11:25:44 1.3
@@ -190,7 +190,30 @@
return chars;
}
-sa_rc_t sa_u2a(sa_addr_t **saa, const char *uri, ...)
+sa_rc_t sa_addr_create(sa_addr_t **saa)
+{
+ if (saa == NULL)
+ return SA_ERR_ARG;
+ if ((*saa = (sa_addr_t *)malloc(sizeof(sa_addr_t))) == NULL)
+ return SA_ERR_MEM;
+ (*saa)->saBuf = NULL;
+ (*saa)->slBuf = 0;
+ (*saa)->nFamily = 0;
+ (*saa)->nProto = 0;
+ return SA_OK;
+}
+
+sa_rc_t sa_addr_destroy(sa_addr_t *saa)
+{
+ if (saa == NULL)
+ return SA_ERR_ARG;
+ if (saa->saBuf != NULL)
+ free(saa->saBuf);
+ free(saa);
+ return SA_OK;
+}
+
+sa_rc_t sa_addr_u2a(sa_addr_t *saa, const char *uri, ...)
{
va_list ap;
int nPort;
@@ -308,20 +331,16 @@
return SA_ERR_ARG;
/* create result address structure */
- if ((*saa = (sa_addr_t *)malloc(sizeof(sa_addr_t))) == NULL)
+ if ((saa->saBuf = (struct sockaddr *)malloc(sl)) == NULL)
return SA_ERR_MEM;
- if (((*saa)->saBuf = (struct sockaddr *)malloc(sl)) == NULL) {
- free(*saa);
- return SA_ERR_MEM;
- }
- memcpy((*saa)->saBuf, sa, sl);
- (*saa)->slBuf = sl;
- (*saa)->nFamily = sf;
- (*saa)->nProto = nProto;
+ memcpy(saa->saBuf, sa, sl);
+ saa->slBuf = sl;
+ saa->nFamily = sf;
+ saa->nProto = nProto;
return SA_OK;
}
-sa_rc_t sa_s2a(sa_addr_t **saa, const struct sockaddr *sabuf, socklen_t salen)
+sa_rc_t sa_addr_s2a(sa_addr_t *saa, const struct sockaddr *sabuf, socklen_t salen)
{
struct sockaddr_in *sa4;
#ifdef AF_INET6
@@ -334,14 +353,10 @@
return SA_ERR_ARG;
/* create result address structure */
- if ((*saa = (sa_addr_t *)malloc(sizeof(sa_addr_t))) == NULL)
- return SA_ERR_MEM;
- if (((*saa)->saBuf = (struct sockaddr *)malloc(salen)) == NULL) {
- free(*saa);
+ if ((saa->saBuf = (struct sockaddr *)malloc(salen)) == NULL)
return SA_ERR_MEM;
- }
- memcpy((*saa)->saBuf, sabuf, salen);
- (*saa)->slBuf = salen;
+ memcpy(saa->saBuf, sabuf, salen);
+ saa->slBuf = salen;
/* fill in family */
sf = 0;
@@ -357,17 +372,17 @@
sf = AF_INET6;
}
#endif
- (*saa)->nFamily = sf;
+ saa->nFamily = sf;
/* fill in protocol */
if ((pe = getprotobyname("tcp")) != NULL)
- (*saa)->nProto = pe->p_proto;
+ saa->nProto = pe->p_proto;
else
- (*saa)->nProto = 0;
+ saa->nProto = 0;
return SA_OK;
}
-sa_rc_t sa_a2u(const sa_addr_t *saa, char **uri)
+sa_rc_t sa_addr_a2u(sa_addr_t *saa, char **uri)
{
char uribuf[1024];
struct protoent *pe;
@@ -401,7 +416,7 @@
return SA_OK;
}
-sa_rc_t sa_a2s(const sa_addr_t *saa, struct sockaddr **sabuf, socklen_t *salen)
+sa_rc_t sa_addr_a2s(sa_addr_t *saa, struct sockaddr **sabuf, socklen_t *salen)
{
if (saa == NULL || sabuf == NULL || salen == 0)
return SA_ERR_ARG;
@@ -674,10 +689,14 @@
sa_len = sizeof(sa_buf);
if ((s = accept(sa->sSocket, (struct sockaddr *)&sa_buf, &sa_len)) == -1)
return SA_ERR_SYS;
- if ((rv = sa_s2a(caddr, (struct sockaddr *)&sa_buf, sa_len)) != SA_OK)
+ if ((rv = sa_addr_create(caddr)) != SA_OK)
+ return rv;
+ if ((rv = sa_addr_s2a(*caddr, (struct sockaddr *)&sa_buf, sa_len)) != SA_OK) {
+ sa_addr_destroy(*caddr);
return rv;
+ }
if ((rv = sa_create(csa)) != SA_OK) {
- free(*caddr);
+ sa_addr_destroy(*caddr);
return rv;
}
(*csa)->sSocket = s;
@@ -700,8 +719,12 @@
sa_len = sizeof(sa_buf);
if (getpeername(sa->sSocket, (struct sockaddr *)&sa_buf, &sa_len) < 0)
return SA_ERR_SYS;
- if ((rv = sa_s2a(raddr, (struct sockaddr *)&sa_buf, sa_len)) != SA_OK)
+ if ((rv = sa_addr_create(raddr)) != SA_OK)
return rv;
+ if ((rv = sa_addr_s2a(*raddr, (struct sockaddr *)&sa_buf, sa_len)) != SA_OK) {
+ sa_addr_destroy(*raddr);
+ return rv;
+ }
return SA_OK;
}
@@ -721,8 +744,12 @@
sa_len = sizeof(sa_buf);
if (getsockname(sa->sSocket, (struct sockaddr *)&sa_buf, &sa_len) < 0)
return SA_ERR_SYS;
- if ((rv = sa_s2a(laddr, (struct sockaddr *)&sa_buf, sa_len)) != SA_OK)
+ if ((rv = sa_addr_create(laddr)) != SA_OK)
+ return rv;
+ if ((rv = sa_addr_s2a(*laddr, (struct sockaddr *)&sa_buf, sa_len)) != SA_OK) {
+ sa_addr_destroy(*laddr);
return rv;
+ }
return SA_OK;
}
@@ -842,8 +869,12 @@
sa_len = sizeof(sa_buf);
if ((n = recvfrom(sa->sSocket, buf, buflen, 0, (struct sockaddr *)&sa_buf, &sa_len)) == -1)
return SA_ERR_SYS;
- if ((rv = sa_s2a(raddr, (struct sockaddr *)&sa_buf, sa_len)) != SA_OK)
+ if ((rv = sa_addr_create(raddr)) != SA_OK)
+ return rv;
+ if ((rv = sa_addr_s2a(*raddr, (struct sockaddr *)&sa_buf, sa_len)) != SA_OK) {
+ sa_addr_destroy(*raddr);
return rv;
+ }
if (bufdone != NULL)
*bufdone = n;
return SA_OK;
|