Exemple #1
0
/* int socket(int domain, int type, int protocol);*/
int socket(int domain, int type, int protocol)
{
	struct sock *conn;
	int i;

	/* create a sock */
	switch (type) 
	{  
		case SOCK_DGRAM:
			conn = netconn_new( NETCONN_UDP);
			break;
		case SOCK_STREAM:
			conn = netconn_new(NETCONN_TCP);
			break;
		default:
			return -1;
	}

	if (!conn) 
	{
		return -1;
	}

	i = alloc_socket(conn);

	if (i == -1) 
	{
		netconn_delete(conn);
		return -1;
	}

	conn->socket = i;
	return i;
}
static int	host_init(host_t* self, const char* addr, short port)
{
  /* Try to connect
   */
  char* dup_addr = strdup(addr);
  if (!dup_addr)
    return -1;
  socket_t* sock = alloc_socket();
  if (!sock)
    {
      free(dup_addr);
      return -1;
    }
  if (sock->init_and_connect_to(sock, addr, port) == -1)
    {
      free(dup_addr);
      sock->destroy(sock);
      return -1;
    }
  /* Init members
   */
  self->addr = dup_addr;
  self->port = port;
  return host_init_with_socket(self, sock);
}
Exemple #3
0
int sip_socket(int domain, int type, int protocol)
{
	struct sock *sock;
	int i = 0;

	if(domain != AF_INET || protocol != 0)		/*协议类型不对*/
		return -1;

	switch (type) 							/*按照类型建立不同的套接字*/
	{  
		case SOCK_DGRAM:					/*数据报类型*/
			sock = (struct sock *)SIP_SockNew( SOCK_DGRAM);/*建立套接字*/
			break;
		case SOCK_STREAM:					/*流式类型*/
			break;
		default:
			return -1;
	}

	if (!sock) {								/*建立套接字失败*/
		return -1;
	}

	i = alloc_socket(sock);					/*初始化socket变量,并分配文件描述符*/
	if (i == -1) {								/*上述操作失败*/
		SIP_SockDelete(sock);				/*释放sock类型变量*/
		return -1;
	}

	sock->socket = i;							/*设置sock结构中的socket值*/
	return i;
}
Exemple #4
0
arg_t make_socket(struct sockinfo *s, struct socket **np)
{
	struct socket *n;
	int8_t uindex;
	int8_t oftindex;
	inoptr ino;

	/* RAW sockets are superuser */
	if (s->priv && esuper())
		return -1;

	if (np)
		n = *np;
	else {
		n = alloc_socket();
		if (n == NULL)
			return -1;
	}
	n->s_type = s - socktypes;	/* Pointer or uint8_t best ? */
	n->s_state = SS_INIT;

	if (net_init(n) == -1)
		goto nosock;

	/* Start by getting the file and inode table entries */
	if ((uindex = uf_alloc()) == -1)
		goto nosock;
	if ((oftindex = oft_alloc()) == -1)
		goto nooft;

	/* We need an inode : FIXME - do we want a pipedev aka Unix ? */
	if (!(ino = i_open(root_dev, 0)))
		goto noalloc;
	/* All good - now set it up */
	/* The nlink cheat needs to be taught to fsck! */
	ino->c_node.i_mode = F_SOCK | 0777;
	ino->c_node.i_nlink = n->s_num;	/* Cheat !! */
	ino->c_readers = 1;
	ino->c_writers = 1;

	of_tab[oftindex].o_inode = ino;
	of_tab[oftindex].o_access = O_RDWR;

	udata.u_files[uindex] = oftindex;

	sock_wait_leave(n, 0, SS_INIT);
	if (np)
		*np = n;
	return uindex;

noalloc:
	oft_deref(oftindex);	/* Will call i_deref! */
nooft:
	udata.u_files[uindex] = NO_FILE;
nosock:
	n->s_state = SS_UNUSED;
	return -1;
}
/*-----------------------------------------------------------------------------------*/
int
lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
{
  struct lwip_socket *sock;
  struct netconn *newconn;
  struct ip_addr naddr;
  u16_t port;
  int newsock;
  struct sockaddr_in sin;

  DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d)...\n", s));
  sock = get_socket(s);
  if (!sock) {
    return -1;
  }
  
  newconn = netconn_accept(sock->conn);
    
  /* get the IP address and port of the remote host */
  netconn_peer(newconn, &naddr, &port);
  
  memset(&sin, 0, sizeof(sin));
  sin.sin_len = sizeof(sin);
  sin.sin_family = AF_INET;
  sin.sin_port = htons(port);
  sin.sin_addr.s_addr = naddr.addr;

  if (*addrlen > sizeof(sin))
      *addrlen = sizeof(sin);

  memcpy(addr, &sin, *addrlen);

  newsock = alloc_socket(newconn);
  if (newsock == -1) {  
    netconn_delete(newconn);
	sock_set_errno(sock, ENOBUFS);
	return -1;
  }
  newconn->callback = event_callback;
  sock = get_socket(newsock);
  
  sys_sem_wait(socksem);
  sock->rcvevent += -1 - newconn->socket;
  newconn->socket = newsock;
  sys_sem_signal(socksem);

#if SOCKETS_DEBUG
  DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d) returning new sock=%d addr=", s, newsock));
  ip_addr_debug_print(&naddr);
  DEBUGF(SOCKETS_DEBUG, (" port=%u\n", port));
