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

📄 nasl_socket.c

📁 大国补丁后的nessus2.2.8的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
  if(e == 0 && type == SOCK_DGRAM) { /*  * As UDP packets may be lost, we retry up to 5 times  */ int retries = 5; int i;  tv.tv_sec = to / retries; tv.tv_usec = (to % retries) *  100000;  for(i=0;i<retries;i++) {  FD_ZERO(&rd);  FD_SET(soc, &rd);    if(select(soc+1, &rd, NULL, NULL, &tv)>0)  {   int e;   e = recv(soc, data+new_len, len-new_len, 0);     if(e <= 0)   {    if(!new_len)    {     efree(&data);      return NULL;    }    else break;   }   else new_len+=e;   if(new_len >= len)break;   break; /* UDP data is never fragmented */  }  else   {   /*     * The packet may have been lost en route - we resend it    */   char * data;   int len;      data = get_udp_data(lexic->script_infos, soc, &len);   if(data != NULL)send(soc, data, len, 0);   tv.tv_sec = to / retries;   tv.tv_usec = ( to % retries) * 100000;   }  } } else {	 	int old = stream_set_timeout(soc, tv.tv_sec); 	new_len = read_stream_connection_min(soc, data, min_len, len);	stream_set_timeout(soc, old);      } if(new_len > 0) {  retc = alloc_tree_cell(0, NULL);  retc->type = CONST_DATA;  retc->x.str_val = nasl_strndup(data, new_len);  retc->size = new_len;  efree(&data);  return retc; } else {	 efree(&data);	 return NULL;  }}tree_cell * nasl_recv_line(lex_ctxt * lexic){ int len = get_int_local_var_by_name(lexic, "length", -1); int soc = get_int_local_var_by_name(lexic, "socket", 0); int timeout = get_int_local_var_by_name(lexic, "timeout", -1); char * data; int new_len = 0; int n = 0; tree_cell * retc; time_t		t1 = 0; if(len == -1 || soc <= 0)   {     nasl_perror(lexic, "recv_line: missing or undefined parameter length or soc\n");     return NULL;   } if (timeout >= 0)	/* sycalls are much more expensive than simple tests */   t1 = time(NULL); if ( fd_is_stream(soc) != 0 ) {  int bufsz = stream_get_buffer_sz ( soc );  if ( bufsz <= 0 )	stream_set_buffer(soc, len + 1 ); } data = emalloc(len+1); for(;;) {  int e = read_stream_connection_min(soc, data+n, 1, 1);  if(e < 0)    break;  if(e == 0)  {   if( timeout >= 0 && time(NULL) - t1 < timeout)  	continue;    else   	break;  }	  n++;    if((data[n-1] == '\n') ||     (n >= len))break; }    if(n <= 0)   {     efree(&data);     return NULL;   }  new_len = n;     retc = alloc_tree_cell(0, NULL); retc->type = CONST_DATA; retc->size = new_len; retc->x.str_val = nasl_strndup(data, new_len); efree(&data); return retc;}/*---------------------------------------------------------------------*/tree_cell * nasl_send(lex_ctxt * lexic){ int soc = get_int_local_var_by_name(lexic, "socket", 0); char * data = get_str_local_var_by_name(lexic, "data"); int option = get_int_local_var_by_name(lexic, "option", 0); int length = get_int_local_var_by_name(lexic, "length", 0); int data_length = get_var_size_by_name(lexic, "data"); int n; tree_cell * retc; int type; unsigned int type_len = sizeof(type);  if(soc <= 0 || data == NULL) { 	nasl_perror(lexic, "Syntax error with the send() function\n");	nasl_perror(lexic, "Correct syntax is : send(socket:<soc>, data:<data>\n");	return NULL; } if( length <= 0 || length > data_length )	length = data_length;   if(!fd_is_stream(soc) &&     getsockopt(soc, SOL_SOCKET, SO_TYPE, &type, &type_len) == 0 &&    type == SOCK_DGRAM) {		n = send(soc, data, length, option);		add_udp_data(lexic->script_infos, soc, data, length); } else  n = nsend(soc, data, length,option);			  retc = alloc_tree_cell(0, NULL);  retc->type = CONST_INT;  retc->x.i_val = n;  return retc;}/*---------------------------------------------------------------------*/tree_cell * nasl_close_socket(lex_ctxt * lexic){ int soc; int type; unsigned int opt_len = sizeof(type); int e;  soc = get_int_var_by_num(lexic, 0, -1); if(soc <= 4)	{ 	 nasl_perror(lexic, "close(): invalid argument\n");	 return NULL; 	} if ( fd_is_stream(soc) )  return close_stream_connection(soc) < 0 ? NULL:FAKE_CELL;  e = getsockopt(soc, SOL_SOCKET, SO_TYPE, &type, &opt_len); if(e == 0 ) {  if (type == SOCK_DGRAM)  {   rm_udp_data(lexic->script_infos, soc);   return FAKE_CELL;  }  close(soc);  return FAKE_CELL; } else nasl_perror(lexic, "close(): invalid argument\n"); return NULL;}static struct jmg {  struct in_addr	in;  int			count;  int			s;} *jmg_desc = NULL;static int	jmg_max = 0;tree_cell*nasl_join_multicast_group(lex_ctxt *lexic){  char		*a;  int		s, i, j;  struct ip_mreq	m;  tree_cell	*retc = NULL;  void		*p;  a = get_str_var_by_num(lexic, 0);  if (a == NULL)    {      nasl_perror(lexic, "join_multicast_group: missing parameter\n");      return NULL;    }  if (! inet_aton(a, &m.imr_multiaddr))    {      nasl_perror(lexic, "join_multicast_group: invalid parameter '%s'\n", a);      return NULL;    }  m.imr_interface.s_addr = INADDR_ANY;  j = -1;  for (i = 0; i < jmg_max; i ++)    if (jmg_desc[i].in.s_addr == m.imr_multiaddr.s_addr && jmg_desc[i].count > 0)      {	jmg_desc[i].count ++;	break;      }    else if (jmg_desc[i].count <= 0)      j = i;        if (i >= jmg_max)    {      s = socket(AF_INET, SOCK_DGRAM, 0);      if (s < 0)	{	  nasl_perror(lexic, "join_multicast_group: socket: %s\n", strerror(errno));	  return NULL;	}        if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &m, sizeof(m)) < 0)	{	  nasl_perror(lexic, "join_multicast_group: setsockopt(IP_ADD_MEMBERSHIP): %s\n", strerror(errno));	  close(s);	  return NULL;	}      if (j < 0)	{	  p = erealloc(jmg_desc, sizeof(*jmg_desc) * (jmg_max + 1));	  if (p == NULL)	    {	      nasl_perror(lexic, "join_multicast_group: realloc failed\n");	      close(s);	      return NULL;	    }	  jmg_desc = p;	  j = jmg_max ++;	}      jmg_desc[j].s = s;      jmg_desc[j].in = m.imr_multiaddr;      jmg_desc[j].count = 1;    }  retc = alloc_typed_cell(CONST_INT);  retc->x.i_val = 1;  return retc;}tree_cell*nasl_leave_multicast_group(lex_ctxt *lexic){  char		*a;  struct in_addr	ia;  int		i;  a = get_str_var_by_num(lexic, 0);  if (a == NULL)    {      nasl_perror(lexic, "leave_multicast_group: missing parameter\n");      return NULL;    }  if (! inet_aton(a, &ia))    {      nasl_perror(lexic, "leave_multicast_group: invalid parameter '%s'\n", a);      return NULL;    }  for (i = 0; i < jmg_max; i ++)    if (jmg_desc[i].count > 0 && jmg_desc[i].in.s_addr == ia.s_addr)      {	if (-- jmg_desc[i].count <= 0)	  close(jmg_desc[i].s);	return FAKE_CELL;      }  nasl_perror(lexic, "leave_multicast_group: never joined group %s\n", a);  return NULL;}tree_cell*nasl_get_source_port(lex_ctxt* lexic){  struct sockaddr_in	ia;  int		i, s, fd;  unsigned  int	l;  tree_cell	*retc;  int         type;  unsigned int type_len = sizeof(type);  s = get_int_var_by_num(lexic, 0, -1);  if (s < 0)    {      nasl_perror(lexic, "get_source_port: missing socket parameter\n");      return NULL;    }  if(!fd_is_stream(s) && getsockopt(s, SOL_SOCKET, SO_TYPE, &type, &type_len) == 0 && type == SOCK_DGRAM)       fd = s;   else       fd = nessus_get_socket_from_connection(s);   if (fd < 0)    {      nasl_perror(lexic, "get_source_port: invalid socket parameter %d\n", s);      return NULL;    }  l = sizeof(ia);  if (getsockname(fd, (struct sockaddr*)&ia, &l) < 0)    {      nasl_perror(lexic, "get_source_port: getsockname(%d): %s\n", fd, strerror(errno));      return NULL;    }  retc = alloc_typed_cell(CONST_INT);  retc->x.i_val = ntohs(ia.sin_port);  return retc;  }tree_cell*nasl_socket_get_error(lex_ctxt* lexic){  int soc = get_int_var_by_num(lexic, 0, -1);  tree_cell * retc;  int err;  if ( soc < 0 || ! fd_is_stream(soc) )	return NULL;  err = stream_get_err(soc);  retc = alloc_typed_cell(CONST_INT);    switch ( err )  {    case 0 :	retc->x.i_val = NASL_ERR_NOERR;	break;    case ETIMEDOUT:	retc->x.i_val = NASL_ERR_ETIMEDOUT;	break;    case EBADF:    case EPIPE:#ifdef ECONNRESET    case ECONNRESET:#endif#ifdef ENOTSOCK    case ENOTSOCK:#endif	 retc->x.i_val = NASL_ERR_ECONNRESET;         break;    case ENETUNREACH:    case EHOSTUNREACH:	 retc->x.i_val = NASL_ERR_EUNREACH;         break;default:	fprintf(stderr, "Unknown error %d %s\n", err, strerror(err));  } return retc;}

⌨️ 快捷键说明

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