odyssey/src/mm_bind.c

46 lines
748 B
C
Raw Normal View History

2017-03-21 12:30:50 +00:00
/*
* machinarium.
*
* cooperative multitasking engine.
*/
#include <machinarium.h>
#include <machinarium_private.h>
2017-03-21 12:30:50 +00:00
MACHINE_API int
machine_bind(machine_io_t obj, struct sockaddr *sa)
{
mm_io_t *io = obj;
2017-03-29 11:58:27 +00:00
mm_io_set_errno(io, 0);
2017-04-11 14:39:20 +00:00
if (io->connected) {
mm_io_set_errno(io, EINPROGRESS);
2017-03-29 11:58:27 +00:00
return -1;
}
2017-04-11 14:39:20 +00:00
int rc;
rc = mm_io_socket(io, sa);
if (rc == -1)
goto error;
rc = mm_socket_set_reuseaddr(io->fd, 1);
if (rc == -1) {
mm_io_set_errno(io, errno);
goto error;
}
rc = mm_socket_bind(io->fd, sa);
if (rc == -1) {
mm_io_set_errno(io, errno);
goto error;
}
rc = machine_io_attach(io);
if (rc == -1)
goto error;
2017-03-21 12:30:50 +00:00
return 0;
2017-04-11 14:39:20 +00:00
error:
if (io->fd != -1) {
close(io->fd);
io->fd = -1;
}
io->handle.fd = -1;
return -1;
2017-03-21 12:30:50 +00:00
}