#endif
  
  sock_set_errno(sock, 0);
  return newsock;
}
Exemple #6
0
arg_t make_socket(struct sockinfo *s, int8_t *np)
{
	int8_t n;
	int8_t uindex;
	int8_t oftindex;
	inoptr ino;

	/* RAW sockets are superuser */
	if (s->priv && esuper())
		return -1;

	/* Start by getting the file and inode table entries */
	if ((uindex = uf_alloc()) == -1)
		return -1;
	if ((oftindex = oft_alloc()) == -1)
		goto nooft;

	if (np)
		n = *np;
	else {
		n = alloc_socket();
		if (n == -1)
			goto noalloc;
	}
	/* We need an inode : FIXME - do we want a pipedev aka Unix ? */
	if (!(ino = i_open(root_dev, 0)))
		goto noalloc;
	/* All good - now set it up */
	/* The nlink cheat needs to be taught to fsck! */
	ino->c_node.i_mode = F_SOCK | 0777;
	ino->c_node.i_nlink = n;	/* Cheat !! */

	of_tab[oftindex].o_inode = ino;
	of_tab[oftindex].o_access = O_RDWR;

	udata.u_files[uindex] = oftindex;

	sockets[n].s_inode = ino;
	sockets[n].s_type = s - socktypes;	/* Pointer or uint8_t best ? */
	sockets[n].s_state = SS_UNCONNECTED;

	if (np)
		*np = n;
	return uindex;

noalloc:
	oft_deref(oftindex);	/* Will call i_deref! */
nooft:
	udata.u_files[uindex] = NO_FILE;
	return -1;
}
Exemple #7
0
struct socket *sock_alloc_accept(struct socket *s)
{
	struct socket *n = alloc_socket();
	int sockno;
	if (n == NULL)
		return NULL;

	sockno = n->s_num;
	memcpy(n, s, sizeof(*n));
	n->s_num = sockno;
	n->s_state = SS_ACCEPTING;
	n->s_data = s->s_num;
	return n;
}
Exemple #8
0
int
lwip_socket(int domain, int type, int protocol)
{
  struct netconn *conn;
  int i;

	if (!socksem)
		socksem = sys_sem_new(1);

  /* create a netconn */
  switch (type) {
  case SOCK_RAW:
    conn = netconn_new_with_proto_and_callback(NETCONN_RAW, protocol, event_callback);
    LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_RAW, %d) = ", domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
    break;
  case SOCK_DGRAM:
    conn = netconn_new_with_callback(NETCONN_UDP, event_callback);
    LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_DGRAM, %d) = ", domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
    break;
  case SOCK_STREAM:
    conn = netconn_new_with_callback(NETCONN_TCP, event_callback);
    LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_STREAM, %d) = ", domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
    break;
  default:
    LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%d, %d/UNKNOWN, %d) = -1\n", domain, type, protocol));
    set_errno(EINVAL);
    return -1;
  }

  if (!conn) {
    LWIP_DEBUGF(SOCKETS_DEBUG, ("-1 / ENOBUFS (could not create netconn)\n"));
    set_errno(ENOBUFS);
    return -1;
  }

  i = alloc_socket(conn);

  if (i == -1) {
    netconn_delete(conn);
  set_errno(ENOBUFS);
  return -1;
  }
  conn->socket = i;
  LWIP_DEBUGF(SOCKETS_DEBUG, ("%d\n", i));
  set_errno(0);
  return i;
}
Exemple #9
0
struct socket *_socket(int family, int type, int protocol)
{
	struct socket *sock = NULL;
	/* only support AF_INET */
	if (family != AF_INET)
		goto out;
	/* alloc new socket */
	sock = alloc_socket(family, type);
	if (!sock)
		goto out;
	/* only support AF_INET */
	sock->ops = &inet_ops;
	/* assert sock->ops->socket */
	if (sock->ops->socket(sock, protocol) < 0) {
		free_socket(sock);
		sock = NULL;
	}
	/* only support AF_INET */
out:
	return sock;
}
Exemple #10
0
static int alloc_new_socket( const uint16 src_port, const uint16 dest_port, const uint32 ip_dest )
{
	int t = alloc_socket();

	if(t >= 0) {
		sockets[t].s = _socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
		if(sockets[t].s == INVALID_SOCKET) {
			free_socket( t );
			t = -1;
		} else {
			sockets[t].src_port = src_port;
			sockets[t].dest_port = dest_port;
			
			sockets[t].from_len = sizeof(sockets[t].from);
			memset( &sockets[t].from, 0, sockets[t].from_len );
			sockets[t].from.sin_family = AF_INET;
			sockets[t].from.sin_port = htons(dest_port);
			sockets[t].from.sin_addr.s_addr = htonl(ip_dest);

			struct sockaddr_in to;
			memset( &to, 0, sizeof(to) );
			to.sin_family = AF_INET;

			if(	_bind ( sockets[t].s, (const struct sockaddr *)&to, sizeof(to) ) == 0 ) {
				D(bug("<%d> socket bound\r\n", t));
			} else {
				if( _WSAGetLastError() == WSAEINPROGRESS ) {
					D(bug("<%d> bind: a blocking call is in progress.\r\n", t));
				} else {
					D(bug("<%d> bind failed with error code %d\r\n", t, _WSAGetLastError()));
				}
				free_socket( t );
				t = -1;
			}
		}
	}
	return t;
}
Exemple #11
0
struct socket *_accept(struct socket *sock, struct sock_addr *skaddr)
{
	struct socket *newsock = NULL;
	int err;
	if (!sock)
		goto out;
	get_socket(sock);
	/* alloc slave socket */
	newsock = alloc_socket(sock->family, sock->type);
	if (!newsock)
		goto out_free;
	newsock->ops = sock->ops;
	/* real accepting process */
	if (sock->ops)
		err = sock->ops->accept(sock, newsock, skaddr);
	if (err < 0) {
		free(newsock);
		newsock = NULL;
	}
out_free:
	free_socket(sock);
out:
	return newsock;
}