Exemple #1
0
void sendMsg(message_t msg){
    int numbytes;
    int sockfd = -1;

    sockfd = connectIP(PORT_NUM, FLIP1);

    numbytes = write(sockfd, (char *) &msg, sizeof(message_t));
    if(numbytes == -1){
        perror("Write Error");
        exit(EXIT_FAILURE);
    }
    if(debug_value > 0){
        printf("DEBUG: Message sent successfully!\n");
    }
    
    r_msg(msg, sockfd);
}
Exemple #2
0
int bbsnet(struct _select_def *conf, int n)
{
    time_t now;
    struct hostent *pHost = NULL;
    struct sockaddr_in remote;
    unsigned char buf[BUFSIZ];
    int rc;
    int rv;
    int maxfdp1;
    fd_set readset;
    struct timeval tv;
    int tos = 020; /* Low delay bit */
    int i;
    sig_t oldsig;
    int ret;
    int key;

    char buf1[40], buf2[40], c, buf3[40];        //增加的变量
    int l;                      //判断是不是port
    int j;

    if (strcmp(host2[n], "般若波羅密") == 0) {  //如果是自定义站点,等待输入ip或域名

        memset(buf1, 0, sizeof(buf1));
        move(22,2);
        prints("\033[1;32m连往: \033[m");
        refresh();
        j = 0;
        l = 0;
        for (i = 0; i < 30;) {
            c = igetch();
            if (c == ' ' || c == '\015' || c == '\0' || c == '\n')
                break;
            if (c == ':') {
                l = 1;
                sprintf(buf3, "%c", c);
                prints("\033[0;1m");
                prints(buf3);
                prints("\033[1;33m");
                refresh();
                strncpy(ip_zdh, buf1, 40);
                strncpy(ip[n], ip_zdh, 40);
                memset(buf1, 0, sizeof(buf1));
                j = 0;
                i++;
            }
            if ((c >= '0' && c <= '9') || (((c == '.') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) && !l)) {
                sprintf(buf3, "%c", c);
                sprintf(buf2, "%s%c", buf1, c);
                sprintf(buf1, "%s", buf2);
                prints(buf3);
                refresh();
                j++;
                i++;
            }
            if ((c == 8) && (j >= 1)) {
                buf1[j - 1] = '\0';
                sprintf(buf3, "\033[D\033[1;33m_\033[%sm\033[D", l ? "1;33" : "0");
                prints(buf3);
                refresh();
                i--;
                j--;
            }
        }
        if (l == 0) {
            strncpy(ip_zdh, buf1, 40);
            strcpy(ip[n], ip_zdh);
        } else {
            port[n] = atoi(buf1);
        }
    }


    now = time(NULL);
    clear();
    prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\n",
           host2[n], ip[n]);
    prints("\033[1;32m如果在 %d 秒内无法连上,穿梭程序将放弃连接。\033[m\n",
           TIME_OUT);
    if (setjmp(jb) == 0) {
        oldsig = signal(SIGALRM, bbsnet_timeout);
        alarm(TIME_OUT);
        pHost = gethostbyname(ip[n]);
        alarm(0);
        signal(SIGALRM, oldsig);
    }
    if (pHost == NULL) {
        prints("\033[1;31m查找主机名失败!\033[m\n");
        pressreturn();
        return -1;
    }
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        prints("\033[1;31m无法创建socket!\033[m\n");
        pressreturn();
        return -1;
    }
    bzero(&remote, sizeof(remote));
    remote.sin_family = AF_INET;
    remote.sin_port = htons(port[n]);
    remote.sin_addr = *(struct in_addr *)pHost->h_addr_list[0];

    prints("\033[1;32m穿梭进度条提示您当前已使用的时间。\033[m\n");
    process_bar(0, MAX_PROCESS_BAR_LEN);
    for (i = 0; i < MAX_PROCESS_BAR_LEN; i++) {
        if (i == 0)
            rv = NonBlockConnectEx(sockfd, (struct sockaddr *)&remote,
                                   sizeof(remote), 500, 1);
        else
            rv = NonBlockConnectEx(sockfd, (struct sockaddr *)&remote,
                                   sizeof(remote), 500, 0);
        if (rv == ERR_TCPLIB_TIMEOUT) {
            process_bar(i+1, MAX_PROCESS_BAR_LEN);
            continue;
        } else if (rv == 0)
            break;
        else {
            prints("\033[1;31m连接失败!\033[m\n");
            pressreturn();
            ret = -1;
            goto on_error;
        }
    }
    if (i == MAX_PROCESS_BAR_LEN) {
        prints("\033[1;31m连接超时!\033[m\n");
        pressreturn();
        ret =  -1;
        goto on_error;
    }
    setsockopt(sockfd, IPPROTO_IP, IP_TOS, &tos, sizeof(int));
    prints("\033[1;31m连接成功!\033[m\n");
    bbsnet_report(host2[n], ip[n], now, 0);
    clear();
    refresh();
    bbsnet_send_winsize(sockfd);
    for (;;) {
        FD_ZERO(&readset);
        FD_SET(0, &readset);
        FD_SET(sockfd, &readset);
        maxfdp1 = sockfd + 1;
        tv.tv_sec = 1200;
        tv.tv_usec = 0;

        /*if ((rv = SignalSafeSelect(maxfdp1, &readset, NULL, NULL, &tv)) == -1)*/
        if ((rv = select(maxfdp1, &readset, NULL, NULL, &tv)) == -1) {
            if (errno == EINTR) {
                while (msg_count) {
                    msg_count--;
                    r_msg();
                }
                continue;
            }
            ret = -1;
            goto on_error;
        }
        if (rv == 0) {
            ret = -1;
            goto on_error;
        }

        if (FD_ISSET(sockfd, &readset)) {
            if ((rc = read(sockfd, buf, BUFSIZ)) < 0) {
                ret = -1;
                goto on_error;
            } else if (rc == 0)
                break;
            else if (strchr((void*)buf, 255)) /* 查找是否含有TELNET命令IAC */
                telnetopt(sockfd, (void*)buf, rc);
            else {
                output((void*)buf, rc);
                oflush();
            }
        }
        if (FD_ISSET(0, &readset)) {
            if ((rc = bbsnet_read(0, (void*)buf, BUFSIZ)) < 0) {
                ret = -1;
                goto on_error;
            }
            if (rc == 0)
                break;

            if ((key = list_select_remove_key(conf)) != KEY_INVALID) {
                if (key == KEY_ONSIZE) {
                    bbsnet_send_winsize(sockfd);
                }
            }

            write(sockfd, buf, rc);
        }
    }
    bbsnet_report(host2[n], ip[n], now, 1);
    ret = 0;
