added host/location specific dns/urs server
This commit is contained in:
		| @ -38,19 +38,21 @@ struct task_proxy_t | ||||
| #define PROXY_INIT_FAILED          (1 << 0) | ||||
| #define PROXY_RAW                  (1 << 1) | ||||
| #define PROXY_TRANSPARENT          (1 << 2) | ||||
| #define PROXY_OUTBAND_PEER_NAME    (1 << 3) /* the peer_name pointer points to | ||||
| #define PROXY_DNS_SERVER           (1 << 3) /* dns server address specified */ | ||||
| #define PROXY_URS_SERVER           (1 << 4) /* urs server address specified */ | ||||
| #define PROXY_OUTBAND_PEER_NAME    (1 << 5) /* the peer_name pointer points to | ||||
|                                                a separate memory chunk outside | ||||
|                                                the task_proxy_t chunk. explicit | ||||
|                                                deallocatin is required */ | ||||
| #define PROXY_RESOLVE_PEER_NAME    (1 << 4) | ||||
| #define PROXY_PEER_NAME_RESOLVED   (1 << 5) | ||||
| #define PROXY_PEER_NAME_UNRESOLVED (1 << 6) | ||||
| #define PROXY_REWRITE_URL          (1 << 7) | ||||
| #define PROXY_URL_REWRITTEN        (1 << 8) | ||||
| #define PROXY_URL_REDIRECTED       (1 << 9) | ||||
| #define PROXY_X_FORWARDED_FOR      (1 << 10) /* X-Forwarded-For added */ | ||||
| #define PROXY_VIA                  (1 << 11) /* Via added to the request */ | ||||
| #define PROXY_VIA_RETURNING        (1 << 12) /* Via added to the response */ | ||||
| #define PROXY_RESOLVE_PEER_NAME    (1 << 6) | ||||
| #define PROXY_PEER_NAME_RESOLVED   (1 << 7) | ||||
| #define PROXY_PEER_NAME_UNRESOLVED (1 << 8) | ||||
| #define PROXY_REWRITE_URL          (1 << 9) | ||||
| #define PROXY_URL_REWRITTEN        (1 << 10) | ||||
| #define PROXY_URL_REDIRECTED       (1 << 11) | ||||
| #define PROXY_X_FORWARDED_FOR      (1 << 12) /* X-Forwarded-For added */ | ||||
| #define PROXY_VIA                  (1 << 13) /* Via added to the request */ | ||||
| #define PROXY_VIA_RETURNING        (1 << 14) /* Via added to the response */ | ||||
| 	int flags; | ||||
| 	qse_httpd_t* httpd; | ||||
| 	qse_httpd_client_t* client; | ||||
| @ -64,6 +66,8 @@ struct task_proxy_t | ||||
| 	qse_size_t qpath_pos_in_reqfwdbuf; | ||||
| 	qse_size_t qpath_len_in_reqfwdbuf; | ||||
|  | ||||
| 	qse_nwad_t dns_server; | ||||
| 	qse_nwad_t urs_server; | ||||
| 	qse_mchar_t* pseudonym; | ||||
| 	qse_htrd_t* peer_htrd; | ||||
|  | ||||
| @ -939,7 +943,18 @@ static int task_init_proxy ( | ||||
| 	if (arg->rsrc->flags & QSE_HTTPD_RSRC_PROXY_RAW) proxy->flags |= PROXY_RAW; | ||||
| 	if (arg->rsrc->flags & QSE_HTTPD_RSRC_PROXY_TRANSPARENT) proxy->flags |= PROXY_TRANSPARENT; | ||||
|  | ||||
| 	if (arg->rsrc->flags & QSE_HTTPD_RSRC_PROXY_URS) | ||||
| 	if (arg->rsrc->flags & QSE_HTTPD_RSRC_PROXY_DNS_SERVER) | ||||
| 	{ | ||||
| 		proxy->flags |= PROXY_DNS_SERVER; | ||||
| 		proxy->dns_server = arg->rsrc->dns_server; | ||||
| 	} | ||||
| 	if (arg->rsrc->flags & QSE_HTTPD_RSRC_PROXY_URS_SERVER) | ||||
| 	{ | ||||
| 		proxy->flags |= PROXY_URS_SERVER; | ||||
| 		proxy->urs_server = arg->rsrc->urs_server; | ||||
| 	} | ||||
|  | ||||
| 	if (arg->rsrc->flags & QSE_HTTPD_RSRC_PROXY_ENABLE_URS) | ||||
| 	{ | ||||
| 		const qse_mchar_t* qpath; | ||||
| 		const qse_mchar_t* metnam; | ||||
| @ -2063,7 +2078,8 @@ static int task_main_proxy ( | ||||
| 	if (proxy->flags & PROXY_REWRITE_URL) | ||||
| 	{ | ||||
| 		/* note that url_to_rewrite is URL + extra information. */ | ||||
| 		if (qse_httpd_rewriteurl (httpd, proxy->url_to_rewrite, on_url_rewritten, task) <= -1) goto oops; | ||||
| 		if (qse_httpd_rewriteurl (httpd, proxy->url_to_rewrite, on_url_rewritten,  | ||||
| 		                          ((proxy->flags & PROXY_URS_SERVER)? &proxy->urs_server: QSE_NULL), task) <= -1) goto oops; | ||||
|  | ||||
| 		if (proxy->flags & PROXY_INIT_FAILED) goto oops; | ||||
| 		 | ||||
| @ -2079,7 +2095,8 @@ static int task_main_proxy ( | ||||
|  | ||||
| 		QSE_ASSERT (proxy->peer_name != QSE_NULL); | ||||
|  | ||||
| 		if (qse_httpd_resolname (httpd, proxy->peer_name, on_peer_name_resolved, task) <= -1) goto oops; | ||||
| 		if (qse_httpd_resolname (httpd, proxy->peer_name, on_peer_name_resolved,  | ||||
| 		                         ((proxy->flags & PROXY_DNS_SERVER)? &proxy->dns_server: QSE_NULL), task) <= -1) goto oops; | ||||
|  | ||||
| 		/* if the name could be resolved without sending a request  | ||||
| 		 * in qse_httpd_resolname(), on_peer_name_resolve would be  | ||||
|  | ||||
| @ -129,6 +129,7 @@ struct dns_ctx_t | ||||
|  | ||||
| 	qse_skad_t skad; | ||||
| 	int skadlen; | ||||
| 	int dns_socket; | ||||
|  | ||||
| 	qse_uint16_t seq; | ||||
| 	dns_req_t* reqs[2048]; /* TOOD: choose the right size or make it configurable. must be < DNS_SEQ_RANGE_SIZE */ | ||||
| @ -161,6 +162,10 @@ struct dns_req_t | ||||
| 	int qaaaalen; | ||||
|  | ||||
| 	dns_ctx_t* dc; | ||||
| 	qse_skad_t dns_skad; | ||||
| 	int dns_skadlen; | ||||
| 	int dns_socket; | ||||
|  | ||||
| 	qse_tmr_index_t tmr_tmout; | ||||
| 	int resends; | ||||
|  | ||||
| @ -281,8 +286,6 @@ static int dns_open (qse_httpd_t* httpd, qse_httpd_dns_t* dns) | ||||
| 	qse_httpd_seterrnum (httpd, QSE_HTTPD_ENOIMPL); | ||||
| 	return -1; | ||||
| #else | ||||
| 	sock_t fd = SOCK_INIT;  | ||||
| 	int flag; | ||||
| 	qse_nwad_t nwad; | ||||
| 	dns_ctx_t* dc; | ||||
| 	httpd_xtn_t* httpd_xtn; | ||||
| @ -355,53 +358,45 @@ static int dns_open (qse_httpd_t* httpd, qse_httpd_dns_t* dns) | ||||
| 		qse_httpd_act_t msg; | ||||
| 		qse_size_t pos; | ||||
| 		msg.code = QSE_HTTPD_CATCH_MDBGMSG; | ||||
| 		pos = qse_mbsxcpy (msg.u.mdbgmsg, QSE_COUNTOF(msg.u.mdbgmsg), "nameserver set to "); | ||||
| 		pos = qse_mbsxcpy (msg.u.mdbgmsg, QSE_COUNTOF(msg.u.mdbgmsg), "global nameserver set to "); | ||||
| 		qse_nwadtombs (&nwad, &msg.u.mdbgmsg[pos], QSE_COUNTOF(msg.u.mdbgmsg) - pos, QSE_NWADTOMBS_ALL); | ||||
| 		httpd->opt.rcb.logact (httpd, &msg); | ||||
| 	} | ||||
|  | ||||
| 	fd = socket (qse_skadfamily(&dc->skad), SOCK_DGRAM, IPPROTO_UDP); | ||||
| 	if (!is_valid_socket(fd))  | ||||
| 	dns->handle[0].i = open_udp_socket (httpd, AF_INET); | ||||
| #if defined(AF_INET6) | ||||
| 	dns->handle[1].i = open_udp_socket (httpd, AF_INET6); | ||||
| #endif | ||||
| 	if (!is_valid_socket(dns->handle[0].i) &&  | ||||
| 	    !is_valid_socket(dns->handle[1].i)) | ||||
| 	{ | ||||
| 		qse_httpd_seterrnum (httpd, SKERR_TO_ERRNUM()); | ||||
| 		goto oops; | ||||
| 	} | ||||
|  | ||||
| 	#if defined(FD_CLOEXEC) | ||||
| 	flag = fcntl (fd, F_GETFD); | ||||
| 	if (flag >= 0) fcntl (fd, F_SETFD, flag | FD_CLOEXEC); | ||||
| 	#endif | ||||
| 	if (nwad.type == QSE_NWAD_IN4) | ||||
| 		dc->dns_socket = dns->handle[0].i; | ||||
| 	else | ||||
| 		dc->dns_socket = dns->handle[1].i; | ||||
|  | ||||
| 	#if defined(SO_REUSEADDR) | ||||
| 	flag = 1; | ||||
| 	setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (void*)&flag, QSE_SIZEOF(flag)); | ||||
| 	#endif | ||||
| 	 | ||||
| 	#if defined(SO_REUSEPORT) | ||||
| 	flag = 1; | ||||
| 	setsockopt (fd, SOL_SOCKET, SO_REUSEPORT, (void*)&flag, QSE_SIZEOF(flag)); | ||||
| 	#endif | ||||
|  | ||||
| 	if (set_socket_nonblock (httpd, fd, 1) <= -1) goto oops; | ||||
|  | ||||
| 	dns->handle.i = fd; | ||||
| 	dns->handle_count = 2; | ||||
| 	dns->ctx = dc; | ||||
|  | ||||
| 	return 0; | ||||
|  | ||||
| oops: | ||||
| 	if (is_valid_socket(fd)) close_socket (fd); | ||||
| 	if (is_valid_socket(dns->handle[0].i)) close_socket (dns->handle[0].i); | ||||
| 	if (is_valid_socket(dns->handle[1].i)) close_socket (dns->handle[1].i); | ||||
| 	if (dc) qse_httpd_freemem (httpd, dc); | ||||
| 	return -1; | ||||
|  | ||||
| #endif | ||||
| } | ||||
|  | ||||
| static void dns_remove_tmr_tmout (dns_req_t* req) | ||||
| static void dns_remove_tmr_tmout (qse_httpd_t* httpd, dns_req_t* req) | ||||
| { | ||||
| 	if (req->tmr_tmout != QSE_TMR_INVALID_INDEX) | ||||
| 	{ | ||||
| 		qse_httpd_removetimerevent (req->dc->httpd, req->tmr_tmout); | ||||
| 		qse_httpd_removetimerevent (httpd, req->tmr_tmout); | ||||
| 		req->tmr_tmout = QSE_TMR_INVALID_INDEX; | ||||
| 	} | ||||
| } | ||||
| @ -418,7 +413,7 @@ static void dns_close (qse_httpd_t* httpd, qse_httpd_dns_t* dns) | ||||
| 		{ | ||||
| 			next_req = dc->reqs[i]->next; | ||||
|  | ||||
| 			dns_remove_tmr_tmout (dc->reqs[i]); | ||||
| 			dns_remove_tmr_tmout (httpd, dc->reqs[i]); | ||||
| 			qse_httpd_freemem (httpd, dc->reqs[i]); | ||||
|  | ||||
| 			dc->reqs[i] = next_req; | ||||
| @ -439,12 +434,14 @@ static void dns_close (qse_httpd_t* httpd, qse_httpd_dns_t* dns) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	close_socket (dns->handle.i); | ||||
| 	for (i = 0; i < dns->handle_count; i++)  | ||||
| 	{ | ||||
| 		if (is_valid_socket(dns->handle[i].i)) | ||||
| 			close_socket (dns->handle[i].i); | ||||
| 	} | ||||
| 	qse_httpd_freemem (httpd, dns->ctx); | ||||
| } | ||||
|  | ||||
|  | ||||
|  | ||||
| static void dns_cache_answer (dns_ctx_t* dc, dns_req_t* req, const qse_nwad_t* nwad, qse_uint32_t ttl) | ||||
| { | ||||
| 	dns_ans_t* ans, * prv, * cur; | ||||
| @ -529,7 +526,7 @@ static dns_ans_t* dns_get_answer_from_cache (dns_ctx_t* dc, const qse_mchar_t* n | ||||
| 	return QSE_NULL; | ||||
| } | ||||
|  | ||||
| static int dns_recv (qse_httpd_t* httpd, qse_httpd_dns_t* dns) | ||||
| static int dns_recv (qse_httpd_t* httpd, qse_httpd_dns_t* dns, qse_ubi_t handle) | ||||
| { | ||||
| 	dns_ctx_t* dc = (dns_ctx_t*)dns->ctx; | ||||
| 	httpd_xtn_t* httpd_xtn; | ||||
| @ -559,9 +556,7 @@ printf ("DNS_RECV....\n"); | ||||
| 	httpd_xtn = qse_httpd_getxtn (httpd); | ||||
|  | ||||
| 	fromlen = QSE_SIZEOF(fromaddr); | ||||
| 	len = recvfrom (dns->handle.i, buf, QSE_SIZEOF(buf), 0, (struct sockaddr*)&fromaddr, &fromlen); | ||||
|  | ||||
| /* TODO: check if fromaddr matches the dc->skad... */ | ||||
| 	len = recvfrom (handle.i, buf, QSE_SIZEOF(buf), 0, (struct sockaddr*)&fromaddr, &fromlen); | ||||
|  | ||||
| 	if (len < QSE_SIZEOF(*hdr)) goto done; /* packet too small */ | ||||
|  | ||||
| @ -606,6 +601,7 @@ printf ("DNS_RECV....\n"); | ||||
| 					if ((id == req->seqa || id == req->seqaaaa) && | ||||
| 					    req->dnlen == dnlen && QSE_MEMCMP (req->dn, plptr, req->dnlen) == 0)  | ||||
| 					{ | ||||
| /* TODO: check if fromadd/fromlen matches req->dns_skad/req->dns_skadlen */ | ||||
| 						/* found a match. note that the test here is a bit loose | ||||
| 						 * in that it doesn't really check if the original question  | ||||
| 						 * was A or AAAA. it is possible that it can process an AAAA answer | ||||
| @ -700,7 +696,7 @@ resolved: | ||||
| 	QSE_ASSERT (req != QSE_NULL); | ||||
| 	QSE_ASSERT (xid >= 0 && xid < QSE_COUNTOF(dc->reqs)); | ||||
|  | ||||
| 	dns_remove_tmr_tmout (req); | ||||
| 	dns_remove_tmr_tmout (httpd, req); | ||||
| 	req->resol (httpd, req->name, resolved_nwad, req->ctx); | ||||
|  | ||||
| 	/* detach the request off dc->reqs */ | ||||
| @ -753,8 +749,8 @@ printf (">>tmr_dns_tmout_handle  req->>%p\n", req); | ||||
| 		tmout_event.handler = tmr_dns_tmout_handle; | ||||
| 		tmout_event.updater = tmr_dns_tmout_update; | ||||
|  | ||||
| 		if ((!(req->flags & DNS_REQ_A_NX) && req->qalen > 0 && sendto (dc->dns->handle.i, req->qa, req->qalen, 0, (struct sockaddr*)&dc->skad, dc->skadlen) != req->qalen) || | ||||
| 			(!(req->flags & DNS_REQ_AAAA_NX) && req->qaaaalen > 0 && sendto (dc->dns->handle.i, req->qaaaa, req->qaaaalen, 0, (struct sockaddr*)&dc->skad, dc->skadlen) != req->qaaaalen)) | ||||
| 		if ((!(req->flags & DNS_REQ_A_NX) && req->qalen > 0 && sendto (req->dns_socket, req->qa, req->qalen, 0, (struct sockaddr*)&req->dns_skad, req->dns_skadlen) != req->qalen) || | ||||
| 			(!(req->flags & DNS_REQ_AAAA_NX) && req->qaaaalen > 0 && sendto (req->dns_socket, req->qaaaa, req->qaaaalen, 0, (struct sockaddr*)&req->dns_skad, req->dns_skadlen) != req->qaaaalen)) | ||||
| 		{ | ||||
| 			/* resend failed. fall thru and destroy the request*/ | ||||
| 		} | ||||
| @ -792,7 +788,7 @@ printf (">>tmr_dns_tmout_handle  req->>%p\n", req); | ||||
| 	dc->req_count--; | ||||
| } | ||||
|  | ||||
| static int dns_send (qse_httpd_t* httpd, qse_httpd_dns_t* dns, const qse_mchar_t* name, qse_httpd_resol_t resol, void* ctx) | ||||
| static int dns_send (qse_httpd_t* httpd, qse_httpd_dns_t* dns, const qse_mchar_t* name, qse_httpd_resol_t resol, const qse_nwad_t* dns_server, void* ctx) | ||||
| { | ||||
| 	dns_ctx_t* dc = (dns_ctx_t*)dns->ctx; | ||||
| 	httpd_xtn_t* httpd_xtn; | ||||
| @ -870,6 +866,29 @@ printf ("DNS REALLY SENING>>>>>>>>>>>>>>>>>>>>>>>\n"); | ||||
| 		goto oops; | ||||
| 	} | ||||
|  | ||||
| 	if (dns_server)  | ||||
| 	{ | ||||
| printf ("GETTING DNS_SERVER XXXXXXXXXXXXXXXXXXXXXXX\n"); | ||||
| 		req->dns_skadlen = qse_nwadtoskad (dns_server, &req->dns_skad); | ||||
| 		if (req->dns_skadlen <= -1) | ||||
| 		{ | ||||
| 			qse_httpd_seterrnum (httpd, QSE_HTTPD_EINVAL); | ||||
| 			goto oops; | ||||
| 		} | ||||
|  | ||||
| 		if (dns_server->type == QSE_NWAD_IN4) | ||||
| 			req->dns_socket = dns->handle[0].i; | ||||
| 		else  | ||||
| 			req->dns_socket = dns->handle[1].i; | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| printf ("GETTING DNS_SERVER XXXXXXXXXXXXXXXXXXXXXXX GLOBAL XXXXXXXXXXXXXXXXXXXXXX\n"); | ||||
| 		req->dns_skad = dc->skad; | ||||
| 		req->dns_skadlen = dc->skadlen; | ||||
| 		req->dns_socket = dc->dns_socket; | ||||
| 	} | ||||
|  | ||||
| 	req->resends = httpd_xtn->dns.resends; | ||||
|  | ||||
| 	qse_gettime (&tmout_event.when); | ||||
| @ -879,8 +898,8 @@ printf ("DNS REALLY SENING>>>>>>>>>>>>>>>>>>>>>>>\n"); | ||||
| 	tmout_event.updater = tmr_dns_tmout_update; | ||||
| 	if (qse_httpd_inserttimerevent (httpd, &tmout_event, &req->tmr_tmout) <= -1) goto oops; | ||||
|  | ||||
| 	if ((req->qalen > 0 && sendto (dns->handle.i, req->qa, req->qalen, 0, (struct sockaddr*)&dc->skad, dc->skadlen) != req->qalen) || | ||||
| 	    (req->qaaaalen > 0 && sendto (dns->handle.i, req->qaaaa, req->qaaaalen, 0, (struct sockaddr*)&dc->skad, dc->skadlen) != req->qaaaalen)) | ||||
| 	if ((req->qalen > 0 && sendto (req->dns_socket, req->qa, req->qalen, 0, (struct sockaddr*)&req->dns_skad, req->dns_skadlen) != req->qalen) || | ||||
| 	    (req->qaaaalen > 0 && sendto (req->dns_socket, req->qaaaa, req->qaaaalen, 0, (struct sockaddr*)&req->dns_skad, req->dns_skadlen) != req->qaaaalen)) | ||||
| 	{ | ||||
| 		qse_httpd_seterrnum (httpd, SKERR_TO_ERRNUM()); | ||||
| 		goto oops; | ||||
| @ -908,7 +927,7 @@ printf ("DNS REALLY SENT>>>>>>>>>>>>>>>>>>>>>>>\n"); | ||||
| oops: | ||||
| 	if (req) | ||||
| 	{ | ||||
| 		dns_remove_tmr_tmout (req); | ||||
| 		dns_remove_tmr_tmout (httpd, req); | ||||
| 		qse_httpd_freemem (httpd, req); | ||||
| 	} | ||||
| 	return -1; | ||||
|  | ||||
| @ -53,6 +53,7 @@ struct urs_ctx_t | ||||
|  | ||||
| 	qse_skad_t skad; | ||||
| 	int skadlen; | ||||
| 	int urs_socket; | ||||
|  | ||||
| 	qse_uint16_t seq; /* TODO: change to uint32_t??? */ | ||||
| 	urs_req_t* reqs[1024]; /* TOOD: choose the right size */ | ||||
| @ -72,6 +73,10 @@ struct urs_req_t | ||||
| 	void* ctx; | ||||
|  | ||||
| 	urs_ctx_t* dc; | ||||
| 	qse_skad_t urs_skad; | ||||
| 	int urs_skadlen; | ||||
| 	int urs_socket; | ||||
|  | ||||
| 	qse_tmr_index_t tmr_tmout; | ||||
| 	int resends; | ||||
|  | ||||
| @ -86,8 +91,6 @@ static int urs_open (qse_httpd_t* httpd, qse_httpd_urs_t* urs) | ||||
| 	qse_httpd_seterrnum (httpd, QSE_HTTPD_ENOIMPL); | ||||
| 	return -1; | ||||
| #else | ||||
| 	sock_t fd = SOCK_INIT;  | ||||
| 	int flag; | ||||
| 	qse_nwad_t nwad; | ||||
| 	urs_ctx_t* dc; | ||||
| 	httpd_xtn_t* httpd_xtn; | ||||
| @ -127,49 +130,40 @@ static int urs_open (qse_httpd_t* httpd, qse_httpd_urs_t* urs) | ||||
| 		httpd->opt.rcb.logact (httpd, &msg); | ||||
| 	} | ||||
|  | ||||
| 	fd = socket (qse_skadfamily(&dc->skad), SOCK_DGRAM, IPPROTO_UDP); | ||||
| 	if (!is_valid_socket(fd))  | ||||
| 	urs->handle[0].i = open_udp_socket (httpd, AF_INET); | ||||
| #if defined(AF_INET6) | ||||
| 	urs->handle[1].i = open_udp_socket (httpd, AF_INET6); | ||||
| #endif | ||||
| 	if (!is_valid_socket(urs->handle[0].i) &&  | ||||
| 	    !is_valid_socket(urs->handle[1].i)) | ||||
| 	{ | ||||
| 		qse_httpd_seterrnum (httpd, SKERR_TO_ERRNUM()); | ||||
| 		goto oops; | ||||
| 	} | ||||
|  | ||||
| /* TODO: set socket send/recv buffer size. it's needed as urs may be long */ | ||||
| 	if (nwad.type == QSE_NWAD_IN4) | ||||
| 		dc->urs_socket = urs->handle[0].i; | ||||
| 	else | ||||
| 		dc->urs_socket = urs->handle[1].i; | ||||
|  | ||||
| 	#if defined(FD_CLOEXEC) | ||||
| 	flag = fcntl (fd, F_GETFD); | ||||
| 	if (flag >= 0) fcntl (fd, F_SETFD, flag | FD_CLOEXEC); | ||||
| 	#endif | ||||
| 	urs->handle_count = 2; | ||||
|  | ||||
| 	#if defined(SO_REUSEADDR) | ||||
| 	flag = 1; | ||||
| 	setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (void*)&flag, QSE_SIZEOF(flag)); | ||||
| 	#endif | ||||
| 	 | ||||
| 	#if defined(SO_REUSEPORT) | ||||
| 	flag = 1; | ||||
| 	setsockopt (fd, SOL_SOCKET, SO_REUSEPORT, (void*)&flag, QSE_SIZEOF(flag)); | ||||
| 	#endif | ||||
|  | ||||
| 	if (set_socket_nonblock (httpd, fd, 1) <= -1) goto oops; | ||||
| 	 | ||||
| 	urs->handle.i = fd; | ||||
| 	urs->ctx = dc; | ||||
| 	return 0; | ||||
|  | ||||
| oops: | ||||
| 	if (is_valid_socket(fd)) close_socket (fd); | ||||
| 	if (is_valid_socket(urs->handle[0].i)) close_socket (urs->handle[0].i); | ||||
| 	if (is_valid_socket(urs->handle[1].i)) close_socket (urs->handle[1].i); | ||||
| 	if (dc) qse_httpd_freemem (httpd, dc); | ||||
| 	return -1; | ||||
|  | ||||
| #endif | ||||
| } | ||||
|  | ||||
| static void urs_remove_tmr_tmout (urs_req_t* req) | ||||
| static void urs_remove_tmr_tmout (qse_httpd_t* httpd, urs_req_t* req) | ||||
| { | ||||
| 	if (req->tmr_tmout != QSE_TMR_INVALID_INDEX) | ||||
| 	{ | ||||
| 		qse_httpd_removetimerevent (req->dc->httpd, req->tmr_tmout); | ||||
| 		qse_httpd_removetimerevent (httpd, req->tmr_tmout); | ||||
| 		req->tmr_tmout = QSE_TMR_INVALID_INDEX; | ||||
| 	} | ||||
| } | ||||
| @ -185,7 +179,7 @@ static void urs_close (qse_httpd_t* httpd, qse_httpd_urs_t* urs) | ||||
| 		while (dc->reqs[i]) | ||||
| 		{ | ||||
| 			next_req = dc->reqs[i]->next; | ||||
| 			urs_remove_tmr_tmout (dc->reqs[i]); | ||||
| 			urs_remove_tmr_tmout (httpd, dc->reqs[i]); | ||||
| 			qse_httpd_freemem (httpd, dc->reqs[i]); | ||||
| 			dc->reqs[i] = next_req; | ||||
| 			dc->req_count--; | ||||
| @ -194,12 +188,17 @@ static void urs_close (qse_httpd_t* httpd, qse_httpd_urs_t* urs) | ||||
|  | ||||
| 	QSE_ASSERT (dc->req_count == 0); | ||||
|  | ||||
| 	close_socket (urs->handle.i); | ||||
| 	for (i = 0; i < urs->handle_count; i++)  | ||||
| 	{ | ||||
| 		if (is_valid_socket(urs->handle[i].i))  | ||||
| 			close_socket (urs->handle[i].i); | ||||
| 	} | ||||
|  | ||||
| 	qse_httpd_freemem (httpd, urs->ctx); | ||||
| } | ||||
|  | ||||
|  | ||||
| static int urs_recv (qse_httpd_t* httpd, qse_httpd_urs_t* urs) | ||||
| static int urs_recv (qse_httpd_t* httpd, qse_httpd_urs_t* urs, qse_ubi_t handle) | ||||
| { | ||||
| 	urs_ctx_t* dc = (urs_ctx_t*)urs->ctx; | ||||
| 	httpd_xtn_t* httpd_xtn; | ||||
| @ -217,7 +216,7 @@ printf ("URS_RECV....\n"); | ||||
| 	httpd_xtn = qse_httpd_getxtn (httpd); | ||||
|  | ||||
| 	fromlen = QSE_SIZEOF(fromaddr); | ||||
| 	len = recvfrom (urs->handle.i, dc->rcvbuf, QSE_SIZEOF(dc->rcvbuf) - 1, 0, (struct sockaddr*)&fromaddr, &fromlen); | ||||
| 	len = recvfrom (handle.i, dc->rcvbuf, QSE_SIZEOF(dc->rcvbuf) - 1, 0, (struct sockaddr*)&fromaddr, &fromlen); | ||||
|  | ||||
| /* TODO: check if fromaddr matches the dc->skad... */ | ||||
|  | ||||
| @ -232,7 +231,7 @@ printf ("URS_RECV....\n"); | ||||
| 			{ | ||||
| 				pkt->url[qse_ntoh16(pkt->hdr.len)] = QSE_MT('\0'); | ||||
|  | ||||
| 				urs_remove_tmr_tmout (req); | ||||
| 				urs_remove_tmr_tmout (httpd, req); | ||||
| 				req->rewrite (httpd, req->pkt->url, pkt->url, req->ctx); | ||||
|  | ||||
| 				/* detach the request off dc->reqs */ | ||||
| @ -287,7 +286,7 @@ static void tmr_urs_tmout_handle (qse_tmr_t* tmr, const qse_ntime_t* now, void* | ||||
| 		tmout_event.handler = tmr_urs_tmout_handle; | ||||
| 		tmout_event.updater = tmr_urs_tmout_update; | ||||
|  | ||||
| 		if (sendto (dc->urs->handle.i, req->pkt, req->pktlen, 0, (struct sockaddr*)&dc->skad, dc->skadlen) != req->pktlen) | ||||
| 		if (sendto (req->urs_socket, req->pkt, req->pktlen, 0, (struct sockaddr*)&dc->skad, dc->skadlen) != req->pktlen) | ||||
| 		{ | ||||
| 			/* error. fall thru */ | ||||
| 		} | ||||
| @ -321,7 +320,7 @@ printf ("urs timed out....\n"); | ||||
| 	dc->req_count--; | ||||
| } | ||||
|  | ||||
| static int urs_send (qse_httpd_t* httpd, qse_httpd_urs_t* urs, const qse_mchar_t* url, qse_httpd_rewrite_t rewrite, void* ctx) | ||||
| static int urs_send (qse_httpd_t* httpd, qse_httpd_urs_t* urs, const qse_mchar_t* url, qse_httpd_rewrite_t rewrite, const qse_nwad_t* urs_server, void* ctx) | ||||
| { | ||||
| 	urs_ctx_t* dc = (urs_ctx_t*)urs->ctx; | ||||
| 	httpd_xtn_t* httpd_xtn; | ||||
| @ -374,6 +373,27 @@ printf ("URS REALLY SENING>>>>>>>>>>>>>>>>>>>>>>>\n"); | ||||
| 	req->ctx = ctx; | ||||
| 	req->resends = httpd_xtn->urs.resends; | ||||
|  | ||||
| 	if (urs_server)  | ||||
| 	{ | ||||
| 		req->urs_skadlen = qse_nwadtoskad (urs_server, &req->urs_skad); | ||||
| 		if (req->urs_skadlen <= -1) | ||||
| 		{ | ||||
| 			qse_httpd_seterrnum (httpd, QSE_HTTPD_EINVAL); | ||||
| 			goto oops; | ||||
| 		} | ||||
|  | ||||
| 		if (urs_server->type == QSE_NWAD_IN4) | ||||
| 			req->urs_socket = urs->handle[0].i; | ||||
| 		else  | ||||
| 			req->urs_socket = urs->handle[1].i; | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		req->urs_skad = dc->skad; | ||||
| 		req->urs_skadlen = dc->skadlen; | ||||
| 		req->urs_socket = dc->urs_socket; | ||||
| 	} | ||||
|  | ||||
| 	qse_gettime (&tmout_event.when); | ||||
| 	qse_addtime (&tmout_event.when, &httpd_xtn->urs.tmout, &tmout_event.when); | ||||
| 	tmout_event.ctx = req; | ||||
| @ -381,7 +401,7 @@ printf ("URS REALLY SENING>>>>>>>>>>>>>>>>>>>>>>>\n"); | ||||
| 	tmout_event.updater = tmr_urs_tmout_update; | ||||
| 	if (qse_httpd_inserttimerevent (httpd, &tmout_event, &req->tmr_tmout) <= -1) goto oops; | ||||
|  | ||||
| 	if (sendto (urs->handle.i, req->pkt, req->pktlen, 0, (struct sockaddr*)&dc->skad, dc->skadlen) != req->pktlen) | ||||
| 	if (sendto (req->urs_socket, req->pkt, req->pktlen, 0, (struct sockaddr*)&dc->skad, dc->skadlen) != req->pktlen) | ||||
| 	{ | ||||
| 		qse_httpd_seterrnum (httpd, SKERR_TO_ERRNUM()); | ||||
| 		goto oops; | ||||
| @ -403,7 +423,7 @@ printf ("URS REALLY SENT>>>>>>>>>>>>>>>>>>>>>>>\n"); | ||||
| oops: | ||||
| 	if (req) | ||||
| 	{ | ||||
| 		urs_remove_tmr_tmout (req); | ||||
| 		urs_remove_tmr_tmout (httpd, req); | ||||
| 		qse_httpd_freemem (httpd, req); | ||||
| 	} | ||||
| 	return -1; | ||||
|  | ||||
| @ -650,10 +650,10 @@ void* qse_httpd_getxtnstd (qse_httpd_t* httpd) | ||||
|  | ||||
| #if defined(_WIN32) | ||||
| 	typedef SOCKET sock_t; | ||||
| #	define SOCK_INIT INVALID_SOCKET | ||||
| #	define SOCK_INVALID INVALID_SOCKET | ||||
| #else | ||||
| 	typedef int sock_t; | ||||
| #	define SOCK_INIT -1 | ||||
| #	define SOCK_INVALID -1 | ||||
| #endif | ||||
|  | ||||
| #if !defined(HAVE_SOCKLEN_T) | ||||
| @ -720,6 +720,42 @@ static int set_socket_nonblock (qse_httpd_t* httpd, sock_t fd, int enabled) | ||||
|  | ||||
| } | ||||
|  | ||||
| static sock_t open_udp_socket (qse_httpd_t* httpd, int domain) | ||||
| { | ||||
| 	sock_t fd; | ||||
| 	int flag; | ||||
|  | ||||
| 	fd = socket (domain, SOCK_DGRAM, IPPROTO_UDP); | ||||
| 	if (!is_valid_socket(fd))  | ||||
| 	{ | ||||
| 		qse_httpd_seterrnum (httpd, SKERR_TO_ERRNUM()); | ||||
| 		goto oops; | ||||
| 	} | ||||
|  | ||||
| 	#if defined(FD_CLOEXEC) | ||||
| 	flag = fcntl (fd, F_GETFD); | ||||
| 	if (flag >= 0) fcntl (fd, F_SETFD, flag | FD_CLOEXEC); | ||||
| 	#endif | ||||
|  | ||||
| 	#if defined(SO_REUSEADDR) | ||||
| 	flag = 1; | ||||
| 	setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (void*)&flag, QSE_SIZEOF(flag)); | ||||
| 	#endif | ||||
|  | ||||
| 	#if defined(SO_REUSEPORT) | ||||
| 	flag = 1; | ||||
| 	setsockopt (fd, SOL_SOCKET, SO_REUSEPORT, (void*)&flag, QSE_SIZEOF(flag)); | ||||
| 	#endif | ||||
|  | ||||
| 	if (set_socket_nonblock (httpd, fd, 1) <= -1) goto oops; | ||||
|  | ||||
| 	return fd; | ||||
|  | ||||
| oops: | ||||
| 	if (is_valid_socket(fd)) close_socket (fd); | ||||
| 	return SOCK_INVALID; | ||||
| } | ||||
|  | ||||
| /* ------------------------------------------------------------------- */ | ||||
|  | ||||
| static int server_open (qse_httpd_t* httpd, qse_httpd_server_t* server) | ||||
| @ -728,7 +764,7 @@ static int server_open (qse_httpd_t* httpd, qse_httpd_server_t* server) | ||||
| 	qse_httpd_seterrnum (httpd, QSE_HTTPD_ENOIMPL); | ||||
| 	return -1; | ||||
| #else | ||||
| 	sock_t fd = SOCK_INIT, flag; | ||||
| 	sock_t fd = SOCK_INVALID, flag; | ||||
| 	qse_skad_t addr; | ||||
| 	int addrsize; | ||||
|  | ||||
| @ -872,7 +908,7 @@ static int server_accept ( | ||||
| #else | ||||
| 	qse_skad_t addr; | ||||
| 	socklen_t addrlen; | ||||
| 	sock_t fd = SOCK_INIT; | ||||
| 	sock_t fd = SOCK_INVALID; | ||||
| 	int flag; | ||||
|  | ||||
| 	addrlen = QSE_SIZEOF(addr); | ||||
| @ -963,7 +999,7 @@ static int peer_open (qse_httpd_t* httpd, qse_httpd_peer_t* peer) | ||||
| 	qse_skad_t connaddr, bindaddr; | ||||
| 	int connaddrsize, bindaddrsize; | ||||
| 	int connected = 1; | ||||
| 	sock_t fd = SOCK_INIT; | ||||
| 	sock_t fd = SOCK_INVALID; | ||||
| #if defined(_WIN32) | ||||
| 	unsigned long cmd; | ||||
| #elif defined(__OS2__) | ||||
| @ -2815,7 +2851,7 @@ static int make_resource ( | ||||
|  | ||||
| 					server_xtn->auth.ptr = tptr; | ||||
| 					/* the maximum capacity that can hold the largest authorization value */ | ||||
| 					server_xtn->auth.len = authl;	  | ||||
| 					server_xtn->auth.len = authl; | ||||
| 				} | ||||
|  | ||||
| 				/* decoding a base64-encoded string result in a shorter value than the input. | ||||
|  | ||||
| @ -822,14 +822,31 @@ qse_httpd_server_t* qse_httpd_getprevserver (qse_httpd_t* httpd, qse_httpd_serve | ||||
|  | ||||
| static int activate_dns (qse_httpd_t* httpd) | ||||
| { | ||||
| 	int i; | ||||
|  | ||||
| 	QSE_MEMSET (&httpd->dns, 0, QSE_SIZEOF(httpd->dns)); | ||||
| 	for (i = 0; i < QSE_COUNTOF(httpd->dns.handle); i++) | ||||
| 		httpd->dns.handle[i].i = -1; | ||||
|  | ||||
| 	if (httpd->opt.scb.dns.open (httpd, &httpd->dns) <= -1) return -1; | ||||
|  | ||||
| 	httpd->dns.type = QSE_HTTPD_DNS; | ||||
|  | ||||
| 	if (httpd->opt.scb.mux.addhnd (httpd, httpd->mux, httpd->dns.handle, QSE_HTTPD_MUX_READ, &httpd->dns) <= -1)  | ||||
| 	for (i = 0; i < httpd->dns.handle_count; i++) | ||||
| 	{ | ||||
| 		httpd->opt.scb.dns.close (httpd, &httpd->dns); | ||||
| 		return -1; | ||||
| 		if (httpd->dns.handle[i].i >= 0) | ||||
| 		{ | ||||
| 			if (httpd->opt.scb.mux.addhnd (httpd, httpd->mux, httpd->dns.handle[i], QSE_HTTPD_MUX_READ, &httpd->dns) <= -1)  | ||||
| 			{ | ||||
| 				while (i > 0) | ||||
| 				{ | ||||
| 					qse_ubi_t handle = httpd->dns.handle[--i]; | ||||
| 					if (handle.i >= 0) httpd->opt.scb.mux.delhnd (httpd, httpd->mux, handle); | ||||
| 				} | ||||
| 				httpd->opt.scb.dns.close (httpd, &httpd->dns); | ||||
| 				return -1; | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return 0; | ||||
| @ -837,7 +854,14 @@ static int activate_dns (qse_httpd_t* httpd) | ||||
|  | ||||
| static void deactivate_dns (qse_httpd_t* httpd) | ||||
| { | ||||
| 	httpd->opt.scb.mux.delhnd (httpd, httpd->mux, httpd->dns.handle); | ||||
| 	int i; | ||||
|  | ||||
| 	for (i = 0; i < httpd->dns.handle_count; i++) | ||||
| 	{ | ||||
| 		if (httpd->dns.handle[i].i >= 0) | ||||
| 			httpd->opt.scb.mux.delhnd (httpd, httpd->mux, httpd->dns.handle[i]); | ||||
| 	} | ||||
|  | ||||
| 	httpd->opt.scb.dns.close (httpd, &httpd->dns); | ||||
| } | ||||
| /* ----------------------------------------------------------------------- */ | ||||
| @ -845,14 +869,28 @@ static void deactivate_dns (qse_httpd_t* httpd) | ||||
| static int activate_urs (qse_httpd_t* httpd) | ||||
| { | ||||
| /* TODO: how to disable URS??? */ | ||||
| 	int i; | ||||
|  | ||||
| 	QSE_MEMSET (&httpd->urs, 0, QSE_SIZEOF(httpd->urs)); | ||||
| 	for (i = 0; i < QSE_COUNTOF(httpd->urs.handle); i++) | ||||
| 		httpd->urs.handle[i].i = -1; | ||||
|  | ||||
| 	if (httpd->opt.scb.urs.open (httpd, &httpd->urs) <= -1) return -1; | ||||
|  | ||||
| 	httpd->urs.type = QSE_HTTPD_URS; | ||||
|  | ||||
| 	if (httpd->opt.scb.mux.addhnd (httpd, httpd->mux, httpd->urs.handle, QSE_HTTPD_MUX_READ, &httpd->urs) <= -1)  | ||||
| 	for (i = 0; i < httpd->dns.handle_count; i++) | ||||
| 	{ | ||||
| 		httpd->opt.scb.urs.close (httpd, &httpd->urs); | ||||
| 		return -1; | ||||
| 		if (httpd->opt.scb.mux.addhnd (httpd, httpd->mux, httpd->urs.handle[i], QSE_HTTPD_MUX_READ, &httpd->urs) <= -1)  | ||||
| 		{ | ||||
| 			while (i > 0)  | ||||
| 			{ | ||||
| 				qse_ubi_t handle = httpd->urs.handle[--i]; | ||||
| 				httpd->opt.scb.mux.delhnd (httpd, httpd->mux, handle); | ||||
| 			} | ||||
| 			httpd->opt.scb.urs.close (httpd, &httpd->urs); | ||||
| 			return -1; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return 0; | ||||
| @ -860,7 +898,14 @@ static int activate_urs (qse_httpd_t* httpd) | ||||
|  | ||||
| static void deactivate_urs (qse_httpd_t* httpd) | ||||
| { | ||||
| 	httpd->opt.scb.mux.delhnd (httpd, httpd->mux, httpd->urs.handle); | ||||
| 	int i; | ||||
|  | ||||
| 	for (i = 0; i < httpd->urs.handle_count; i++) | ||||
| 	{ | ||||
| 		if (httpd->urs.handle[i].i >= 0) | ||||
| 			httpd->opt.scb.mux.delhnd (httpd, httpd->mux, httpd->urs.handle[i]); | ||||
| 	} | ||||
|  | ||||
| 	httpd->opt.scb.urs.close (httpd, &httpd->urs); | ||||
| } | ||||
|  | ||||
| @ -1411,7 +1456,7 @@ static int perform_dns (qse_httpd_t* httpd, void* mux, qse_ubi_t handle, int mas | ||||
| 	QSE_ASSERT (mask & QSE_HTTPD_MUX_READ); | ||||
| 	QSE_ASSERT (&httpd->dns == dns); | ||||
|  | ||||
| 	return httpd->opt.scb.dns.recv (httpd, dns); | ||||
| 	return httpd->opt.scb.dns.recv (httpd, dns, handle); | ||||
| } | ||||
|  | ||||
| static int perform_urs (qse_httpd_t* httpd, void* mux, qse_ubi_t handle, int mask, void* cbarg) | ||||
| @ -1421,7 +1466,7 @@ static int perform_urs (qse_httpd_t* httpd, void* mux, qse_ubi_t handle, int mas | ||||
| 	QSE_ASSERT (mask & QSE_HTTPD_MUX_READ); | ||||
| 	QSE_ASSERT (&httpd->urs == urs); | ||||
|  | ||||
| 	return httpd->opt.scb.urs.recv (httpd, urs); | ||||
| 	return httpd->opt.scb.urs.recv (httpd, urs, handle); | ||||
| } | ||||
|  | ||||
| static void purge_bad_clients (qse_httpd_t* httpd) | ||||
| @ -1722,7 +1767,7 @@ qse_mchar_t* qse_httpd_escapehtml (qse_httpd_t* httpd, const qse_mchar_t* str) | ||||
|  | ||||
| /* ----------------------------------------------------------------------- */ | ||||
|  | ||||
| int qse_httpd_resolname (qse_httpd_t* httpd, const qse_mchar_t* name, qse_httpd_resol_t resol, void* ctx) | ||||
| int qse_httpd_resolname (qse_httpd_t* httpd, const qse_mchar_t* name, qse_httpd_resol_t resol, const qse_nwad_t* dns_server, void* ctx) | ||||
| { | ||||
| 	/* TODO: find the name in cache */ | ||||
|  | ||||
| @ -1734,10 +1779,10 @@ printf ("DNS_SEND.........................\n"); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	return httpd->opt.scb.dns.send (httpd, &httpd->dns, name, resol, ctx); | ||||
| 	return httpd->opt.scb.dns.send (httpd, &httpd->dns, name, resol, dns_server, ctx); | ||||
| } | ||||
|  | ||||
| int qse_httpd_rewriteurl (qse_httpd_t* httpd, const qse_mchar_t* url, qse_httpd_rewrite_t rewrite, void* ctx) | ||||
| int qse_httpd_rewriteurl (qse_httpd_t* httpd, const qse_mchar_t* url, qse_httpd_rewrite_t rewrite, const qse_nwad_t* urs_server, void* ctx) | ||||
| { | ||||
| 	if (!httpd->ursactive) | ||||
| 	{ | ||||
| @ -1745,7 +1790,7 @@ int qse_httpd_rewriteurl (qse_httpd_t* httpd, const qse_mchar_t* url, qse_httpd_ | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	return httpd->opt.scb.urs.send (httpd, &httpd->urs, url, rewrite, ctx); | ||||
| 	return httpd->opt.scb.urs.send (httpd, &httpd->urs, url, rewrite, urs_server, ctx); | ||||
| } | ||||
|  | ||||
| int qse_httpd_activatetasktrigger (qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* task) | ||||
|  | ||||
		Reference in New Issue
	
	Block a user