Example #1
0
/**
 * Close the connection before the GC collect the object.
 */
static int meth_destroy(lua_State *L)
{
  p_ssl ssl = (p_ssl)luaL_checkudata(L, 1, "SSL:Connection");
  if (ssl->state == LSEC_STATE_CONNECTED) {
    socket_setblocking(&ssl->sock);
    SSL_shutdown(ssl->ssl);
  }
  if (ssl->sock != SOCKET_INVALID) {
    socket_destroy(&ssl->sock);
  }
  ssl->state = LSEC_STATE_CLOSED;
  if (ssl->ssl) {
    /* Clear the registries */
    luaL_getmetatable(L, "SSL:Verify:Registry");
    lua_pushlightuserdata(L, (void*)ssl->ssl);
    lua_pushnil(L);
    lua_settable(L, -3);
    luaL_getmetatable(L, "SSL:SNI:Registry");
    lua_pushlightuserdata(L, (void*)ssl->ssl);
    lua_pushnil(L);
    lua_settable(L, -3);
    /* Destroy the object */
    SSL_free(ssl->ssl);
    ssl->ssl = NULL;
  }
  return 0;
}
Example #2
0
/*-------------------------------------------------------------------------*\
* Binds or returns error message
\*-------------------------------------------------------------------------*/
int socket_bind(p_socket ps, SA *addr, socklen_t len) {
    int err = IO_DONE;
    socket_setblocking(ps);
    if (bind(*ps, addr, len) < 0) err = peusock_errno; 
    socket_setnonblocking(ps);
    return err;
}
Example #3
0
/*-------------------------------------------------------------------------*\
* Close and inutilize socket
\*-------------------------------------------------------------------------*/
void socket_destroy(p_socket ps) {
    if (*ps != SOCKET_INVALID) {
        socket_setblocking(ps); /* close can take a long time on WIN32 */
        closesocket(*ps);
        *ps = SOCKET_INVALID;
    }
}
Example #4
0
/*-------------------------------------------------------------------------*\
* 
\*-------------------------------------------------------------------------*/
int socket_listen(p_socket ps, int backlog) {
    int err = IO_DONE;
    socket_setblocking(ps);
    if (listen(*ps, backlog) < 0) err = WSAGetLastError();
    socket_setnonblocking(ps);
    return err;
}
ESErrorCode process_connect_to_host(const string& name, process_t* p) {
	const string pipeName = name;
	struct sockaddr_un addr;
	SOCKET fd;
	p->handle = INVALID_PROCESS;
	p->pipe = INVALID_PIPE;

	if ((fd = ::socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
		return ESERR_PIPE_CONNECT;
	}

	socket_setblocking(fd);

	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;
	strcpy(addr.sun_path, pipeName.c_str());

	if (::connect(fd, (struct sockaddr*) &(addr), sizeof(addr)) < 0) {
		socket_close(fd);
		return ESERR_PIPE_CONNECT;
	}

	p->pipe = fd;
	return p->pipe < 0 ? ESERR_PIPE_CONNECT : ESERR_NO_ERROR;
}
Example #6
0
/*-------------------------------------------------------------------------*\
* Binds or returns error message
\*-------------------------------------------------------------------------*/
int socket_bind(p_socket ps, SA *addr, socklen_t len) {
    int err = IO_DONE;
    socket_setblocking(ps);
    if (bind(*ps, addr, len) < 0) err = WSAGetLastError();
    socket_setnonblocking(ps);
    return err;
}
ESErrorCode _gcc_pipe_open(const string& name, int* pipe) {
	unlink(name.c_str());
	const string pipeName = name;
	struct sockaddr_un addr;
	SOCKET fd;
	*pipe = INVALID_PIPE;

	if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
		return ESERR_PIPE_CREATE;
	}

	socket_setblocking(fd);

	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;
	unlink(pipeName.c_str());
	strcpy(addr.sun_path, pipeName.c_str());

	if (::bind(fd, (struct sockaddr*) &(addr), sizeof(addr)) < 0) {
		socket_close(fd);
		return ESERR_PIPE_LISTEN;
	}

	if (::listen(fd, 1) < 0) {
		socket_close(fd);
		return ESERR_PIPE_LISTEN;
	}

	*pipe = fd;
	return ESERR_NO_ERROR;
}
Example #8
0
/*-------------------------------------------------------------------------*\
* 
\*-------------------------------------------------------------------------*/
int socket_listen(p_socket ps, int backlog) {
    int err = IO_DONE; 
    socket_setblocking(ps);
    if (listen(*ps, backlog)) err = peusock_errno; 
    socket_setnonblocking(ps);
    return err;
}
Example #9
0
/*-------------------------------------------------------------------------*\
* Close and inutilize socket
\*-------------------------------------------------------------------------*/
void socket_destroy(p_socket ps) {
    if (*ps != SOCKET_INVALID) {
        socket_setblocking(ps);
        close(*ps);
        *ps = SOCKET_INVALID;
    }
}
Example #10
0
T_ERRCODE socket_listen(p_socket sock, int backlog) {
  int ret = SUCCESS;
  socket_setblocking(sock);
  if (listen(*sock, backlog)) {
    ret = errno;
  }
  socket_setnonblocking(sock);
  return ret;
}
Example #11
0
T_ERRCODE socket_bind(p_socket sock, p_sa addr, int addr_len) {
  int ret = SUCCESS;
  socket_setblocking(sock);
  if (bind(*sock, addr, addr_len)) {
    ret = errno;
  }
  socket_setnonblocking(sock);
  return ret;
}
Example #12
0
T_ERRCODE socket_destroy(p_socket sock) {
  // TODO Figure out if I should be free-ing this
  if (*sock > 0) {
    socket_setblocking(sock);
    close(*sock);
    *sock = -1;
  }
  return SUCCESS;
}
Example #13
0
File: ssl.c Project: eddix/luasec
/**
 * Close the connection before the GC collect the object.
 */
static int meth_destroy(lua_State *L)
{
  p_ssl ssl = (p_ssl) lua_touserdata(L, 1);
  if (ssl->ssl) {
    socket_setblocking(&ssl->sock);
    SSL_shutdown(ssl->ssl);
    socket_destroy(&ssl->sock);
    SSL_free(ssl->ssl);
    ssl->ssl = NULL;
  }
  return 0;
}
Example #14
0
SOCKET socket_accept_blocking(SOCKET serverSocket) {
	SOCKET socket = ::accept(serverSocket, NULL, 0);
	if (socket == INVALID_SOCKET) {
		return INVALID_SOCKET;
	}

	// Make sure to block
	ESErrorCode err = socket_setblocking(socket);
	if (isError(err)) {
		socket_close(socket);
		return 0;
	}

	socket_nodelay(socket);
	socket_setbufsize(socket, DEFAULT_MAX_DATA_SEND_SIZE);
	return socket;
}
Example #15
0
/*-------------------------------------------------------------------------*\
* Binds or returns error message
\*-------------------------------------------------------------------------*/
int socket_bind(p_socket ps, SA *addr, socklen_t len) {
    int err = IO_DONE;
	/*
    char buf[256];
	SOCKADDR_IN * pAddr = (SOCKADDR_IN*)(addr);

	sprintf(buf, "expected size of SOCKADDR_IN: %d sa_family: %d sin_addr.S_un.S_addr: %d\n", (int)(sizeof(SOCKADDR_IN)), (int)(pAddr->sin_family), (int)(pAddr->sin_addr.S_un.S_addr));
	OutputDebugStringA(buf);
	
	sprintf(buf, "Trying to bind socket %d with addrlen %d\n", (int)(*ps), len);
	OutputDebugStringA(buf);
	*/
    socket_setblocking(ps);
    if (bind(*ps, addr, len) < 0) err = WSAGetLastError();
    socket_setnonblocking(ps);
    return err;
}
Example #16
0
SOCKET socket_create_blocking() {
	// Create a socket
	SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (s == INVALID_SOCKET) {
		return s;
	}

	// Make sure to block
	ESErrorCode err = socket_setblocking(s);
	if (isError(err)) {
		socket_close(s);
		return 0;
	}

	socket_nodelay(s);
	socket_setbufsize(s, DEFAULT_MAX_DATA_SEND_SIZE);
	return s;
}
Example #17
0
/*-------------------------------------------------------------------------*\
* 
\*-------------------------------------------------------------------------*/
void socket_shutdown(p_socket ps, int how) {
    socket_setblocking(ps);
    shutdown(*ps, how);
    socket_setnonblocking(ps);
}
Example #18
0
/**
 * initialize and open a SACD device or file.
 */
static sacd_input_t sacd_net_input_open(const char *target)
{
    ServerRequest request;
    ServerResponse response;
    sacd_input_t dev = 0;
    const char *err = 0;
    t_timeout tm;
    pb_istream_t input;
    pb_ostream_t output;
    uint8_t zero = 0;

    /* Allocate the library structure */
    dev = (sacd_input_t) calloc(sizeof(*dev), 1);
    if (dev == NULL)
    {
        fprintf(stderr, "libsacdread: Could not allocate memory.\n");
        return NULL;
    }

    dev->input_buffer = (uint8_t *) malloc(MAX_PROCESSING_BLOCK_SIZE * SACD_LSN_SIZE + 1024);
    if (dev->input_buffer == NULL)
    {
        fprintf(stderr, "libsacdread: Could not allocate memory.\n");
        goto error;
    }

    socket_open();

    socket_create(&dev->fd, AF_INET, SOCK_STREAM, 0);
    socket_setblocking(&dev->fd);

    timeout_markstart(&tm); 
    err = inet_tryconnect(&dev->fd, 
            substr(target, 0, strchr(target, ':') - target), 
            atoi(strchr(target, ':') + 1), &tm);
    if (err)
    {
        fprintf(stderr, "Failed to connect\n");
        goto error;
    }
    socket_setblocking(&dev->fd);

    input = pb_istream_from_socket(&dev->fd);

    output = pb_ostream_from_socket(&dev->fd);

    request.type = ServerRequest_Type_DISC_OPEN;

    if (!pb_encode(&output, ServerRequest_fields, &request))
    {
        fprintf(stderr, "Failed to encode request\n");
        goto error;
    }

    /* We signal the end of request with a 0 tag. */
    pb_write(&output, &zero, 1);

    if (!pb_decode(&input, ServerResponse_fields, &response))
    {
        fprintf(stderr, "Failed to decode response\n");
        goto error;
    }

    if (response.result != 0 || response.type != ServerResponse_Type_DISC_OPENED)
    {
        fprintf(stderr, "Response result non-zero or disc opened\n");
        goto error;
    }

    return dev;

error:

    sacd_input_close(dev);

    return 0;
}