⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 protocol.c

📁 linux下的飞鸽传书 日本人设计 使用非常方便
💻 C
📖 第 1 页 / 共 2 页
字号:
#ifdef HAVE_CONFIG_H#  include <config.h>#endif#include <stdio.h>#include <string.h>#include <errno.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/time.h>#include <unistd.h>#include <gnome.h>#include <glib.h>#include "common.h"#define MSG_STRING         "1:%ld:%s:%s:%lu:%s"#define MSG_GETINFO_STRING "1:%ld:%s:%s:%lu:"#define MSG_ABSENT_STRING  "1:%ld:%s:%s:%lu:%s[%s]"#define MSG_STRING_RECVMSG "1:%ld:%s:%s:%lu:%d"#define MSG_STRING_INFOMSG "1:%ld:%s:%s:%lu:%s"static long pkt_no=0;GStaticMutex pktno_mutex = G_STATIC_MUTEX_INIT;longipmsg_get_pkt_no(void) {  g_static_mutex_lock(&pktno_mutex);  ++pkt_no;  g_static_mutex_unlock(&pktno_mutex);  return (time(NULL)%100000 + pkt_no);}static intconstruct_message(const char *base,const char *ext,char **msg,size_t *len){  char *sp;  char *basep;  char *extp;  size_t base_len=0;  size_t ext_len=0;  size_t buff_len;  char *buffer;  if ( (!msg) || (*msg) || (!len) )    return -EINVAL;  base_len=strlen(base)+1;  /*ヌル終端をふくめた長さ */  if (ext)    ext_len = strlen(ext)+1;  buff_len=base_len+ext_len;  buffer=malloc(buff_len);  if (!buffer)     return -ENOMEM;  sp=basep=buffer;  strncpy(sp,base,strlen(base));   sp += strlen(base);  *sp='\0';  ++sp;  extp=sp;  if (ext) {    strncpy(sp,ext,strlen(ext));    sp += strlen(ext);    *sp='\0';  }  if (ext)    dbg_out("Constructed: base:%s ext:%s\n",basep,extp);  else    dbg_out("Constructed: base:%s\n",basep);  *len=buff_len; /* 送信するデータ長を答える */  *msg=buffer;  return 0;}static intipmsg_construct_file_attach_message(const char *msg_string,const char *ext_part,int flags,size_t *len,char **msg,long pktno){  char base[IPMSG_BUFSIZ];  char *message;  size_t msg_len;  int rc;  if ( (!msg_string) || (!ext_part) || (!msg) || (!len) )    return -EINVAL;  if (!(flags & IPMSG_FILEATTACHOPT))    return -EINVAL;  dbg_out("ext:%s\n",ext_part);  memset(base,0,IPMSG_BUFSIZ);  snprintf(base,IPMSG_BUFSIZ-1,MSG_STRING,	   pktno,	   hostinfo_refer_user_name(),	   hostinfo_refer_host_name(),	   IPMSG_SENDMSG|flags,	   msg_string);  message=NULL;  rc=construct_message(base,ext_part,&message,&msg_len);  *len=msg_len;  if (rc<0)    return rc;  *msg=message;  return 0;}static intipmsg_construct_normal_message(const char *msg_string,int flags,size_t *len,char **msg,long pktno){  char base[IPMSG_BUFSIZ];  char *message;  size_t msg_len;  int rc;  if ( (!msg_string) || (!msg) || (!len) )    return -EINVAL;  memset(base,0,IPMSG_BUFSIZ);  snprintf(base,IPMSG_BUFSIZ-1,MSG_STRING,	   pktno,	   hostinfo_refer_user_name(),	   hostinfo_refer_host_name(),	   IPMSG_SENDMSG|flags,	   msg_string);  message=NULL;  rc=construct_message(base,NULL,&message,&msg_len);  if (rc<0)    return rc;  *msg=message;  *len=msg_len;  return 0;}intipmsg_construct_getfile_message(unsigned long ftype,const char *msg_string,int flags,size_t *len,char **msg,long pktno){  char base[IPMSG_BUFSIZ];  char *message;  size_t msg_len;  unsigned long local_flags;  int rc;  if ( (!msg_string) || (!msg) || (!len) )    return -EINVAL;    local_flags=flags;  local_flags|=((ftype == IPMSG_FILE_DIR)?		(IPMSG_GETDIRFILES):		(IPMSG_GETFILEDATA));  local_flags|=hostinfo_get_normal_send_flags();  memset(base,0,IPMSG_BUFSIZ);  snprintf(base,IPMSG_BUFSIZ-1,MSG_STRING,	   pktno,	   hostinfo_refer_user_name(),	   hostinfo_refer_host_name(),	   local_flags,	   msg_string);  message=NULL;  rc=construct_message(base,NULL,&message,&msg_len);  if (rc<0)    return rc;  *msg=message;  *len=msg_len;  return 0;}static intipmsg_send_absent_msg(const udp_con_t *con,const int flags){  char *absent_message=NULL;  gchar *absent_message_proto=NULL;  char  *send_string;  int index;  size_t len;  int local_flags;  int rc;  char *ipaddr;  rc=-EINVAL;  if (!con)    goto no_need_free_out;  local_flags=(flags|IPMSG_AUTORETOPT);  local_flags &= ~IPMSG_SENDCHECKOPT;  g_assert(!hostinfo_get_absent_id(&index));  rc=hostinfo_get_absent_message(index,(const char **)&absent_message);  if (!absent_message)    goto no_need_free_out;  convert_string_ipmsg_proto(absent_message,(const gchar **)&absent_message_proto);  if (!absent_message_proto)     goto free_absent_message;  pkt_no=ipmsg_get_pkt_no();  rc=ipmsg_construct_normal_message(absent_message_proto,local_flags,&len,&send_string,pkt_no);  if (rc<0){    if (send_string)      goto free_send_string;  }  if (!send_string)    goto free_absent_message;  ipaddr=(char *)udp_get_peeraddr(con);  dbg_out("send absent message to %s:%s\n",ipaddr,send_string);  rc=udp_send_peer(con,send_string,len);  if (rc<0) {    rc*=-1;    err_out("Can not send: %s %d\n",strerror(rc),rc);    goto free_send_string;  }  logfile_send_log(ipaddr,absent_message);  rc=0;free_send_string:  free(send_string);free_absent_message_proto:  free (absent_message_proto);free_absent_message:  free(absent_message);no_need_free_out:  return rc;}intipmsg_send_br_isgetlist2(const udp_con_t *con,const int flags){  char base[IPMSG_BUFSIZ];  char *msg=NULL;  size_t len;  int rc;  unsigned long local_flags;  local_flags  = flags;  local_flags |= hostinfo_get_normal_entry_flags();  local_flags |= IPMSG_BR_ISGETLIST2;  memset(base,0,IPMSG_BUFSIZ);  snprintf(base,IPMSG_BUFSIZ-1,MSG_STRING,ipmsg_get_pkt_no(),hostinfo_refer_user_name(),hostinfo_refer_host_name(),local_flags,"");  rc=construct_message(base,NULL,&msg,&len);  if (rc<0)    return rc;  ipmsg_send_broad_cast(con,msg,len);  free(msg);  return 0;}intipmsg_send_br_entry(const udp_con_t *con,const int flags){  char base[IPMSG_BUFSIZ];  char ext[IPMSG_BUFSIZ];  char *msg=NULL;  size_t len;  int rc;  unsigned long local_flags;  if (hostinfo_refer_ipmsg_is_get_hlist())    ipmsg_send_br_isgetlist2(con,flags);  local_flags  = flags;  local_flags |= hostinfo_get_normal_entry_flags();  local_flags |= IPMSG_BR_ENTRY;  memset(base,0,IPMSG_BUFSIZ);  memset(ext,0,IPMSG_BUFSIZ);  snprintf(base,IPMSG_BUFSIZ-1,MSG_STRING,ipmsg_get_pkt_no(),hostinfo_refer_user_name(),hostinfo_refer_host_name(),local_flags,hostinfo_refer_nick_name_proto());  snprintf(ext,IPMSG_BUFSIZ-1,"%s",hostinfo_refer_group_name_proto());  if (hostinfo_is_ipmsg_absent()) {    char *absent_title=NULL;    gchar *absent_title_proto=NULL;    int index;    if (!hostinfo_get_absent_id(&index)) {      hostinfo_get_absent_title(index,(const char **)&absent_title);      g_assert (absent_title);      convert_string_ipmsg_proto(absent_title,(const gchar **)&absent_title_proto);      free(absent_title);      g_assert (absent_title_proto);      if (absent_title_proto) {	snprintf(base,IPMSG_BUFSIZ-1,MSG_ABSENT_STRING,ipmsg_get_pkt_no(),hostinfo_refer_user_name(),hostinfo_refer_host_name(),local_flags,hostinfo_refer_nick_name_proto(),absent_title_proto);	free (absent_title_proto);      }    }  }  rc=construct_message(base,ext,&msg,&len);  if (rc<0)    return rc;  ipmsg_send_broad_cast(con,msg,len);  free(msg);  return 0;}intipmsg_send_br_exit(const udp_con_t *con,const int flags){  char base[IPMSG_BUFSIZ];  char ext[IPMSG_BUFSIZ];  char *msg=NULL;  size_t len;  int rc;  unsigned long local_flags;  local_flags  = flags;  local_flags |= hostinfo_get_normal_entry_flags();  local_flags |= IPMSG_BR_EXIT;  memset(base,0,IPMSG_BUFSIZ);  memset(ext,0,IPMSG_BUFSIZ);  snprintf(base,IPMSG_BUFSIZ-1,MSG_STRING,ipmsg_get_pkt_no(),hostinfo_refer_user_name(),hostinfo_refer_host_name(),local_flags,hostinfo_refer_nick_name_proto());  snprintf(ext,IPMSG_BUFSIZ-1,"%s",hostinfo_refer_group_name_proto());  rc=construct_message(base,ext,&msg,&len);  if (rc<0)    return rc;  ipmsg_send_broad_cast(con,msg,len);  free(msg);  return 0;}intipmsg_send_br_absence(const udp_con_t *con,const int flags){  char base[IPMSG_BUFSIZ];  char ext[IPMSG_BUFSIZ];  char *msg=NULL;  size_t len;  int rc;  unsigned long local_flags;  local_flags  = flags;  local_flags |= hostinfo_get_normal_entry_flags();  local_flags |= IPMSG_BR_ABSENCE;  memset(base,0,IPMSG_BUFSIZ);  memset(ext,0,IPMSG_BUFSIZ);  snprintf(base,IPMSG_BUFSIZ-1,MSG_STRING,ipmsg_get_pkt_no(),hostinfo_refer_user_name(),hostinfo_refer_host_name(),local_flags,hostinfo_refer_nick_name_proto());  base[IPMSG_BUFSIZ-1]='\0';  snprintf(ext,IPMSG_BUFSIZ-1,"%s",hostinfo_refer_group_name_proto());  ext[IPMSG_BUFSIZ-1]='\0';  if (hostinfo_is_ipmsg_absent()) {    char *absent_title=NULL;    gchar *absent_title_proto=NULL;    int index;    if (!hostinfo_get_absent_id(&index)) {      hostinfo_get_absent_title(index,(const char **)&absent_title);      g_assert (absent_title);      convert_string_ipmsg_proto(absent_title,(const gchar **)&absent_title_proto);      free(absent_title);      g_assert (absent_title_proto);      if (absent_title_proto) {	memset(base,0,IPMSG_BUFSIZ);	snprintf(base,IPMSG_BUFSIZ-1,MSG_ABSENT_STRING,		 ipmsg_get_pkt_no(),		 hostinfo_refer_user_name(),		 hostinfo_refer_host_name(),		 local_flags,		 hostinfo_refer_nick_name_proto(),		 absent_title_proto);	base[IPMSG_BUFSIZ-1]='\0';	free (absent_title_proto);      }    }  }  rc=construct_message(base,ext,&msg,&len);  if (rc<0)    return rc;  ipmsg_send_broad_cast(con,msg,len);  free(msg);  return 0;}intipmsg_send_ans_entry(const udp_con_t *con,const int flags){  char base[IPMSG_BUFSIZ];  char ext[IPMSG_BUFSIZ];  char *msg=NULL;  size_t len;  int rc;  unsigned long local_flags;  local_flags  = flags;  local_flags |= hostinfo_get_normal_entry_flags();  local_flags |= IPMSG_ANSENTRY;  if (hostinfo_is_ipmsg_absent())     local_flags |= IPMSG_ABSENCEOPT;  memset(base,0,IPMSG_BUFSIZ);  memset(ext,0,IPMSG_BUFSIZ);  snprintf(base,IPMSG_BUFSIZ-1,MSG_STRING,ipmsg_get_pkt_no(),hostinfo_refer_user_name(),hostinfo_refer_host_name(),local_flags,hostinfo_refer_nick_name_proto());  snprintf(ext,IPMSG_BUFSIZ-1,"%s",hostinfo_refer_group_name_proto());  if (hostinfo_is_ipmsg_absent()) {    char *absent_title=NULL;    gchar *absent_title_proto=NULL;    int index;    if (!hostinfo_get_absent_id(&index)) {      hostinfo_get_absent_title(index,(const char **)&absent_title);      g_assert (absent_title);      convert_string_ipmsg_proto(absent_title,(const gchar **)&absent_title_proto);      free(absent_title);      g_assert (absent_title_proto);      if (absent_title_proto) {	snprintf(ext,IPMSG_BUFSIZ-1,"%s[%s]",hostinfo_refer_group_name_proto(),absent_title_proto);	free (absent_title_proto);      }    }  }  rc=construct_message(base,ext,&msg,&len);  if (rc<0)    return rc;  dbg_out("AnserEntry:%s\n",msg);  udp_send_peer(con,msg,len);  free(msg);  return 0;}intipmsg_send_recv_msg(const udp_con_t *con,const msg_data_t *orig_msg){  char base[IPMSG_BUFSIZ];  char *msg=NULL;  size_t len;  int rc;  memset(base,0,IPMSG_BUFSIZ);  snprintf(base,IPMSG_BUFSIZ-1,MSG_STRING_RECVMSG,ipmsg_get_pkt_no(),hostinfo_refer_user_name(),hostinfo_refer_host_name(),(IPMSG_RECVMSG|IPMSG_AUTORETOPT),refer_pkt_no_name_from_msg(orig_msg));  rc=construct_message(base,NULL,&msg,&len);  if (rc<0)    return rc;  udp_send_peer(con,msg,len);  free(msg);  return 0;}intipmsg_send_release_files(const udp_con_t *con,const char *ipaddr,int pktno){  char base[IPMSG_BUFSIZ];  char *msg=NULL;  size_t len;  int rc;  memset(base,0,IPMSG_BUFSIZ);  snprintf(base,IPMSG_BUFSIZ-1,MSG_STRING_RECVMSG,ipmsg_get_pkt_no(),hostinfo_refer_user_name(),hostinfo_refer_host_name(),IPMSG_RELEASEFILES,pktno);  rc=construct_message(base,NULL,&msg,&len);  if (rc<0)    return rc;  rc=udp_send_message(con,ipaddr,hostinfo_refer_ipmsg_port(),msg,len);  free(msg);  if (rc<0)    return rc;  return 0;}intipmsg_send_get_list(const udp_con_t *con,const char *ipaddr,int index){  char base[IPMSG_BUFSIZ];  char *msg=NULL;  size_t len;  int rc;  if ( (!con) || (!ipaddr) )    return -EINVAL;  memset(base,0,IPMSG_BUFSIZ);  snprintf(base,IPMSG_BUFSIZ-1,MSG_STRING_RECVMSG,ipmsg_get_pkt_no(),hostinfo_refer_user_name(),hostinfo_refer_host_name(),IPMSG_GETLIST,index);  rc=construct_message(base,NULL,&msg,&len);  if (rc<0)    return rc;  rc=udp_send_message(con,ipaddr,hostinfo_refer_ipmsg_port(),msg,len);  free(msg);  if (rc<0)    return rc;  return 0;}intipmsg_send_read_msg(const udp_con_t *con,const char *ipaddr,int pktno){  char base[IPMSG_BUFSIZ];  char *msg=NULL;  size_t len;  int rc;  memset(base,0,IPMSG_BUFSIZ);  snprintf(base,IPMSG_BUFSIZ-1,MSG_STRING_RECVMSG,ipmsg_get_pkt_no(),hostinfo_refer_user_name(),hostinfo_refer_host_name(),IPMSG_READMSG,pktno);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -