static int mg_accept_conn(struct mg_connection *lc) { struct mg_connection *nc; union socket_address sa; socklen_t sa_len = sizeof(sa); /* NOTE(lsm): on Windows, sock is always > FD_SETSIZE */ sock_t sock = accept(lc->sock, &sa.sa, &sa_len); if (sock == INVALID_SOCKET) { if (mg_is_error()) DBG(("%p: failed to accept: %d", lc, mg_get_errno())); return 0; } nc = mg_if_accept_new_conn(lc); if (nc == NULL) { closesocket(sock); return 0; } DBG(("%p conn from %s:%d", nc, inet_ntoa(sa.sin.sin_addr), ntohs(sa.sin.sin_port))); mg_sock_set(nc, sock); #if MG_ENABLE_SSL if (lc->flags & MG_F_SSL) { if (mg_ssl_if_conn_accept(nc, lc) != MG_SSL_OK) mg_close_conn(nc); } else #endif { mg_if_accept_tcp_cb(nc, &sa, sa_len); } return 1; }
static err_t mg_lwip_accept_cb(void *arg, struct tcp_pcb *newtpcb, err_t err) { struct mg_connection *lc = (struct mg_connection *) arg, *nc; union socket_address sa; DBG(("%p conn from %s:%u\n", lc, ipaddr_ntoa(&newtpcb->remote_ip), newtpcb->remote_port)); sa.sin.sin_addr.s_addr = newtpcb->remote_ip.addr; sa.sin.sin_port = htons(newtpcb->remote_port); nc = mg_if_accept_tcp_cb(lc, &sa, sizeof(sa.sin)); if (nc == NULL) { tcp_abort(newtpcb); return ERR_ABRT; } nc->sock = (int) newtpcb; tcp_arg(newtpcb, nc); tcp_err(newtpcb, mg_lwip_tcp_error_cb); tcp_sent(newtpcb, mg_lwip_tcp_sent_cb); tcp_recv(newtpcb, mg_lwip_tcp_recv_cb); return ERR_OK; }
static void mg_ssl_begin(struct mg_connection *nc) { int server_side = (nc->listener != NULL); enum mg_ssl_if_result res = mg_ssl_if_handshake(nc); DBG(("%p %d res %d", nc, server_side, res)); if (res == MG_SSL_OK) { nc->flags |= MG_F_SSL_HANDSHAKE_DONE; nc->flags &= ~(MG_F_WANT_READ | MG_F_WANT_WRITE); if (server_side) { union socket_address sa; socklen_t sa_len = sizeof(sa); (void) getpeername(nc->sock, &sa.sa, &sa_len); mg_if_accept_tcp_cb(nc, &sa, sa_len); } else { mg_if_connect_cb(nc, 0); } } else if (res != MG_SSL_WANT_READ && res != MG_SSL_WANT_WRITE) { if (!server_side) { mg_if_connect_cb(nc, res); } nc->flags |= MG_F_CLOSE_IMMEDIATELY; } }
void mg_lwip_accept_conn(struct mg_connection *nc, struct tcp_pcb *tpcb) { union socket_address sa; sa.sin.sin_addr.s_addr = tpcb->remote_ip.addr; sa.sin.sin_port = htons(tpcb->remote_port); mg_if_accept_tcp_cb(nc, &sa, sizeof(sa.sin)); }