Exemplo n.º 1
0
void Pipe::clear()
{
	char buf[4096];
	ssize_t rv;

	do rv = ::read(readFd(), buf, sizeof(buf));
	while (rv > 0);
}
Exemplo n.º 2
0
ssize_t Pipe::read(void* buf, size_t size)
{
	ssize_t rv = ::read(readFd(), buf, size);

	if (rv > 0)
		size_ -= rv;

	return rv;
}
Exemplo n.º 3
0
ssize_t readFile(const char *path, void *buf, size_t max) {
	int fd = open(path, O_RDWR);
	ssize_t result;

	if (fd >= 0) {
		result = readFd(fd, buf, max);
		close(fd);
	} else {
		result = -1;
	}

	return result;
}
Exemplo n.º 4
0
ssize_t Pipe::read(int fd, off_t* fd_off, size_t size)
{
	ssize_t rv = 0;

#ifdef __APPLE__
	assert(__APPLE__);
#else
	rv = splice(readFd(), fd_off, fd, NULL, size, SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
#endif

	if (rv > 0)
		size_ -= rv;

	return rv;
}
Exemplo n.º 5
0
void OPipe::registerFd() {
    if(par) {
		par->registerReadFD(readFd(), this);
    }
}
Exemplo n.º 6
0
OO::HANDLE OPipe::handle() const {
    return readFd();
}
Exemplo n.º 7
0
void OPipe::unregisterFd() {
	if(par) {
		par->unregisterReadFD(readFd());
	}
}
int main(int argc, char **argv) {
    struct sockaddr_in config, peer;
    struct epoll_event ev, events[EP_SIZE];
    struct conn_status status[MAX_FD], *curr;

    in_port_t port = L_PORT;
    int listenfd, epfd, connfd;
    int i, flag, nfds, current_fd;
    socklen_t len;

    char buffer[FILE_BUFFER];

    // init
    sockinit(&config);
    sockset(&config, port, NULL);
    memset(status, 0, sizeof(status));
    memset(buffer, 0, sizeof(buffer));
    signal(SIGPIPE, SIG_IGN);

    // create server
    listenfd = create_server(&config);
    if (listenfd < 0) {
        perror("xtrans: server create failed.");
        exit(EXIT_FAILURE);
    }

    // create epoll fd
    epfd = ep_ini();
    if (epfd < 0) {
        perror("xtrans: epoll init failed.");
        exit(EXIT_FAILURE); 
    }

    // add listen fd to epoll
    ev.events = EPOLLIN;
    ev.data.fd = listenfd;
    if (ep_add(epfd, listenfd, &ev) == -1) {
        perror("xtrans: add epoll event failed.");
        exit(EXIT_FAILURE);
    }

    // main loop
    for(;;) {
        nfds = ep_col(epfd, events);

        for (i = 0; i < nfds; i++) {
            current_fd = events[i].data.fd;

            if (current_fd == listenfd) {
                connfd = accept(listenfd, (SA *)&peer, &len);
                if (connfd == -1) {
                    continue;
                }
                memcpy(&(status[connfd].client), &peer, sizeof(struct sockaddr_in));

                ev.events = EPOLLIN;
                ev.data.fd = connfd;
                if (ep_add(epfd, connfd, &ev) == -1) {
                    continue;
                }
                
                flag = welcome(connfd, &(status[connfd]));
                if (flag >= 0) {
                    x_log(LOG_CONN, &(status[connfd]));
                } else {
                    x_log(LOG_DISC, &(status[connfd]));
                    terminate(epfd, connfd, &(status[connfd]));
                }

            } else if (events[i].events & EPOLLIN) {
                curr = &(status[current_fd]);

                switch (curr->status) {
                    case STATUS_INIT:
                        memset(&(curr->username), 0, sizeof(char)*12);

                        flag = readln(current_fd, curr->username, 1); 
                        if (flag < 0) {
                            terminate(epfd, current_fd, curr);
                            x_log(LOG_DISC, curr);
                        }

                        flag = get_username(current_fd, curr);
                        if (flag < 0) {
                            terminate(epfd, current_fd, curr);
                            x_log(LOG_DISC, curr);
                        }

                        break;
                    case STATUS_NAME:
                        memset(&(curr->passwd), 0, sizeof(char)*32);

                        flag = readln(current_fd, curr->passwd, 1);
                        if (flag < 0) {
                            terminate(epfd, current_fd, curr);
                            x_log(LOG_DISC, curr);
                        }

                        flag = get_passwd(current_fd, curr);
                        if (flag < 0) {
                            terminate(epfd, current_fd, curr);
                            x_log(LOG_DISC, curr);
                        }
                        
                        flag = auth(curr->username, curr->passwd);
                        if (flag < 0) {
                            flag = auth_failed(current_fd, curr);
                            terminate(epfd, current_fd, curr);

                            x_log(LOG_REFS, curr);
                        } else {
                            flag = auth_success(current_fd, curr);
                            if (flag < 0) {
                                x_log(LOG_DISC, curr);
                            } else {
                                x_log(LOG_LOGI, curr);
                            }
                        }

                        break;
                    case STATUS_SUCC:
                        flag = readFd(current_fd, buffer, 1024);
                        if (flag < 0) {
                            terminate(epfd, current_fd, curr);
                            x_log(LOG_DISC, curr);
                        }

                        curr->recvb = flag;
                        terminate(epfd, current_fd, curr);
                        x_log(LOG_FINS, curr);

                        break;
                    default:
                        perror("xtrans: status not define, default action triggered.");
                        terminate(epfd, current_fd, curr);
                }
            }
        }   
    }

    return 0;
}
Exemplo n.º 9
0
ssize_t Pipe::read(int fd, off_t* fd_off, size_t size)
{
	return splice(readFd(), fd_off, fd, NULL, size, SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
}
Exemplo n.º 10
0
ssize_t Pipe::read(void* buf, size_t size)
{
	return ::read(readFd(), buf, size);
}