int fd; /* System-dependent data */
node n;
+ int entered;
} sock;
sock *sk_new(pool *); /* Allocate new socket */
int sk_open(sock *); /* Open socket */
+void sk_close(sock *); /* Safe close of socket even from socket hook */
int sk_send(sock *, unsigned len); /* Send data, <0=err, >0=ok, 0=sleep */
int sk_send_to(sock *, unsigned len, ip_addr to, unsigned port); /* sk_send to given destination */
void sk_dump_all(void);
#define SK_MAGIC 7 /* Internal use by sysdep code */
#define SK_UNIX_PASSIVE 8
#define SK_UNIX 9
-#define SK_DELETED 10 /* Set to this if you want to delete socket from err_hook */
+#define SK_DELETED 10 /* Internal use by sk_close */
/*
* Multicast sockets are slightly different from the other ones:
sock *s = (sock *) r;
if (s->fd >= 0)
- rem_node(&s->n);
+ {
+ close(s->fd);
+ rem_node(&s->n);
+ }
}
static void
s->tbsize = 0;
s->err_hook = NULL;
s->fd = -1;
+ s->entered = 0;
return s;
}
static void
sk_tcp_connected(sock *s)
{
- s->rx_hook(s, 0);
s->type = SK_TCP;
sk_alloc_bufs(s);
+ s->tx_hook(s);
}
static int
switch (type)
{
case SK_TCP_ACTIVE:
+ s->ttx = ""; /* Force s->ttx != s->tpos */
+ /* Fall thru */
case SK_TCP_PASSIVE:
fd = socket(BIRD_PF, SOCK_STREAM, IPPROTO_TCP);
break;
case SK_MAGIC:
break;
default:
+ sk_alloc_bufs(s);
#ifdef IPV6
#ifdef IPV6_MTU_DISCOVER
{
#endif
}
- sk_alloc_bufs(s);
add_tail(&sock_list, &s->n);
return 0;
return -1;
}
+void
+sk_close(sock *s)
+{
+ if (s->entered)
+ s->type = SK_DELETED;
+ else
+ rfree(s);
+}
+
static int
sk_maybe_write(sock *s)
{
{
switch (s->type)
{
- case SK_TCP_ACTIVE:
- {
- sockaddr sa;
- fill_in_sockaddr(&sa, s->daddr, s->dport);
- if (connect(s->fd, (struct sockaddr *) &sa, sizeof(sa)) >= 0)
- sk_tcp_connected(s);
- else if (errno != EINTR && errno != EAGAIN && errno != EINPROGRESS)
- {
- log(L_ERR "connect: %m");
- s->err_hook(s, errno);
- }
- return 0;
- }
case SK_TCP_PASSIVE:
{
sockaddr sa;
}
case SK_MAGIC:
return s->rx_hook(s, 0);
+ case SK_DELETED:
+ return 0;
default:
{
sockaddr sa;
static void
sk_write(sock *s)
{
- while (s->ttx != s->tbuf && sk_maybe_write(s) > 0)
- s->tx_hook(s);
+ switch (s->type)
+ {
+ case SK_TCP_ACTIVE:
+ {
+ sockaddr sa;
+ fill_in_sockaddr(&sa, s->daddr, s->dport);
+ if (connect(s->fd, (struct sockaddr *) &sa, sizeof(sa)) >= 0)
+ sk_tcp_connected(s);
+ else if (errno != EINTR && errno != EAGAIN && errno != EINPROGRESS)
+ {
+ log(L_ERR "connect: %m");
+ s->err_hook(s, errno);
+ }
+ break;
+ }
+ case SK_DELETED:
+ return;
+ default:
+ while (s->ttx != s->tbuf && sk_maybe_write(s) > 0)
+ s->tx_hook(s);
+ }
}
void
WALK_LIST_DELSAFE(n, p, sock_list)
{
s = SKIP_BACK(sock, n, n);
+ s->entered = 1;
if (FD_ISSET(s->fd, &rd))
{
FD_CLR(s->fd, &rd);
FD_CLR(s->fd, &wr);
sk_write(s);
}
+ s->entered = 0;
if (s->type == SK_DELETED)
rfree(s);
}