--- l2_ut_sa.c 2001/09/13 12:19:45 1.7
+++ l2_ut_sa.c 2001/09/13 20:07:17 1.8
@@ -56,6 +56,18 @@
int saa_proto;
};
+struct sa_st {
+ int sSocket;
+ int bTimeout;
+ struct timeval tvTimeout;
+ int nReadLen;
+ int nReadSize;
+ char *cpReadBuf;
+ int nWriteLen;
+ int nWriteSize;
+ char *cpWriteBuf;
+};
+
#if !defined(AF_LOCAL) && defined(AF_UNIX)
#define AF_LOCAL AF_UNIX
#endif
@@ -362,18 +374,6 @@
return SA_OK;
}
-struct sa_st {
- int sSocket;
- int bTimeout;
- struct timeval tvTimeout;
- int nReadLen;
- int nReadSize;
- char *cpReadBuf;
- int nWriteLen;
- int nWriteSize;
- char *cpWriteBuf;
-};
-
static sa_rc_t sa_socket_init(sa_t *sa, int family, int proto)
{
if (sa == NULL)
@@ -474,11 +474,14 @@
{
sa_rc_t rv;
+ 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)
return rv;
- /* NOT YET IMPLEMENTED */
- return SA_ERR_INT;
+ if (bind(sa->sSocket, laddr->saa_buf, laddr->saa_len) == -1)
+ return SA_ERR_SYS;
+ return SA_OK;
}
sa_rc_t sa_connect(sa_t *sa, sa_addr_t *raddr)
@@ -557,16 +560,59 @@
return rv;
}
-sa_rc_t sa_listen(sa_t *sa)
+sa_rc_t sa_listen(sa_t *sa, int backlog)
{
- /* FIXME: NOT YET IMPLEMENTED */
- return SA_ERR_INT;
+ if (sa == NULL)
+ return SA_ERR_ARG;
+ if (sa->sSocket == -1)
+ /* at least sa_bind() has to be already performed */
+ return SA_ERR_USE;
+ if (listen(sa->sSocket, backlog) == -1)
+ return SA_ERR_SYS;
+ return SA_OK;
}
-sa_rc_t sa_accept(sa_t *sa, sa_t **sa_new)
+sa_rc_t sa_accept(sa_t *sa, sa_addr_t **caddr, sa_t **csa)
{
- /* FIXME: NOT YET IMPLEMENTED */
- return SA_ERR_INT;
+ sa_rc_t rv;
+ int n;
+ fd_set fds;
+ union {
+ struct sockaddr_in sa4;
+#ifdef AF_INET6
+ struct sockaddr_in6 sa6;
+#endif
+ } sa4a6;
+ socklen_t sa_len;
+ int s;
+
+ if (sa == NULL)
+ return SA_ERR_ARG;
+ if (sa->sSocket == -1)
+ /* at least sa_listen() has to be already performed */
+ return SA_ERR_USE;
+ if (sa->bTimeout) {
+ FD_ZERO(&fds);
+ FD_SET(sa->sSocket, &fds);
+ do {
+ n = select(sa->sSocket+1, &fds, NULL, NULL, &sa->tvTimeout);
+ } while (n == -1 && errno == EINTR);
+ if (n == 0)
+ errno = ETIMEDOUT;
+ if (n <= 0)
+ return SA_ERR_SYS;
+ }
+ sa_len = sizeof(sa4a6);
+ if ((s = accept(sa->sSocket, (struct sockaddr *)&sa4a6, &sa_len)) == -1)
+ return SA_ERR_SYS;
+ if ((rv = sa_s2a(caddr, (struct sockaddr *)&sa4a6, sa_len)) != SA_OK)
+ return rv;
+ if ((rv = sa_create(csa)) != SA_OK) {
+ free(*caddr);
+ return rv;
+ }
+ (*csa)->sSocket = s;
+ return SA_OK;
}
static int sa_read_raw(sa_t *sa, char *cpBuf, int nBufLen)
|