int main(int argc, char *argv[]) { struct sockaddr_un addr; size_t addr_len; int fd = socket(PF_UNIX, SOCK_STREAM, 0); if (fd < 0) { perror("socket"); exit(1); } addr.sun_family = AF_UNIX; strcpy(addr.sun_path, "/tmp/nmd_ctrl"); addr_len = sizeof(addr); if (connect(fd, (struct sockaddr *)&addr, addr_len) != 0) { perror("connect"); exit(1); } conn = io_new_fd(fd); struct io *io_in = io_new_fd(0); io_watch(io_in, IO_IN, stdin_send, NULL); io_run(); }
IO * http_init(int port) { struct sockaddr_in addr; int fd; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = INADDR_ANY; fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) die("socket on port %d failed\n", port); socket_set_reuseaddr(fd, 1); socket_set_nonblock(fd); if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) die("bind on port %d failed\n", port); if (listen(fd, 10) == -1) die("listen on port %d failed\n", port); return io_new_fd(fd); }
void http_accept(IO *io, int events, void *arg) { int cfd, fd = io_get_fd(io); struct sockaddr_in addr = { 0 }; size_t addr_len = sizeof(addr); IO *client; struct service *service = arg; if (events & IO_IN) { if ((cfd = accept(fd, (struct sockaddr *)&addr, &addr_len)) > -1) { socket_set_nonblock(cfd); client = io_new_fd(cfd); /* TODO: create conn */ struct conn *conn = conn_new(service, client); printf("--> accepted\n"); io_watch(client, IO_IN | IO_HUP, http_incoming, conn); } } }
static i_io_glue_t * mathom_io_new_fd(int fd) { return io_new_fd(fd); }