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

📄 netcommon.c

📁 飞鸽传书的linux源代码
💻 C
字号:
#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <errno.h>#include <glib.h>#include <unistd.h>#include "common.h"static intinternal_set_buffer(const int soc,int type,int max,int min,int *actual){  int rc;  int size;  if ( (soc<0) || (!actual) )    return -EINVAL;  if ( (type != SO_RCVBUF) && (type != SO_SNDBUF) )    return -EINVAL;  size=max;  rc=setsockopt(soc, SOL_SOCKET, type, (void*)&size, sizeof(int));  if (rc<0) {    size=min;    rc=setsockopt(soc, SOL_SOCKET, type, (void*)&size, sizeof(int));    if (rc<0) {      err_out("Can not set broad cast:%s(%d)\n",strerror(errno),errno);      *actual=size;      return -errno;    }  }  *actual=size;  return 0;}intsock_set_buffer(int soc){  int rc;  int size=0;  if (soc<0)    return -EINVAL;  //_MSG_BUF_SIZE include "common.h"  rc=internal_set_buffer(soc,SO_RCVBUF,_MSG_BUF_SIZE,_MSG_BUF_MIN_SIZE,&size);  if (size < _MSG_BUF_MIN_SIZE) {    err_out("Can not set recv buf size:%s (%d)\n",	    strerror(-rc),	    -rc);    return rc;  }  size=0;  rc=internal_set_buffer(soc,SO_SNDBUF,_MSG_BUF_SIZE,_MSG_BUF_MIN_SIZE,&size);  if (size < _MSG_BUF_MIN_SIZE) {    err_out("Can not set send buf size:%s (%d)\n",	    strerror(-rc),	    -rc);    return rc;  }  return 0;}static intinternal_set_timeout(const int soc,int type,unsigned long msec){  int rc;  int size;  struct timeval tmout;  int sec;  int usec;  if (soc<0)    return -EINVAL;  if ( (type != SO_RCVTIMEO) && (type != SO_SNDTIMEO) )    return -EINVAL;  sec=msec/1000;  usec=( (msec % 1000) * 1000 );  tmout.tv_sec=sec;  tmout.tv_usec=usec;  dbg_out("Try to set %d socket for %s timeout %d sec %d msec\n",	  soc,	  (type == SO_RCVTIMEO) ? ("Receive") : ("Send"),	  sec,	  (usec/1000) );  rc=setsockopt(soc, SOL_SOCKET, type, (void*)&tmout, sizeof(struct timeval));  if (rc<0) {    err_out("Can not set broad cast:%s(%d)\n",strerror(errno),errno);    return -errno;  }  dbg_out("Set socket %d for %s timeout %d sec %d msec\n",	  soc,	  (type == SO_RCVTIMEO) ? ("Receive") : ("Send"),	  sec,	  (usec/1000) );  return 0;}intsock_recv_time_out(int soc,long msec){  int rc;  if ( (soc<0) || (msec<0) )    return -EINVAL;  rc=internal_set_timeout(soc,SO_RCVTIMEO,(unsigned long)msec);  if (rc<0)     return rc;  return 0;}intsock_send_time_out(int soc,long msec){  int rc;  if ( (soc<0) || (msec<0) )    return -EINVAL;  rc=internal_set_timeout(soc,SO_SNDTIMEO,(unsigned long)msec);  if (rc<0)     return rc;  return 0;}intsetup_addr_info(struct addrinfo **infop, const char *ipaddr,int port,int stype,int family){  int rc;  int soc;  struct addrinfo hints;  struct addrinfo *res;  struct addrinfo *node;  char port_str[11];  char host[NI_MAXHOST];  char serv[NI_MAXSERV];  if (!infop)    return -EINVAL;  memset(&hints,0,sizeof(struct addrinfo));  snprintf(port_str,10,"%d",port);  if (family)    hints.ai_family=family;  else    hints.ai_family=PF_UNSPEC;    hints.ai_socktype = stype;    if (!ipaddr) {    hints.ai_flags=AI_PASSIVE;//套接字用于监听绑定  }	//getaddrinfo 主机名和服务名映射到一个地址    //include <sys/socket.h> <netdb.h>,返回结构addrinfo链表res  rc=getaddrinfo(ipaddr, port_str,&hints,&res);  if (rc) {    err_out("Can not get addr info:%s(%d)\n",gai_strerror(rc),rc);    rc=(rc<0)?(rc):(-rc);    goto err_out;  }   //输出主机名   for(node=res;node;node=node->ai_next) {	  //getnameinfo将地址转换成主机或服务名    rc=getnameinfo(res->ai_addr, res->ai_addrlen,		host, sizeof(host),		serv, sizeof(serv), 		NI_NUMERICHOST|NI_NUMERICSERV);//以数字形式    g_assert (!rc);    dbg_out("host:%s service:%s (family,type,proto)=(%d,%d,%d)\n",	    host,	    serv,	    res->ai_family,	    res->ai_socktype,	    res->ai_protocol);  }//for  *infop=res;  return 0; err_out:  return rc;}intwait_socket(int soc,int wait_for,int sec) {  fd_set rd_set;  fd_set wr_set;  int rc;  int max_count;  struct timeval tmout_val;  int i;  for(i=0;i<NET_MAX_RETRY;++i) {    max_count=(sec*1000UL)/WAIT_UNIT_MS;    while(max_count>0) {      FD_ZERO(&rd_set);      FD_ZERO(&wr_set);      if (wait_for & WAIT_FOR_READ)	FD_SET(soc,&rd_set);      if (wait_for & WAIT_FOR_WRITE)	FD_SET(soc,&wr_set);      ipmsg_update_ui();      dbg_out("select READ[%s] WRITE[%s]\n",	      (wait_for & WAIT_FOR_READ)?("WAIT"):("NO"),	      (wait_for & WAIT_FOR_WRITE)?("WAIT"):("NO") );      tmout_val.tv_sec=0;      tmout_val.tv_usec=(WAIT_UNIT_MS*1000);      rc=select(soc+1,&rd_set,&wr_set,NULL,&tmout_val);      if (rc>0) {	dbg_out("selectOK\n");	break;      }      if (rc<0) {	if (errno == EINTR)	  continue;	dbg_out("select Fail:%s (%d)\n",strerror(errno),errno);	return -errno;      }      --max_count;    }    if ( (wait_for & WAIT_FOR_READ ) && (FD_ISSET(soc,&rd_set)) )      return 0;    if ( (wait_for & WAIT_FOR_WRITE ) && (FD_ISSET(soc,&wr_set)) )      return 0;  }  return -EINVAL; }int sock_set_recv_timeout(int soc,int sec){  int rc;  struct timeval tmout_val;    tmout_val.tv_sec=sec;  tmout_val.tv_usec=0;  dbg_out("Set timeout:%d\n",sec);  rc=setsockopt(soc, SOL_SOCKET, SO_RCVTIMEO, (void*)&tmout_val, sizeof(struct timeval));  if (rc<0) {    err_out("Can not set timeout:%s(%d)\n",strerror(errno),errno);    return -errno;  }  return 0;}

⌨️ 快捷键说明

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