void take_connection(int sockfd) { int status; char *buff; /* chek for client ip */ if (strcmp(client_ip, "127.0.0.1")) { close(sockfd); return; } send_line(sockfd, message(MSG_WELCOME), strlen(message(MSG_WELCOME))); if (helo_cmd(sockfd)) { close(sockfd); return; } buff = calloc(MAX_MSG_SIZE, sizeof(char)); while (1) { memset(buff, '\0', MAX_MSG_SIZE); if (recv_line(sockfd, buff, MAX_MSG_SIZE - 1) <= 0) { free(buff); break; } else { status = lr_cmd(sockfd, buff); /* if something went wrong break */ if (status <= -1) { break; /* if it went ok continue */ } else if (status == 0) { continue; /* else: nothing happened, this command wasn't requested */ } else { status = bye_cmd(sockfd, buff); if (status <= 0 || send_line( sockfd, message(MSG_BAD_SYNTAX), strlen(message(MSG_BAD_SYNTAX)) ) < 0) { break; } } } } sleep(1); close(sockfd); }
void take_connection(int sockfd) { license_data_type ld; /* chek for client ip */ if (strcmp(client_ip,"127.0.0.1")) { /* ints not local host */ close(sockfd); return; } send_line(sockfd, message(MSG_WELCOME), strlen(message(MSG_WELCOME))); if (helo_cmd(sockfd, &ld)) { close(sockfd); return; } if (lr_cmd(sockfd, &ld)) { close(sockfd); return; } if (bye_cmd(sockfd)) { close(sockfd); return; } sleep(1); close(sockfd); }
void commands(struct clients *client_t) { char rspsoneBuf[MAX_LINE * 2] = {0}; struct data_st *recv_pdt = client_t->recv_data; if ((recv_pdt->data_len > 0) && (recv_pdt->data != NULL)) { // SYSADMIN: 用于管理员调试 if ( strncasecmp(recv_pdt->data, "SYSADMIN ", 9) == 0 ) { } // LOGIN: 用于表示客户端登录 LOGIN account:abalam password:123qwe ios_token:111 get_friend_list:1 \r\n\r\n if ( strncasecmp(recv_pdt->data, "LOGIN ", 6) == 0 ) { char *pbuf = recv_pdt->data + 5; char myuid[128] = {0}; int ret = login_cmd(client_t, pbuf, strlen(pbuf)); if (ret != 0) { // 认证失败 snprintf(rspsoneBuf, sizeof(rspsoneBuf), "%s FAIL%s", TAG_LOGIN, DATA_END); safe_write(client_t, rspsoneBuf, strlen(rspsoneBuf)); } // init recv data reinit_data_without_free(recv_pdt); return ; } // HELO: 用于表示客户端信息 helo uid:0000001 ios_token:xxxxxxxxxxxxxx fid:0000220 if ( strncasecmp(recv_pdt->data, "HELO ", 5) == 0 ) { char *pbuf = recv_pdt->data + 5; char myuid[128] = {0}; int ret = helo_cmd(client_t, pbuf, strlen(pbuf), myuid, sizeof(myuid)); if (ret == 0) { // 检查是否有离线消息 int offline_msg_num = has_offline_msg(myuid); snprintf(rspsoneBuf, sizeof(rspsoneBuf), "%s OK %d%s", TAG_HELO, offline_msg_num, DATA_END); } else { snprintf(rspsoneBuf, sizeof(rspsoneBuf), "%s FAIL 0%s", TAG_HELO, DATA_END); } // init recv data reinit_data_without_free(recv_pdt); safe_write(client_t, rspsoneBuf, strlen(rspsoneBuf)); return ; } // ALIVE: keep alive 保持在线 if ( strncasecmp(recv_pdt->data, "ALIVE ", 6) == 0 ) { char *pbuf = recv_pdt->data + 6; snprintf(rspsoneBuf, sizeof(rspsoneBuf), "%s OK %s", TAG_ALIVE, DATA_END); // init recv data reinit_data_without_free(recv_pdt); safe_write(client_t, rspsoneBuf, strlen(rspsoneBuf)); return; } // QUIT: 用户退出 if ( strncasecmp(recv_pdt->data, "QUIT ", 5) == 0 ) { char *pbuf = recv_pdt->data + 5; int ret = quit_cmd(client_t, pbuf, strlen(pbuf)); /* if (ret == 0) { snprintf(rspsoneBuf, sizeof(rspsoneBuf), "%s OK %s", TAG_QUIT, DATA_END); } else { snprintf(rspsoneBuf, sizeof(rspsoneBuf), "%s FAIL %s", TAG_QUIT, DATA_END); } */ // init recv data reinit_data_without_free(recv_pdt); //safe_write(client_t, rspsoneBuf, strlen(rspsoneBuf)); return ; } // SENDADDFRDREQ: 发送请求加为好友信息 if ( strncasecmp(recv_pdt->data, "SENDADDFRDREQ ", 14) == 0 ) { char *pbuf = recv_pdt->data + 14; char mid[MAX_LINE] = {0}; int ret = sendreq_addfriend(client_t, pbuf, strlen(pbuf), mid, sizeof(mid)); if (ret == 0) { snprintf(rspsoneBuf, sizeof(rspsoneBuf), "%s OK %s", TAG_SENDADDFRIENDREQ, DATA_END); } else { snprintf(rspsoneBuf, sizeof(rspsoneBuf), "%s FAIL %s", TAG_SENDADDFRIENDREQ, DATA_END); } // init recv data reinit_data_without_free(recv_pdt); safe_write(client_t, rspsoneBuf, strlen(rspsoneBuf)); return; } // SENDTXT: 发送送信息给对方 if ( strncasecmp(recv_pdt->data, "SENDTXT ", 8) == 0 ) { char *pbuf = recv_pdt->data + 8; char mid[MAX_LINE] = {0}; int ret = sendtxt_cmd(client_t, pbuf, strlen(pbuf), mid, sizeof(mid)); if (ret == 0) { snprintf(rspsoneBuf, sizeof(rspsoneBuf), "%s OK %s%s", TAG_SENDTXT, mid, DATA_END); } else { snprintf(rspsoneBuf, sizeof(rspsoneBuf), "%s FAIL %s%s", TAG_SENDTXT, mid, DATA_END); } // init recv data reinit_data_without_free(recv_pdt); safe_write(client_t, rspsoneBuf, strlen(rspsoneBuf)); return; } // SENDIMG: 发送送图片给对方 if ( strncasecmp(recv_pdt->data, "SENDIMG ", 8) == 0 ) { char *pbuf = recv_pdt->data + 8; char mid[MAX_LINE] = {0}; char thumb_img_url[MAX_LINE] = {0}; char qid[MAX_LINE] = {0}; int ret = sendimg_cmd(client_t, pbuf, strlen(pbuf), mid, sizeof(mid), thumb_img_url, sizeof(thumb_img_url), qid, sizeof(qid)); if (ret == 0) { snprintf(rspsoneBuf, sizeof(rspsoneBuf), "%s OK %s %s %s%s", TAG_SENDIMG, mid, qid, thumb_img_url, DATA_END); } else { snprintf(rspsoneBuf, sizeof(rspsoneBuf), "%s FAIL %s %s %s%s", TAG_SENDIMG, mid, qid, thumb_img_url, DATA_END); } // init recv data reinit_data_without_free(recv_pdt); safe_write(client_t, rspsoneBuf, strlen(rspsoneBuf)); return; } // SENDAUDIO: 发送送音频给对方 if ( strncasecmp(recv_pdt->data, "SENDAUDIO ", 10) == 0 ) { char *pbuf = recv_pdt->data + 10; char mid[MAX_LINE] = {0}; char qid[MAX_LINE] = {0}; char audio_url[MAX_LINE] = {0}; int ret = sendaudio_cmd(client_t, pbuf, strlen(pbuf), mid, sizeof(mid), audio_url, sizeof(audio_url), qid, sizeof(qid)); if (ret == 0) { snprintf(rspsoneBuf, sizeof(rspsoneBuf), "%s OK %s %s %s %s", TAG_SENDAUDIO, mid, qid, audio_url, DATA_END); } else { snprintf(rspsoneBuf, sizeof(rspsoneBuf), "%s FAIL %s %s %s %s", TAG_SENDAUDIO, mid, qid, audio_url, DATA_END); } // init recv data reinit_data_without_free(recv_pdt); safe_write(client_t, rspsoneBuf, strlen(rspsoneBuf)); return; } } }