Beispiel #1
0
void Delayms (uint32_t ms)
{
    TIMER_STOP();
    TIMER_START(ms * 1000);
    while (!TIMER_EXPIRED());
    TIMER_STOP();
}
static XfceMailwatchNetConnStatus
xfce_mailwatch_net_conn_do_connect(XfceMailwatchNetConn *net_conn,
                                   struct sockaddr *addr,
                                   size_t addrlen,
                                   GError **error)
{
    gint ret;
    TIMER_INIT;

    TIMER_START;
    do {
        ret = connect(net_conn->fd, addr, addrlen);
    } while(ret < 0 && (errno == EINTR || errno == EAGAIN)
            && !TIMER_EXPIRED(RECV_TIMEOUT) && SHOULD_CONTINUE(net_conn));

    if(!ret || (ret < 0 && errno == EINPROGRESS))  /* we're done here */
        return XFCE_MAILWATCH_NET_CONN_SUCCESS;

    /* this is a little different.  the only 'fatal' error at this
     * point in the overall connect operation is if SHOULD_CONTINUE
     * is false.  in that case, we set |error| and return _FATAL.
     * if the timer expired or another error occurred, we just
     * return a non-fatal _ERROR without setting |error|. */
    if(!SHOULD_CONTINUE(net_conn)) {
        if(error) {
            g_set_error(error, XFCE_MAILWATCH_ERROR,
                        XFCE_MAILWATCH_ERROR_ABORTED,
                        _("Operation aborted"));
        }

        return XFCE_MAILWATCH_NET_CONN_FATAL;
    }

    return XFCE_MAILWATCH_NET_CONN_ERROR;
}
static gboolean
xfce_mailwatch_net_conn_tls_handshake(XfceMailwatchNetConn *net_conn,
                                      GError **error)
{
    gint ret;
    TIMER_INIT;

    TIMER_START;
    do {
        ret = gnutls_handshake(net_conn->gt_session);
    } while((ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
            && !TIMER_EXPIRED(RECV_TIMEOUT) && SHOULD_CONTINUE(net_conn));

    if(ret != GNUTLS_E_SUCCESS) {
        gint code = XFCE_MAILWATCH_ERROR_FAILED;
        const gchar *reason;

        if(!SHOULD_CONTINUE(net_conn)) {
            code = XFCE_MAILWATCH_ERROR_ABORTED;
            reason = _("Operation aborted");
        } else if(ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
            reason = strerror(ETIMEDOUT);
        else
            reason = gnutls_strerror(ret);
        if(error)
            g_set_error(error, XFCE_MAILWATCH_ERROR, code, "%s", reason);
        g_critical("XfceMailwatch: TLS handshake failed: %s", reason);

        return FALSE;
    }

    DBG("TLS handshake succeeded");

    return TRUE;
}
Beispiel #4
0
void temp_timer_proc(void *arg)
{
    if (!TIMER_RUNNING(temp_timer)) return;

    if (TIMER_EXPIRED(temp_timer)) {
	temp_showtemp(0);
	TIMER_SET(temp_timer,2*CFE_HZ);
	}
}
Beispiel #5
0
void cfe_sleep(int ticks)
{
    int64_t timer;

    TIMER_SET(timer,ticks);
    while (!TIMER_EXPIRED(timer)) {	
	POLL();
	}
}
Beispiel #6
0
static int
bcm1250_write(cfe_devctx_t *ctx, iocb_buffer_t *buffer)
{
    bcm1250_t *softc = ctx->dev_softc;
    physaddr_t arg_p = softc->mailbox + 0;
    physaddr_t cmd_p = softc->mailbox + 4;
    chunk_desc code;
    uint32_t cmd;
    int64_t timer;
    int res;
    
    /* Note: This code assumes that PTR_TO_PHYS gives a PCI memory space
       address that is accessible via our BAR4 or BAR5 */

    code.addr = PTR_TO_PHYS((uint8_t *)buffer->buf_ptr);
    code.len  = buffer->buf_length;

    cmd = 0x1;      /* load */
    if (!elf_header((uint8_t *)buffer->buf_ptr)) {
	/* No recognizable elf seal, so assume compressed. */
	cmd |= 0x2;
	}

    phys_write32(arg_p | MBOX_SET_BIT, PTR_TO_PHYS(&code));
    phys_write32(cmd_p | MBOX_SET_BIT, cmd);     /* load */

    /* Wait for handshake */

    res = CFE_ERR_TIMEOUT;
    TIMER_SET(timer, 5*CFE_HZ);
    while (!TIMER_EXPIRED(timer)) {
        if ((phys_read32(cmd_p) & 0x3) == 0) {
	    softc->downloaded = 1;
	    buffer->buf_retlen = 0;
	    /* Note that the result code need not be translated only
               because we are assuming a CFE in the device that is
               compatible with us. */
	    res = (int)phys_read32(arg_p);
	    break;
	    }
	POLL();
	}

    return res;
}
static gint
xfce_mailwatch_net_conn_recv_internal(XfceMailwatchNetConn *net_conn,
                                      guchar *buf,
                                      gsize buf_len,
                                      gboolean block,
                                      GError **error)
{
    gint ret, bin = 0, code = XFCE_MAILWATCH_ERROR_FAILED;
    const gchar *reason;
    TIMER_INIT;

    TIMER_START;
    do {
        fd_set rfd;
        struct timeval tv = { 1, 0 };

        FD_ZERO(&rfd);
        FD_SET(net_conn->fd, &rfd);
        if(!block)
            tv.tv_sec = 0;

#ifdef HAVE_SSL_SUPPORT
        /* Read data from the gnutls read buffer, first. */
        if (net_conn->is_secure
            && (ret = gnutls_record_check_pending(net_conn->gt_session)) > 0) {
            break;
        }
#endif
        ret = select(FD_SETSIZE, &rfd, NULL, NULL, &tv);
        if(ret > 0 && FD_ISSET(net_conn->fd, &rfd))
            break;
        else if(ret < 0 && errno != EINTR) {
            g_set_error(error, XFCE_MAILWATCH_ERROR,
                        XFCE_MAILWATCH_ERROR_FAILED, "%s", strerror(errno));
            return -1;
        } else if(!block)
            return 0;
    } while((ret == 0 || (ret < 0 && errno == EINTR))
            && !TIMER_EXPIRED(RECV_TIMEOUT) && SHOULD_CONTINUE(net_conn));

    if(ret < 0 && errno != EINTR) {
        if(error) {
            g_set_error(error, XFCE_MAILWATCH_ERROR,
                        XFCE_MAILWATCH_ERROR_FAILED, "%s", strerror(errno));
        }
        return -1;
    } else if(!SHOULD_CONTINUE(net_conn)) {
        if(error) {
            g_set_error(error, XFCE_MAILWATCH_ERROR,
                        XFCE_MAILWATCH_ERROR_ABORTED, "%s",
                        _("Operation aborted"));
        }
        return -1;
    } else if(TIMER_EXPIRED(RECV_TIMEOUT)) {
        if(error) {
            g_set_error(error, XFCE_MAILWATCH_ERROR,
                        XFCE_MAILWATCH_ERROR_FAILED, "%s",
                        strerror(ETIMEDOUT));
        }
        return -1;
    }

#ifdef HAVE_SSL_SUPPORT
    if(net_conn->is_secure) {
        gint gret;
        code = XFCE_MAILWATCH_ERROR_FAILED;

        TIMER_START;
        do {
            gret = gnutls_record_recv(net_conn->gt_session, buf, buf_len);

            if(gret == GNUTLS_E_REHANDSHAKE) {
                if(!xfce_mailwatch_net_conn_tls_handshake(net_conn, error))
                    return -1;
                gret = GNUTLS_E_AGAIN;
            }
        } while((gret == GNUTLS_E_INTERRUPTED || gret == GNUTLS_E_AGAIN)
                && !TIMER_EXPIRED(RECV_TIMEOUT) && SHOULD_CONTINUE(net_conn));
        
        if(gret < 0) {
            if(error) {
                if(!SHOULD_CONTINUE(net_conn)) {
                    code = XFCE_MAILWATCH_ERROR_ABORTED;
                    reason = _("Operation aborted");
                } else if(TIMER_EXPIRED(RECV_TIMEOUT))
                    reason = strerror(ETIMEDOUT);
                else
                    reason = gnutls_strerror(gret);

                g_set_error(error, XFCE_MAILWATCH_ERROR, code,
                            _("Failed to receive encrypted data: %s"),
                            reason);
            }

            bin = -1;
        } else
            bin = gret;
    } else
#endif
    {
        gint pret;
        code = XFCE_MAILWATCH_ERROR_FAILED;

        TIMER_START;
        do {
            pret = recv(net_conn->fd, buf, buf_len, MSG_NOSIGNAL);
        } while(pret < 0 && (errno == EINTR || errno == EAGAIN)
                && !TIMER_EXPIRED(RECV_TIMEOUT) && SHOULD_CONTINUE(net_conn));

        if(pret < 0) {
            if(error) {
                if(!SHOULD_CONTINUE(net_conn)) {
                    code = XFCE_MAILWATCH_ERROR_ABORTED;
                    reason = _("Operation aborted");
                } else if(TIMER_EXPIRED(RECV_TIMEOUT))
                    reason = strerror(ETIMEDOUT);
                else
                    reason = strerror(errno);

                g_set_error(error, XFCE_MAILWATCH_ERROR, code,
                            _("Failed to receive data: %s"), reason);
            }
            bin = -1;
        } else
            bin = pret;
    }

    return bin;
}
gint
xfce_mailwatch_net_conn_send_data(XfceMailwatchNetConn *net_conn,
                                  const guchar *buf,
                                  gssize buf_len,
                                  GError **error)
{
    gint bout = 0;
    TIMER_INIT;

    g_return_val_if_fail(net_conn && (!error || !*error), -1);
    g_return_val_if_fail(net_conn->fd != -1, -1);

    if(buf_len < 0)
        buf_len = strlen((const gchar *)buf);

#ifdef HAVE_SSL_SUPPORT
    if(net_conn->is_secure) {
        gint ret = 0, totallen = buf_len;
        gint bytesleft = totallen;

        while(bytesleft > 0) {
            TIMER_START;
            do {
                ret = gnutls_record_send(net_conn->gt_session,
                                         buf + totallen - bytesleft,
                                         bytesleft);

                if(ret == GNUTLS_E_REHANDSHAKE) {
                    if(!xfce_mailwatch_net_conn_tls_handshake(net_conn, error))
                        return -1;
                    ret = GNUTLS_E_AGAIN;
                }
            } while((ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN)
                    && !TIMER_EXPIRED(RECV_TIMEOUT) && SHOULD_CONTINUE(net_conn));

            if(ret < 0) {
                gint code = XFCE_MAILWATCH_ERROR_FAILED;
                const gchar *reason;

                if(!SHOULD_CONTINUE(net_conn)) {
                    code = XFCE_MAILWATCH_ERROR_ABORTED;
                    reason = _("Operation aborted");
                } else if(TIMER_EXPIRED(RECV_TIMEOUT))
                    reason = strerror(ETIMEDOUT);
                else
                    reason = gnutls_strerror(ret);
                if(error) {
                    g_set_error(error, XFCE_MAILWATCH_ERROR, code,
                                _("Failed to send encrypted data: %s"),
                                reason);
                }
                DBG("gnutls_record_send() failed (%d): %s", ret, reason);
                return -1;
            } else {
                bout += ret;
                bytesleft -= ret;
            }
        }
    } else
#endif
    {
        TIMER_START;
        do {
            bout = send(net_conn->fd, buf, buf_len, MSG_NOSIGNAL);
        } while(bout < 0 && (errno == EINTR || errno == EAGAIN)
                && !TIMER_EXPIRED(RECV_TIMEOUT) && SHOULD_CONTINUE(net_conn));
    }

    if(bout < 0 && error) {
        gint code = XFCE_MAILWATCH_ERROR_FAILED;
        const gchar *reason;

        if(!SHOULD_CONTINUE(net_conn)) {
            code = XFCE_MAILWATCH_ERROR_ABORTED;
            reason = _("Operation aborted");
        } else if(errno == EINTR || errno == EAGAIN)
            reason = strerror(ETIMEDOUT);
        else
            reason = strerror(errno);

        g_set_error(error, XFCE_MAILWATCH_ERROR, code,
                    _("Failed to send data: %s"), reason);
    }

    return bout;
}
static XfceMailwatchNetConnStatus
xfce_mailwatch_net_conn_get_connect_status(XfceMailwatchNetConn *net_conn,
                                           struct sockaddr *addr,
                                           GError **error)
{
    TIMER_INIT;

    TIMER_START;
    do {
        fd_set wfd;
        struct timeval tv = { 1, 0 };
        int sock_err = 0;
        socklen_t sock_err_len = sizeof(int);

        FD_ZERO(&wfd);
        FD_SET(net_conn->fd, &wfd);

        DBG("checking for a connection...");

        /* wait until the connect attempt finishes */
        if(select(FD_SETSIZE, NULL, &wfd, NULL, &tv) < 0) {
            if(errno == EINTR)
                continue;
            /* FIXME: should a select() failure actually be fatal? */
            return XFCE_MAILWATCH_NET_CONN_ERROR;
        }

        /* check to see if it finished, and, if so, if there was an
         * error, or if it completed successfully */
        if(!FD_ISSET(net_conn->fd, &wfd))
            continue;

        if(!getsockopt(net_conn->fd, SOL_SOCKET, SO_ERROR,
                       &sock_err, &sock_err_len)
           && !sock_err)
        {
            DBG("    connection succeeded");

            /* figure out the actual port */
            switch(addr->sa_family) {
#ifdef ENABLE_IPV6_SUPPORT
                case AF_INET6:
                {
                    struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
                    net_conn->actual_port = ntohs(addr_in6->sin6_port);
                    break;
                }
#endif
                case AF_INET:
                {
                    struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
                    net_conn->actual_port = ntohs(addr_in->sin_port);
                    break;
                }

                default:
                    g_warning("Unable to determine socket type to get real port number");
                    break;
            }

            errno = 0;
            return XFCE_MAILWATCH_NET_CONN_SUCCESS;
        } else {
            DBG("    connection failed: sock_err is (%d) %s",
                sock_err, strerror(sock_err));
            errno = sock_err;
            return XFCE_MAILWATCH_NET_CONN_ERROR;
        }
    } while(!TIMER_EXPIRED(RECV_TIMEOUT) && SHOULD_CONTINUE(net_conn));

    if(!SHOULD_CONTINUE(net_conn)) {
        if(error) {
            g_set_error(error, XFCE_MAILWATCH_ERROR,
                        XFCE_MAILWATCH_ERROR_ABORTED, _("Operation aborted"));
        }

        return XFCE_MAILWATCH_NET_CONN_FATAL;
    }

    return XFCE_MAILWATCH_NET_CONN_ERROR;
}
Beispiel #10
0
int paa_main(const paa_config_t *  const global_cfg)
{
    cfg = global_cfg;
    const sockaddr_in4_t * paa_pana_sockaddr;
    const sockaddr_in4_t * paa_ep_sockaddr;
    const sockaddr_in4_t * ep_sockaddr;
    int pana_sockfd;
    int ep_sockfd;
    fd_set pana_read_flags;
    fd_set ep_read_flags;
    socklen_t peer_socklen;
    struct timeval selnowait = {0 , 10};  //Nonblocking select
    int ix;
    
    sockaddr_in4_t peer_addr;      // used to store the peers addres per packet
    bytebuff_t * rxbuff = NULL;
    bytebuff_t * txbuff = NULL;
    int ret;
    
    pana_session_t * pacs;

    paa_ctx_t * ctx = NULL;

    
    bzero(&paa_pana_sockaddr, sizeof paa_pana_sockaddr);
    paa_pana_sockaddr = &cfg->paa_pana;
    

    bzero(&paa_ep_sockaddr, sizeof paa_ep_sockaddr);
    paa_ep_sockaddr = &cfg->paa_ep;
    
    
    bzero(&ep_sockaddr, sizeof ep_sockaddr);
    ep_sockaddr = &cfg->ep_addr;
    
    if ((pana_sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
        return ERR_SOCK_ERROR;
    }

    if ((ep_sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
        return ERR_SOCK_ERROR;
    }
    

    if ((bind(pana_sockfd, paa_pana_sockaddr, sizeof (*paa_pana_sockaddr))) < 0) {
        close(pana_sockfd);
        return ERR_BIND_SOCK;
    }

    if ((bind(ep_sockfd, paa_ep_sockaddr, sizeof (*paa_ep_sockaddr))) < 0) {
        close(ep_sockfd);
        return ERR_BIND_SOCK;
    }

    if ((connect(ep_sockfd, ep_sockaddr, sizeof (*ep_sockaddr))) < 0) {
        close(ep_sockfd);
        return ERR_CONNECT_SOCK;
    }

    /*
     * Setup the sockfds (nonblocking)
     */
    
//    if (fcntl(pana_sockfd,F_SETFL, fcntl(pana_sockfd,F_GETFL,0) | O_NONBLOCK) == -1) {
//        close(pana_sockfd);
//        DEBUG("Could not set the socket as nonblocking");
//        dbg_printf(ERR_SETFL_NONBLOCKING,"Could not set the socket as nonblocking");
//        return ERR_NONBLOK_SOCK;
//    }
    FD_SET(pana_sockfd, &pana_read_flags);

//    if (fcntl(ep_sockfd,F_SETFL, fcntl(ep_sockfd,F_GETFL,0) | O_NONBLOCK) == -1) {
//        close(ep_sockfd);
//        dbg_printf(ERR_SETFL_NONBLOCKING,"Could not set the socket as nonblocking");
//        return ERR_NONBLOK_SOCK;
//    }
    FD_SET(ep_sockfd, &ep_read_flags);
    
    rxbuff = bytebuff_alloc(PANA_PKT_MAX_SIZE);

    /*
     * The PAA will keep on going
     */
    while(TRUE) {

        /*
         * While there are incoming pana packets to be processed process them.
         */
        FD_SET(pana_sockfd, &pana_read_flags);
        while(select(pana_sockfd + 1, &pana_read_flags, NULL, NULL, &selnowait) > 0 &&
                (FD_ISSET(pana_sockfd, &pana_read_flags))) {

            ret = recvfrom(pana_sockfd, bytebuff_data(rxbuff), rxbuff->size, 0,
                    &peer_addr, &peer_socklen);
            if (ret <= 0) {
                DEBUG(" No bytes were read");
                continue;
            }
            rxbuff->used = ret;
            dbg_asciihexdump(PANA_PKT_RECVD,"Contents:",
                    bytebuff_data(rxbuff), rxbuff->used);


            if (retrieve_msgType(rxbuff) == PMT_PCI &&
                    !paa_get_session_by_peeraddr(&peer_addr)) {
                pacs = paa_sess_create(cfg, &peer_addr);
                if (pacs != NULL) {
                    ctx = pacs->ctx;
                    ctx->pana_sockfd = pana_sockfd;
                    ctx->ep_sockfd = ep_sockfd;
                    pacs_list = sess_list_insert(pacs_list, pacs); 
                    PKT_RECVD_Set();                     /* sets pkt recv event for current ctx */
                    txbuff = paa_process(pacs, rxbuff);
                    if (pacs->cstate == PAA_STATE_CLOSED) {
                        paa_remove_active_session(pacs);
                    }
                }
                else {
                    DEBUG("New session could not be created");
                }
            } else {
                pacs = paa_get_session_by_sessID(retrieve_sessID(rxbuff));
                if (pacs != NULL) {
                    if (pacs->peer_addr.sin_addr.s_addr == peer_addr.sin_addr.s_addr &&
                            pacs->peer_addr.sin_port == peer_addr.sin_port) {
                        if (pacs->seq_rx - 1 == retrieve_seqNo(rxbuff)) {
                            Resend_cached(pacs);
                        } else if(pacs->seq_rx == retrieve_seqNo(rxbuff)) {
                            PKT_RECVD_Set();
                            txbuff = paa_process(pacs, rxbuff);
                            if (pacs->cstate == PAA_STATE_CLOSED) {
                                paa_remove_active_session(pacs);
                            }
                        } else {
                            DEBUG("Sequence number missmatch.");
                        }
                    } else {
                        DEBUG("!!!!!!!!!!!!!SESSION Hjacking Attempted!!!!!!!!!!!!!!!!!!");
                    }
                } else {
                    DEBUG("Wrong session requested");
                }
            }

            if (txbuff != NULL) {
                dbg_asciihexdump(PANA_PKT_SENDING,"Contents:",
                        bytebuff_data(txbuff), txbuff->used);
                ret = sendto(pana_sockfd, bytebuff_data(txbuff), txbuff->used, 0,
                        &peer_addr, sizeof(peer_addr));
                if (ret < 0 && ret != txbuff->used) {
                    /* will try at retransmission time */
                    
                    DEBUG("There was a problem when sending the message.");
                }
                free_bytebuff(txbuff);
            }
        }
        
        //----------------------------------------------------------------------------------------------
        /*
         * Then check timers for each session
         */

        for (pacs = pacs_list; pacs != NULL; pacs = pacs->next) {
            ctx = pacs->ctx;
            if (ctx == NULL) {
                dbg_printf(MALFORMED_SESSION, "This session is missing context");
                continue;
            }

            if (TIMER_EXPIRED(ctx->rtx_timer)) {
                RTX_TIMEOUT_Set();
                paa_process(pacs, NULL);
                if (pacs->cstate == PAA_STATE_CLOSED) {
                    paa_remove_active_session(pacs);
                }
            }

            if (TIMER_EXPIRED(ctx->session_timer)) {
                SESS_TIMEOUT_Set();
                paa_process(pacs, NULL);
                if (pacs->cstate == PAA_STATE_CLOSED) {
                    paa_remove_active_session(pacs);
                }
            }
        }


        /* check for EP ACK's */
        FD_SET(ep_sockfd, &ep_read_flags);
        while (select(ep_sockfd + 1, &ep_read_flags, NULL, NULL, &selnowait) > 0 &&
                (FD_ISSET(ep_sockfd, &ep_read_flags))) {
            
            ret = recv(ep_sockfd, bytebuff_data(rxbuff), rxbuff->size, 0);
            if (ret <= 0) {
                DEBUG(" No bytes were read");
                continue;
            }
            rxbuff->used = ret;
            ep_rule_unregister(bytebuff_data(rxbuff)[0]);            
        }


        /* check EP_rtx_timers */
        for (ix = 0; ix < 256; ix++) {
            if(ep_ruleslist[ix] != NULL &&
                    TIMER_EXPIRED(ep_ruleslist[ix]->rtx_timer)) {

                txbuff = serialize_ep_pkt(ep_ruleslist[ix], ix);
                if (txbuff != NULL) {
                    ret = send(ep_sockfd, bytebuff_data(txbuff), txbuff->used, 0);
                }
                free_bytebuff(txbuff);
                
                if (ep_ruleslist[ix]->rtx_timer.count > cfg->rtx_max_count) {
                    ep_rule_unregister(ix);
                }
            }
        }

    }
    /*
     * TODO CLeanup
     */
    close(ep_sockfd);
    close(pana_sockfd);
}