on_error:
    close(sockfd);
    clear();
    redoscr();
    return ret;
}
Exemple #3
0
int bbsnet(struct _select_def *conf, int n)
{
    time_t now;
    struct hostent *pHost = NULL;
    struct sockaddr_in remote;
    unsigned char buf[BUFSIZ];
    int rc;
    int rv;
    int maxfdp1;
    fd_set readset;
    struct timeval tv;
    int tos = 020; /* Low delay bit */
    int i;
    sig_t oldsig;
    int ret;
    int key;

    now = time(NULL);
    clear();
    prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\n",
           host1[n], ip[n]);
    prints("\033[1;32m如果在 %d 秒内无法连上,穿梭程序将放弃连接。\033[m\n",
           TIME_OUT);
    if (setjmp(jb) == 0) {
        oldsig = signal(SIGALRM, bbsnet_timeout);
        alarm(TIME_OUT);
        pHost = gethostbyname(ip[n]);
        alarm(0);
        signal(SIGALRM, oldsig);
    }
    if (pHost == NULL) {
        prints("\033[1;31m查找主机名失败!\033[m\n");
        pressreturn();
        return -1;
    }
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        prints("\033[1;31m无法创建socket!\033[m\n");
        pressreturn();
        return -1;
    }
    bzero(&remote, sizeof(remote));
    remote.sin_family = AF_INET;
    remote.sin_port = htons(port[n]);
    remote.sin_addr = *(struct in_addr *)pHost->h_addr_list[0];

    prints("\033[1;32m穿梭进度条提示您当前已使用的时间。\033[m\n");
    process_bar(0, MAX_PROCESS_BAR_LEN);
    for (i = 0; i < MAX_PROCESS_BAR_LEN; i++) {
        if (i == 0)
            rv = NonBlockConnectEx(sockfd, (struct sockaddr *)&remote,
                                   sizeof(remote), 500, 1);
        else
            rv = NonBlockConnectEx(sockfd, (struct sockaddr *)&remote,
                                   sizeof(remote), 500, 0);
        if (rv == ERR_TCPLIB_TIMEOUT) {
            process_bar(i+1, MAX_PROCESS_BAR_LEN);
            continue;
        } else if (rv == 0)
            break;
        else {
            prints("\033[1;31m连接失败!\033[m\n");
            pressreturn();
            ret = -1;
            goto on_error;
        }
    }
    if (i == MAX_PROCESS_BAR_LEN) {
        prints("\033[1;31m连接超时!\033[m\n");
        pressreturn();
        ret =  -1;
        goto on_error;
    }
    setsockopt(sockfd, IPPROTO_IP, IP_TOS, &tos, sizeof(int));
    prints("\033[1;31m连接成功!\033[m\n");
    bbsnet_report(host1[n], ip[n], now, 0);
    clear();
    refresh();
    bbsnet_send_winsize(sockfd);
    for (;;) {
        FD_ZERO(&readset);
        FD_SET(0, &readset);
        FD_SET(sockfd, &readset);
        maxfdp1 = sockfd + 1;
        tv.tv_sec = 1200;
        tv.tv_usec = 0;

        /*if ((rv = SignalSafeSelect(maxfdp1, &readset, NULL, NULL, &tv)) == -1)*/
        if ((rv = select(maxfdp1, &readset, NULL, NULL, &tv)) == -1) {
            if (errno == EINTR) {
                while (msg_count) {
                    msg_count--;
                    r_msg();
                }
                continue;
            }
            ret = -1;
            goto on_error;
        }
        if (rv == 0) {
            ret = -1;
            goto on_error;
        }

        if (FD_ISSET(sockfd, &readset)) {
            if ((rc = read(sockfd, buf, BUFSIZ)) < 0) {
                ret = -1;
                goto on_error;
            } else if (rc == 0)
                break;
            else if (strchr((void*)buf, 255)) /* 查找是否含有TELNET命令IAC */
                telnetopt(sockfd, (void*)buf, rc);
            else {
                output((void*)buf, rc);
                oflush();
            }
        }
        if (FD_ISSET(0, &readset)) {
            if ((rc = bbsnet_read(0, (void*)buf, BUFSIZ)) < 0) {
                ret = -1;
                goto on_error;
            }
            if (rc == 0)
                break;
            if ((key = list_select_remove_key(conf)) != KEY_INVALID)
                if (key == KEY_ONSIZE)
                    bbsnet_send_winsize(sockfd);
            write(sockfd, buf, rc);
        }
    }
    bbsnet_report(host1[n], ip[n], now, 1);
    ret = 0;
