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

📄 protocol.c

📁 Linux下的飞鸽传书
💻 C
📖 第 1 页 / 共 3 页
字号:
/* *  Copyright (C) 2006 Takeharu KATO * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#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=g_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|IPMSG_ENCRYPTOPT);  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;    ipmsg_err_dialog(_("Can not send message to %s. reason: %s error code = %d"), ipaddr, strerror(rc), rc);    goto free_send_string;  }  logfile_send_log(ipaddr,absent_message);  rc=0;free_send_string:  g_free(send_string);free_absent_message_proto:  g_free (absent_message_proto);free_absent_message:  g_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);  g_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;  dbg_out("BR-Entry given Flag :%x\n",local_flags);  local_flags |= hostinfo_get_normal_entry_flags();  local_flags |= IPMSG_BR_ENTRY;  dbg_out("BR-Entry Flag :%x\n",local_flags);  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);      g_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);	g_free (absent_title_proto);      }    }  }  rc=construct_message(base,ext,&msg,&len);  if (rc<0)    return rc;  ipmsg_send_broad_cast(con,msg,len);  g_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);  g_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);      g_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';	g_free (absent_title_proto);      }    }  }  rc=construct_message(base,ext,&msg,&len);  if (rc<0)    return rc;  ipmsg_send_broad_cast(con,msg,len);  g_free(msg);  return 0;}static intipmsg_send_ans_entry_common(const udp_con_t *con, const char *ipaddr, const int flags){  char base[IPMSG_BUFSIZ];  char ext[IPMSG_BUFSIZ];

⌨️ 快捷键说明

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