Esempio n. 1
0
void HTTP_Client_GetResponse(String hostname, String path, unsigned short port, String *resp) {
	Socket socket = Socket_New(Socket_Protocol_TCP);

	SocketConnection conn = Socket_Connect(&socket, hostname, port);

	typedef void (^CleanupBlock)(void *, void *);

	CleanupBlock cleanup = ^(void *a, void *b) {
		SocketConnection_Close(a);
		Socket_Destroy(b);
	};

	String request = String_Format(
		$(
			"GET % HTTP/1.1\r\n"
			"Host: %\r\n"
			"Connection: Close\r\n"
			"\r\n"),
		path,
		hostname);

	try {
		SocketConnection_Write(&conn, request.buf, request.len);
	} clean finally {
		cleanup(&socket, &conn);
		String_Destroy(&request);
	} tryEnd;

	while (HTTP_Client_Receive(&conn, resp));
}
Esempio n. 2
0
void
TAccountXmpp::Socket_ReconnectIfDisconnected()
	{
	if (m_paSocket == NULL || m_paSocket->Socket_FNeedsReconnection())
		{
		Socket_Connect();
		Assert(m_paSocket != NULL);
		Assert(!m_paSocket->Socket_FNeedsReconnection());
		}
	else
		{
		// Since the socket is already connected, attempt to process pending tasks (if any)
		m_paSocket->Socket_ProcessAllPendingTasks();
		}
	Assert(m_paSocket != NULL);
	Assert(!m_paSocket->Socket_FNeedsReconnection());
	}
Esempio n. 3
0
SOCK_ERROR
Socket_Create(const char *name,
              SocketDesc *desc)
{
    if(desc == NULL | name == NULL)return SOCK_ERROR_UNKNOWN;

    //Make sure a socket by this name doesn't already exist
    SocketInfo *sock = sockets;
    do
    {
        if(sock->name != NULL && strncmp(sock->name, name, strlen(name)) == 0)
            return SOCK_ERROR_EXISTS;
        sock = sock->next;
    }
    while(sock != NULL);


    //Register the socket into the table
    sock = kmalloc(sizeof(SocketInfo));
    memset(sock, 0, sizeof(SocketInfo));

    sock->max_connections = desc->max_connections;
    sock->cur_connections = 0;
    sock->flags = desc->flags;

    sock->name = kmalloc(strlen(name) + 1);
    memset(sock->name, 0, strlen(name) + 1);
    strcpy(sock->name, name);

    sock->next = NULL;

    lastSocket->next = sock;
    lastSocket = sock;

    //Connect the current thread to the socket
    SocketConnectionDesc connection;
    connection.size = sizeof(SocketConnectionDesc);
    connection.flags = desc->flags;
    Socket_Connect(name, &connection);

    return SOCK_ERROR_NONE;
}
Esempio n. 4
0
/*
 * Ftp_Connect - connect to remote server
 *
 * return 1 if connected, 0 if not
 */
int Ftp_Connect(const char *host, int port, netbuf **nControl)
{
	int		socket_d = 0;
    netbuf	*ctrl;

    socket_d = Socket_Connect(host, port);
	if (socket_d > 0)
	{
		// allocate and fill in netbuf structure

		ctrl = calloc(1,sizeof(netbuf));
		if (ctrl == NULL)
		{

			Socket_close(socket_d);
			return 0;
		}
		ctrl->buf = malloc(FTP_BUFSIZ);
		if (ctrl->buf == NULL)
		{

			Socket_close(socket_d);
			free(ctrl);
			return 0;
		}
		ctrl->handle = socket_d;
		ctrl->dir = FTP_CONTROL;
		if (readresp('2', ctrl) == 0)
		{

			Socket_close(socket_d);
			free(ctrl->buf);
			free(ctrl);
			return 0;
		}
		*nControl = ctrl;
		return 1;
	}
	else
		return 0;
}
Esempio n. 5
0
int main(void) {
	Socket socket = Socket_New(Socket_Protocol_TCP);

	SocketConnection conn =
		Socket_Connect(&socket, $("www.kernel.org"), 80);

	String d1, d2;

	String_Print(
		d1 = String_Format(
			$("Connected to %:%\n\n"),
			d2 = NetworkAddress_ToString(conn.addr),
			Int16_ToString(conn.addr.port)));

	String_Destroy(&d2);
	String_Destroy(&d1);

	String request = $(
		"GET / HTTP/1.1\r\n"
		"Host: www.kernel.org\r\n"
		"Connection: Close\r\n"
		"\r\n");

	SocketConnection_Write(&conn, request.buf, request.len);

	String response = StackString(2048);

	response.len = SocketConnection_Read(&conn, response.buf, response.size);

	String_Print(response);

	SocketConnection_Close(&conn);

	Socket_Destroy(&socket);

	return ExitStatus_Success;
}