on_error:
    close(sockfd);
    clear();
    redoscr();
    return ret;
}
Exemple #4
0
int igetch()
{
    time_t now;
    char c;
    int hasaddio = 1;
    extern int RMSG;


    if ((uinfo.mode == CHAT1 || uinfo.mode == TALK || uinfo.mode == PAGE) && RMSG == true)
        hasaddio = 0;

igetagain:
    if (ibufsize == icurrchar) {
        fd_set readfds, xds;
        struct timeval to;
        int sr, hifd;

        to.tv_sec = 0;
        to.tv_usec = 0;
        hifd = 1;
        FD_ZERO(&readfds);
        FD_SET(0, &readfds);
        if ((hasaddio && (i_newfd))&&(!inremsg)) {
            FD_SET(i_newfd, &readfds);
            if (hifd <= i_newfd)
                hifd = i_newfd + 1;
        }
        //TODO: igetkey重入问题
        if ((uinfo.mode != POSTING && uinfo.mode != SMAIL && uinfo.mode != EDIT) || DEFINE(getCurrentUser(), DEF_LOGININFORM))
            if (scrint&&!inremsg) {
                while (msg_count) {
                    inremsg = true;
                    msg_count--;
                    r_msg();
                    refresh();
                    inremsg = false;
                }
            }
        if (kicked) return KEY_TIMEOUT;
#ifdef SSHBBS
        sr = ssh_select(hifd, &readfds, NULL, NULL, &to);
#else
        sr = select(hifd, &readfds, NULL, NULL, &to);
#endif
        if (sr < 0 && errno == EINTR) {
            if (talkrequest)
                return KEY_TALK;
            if ((uinfo.mode != POSTING && uinfo.mode != SMAIL && uinfo.mode != EDIT) || DEFINE(getCurrentUser(), DEF_LOGININFORM))
                if (scrint&&!inremsg) {
                    while (msg_count) {
                        inremsg = true;
                        msg_count--;
                        r_msg();
                        inremsg = false;
                    }
                    goto igetagain;
                }
        }
        if (sr < 0 && errno != EINTR)
            abort_bbs(0);
        if (sr == 0) {
            refresh();
            if (flushf)
                (*flushf)();

            while (1) {
                int alarm_timeout;

                hifd = 1;
                FD_ZERO(&xds);
                FD_SET(0, &xds);
                FD_ZERO(&readfds);
                FD_SET(0, &readfds);
                if ((hasaddio && (i_newfd))&&(!inremsg)) {
                    FD_SET(i_newfd, &readfds);
                    if (hifd <= i_newfd)
                        hifd = i_newfd + 1;
                }
                alarm_timeout = 0;
                if (i_top)
                    to = *i_top;
                else {
                    while ((i_timeout != 0)||(i_timeoutusec!=0)) {
                        to.tv_sec = i_timeout - (time(0) - i_begintimeout);
                        to.tv_usec = i_timeoutusec;
                        if ((to.tv_sec < 0) ||((to.tv_sec==0)&&(i_timeoutusec==0))) {
                            i_timeout = 0;
                            i_timeoutusec=0;
                            if (i_timeout_func)
                                (*i_timeout_func)(timeout_data);
                            else
                                return KEY_TIMEOUT;
                            if (kicked) return KEY_TIMEOUT;
                            continue;
                        };
                        alarm_timeout = 1;
                        break;
                    };
                    if (!alarm_timeout)
                        to.tv_sec = IDLE_TIMEOUT;
                }
#ifdef SSHBBS
                sr = ssh_select(hifd, &readfds, NULL, &xds, &to);
#else
                sr = select(hifd, &readfds, NULL, &xds, &to);
#endif
                if (sr < 0 && errno == EINTR) {
                    if (talkrequest)
                        return KEY_TALK;
                }
                if (kicked) return KEY_TIMEOUT;
                if ((uinfo.mode != POSTING && uinfo.mode != SMAIL && uinfo.mode != EDIT) || DEFINE(getCurrentUser(), DEF_LOGININFORM))
                    if (!inremsg) {
                        int saveerrno=errno;
                        while (msg_count) {
                            inremsg = true;
                            msg_count--;
                            r_msg();
                            refresh();
                            inremsg = false;
                        }
                        if (sr<0&&saveerrno==EINTR)continue;
                    }
                if (sr == 0 && alarm_timeout) {
                    i_timeout = 0;
                    i_timeoutusec=0;
                    if (i_timeout_func)
                        (*i_timeout_func)(timeout_data);
                    else
                        return KEY_TIMEOUT;
                    continue;
                }
                if (sr >= 0)
                    break;
                if (errno == EINTR)
                    continue;
                else
                    abort_bbs(0);

            }
            if ((sr == 0) && (!i_top))
                abort_bbs(0);
            if (sr == 0)
                return I_TIMEOUT;
            if (FD_ISSET(0, &xds))
                abort_bbs(0);
        }
        if (hasaddio && (i_newfd && FD_ISSET(i_newfd, &readfds)))
            return I_OTHERDATA;
#ifdef SSHBBS
        while ((ibufsize = ssh_read(0, inbuffer + 1, IBUFSIZE)) <= 0)
#else
        while ((ibufsize = read(0, inbuffer + 1, IBUFSIZE)) <= 0)
#endif
        {
            if (ibufsize == 0)
                longjmp(byebye, -1);
            if (ibufsize < 0 && errno != EINTR)
                longjmp(byebye, -1);
        }
        if (!filter_telnet(inbuffer + 1, &ibufsize)) {
            icurrchar = 0;
            ibufsize = 0;
            goto igetagain;
        }

        /*
         * add by KCN for GB/BIG5 encode
         */
        if (convcode) {
            inbuf = big2gb(inbuffer + 1, &ibufsize, 0, getSession());
            if (ibufsize == 0) {
                icurrchar = 0;
                goto igetagain;
            }
        } else
            inbuf = inbuffer + 1;
        /*
         * end
         */
        icurrchar = 0;
        if (ibufsize > IBUFSIZE) {
            ibufsize = 0;
            goto igetagain;
        }
    }

    if (icurrchar >= ibufsize) {
        ibufsize = icurrchar;
        goto igetagain;
    }
    if (((inbuf[icurrchar] == '\n') && (lastch == '\r'))
            || ((inbuf[icurrchar] == '\r') && (lastch == '\n'))) {
        lastch = 0;
        goto igetagain;
    }

    else if (icurrchar != ibufsize) {
        if (((inbuf[icurrchar] == '\n') && (inbuf[icurrchar + 1] == '\r'))
                || ((inbuf[icurrchar] == '\r') && (inbuf[icurrchar + 1] == '\n'))) {
            icurrchar++;
            lastch = 0;
        }
    } else
        lastch = inbuf[icurrchar];

    idle_count = 0;
    c = inbuf[icurrchar];

    switch (c) {
        case Ctrl('@'):
        case Ctrl('L'):
            redoscr();
            icurrchar++;
            now = time(0);
            uinfo.freshtime = now;
            if (now - old > 60) {
                UPDATE_UTMP(freshtime, uinfo);
                old = now;
            }
            goto igetagain;
        case Ctrl('Z'):
            if (scrint&&uinfo.mode!=LOCKSCREEN&&uinfo.mode!=NEW&&uinfo.mode!=LOGIN&&uinfo.mode!=BBSNET &&uinfo.mode!=WINMINE&&!inremsg) {
                icurrchar++;
                inremsg = true;
                r_msg();
                inremsg = false;
                goto igetagain;
            }
            break;
        default:
            break;
    }
    icurrchar++;
    while ((icurrchar != ibufsize) && (inbuf[icurrchar] == 0))
        icurrchar++;
    now = time(0);
    if (Ctrl('T') != c)
        uinfo.freshtime = now;
    /*
     * add by KCN , decrease temp_numposts
     */
    if (lasttime + 60 * 60 * 8 < now) {
        lasttime = now;
        if (temp_numposts > 0)
            temp_numposts--;
    }
    if (now - old > 60) {
        UPDATE_UTMP(freshtime, uinfo);
        old = now;
    }
    return c;
}