Example #1
0
File: main.c Project: TARRANUM/ppp
/* ----------------------------------------------------------------------------- 
disestablish the socket as a ppp link
----------------------------------------------------------------------------- */
void pppoe_disestablish_ppp(int fd)
{
    int 	x;
    
    remove_fd(fd);

    if (ioctl(fd, PPPIOCDETACH, &x) < 0)
        error("Couldn't detach socket from link layer: %m");

    generic_disestablish_ppp(fd);
}
Example #2
0
/*
* 函数:control_logExit
* 函数功能:退出登录
* 函数参数:文件描述符 
* 返回值:无
*/
void control_logExit(int fd){
    link_p goal_struct = searchStruct_fd(fd);
    if(NULL == goal_struct){
        return;
    }
    user_p goal_node = ((user_p)goal_struct->value_p); 
    goal_node->linkTime = 0;
    goal_node->fd = -1;
    goal_node->online_flag = 0;
    memset(goal_node->ip, 0, sizeof(goal_node->ip));
    remove_fd(vfd, fd); 
}
/* removes fd from register, returning its filename and status via filename
   and status arguments.
*/
void deregister_fd(int fd)
{
	struct pid_reg **pid_iter = find_pid(getpid());
	if (*pid_iter)
	{
		struct fd_reg **fd_iter = find_fd(*pid_iter, fd);
		if (*fd_iter)
		{
			remove_fd(fd_iter);
			if ((*pid_iter)->fd_head == 0) remove_pid(pid_iter);
		}
	}
}
Example #4
0
status_t
close_fd_index(struct io_context* context, int fd)
{
	struct file_descriptor* descriptor = remove_fd(context, fd);

	if (descriptor == NULL)
		return B_FILE_ERROR;

	close_fd(descriptor);
	put_fd(descriptor);
		// the reference associated with the slot

	return B_OK;
}
void remove_pid(struct pid_reg **pid)
{
	struct pid_reg *tmp = *pid;
	struct fd_reg **fd_iter = &(*pid)->fd_head; 

#  if DEBUG == 1
	if (debug) fprintf(stderr, "fl_wrapper.so debug [%d]: PID %d removed.\n",
		getpid(), (*pid)->id);
#  endif

	while (*fd_iter) remove_fd(fd_iter);
	*pid = (*pid)->next;
	free(tmp);
}
Example #6
0
/*
* 函数:control_cliErr
* 函数功能:客户端非正常退出处理
* 函数参数:文件描述符
* 返回值:无
*/
void control_cliErr(int fd){
    link_p goal_struct = searchStruct_fd(fd);
    if(NULL == goal_struct){
        return;
    }
    user_p goal_node = ((user_p)goal_struct->value_p); 
    goal_node->linkTime = 0;
    goal_node->fd = -1;
    goal_node->online_flag = 0;
    memset(goal_node->ip, 0, sizeof(goal_node->ip));
    time_t curr_time = time(NULL);
    fprintf(fp_log, "%s%s非正常退出!\n", ctime(&curr_time), goal_node->name);
    close(fd);
    remove_fd(vfd, fd); 
}
Example #7
0
File: main.c Project: TARRANUM/ppp
/* ----------------------------------------------------------------------------- 
called back everytime we go out of select, and data needs to be read
the hook is called and has a chance to get data out of its file descriptor
in the case of PPPoE, we are not supposed to get data on the socket
if our socket gets awaken, that's because is has been closed
----------------------------------------------------------------------------- */
void pppoe_wait_input()
{
   
    if (sockfd != -1 && is_ready_fd(sockfd)) {
        // looks like we have been disconnected...
        // the status is updated only if link is not already down
        if (linkdown == 0) {
            notice("PPPoE hangup");
            status = EXIT_HANGUP;
        }
        remove_fd(sockfd);
        hungup = 1;
        lcp_lowerdown(0);	/* PPPoE link is no longer available */
        link_terminated(0);
    }
}
Example #8
0
/********************************************************************
 *
 * generic_disestablish_ppp - Restore device components to normal
 * operation, and reconnect the ppp unit to the loopback if in demand
 * mode.  This shouldn't call die() because it's called from die().
*/
void generic_disestablish_ppp(int dev_fd){
    /* Restore loop if needed */
    if(demand)
	restore_loop();

    /* Finally detach the device */
    initfdflags = -1;

    if (new_style_driver) {
	close(ppp_fd);
	ppp_fd = -1;
	if (!looped && ifunit >= 0 && ioctl(ppp_dev_fd, PPPIOCDETACH) < 0)
	    error("Couldn't release PPP unit: %m");
	if (!multilink)
	    remove_fd(ppp_dev_fd);
    }
}
Example #9
0
static fssh_status_t
common_close(int fd, bool kernel)
{
	struct io_context *io = get_current_io_context(kernel);
	struct file_descriptor *descriptor = remove_fd(io, fd);

	if (descriptor == NULL)
		return FSSH_B_FILE_ERROR;

#ifdef TRACE_FD
	if (!kernel)
		TRACE(("_user_close(descriptor = %p)\n", descriptor));
#endif

	close_fd(descriptor);
	put_fd(descriptor);
		// the reference associated with the slot

	return FSSH_B_OK;
}
Example #10
0
int storage_file_close(struct storage_msg *msg,
                       const void *r, size_t req_len)
{
    const struct storage_file_close_req *req = r;

    if (req_len != sizeof(*req)) {
        ALOGE("%s: invalid request length (%zd != %zd)\n",
              __func__, req_len, sizeof(*req));
        msg->result = STORAGE_ERR_NOT_VALID;
        goto err_response;
    }

    int fd = remove_fd(req->handle);
    ALOGV("%s: handle = %u: fd = %u\n", __func__, req->handle, fd);

    int rc = fsync(fd);
    if (rc < 0) {
        rc = errno;
        ALOGE("%s: fsync failed for fd=%u: %s\n",
              __func__, fd, strerror(errno));
        msg->result = translate_errno(rc);
        goto err_response;
    }

    rc = close(fd);
    if (rc < 0) {
        rc = errno;
        ALOGE("%s: close failed for fd=%u: %s\n",
              __func__, fd, strerror(errno));
        msg->result = translate_errno(rc);
        goto err_response;
    }

    msg->result = STORAGE_NO_ERROR;

err_response:
    return ipc_respond(msg, NULL, 0);
}
Example #11
0
void stat_server_push(void)
{
    unsigned int ip;

    if(global.stat_server_fd == INVALID_SOCKET)
    {
        /* attempt to make new connection to stats server */
        if(!*global.stat_user || !*global.stat_pass || !*global.stat_server)
        {
            return;     /* nothing defined */
        }

        global.stat_server_fd = make_tcp_connection(global.stat_server, global.stat_server_port, &ip);
        if(global.stat_server_fd != INVALID_SOCKET)
        {
            /* do a nonblocking connect */
            add_fd(global.stat_server_fd);
            set_write(global.stat_server_fd);
        }
        return;
    }

    snprintf(Buf, sizeof(Buf), "STATS %s %u %u 0 %.0f 0\r\n", global.report_name, global.usersHash->dbsize, global.fileLibCount, global.fileLibSize * 1024);

    if(WRITE(global.stat_server_fd, Buf, strlen(Buf)) == -1)
    {
        log_message_level( LOG_LEVEL_SERVER,  "stat_server_push: write: %s (%d)", strerror(N_ERRNO), N_ERRNO);
        CLOSE(global.stat_server_fd);
#if HAVE_POLL
        remove_fd(global.stat_server_fd);
#else
        FD_CLR(global.stat_server_fd, &global.read_fds);
        FD_CLR(global.stat_server_fd, &global.write_fds);
#endif
        global.stat_server_fd = INVALID_SOCKET;
    }
}
Example #12
0
  void QueryAgent::perform() {
    running_ = true;

    while(1) {
      fd_set read_fds = fds_;

      int ret = select(max_fd_ + 1, &read_fds, 0, 0, 0);

      if(ret < 0) {
        if(errno == EINTR || errno == EAGAIN) continue;
        std::cerr << "[QA: Select error: " << strerror(errno) << "]\n";
        running_ = false;
        return;
      } else if(ret == 0) {
        continue;
      }

      if(FD_ISSET(read_control(), &read_fds)) {
        // Noop, just a wake up!
        // Read the one byte written though so we don't clog up the
        // pipe and have to use the ponies later.
        char buf;
        if(read(read_control(), &buf, 1) < 0) {
          std::cerr << "[QA: Read error: " << strerror(errno) << "]\n";
        }
      } else if(server_fd_ > 0 && FD_ISSET(server_fd_, &read_fds)) {
        // now accept an incoming connection
        struct sockaddr_in sin;
        socklen_t addr_size = sizeof(sin);
        int client = accept(server_fd_, (struct sockaddr *)&sin, &addr_size);

        if(verbose_) {
          std::cerr << "[QA: Connection from " << inet_ntoa(sin.sin_addr)
                    << ":" << ntohs(sin.sin_port) << "]\n";
        }

        bert::IOWriter writer(client);
        bert::Encoder<bert::IOWriter> encoder(writer);
        encoder.write_version();

        encoder.write_tuple(2);
        encoder.write_atom("hello_query_agent");
        encoder.write_binary(RBX_HOST);

        add_fd(client);

        sockets_.push_back(client);
      } else {
        bool found = false;
        for(std::vector<int>::iterator i = sockets_.begin();
            i != sockets_.end(); /* nothing */) {
          int client = *i;
          if(FD_ISSET(client, &read_fds)) {
            found = true;
            if(!process_commands(client)) {
              if(verbose_) {
                struct sockaddr_in sin;
                socklen_t len = sizeof(sin);

                getpeername(*i, (struct sockaddr*)&sin, &len);
                std::cerr << "[QA: Disconnected " << inet_ntoa(sin.sin_addr)
                          << ":" << ntohs(sin.sin_port) << "]\n";
              }
              remove_fd(client);
              close(client);

              i = sockets_.erase(i);
              continue;
            }
          }
          i++;
        }

        if(!found) {
          std::cerr << "[QA: Corruption in select set detected.]\n";
          running_ = false;
          return;
        }
      }
    }
  }
