Lines Matching refs:transport
1 /* dso-transport.c
43 #include "dso-transport.h"
64 static dso_transport_t *dso_transport_states; // DSO transport states that exist.
104 dso_transport_finalize(dso_transport_t *transport, const char *whence)
107 "transport: %p.", transport->dso != NULL ? transport->dso->serial : DSO_STATE_INVALID_SERIAL,
108 transport->serial, whence, transport);
111 for (; *tp != NULL && *tp != transport; tp = &((*tp)->next))
117 " transport: %p.", transport);
120 if (transport->connection != NULL) {
122 MDNS_DISPOSE_NW(transport->connection);
124 mDNSPlatformTCPCloseConnection(transport->connection);
125 transport->connection = NULL;
129 "Finalizing a dso_transport_t with no corresponding underlying connection - transport: %p.", transport);
132 mdns_free(transport);
219 // If cs->dso->transport is non-null, we're already connected.
220 if (cs->dso && cs->dso->transport == NULL) {
280 bool dso_write_start(dso_transport_t *transport, size_t in_length)
282 // The transport doesn't support messages outside of this range.
292 if (transport->to_write != NULL) {
293 nw_release(transport->to_write);
294 transport->to_write = NULL;
298 transport->to_write = dispatch_data_create(lenbuf, 2, dso_dispatch_queue,
300 if (transport->to_write == NULL) {
301 transport->write_failed = true;
304 transport->bytes_to_write = length + 2;
309 if (transport->unsent_bytes > MAX_UNSENT_BYTES) {
315 transport->lenbuf[0] = length >> 8;
316 transport->lenbuf[1] = length & 255;
318 transport->to_write[0] = transport->lenbuf;
319 transport->write_lengths[0] = 2;
320 transport->num_to_write = 1;
322 return mDNSPlatformTCPWritable(transport->connection);
330 bool dso_write_finish(dso_transport_t *transport)
333 const uint32_t serial = transport->dso->serial;
334 const size_t bytes_to_write = transport->bytes_to_write;
335 transport->bytes_to_write = 0;
336 if (transport->write_failed) {
337 dso_state_cancel(transport->dso);
340 transport->unsent_bytes += bytes_to_write;
341 nw_connection_send(transport->connection, transport->to_write, NW_CONNECTION_DEFAULT_STREAM_CONTEXT, true,
354 if (dso != NULL && dso->transport != NULL) {
355 dso->transport->unsent_bytes -= bytes_to_write;
358 dso->transport->unsent_bytes);
367 nw_release(transport->to_write);
368 transport->to_write = NULL;
374 if (transport->num_to_write > MAX_WRITE_HUNKS) {
376 transport->num_to_write, MAX_WRITE_HUNKS);
377 dso_state_cancel(transport->dso);
382 for (i = 0; i < transport->num_to_write; i++) {
383 result = mDNSPlatformWriteTCP(transport->connection, (const char *)transport->to_write[i], transport->write_lengths[i]);
384 if (result != transport->write_lengths[i]) {
386 LogMsg("dso_write_finish: fatal: mDNSPlatformWrite on %s returned %d", transport->dso->remote_name, errno);
389 transport->dso->remote_name, (long)result, (long)total);
391 dso_state_cancel(transport->dso);
405 void dso_write(dso_transport_t *transport, const uint8_t *buf, size_t length)
412 if (transport->write_failed) {
418 transport->write_failed = true;
421 if (transport->to_write != NULL) {
422 dispatch_data_t dpc = dispatch_data_create_concat(transport->to_write, dpd);
424 MDNS_DISPOSE_DISPATCH(transport->to_write);
426 transport->to_write = NULL;
427 transport->write_failed = true;
430 transport->to_write = dpc;
434 if (transport->num_to_write >= MAX_WRITE_HUNKS) {
435 transport->num_to_write++;
439 transport->to_write[transport->num_to_write] = buf;
440 transport->write_lengths[transport->num_to_write] = length;
441 transport->num_to_write++;
448 dso_transport_t *transport = dso->transport;
449 if (transport == NULL || transport->dso == NULL) {
452 if (transport->connection != NULL) {
453 if (dso_write_start(transport, dso_message_length(msg)) || disregard_low_water) {
454 dso_write(transport, msg->buf, msg->no_copy_bytes_offset);
455 dso_write(transport, msg->no_copy_bytes, msg->no_copy_bytes_len);
456 dso_write(transport, &msg->buf[msg->no_copy_bytes_offset], msg->cur - msg->no_copy_bytes_offset);
457 return dso_write_finish(transport);
471 dso_transport_t *transport = dso->transport;
489 (void)dso_write_start(transport, 12);
490 dso_write(transport, (uint8_t *)&response, 12);
491 if (!dso_write_finish(transport)) {
531 static void dso_read_message(dso_transport_t *transport, uint32_t length);
533 static void dso_read_message_length(dso_transport_t *transport)
535 if (transport == NULL) {
541 if (transport->dso == NULL) {
543 "dso_read_message_length: transport->dso is NULL while reading message");
547 const uint32_t serial = transport->dso->serial;
548 if (transport->connection == NULL) {
552 nw_connection_receive(transport->connection, 2, 2,
570 if (dso != NULL && transport->dso == dso && transport->dso->cb != NULL) {
577 dso->transport = NULL;
578 nw_connection_cancel(transport->connection);
579 transport->dso->cb(transport->dso->context, &disconnect_context, transport->dso,
605 dso_read_message(transport, length);
615 void dso_read_message(dso_transport_t *transport, uint32_t length)
617 if (transport == NULL) {
622 if (transport->dso == NULL) {
624 "dso_read_message: transport->dso is NULL while reading message");
628 const uint32_t serial = transport->dso->serial;
629 if (transport->connection == NULL) {
633 nw_connection_receive(transport->connection, length, length,
650 if (dso != NULL && transport->dso == dso && transport->dso->cb != NULL) {
656 dso->transport = NULL;
657 nw_connection_cancel(transport->connection);
658 transport->dso->cb(transport->dso->context, &disconnect_context, transport->dso,
689 dso_read_message_length(transport);
702 dso_transport_t *transport = context;
707 dso = transport->dso;
724 if (transport->message_length == 0) {
725 transport->need_length = true;
726 transport->inbufp = transport->inbuf;
727 transport->bytes_needed = 2;
731 ssize_t count = mDNSPlatformReadTCP(sock, transport->inbufp, transport->bytes_needed, &closed);
747 transport->inbufp += count;
748 transport->bytes_needed -= count;
751 if (transport->bytes_needed == 0) {
753 if (transport->need_length) {
755 size_t bytes_needed = (((size_t)transport->inbuf[0]) << 8) | transport->inbuf[1];
767 if (bytes_needed > transport->inbuf_size - 2) {
774 transport->message_length = bytes_needed;
775 transport->bytes_needed = bytes_needed;
776 transport->inbufp = transport->inbuf + 2;
777 transport->need_length = false;
781 dso_message_payload_t message = { &transport->inbuf[2], transport->message_length };
783 transport->message_length = 0;
796 dso_transport_t *transport;
801 transp = mallocL("dso_transport_create", (sizeof *transport) + outbuf_size);
803 transport = NULL;
807 mDNSPlatformMemZero(transp, sizeof (*transport));
809 transport = (dso_transport_t *)transp;
810 transp += sizeof *transport;
812 transport->outbuf = transp;
813 transport->outbuf_size = outbuf_size;
816 transport->dso = dso_state_create(is_server, max_outstanding_queries, remote_name, cb, context, context_callback,
817 transport);
818 if (transport->dso == NULL) {
819 mDNSPlatformMemFree(transport);
820 transport = NULL;
824 transport->dso = dso;
826 transport->connection = connection;
827 nw_retain(transport->connection);
832 transport->serial = dso_transport_serial++;
834 transport->dso->transport = transport;
835 transport->dso->transport_finalize = dso_transport_finalize;
836 transport->next = dso_transport_states;
837 dso_transport_states = transport;
840 dso_read_message_length(transport);
842 "transport: %p, remote_name: " PRI_S ".", transport->dso->serial, transport->serial, transport, remote_name);
844 return transport;
852 dso_transport_t *transport;
867 transp = mallocL("dso_transport_create", (sizeof *transport) + inbuf_size + outbuf_size);
869 transport = NULL;
873 mDNSPlatformMemZero(transp, sizeof (*transport));
875 transport = (dso_transport_t *)transp;
876 transp += sizeof *transport;
878 transport->inbuf = transp;
879 transport->inbuf_size = inbuf_size;
882 transport->outbuf = transp;
883 transport->outbuf_size = outbuf_size;
886 transport->dso = dso_state_create(is_server, max_outstanding_queries, remote_name, cb, context, context_callback,
887 transport);
888 if (transport->dso == NULL) {
889 mDNSPlatformMemFree(transport);
890 transport = NULL;
894 transport->dso = dso;
896 transport->connection = sock;
901 transport->serial = dso_transport_serial++;
903 status = mDNSPlatformTCPSocketSetCallback(sock, dso_read_callback, transport);
906 dso_state_cancel(transport->dso);
910 transport->dso->transport = transport;
911 transport->dso->transport_finalize = dso_transport_finalize;
912 transport->next = dso_transport_states;
913 dso_transport_states = transport;
915 return transport;
1061 if (cs->transport != NULL && cs->transport->connection != NULL) {
1062 nw_connection_cancel(cs->transport->connection);
1088 dso_transport_t *transport =
1091 if (transport == NULL) {
1098 transport->dso->cb(cs->context, NULL, transport->dso, kDSOEventType_Connected);
1102 // Remember the transport on the connect state so that we can cancel it when needed.
1103 cs->transport = transport;
1287 if (ncs->transport) {
1288 MDNS_DISPOSE_NW(ncs->transport->connection);
1289 // If there is a dso state on the connect state and it is referencing this transport,
1291 if (ncs->dso != NULL && ncs->dso->transport == ncs->transport) {
1292 ncs->dso->transport = NULL;
1294 // If the dso_state_t is still referencing this transport, remove the reference.
1295 if (ncs->transport->dso != NULL && ncs->transport->dso->transport == ncs->transport) {
1296 ncs->transport->dso->transport = NULL;
1298 dso_transport_finalize(ncs->transport, "dso_connect_internal");
1299 ncs->transport = NULL;
1350 dso_transport_t *transport;
1369 transport = dso_transport_create(sock, false, cs->context, cs->dso_context_callback,
1371 if (transport == NULL) {
1379 transport->dso->cb(cs->context, NULL, transport->dso, kDSOEventType_Connected);
1504 if (!cs->connecting && !cs->transport) {
1650 dso_transport_t *transport;
1653 transport = dso_transport_create(sock, mDNStrue, lc->context, lc->dso_context_callback, lc->max_outstanding_queries,
1655 if (transport == NULL) {
1661 transport->remote_addr = *addr;
1662 transport->remote_port = ntohs(port->NotAnInteger);
1663 if (transport->dso->cb) {
1664 transport->dso->cb(lc->context, 0, transport->dso, kDSOEventType_Connected);