libdiscord

libdiscord.git
git clone git://git.lenczewski.org/libdiscord.git
Log | Files | Refs | README | LICENSE

gateway.c (1928B)


      1 #include "internal.h"
      2 
      3 int
      4 gateway_connect(struct conn *conn, struct io_uring *uring,
      5 		char const *host, char const *port)
      6 {
      7 	struct addrinfo hints = {
      8 		.ai_family = AF_UNSPEC,
      9 		.ai_socktype = SOCK_STREAM,
     10 		.ai_protocol = IPPROTO_TCP,
     11 		.ai_flags = AI_CANONNAME | AI_NUMERICSERV,
     12 	}, *addrinfo;
     13 
     14 	if (getaddrinfo(host, port, &hints, &addrinfo))
     15 		return -1;
     16 
     17 	conn->addrinfo = conn->ai_ptr = addrinfo;
     18 
     19 	return conn_queue_connect(conn, uring);
     20 }
     21 
     22 int
     23 gateway_on_connect(struct conn *conn, struct io_uring *uring)
     24 {
     25 	if (conn->socket < 0 && !conn->ai_ptr->ai_next) {
     26 		freeaddrinfo(conn->addrinfo);
     27 		return -1;
     28 	}
     29 
     30 	if (conn->socket < 0 && conn->ai_ptr->ai_next) {
     31 		conn->ai_ptr = conn->ai_ptr->ai_next;
     32 		return conn_queue_connect(conn, uring);
     33 	}
     34 
     35 	char host[NI_MAXHOST], serv[NI_MAXSERV];
     36 	getnameinfo(conn->ai_ptr->ai_addr, conn->ai_ptr->ai_addrlen,
     37 		    host, sizeof host, serv, sizeof serv, NI_NUMERICSERV);
     38 	printf("discord: connected successfully to %s (%s:%s)\n",
     39 			conn->ai_ptr->ai_canonname, host, serv);
     40 
     41 	freeaddrinfo(conn->addrinfo);
     42 
     43 	return conn_do_tls_handshake(conn, uring);
     44 }
     45 
     46 int
     47 gateway_on_tls_handshake(struct conn *conn, struct io_uring *uring)
     48 {
     49 	printf("discord: connected over tls\n");
     50 
     51 	struct discord_gateway *gateway =
     52 		TO_PARENT_PTR(conn, struct discord_gateway, conn);
     53 
     54 	gateway->state = GATEWAY_CONN;
     55 	gateway->http.foo = gateway->ws.foo = 0;
     56 
     57 	return conn_queue_close(conn, uring);
     58 }
     59 
     60 int
     61 gateway_on_close(struct conn *conn, struct io_uring *uring)
     62 {
     63 	return -1;
     64 }
     65 
     66 int
     67 gateway_recv(struct conn *conn, struct io_uring *uring)
     68 {
     69 	return conn_queue_recv(conn, uring);
     70 }
     71 
     72 int
     73 gateway_on_recv(struct conn *conn, struct io_uring *uring,
     74 		struct discord_event *ev)
     75 {
     76 	return -1;
     77 }
     78 
     79 int
     80 gateway_send(struct conn *conn, struct io_uring *uring)
     81 {
     82 	return conn_queue_send(conn, uring);
     83 }
     84 
     85 int
     86 gateway_on_send(struct conn *conn, struct io_uring *uring,
     87 		struct discord_event *ev)
     88 {
     89 	return -1;
     90 }