Example #13
0
void stat_server_read(void)
{
    int     n;
    char   *p;
    int     do_close = 0;
    char   *salt, *servername;

    n = READ(global.stat_server_fd, Buf, sizeof(Buf));
    if(n <= 0)
    {
        if(n == -1)
            logerr("stat_server_read", "read");
        log_message_level( LOG_LEVEL_ERROR, "stat_server_read: got hangup");
        do_close = 1;
    }
    else
    {
        /* find end of line */
        p = strpbrk(Buf, "\r\n");
        if(*p)
            *p = 0;
        p = Buf;

        n = atoi(Buf);
        if(n == 220)
        {
            /* got connect

            * first, save the salt */
            next_arg(&p);
            salt = next_arg(&p);
            if(!salt)
            {
                log_message_level( LOG_LEVEL_ERROR, "stat_server_read:unable to get salt string");
                strcpy(Buf, "QUIT\r\n");
                WRITE(global.stat_server_fd, Buf, strlen(Buf));
            }
            else
            {
                if(stat_server_salt)
                    FREE(stat_server_salt);
                stat_server_salt = STRDUP(salt);

                snprintf(Buf, sizeof(Buf), "USER %s\r\n", global.stat_user);

                WRITE(global.stat_server_fd, Buf, strlen(Buf));
            }
        }
        else if(n == 221)
        {
            /* server hangup */
            do_close = 1;
        }
        else if(n == 300)
        {
            struct md5_ctx md;
            char    hash[33];

            md5_init_ctx(&md);
            md5_process_bytes(stat_server_salt, strlen(stat_server_salt), &md);
            md5_process_bytes(global.stat_pass, strlen(global.stat_pass), &md);
            md5_finish_ctx(&md, hash);
            expand_hex(hash, 16);
            hash[32] = 0;
            snprintf(Buf, sizeof(Buf), "PASS %s\r\n", hash);
            WRITE(global.stat_server_fd, Buf, strlen(Buf));
        }
        else if(n == 201)
        {
            /* auth complete */
            log_message_level( LOG_LEVEL_SERVER, "stat_server_read: logged in");

            /* send updated ip:port in case we are a dynamic server */
            snprintf(Buf, sizeof(Buf), "IPPORT %s %s %d\r\n",global.report_name, global.report_ip, global.report_port);
            WRITE(global.stat_server_fd, Buf, strlen(Buf));

            /* force immediate update */
            stat_server_push();
        }
        /* The >= comparison made the comparison for == 400 some
        lines below not to work any more. So dynamic server
        addresses are not updated any more.
        But this solution is cumbersome - to say the least.
        Is there any better - and ofc any quicker detection
        of an ip address change possible? Now it takes up 
        to stats_click seconds for a hub to recognize the 
        change.

        */
        else if(n / 100 > 4)
        {
            /* something failed */
            log_message_level( LOG_LEVEL_SERVER, "stat_server_read:%s", Buf);
            strcpy(Buf, "QUIT\r\n");
            WRITE(global.stat_server_fd, Buf, strlen(Buf));
        }
        /* this part is needed if a server owner wants to use an alias
        usually server aliases are not resolvable - leodav
        */
        else if(n == 400)
        {
            servername = lookup_hostname2();

            log_message_level( LOG_LEVEL_SERVER, "stat_server_read:%s", Buf);
            strcpy(Buf, "QUIT\r\n");
            WRITE(global.stat_server_fd, Buf, strlen(Buf));
            global.serverIP = lookup_ip(servername);
            global.report_ip = STRDUP(my_ntoa(global.serverIP));
            log_message_level( LOG_LEVEL_ERROR, "napigator.c:debug: %s", servername);
        }
        else if(n == 200)
        {
            /* stats updated successfully */
        }
        else
        {
            log_message_level( LOG_LEVEL_ERROR, "stat_server_read: unhandled:%s", Buf);
        }
    }

    if(do_close)
    {
        log_message_level( LOG_LEVEL_SERVER, "stat_server_read: closing connection");
        CLOSE(global.stat_server_fd);
#if HAVE_POLL
        remove_fd(global.stat_server_fd);
#else
        FD_CLR(global.stat_server_fd, &global.read_fds);
        FD_CLR(global.stat_server_fd, &global.write_fds);
#endif
        global.stat_server_fd = INVALID_SOCKET;
    }
}
Example #14
0
int net_connect(const struct addrinfo *addrinfo)
{
	int sockfd = -1, saved_errno;
	unsigned int i, num;
	const struct addrinfo *ipv4 = NULL, *ipv6 = NULL;
	const struct addrinfo *addr[MAX_PROTOS];
	socklen_t slen[MAX_PROTOS];
	struct pollfd pfd[MAX_PROTOS];

	for (; addrinfo; addrinfo = addrinfo->ai_next) {
		switch (addrinfo->ai_family) {
		case AF_INET:
			if (!ipv4)
				ipv4 = addrinfo;
			break;
		case AF_INET6:
			if (!ipv6)
				ipv6 = addrinfo;
			break;
		}
	}

	num = 0;
	/* We give IPv6 a slight edge by connecting it first. */
	if (ipv6) {
		addr[num] = ipv6;
		slen[num] = sizeof(struct sockaddr_in6);
		pfd[num].fd = socket(AF_INET6, ipv6->ai_socktype,
				     ipv6->ai_protocol);
		if (pfd[num].fd != -1)
			num++;
	}
	if (ipv4) {
		addr[num] = ipv4;
		slen[num] = sizeof(struct sockaddr_in);
		pfd[num].fd = socket(AF_INET, ipv4->ai_socktype,
				     ipv4->ai_protocol);
		if (pfd[num].fd != -1)
			num++;
	}

	for (i = 0; i < num; i++) {
		if (!set_nonblock(pfd[i].fd, true)) {
			remove_fd(pfd, addr, slen, &num, i--);
			continue;
		}
		/* Connect *can* be instant. */
		if (connect(pfd[i].fd, addr[i]->ai_addr, slen[i]) == 0)
			goto got_one;
		if (errno != EINPROGRESS) {
			/* Remove dead one. */
			remove_fd(pfd, addr, slen, &num, i--);
		}
		pfd[i].events = POLLOUT;
	}

	while (num && poll(pfd, num, -1) != -1) {
		for (i = 0; i < num; i++) {
			int err;
			socklen_t errlen = sizeof(err);
			if (!pfd[i].revents)
				continue;
			if (getsockopt(pfd[i].fd, SOL_SOCKET, SO_ERROR, &err,
				       &errlen) != 0)
				goto out;
			if (err == 0)
				goto got_one;

			/* Remove dead one. */
			errno = err;
			remove_fd(pfd, addr, slen, &num, i--);
		}
	}

got_one:
	/* We don't want to hand them a non-blocking socket! */
	if (set_nonblock(pfd[i].fd, false))
		sockfd = pfd[i].fd;

out:
	saved_errno = errno;
	for (i = 0; i < num; i++)
		if (pfd[i].fd != sockfd)
			close(pfd[i].fd);
	errno = saved_errno;
	return sockfd;
}