bool flush_meta(connection_t *c) { int result; ifdebug(META) logger(LOG_DEBUG, "Flushing %d bytes to %s (%s)", c->outbuflen, c->name, c->hostname); while(c->outbuflen) { result = send(c->socket, c->outbuf + c->outbufstart, c->outbuflen, 0); if(result <= 0) { if(!errno || errno == EPIPE) { ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)", c->name, c->hostname); } else if(errno == EINTR) { continue; } else if(sockwouldblock(sockerrno)) { ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Flushing %d bytes to %s (%s) would block", c->outbuflen, c->name, c->hostname); return true; } else { logger(LOG_ERR, "Flushing meta data to %s (%s) failed: %s", c->name, c->hostname, sockstrerror(sockerrno)); } return false; } c->outbufstart += result; c->outbuflen -= result; } c->outbufstart = 0; /* avoid unnecessary memmoves */ return true; }
void mtu_probe_h(node_t *n, vpn_packet_t *packet, length_t len) { ifdebug(TRAFFIC) logger(LOG_INFO, "Got MTU probe length %d from %s (%s)", packet->len, n->name, n->hostname); if(!packet->data[0]) { packet->data[0] = 1; send_udppacket(n, packet); } else { if(n->mtuprobes > 30) { if (len == n->maxmtu + 8) { ifdebug(TRAFFIC) logger(LOG_INFO, "Increase in PMTU to %s (%s) detected, restarting PMTU discovery", n->name, n->hostname); n->maxmtu = MTU; n->mtuprobes = 10; return; } if(n->minmtu) n->mtuprobes = 30; else n->mtuprobes = 1; } if(len > n->maxmtu) len = n->maxmtu; if(n->minmtu < len) n->minmtu = len; } }
static void purge(void) { avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext; node_t *n; edge_t *e; subnet_t *s; ifdebug(PROTOCOL) logger(LOG_DEBUG, "Purging unreachable nodes"); /* Remove all edges and subnets owned by unreachable nodes. */ for(nnode = node_tree->head; nnode; nnode = nnext) { nnext = nnode->next; n = nnode->data; if(!n->status.reachable) { ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Purging node %s (%s)", n->name, n->hostname); for(snode = n->subnet_tree->head; snode; snode = snext) { snext = snode->next; s = snode->data; send_del_subnet(everyone, s); if(!strictsubnets) subnet_del(n, s); } for(enode = n->edge_tree->head; enode; enode = enext) { enext = enode->next; e = enode->data; if(!tunnelserver) send_del_edge(everyone, e); edge_del(e); } } } /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */ for(nnode = node_tree->head; nnode; nnode = nnext) { nnext = nnode->next; n = nnode->data; if(!n->status.reachable) { for(enode = edge_weight_tree->head; enode; enode = enext) { enext = enode->next; e = enode->data; if(e->to == n) break; } if(!enode && (!strictsubnets || !n->subnet_tree->head)) /* in strictsubnets mode do not delete nodes with subnets */ node_del(n); } } }
/* Check if the other end is active. If we have sent packets, but didn't receive any, then possibly the other end is dead. We send a PING request over the meta connection. If the other end does not reply in time, we consider them dead and close the connection. */ static void check_dead_connections(void) { avl_node_t *node, *next; connection_t *c; for(node = connection_tree->head; node; node = next) { next = node->next; c = node->data; if(c->last_ping_time + pingtimeout <= now) { if(c->status.active) { if(c->status.pinged) { ifdebug(CONNECTIONS) logger(LOG_INFO, "%s (%s) didn't respond to PING in %ld seconds", c->name, c->hostname, (long)now - c->last_ping_time); c->status.timeout = true; terminate_connection(c, true); } else if(c->last_ping_time + pinginterval <= now) { send_ping(c); } } else { if(c->status.remove) { logger(LOG_WARNING, "Old connection_t for %s (%s) status %04x still lingering, deleting...", c->name, c->hostname, bitfield_to_int(&c->status, sizeof c->status)); connection_del(c); continue; } ifdebug(CONNECTIONS) logger(LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname); if(c->status.connecting) { c->status.connecting = false; closesocket(c->socket); do_outgoing_connection(c); } else { terminate_connection(c, false); } } } if(c->outbuflen > 0 && c->last_flushed_time + pingtimeout <= now) { if(c->status.active) { ifdebug(CONNECTIONS) logger(LOG_INFO, "%s (%s) could not flush for %ld seconds (%d bytes remaining)", c->name, c->hostname, (long)now - c->last_flushed_time, c->outbuflen); c->status.timeout = true; terminate_connection(c, true); } } } }
void PDF_PrintGraphicObject(OBJECT x) { OBJECT y, link; debug3(DPF, D, "PDF_PrintGraphicObject(%s %s %s)", EchoFilePos(&fpos(x)), Image(type(x)), EchoObject(x)); switch( type(x) ) { case WORD: case QWORD: PDFPage_WriteGraphic(out_fp, string(x)); break; case ACAT: for( link = Down(x); link != x; link = NextDown(link) ) { Child(y, link); if( type(y) == GAP_OBJ ) { if( vspace(y) > 0 ) PDFPage_Write(out_fp, "\n"); else if( hspace(y) > 0 ) PDFPage_Write(out_fp, " "); } else if( is_word(type(y)) || type(y) == ACAT ) PDF_PrintGraphicObject(y); else if( type(y) == WIDE || is_index(type(y)) ) { /* ignore: @Wide, indexes are sometimes inserted by Manifest */ } else { Error(50, 2, "error in left parameter of %s", WARN, &fpos(x), KW_GRAPHIC); debug1(DPF, D, " type(y) = %s, y =", Image(type(y))); ifdebug(DPF, D, DebugObject(y)); } } break; default: Error(50, 3, "error in left parameter of %s", WARN, &fpos(x), KW_GRAPHIC); debug1(DPF, D, " type(x) = %s, x =", Image(type(x))); ifdebug(DPF, D, DebugObject(x)); break; } debug0(DPF, D, "PDF_PrintGraphicObject returning"); } /* end PDF_PrintGraphicObject */
void finish_connecting(connection_t *c) { ifdebug(CONNECTIONS) logger(LOG_INFO, "Connected to %s (%s)", c->name, c->hostname); c->last_ping_time = now; send_id(c); }
bool send_request(connection_t *c, const char *format, ...) { va_list args; char buffer[MAXBUFSIZE]; int len, request = 0; /* Use vsnprintf instead of vxasprintf: faster, no memory fragmentation, cleanup is automatic, and there is a limit on the input buffer anyway */ va_start(args, format); len = vsnprintf(buffer, sizeof buffer, format, args); buffer[sizeof buffer - 1] = 0; va_end(args); if(len < 0 || len > sizeof buffer - 1) { logger(LOG_ERR, "Output buffer overflow while sending request to %s (%s)", c->name, c->hostname); return false; } ifdebug(PROTOCOL) { sscanf(buffer, "%d", &request); ifdebug(META) logger(LOG_DEBUG, "Sending %s to %s (%s): %s", request_name[request], c->name, c->hostname, buffer); else logger(LOG_DEBUG, "Sending %s to %s (%s)", request_name[request], c->name, c->hostname); }
/* Terminate a connection: - Close the socket - Remove associated edge and tell other connections about it if report = true - Check if we need to retry making an outgoing connection - Deactivate the host */ void terminate_connection(connection_t *c, bool report) { if(c->status.remove) return; ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Closing connection with %s (%s)", c->name, c->hostname); c->status.remove = true; c->status.active = false; if(c->node) c->node->connection = NULL; if(c->socket) closesocket(c->socket); if(c->edge) { if(!c->node) { logger(LOG_ERR, "Connection to %s (%s) has an edge but node is NULL!", c->name, c->hostname); // And that should never happen. abort(); } if(report && !tunnelserver) send_del_edge(everyone, c->edge); edge_del(c->edge); /* Run MST and SSSP algorithms */ graph(); /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */ if(report && !c->node->status.reachable) { edge_t *e; e = lookup_edge(c->node, myself); if(e) { if(!tunnelserver) send_del_edge(everyone, e); edge_del(e); } } } free_connection_partially(c); /* Check if this was our outgoing connection */ if(c->outgoing) { c->status.remove = false; do_outgoing_connection(c); } #ifndef HAVE_MINGW /* Clean up dead proxy processes */ while(waitpid(-1, NULL, WNOHANG) > 0); #endif }
OBJECT StartMoment(void) { OBJECT res; debug0(DTK, D, "StartMoment()"); assert(current_moment != nilobj, "StartMoment: current_moment == nilobj!"); res = CopyObject(current_moment, no_fpos); debug0(DTK, D, "StartMoment returning"); ifdebug(DTK, D, DebugObject(res)); return res; }
OBJECT SetEnv(OBJECT x, OBJECT y) { OBJECT res; debug1(DCE, DD, "SetEnv( x, %s ), x =", EchoObject(y)); ifdebug(DCE, DD, DebugObject(x)); assert( x!=nilobj && type(x)==CLOSURE, "SetEnv: x==nilobj or not CLOSURE!" ); assert( y==nilobj || type(y)==ENV, "SetEnv: y!=nilobj && type(y) != ENV!" ); New(res, ENV); Link(res, x); if( y != nilobj ) Link(res, y); debug1(DCE, DD, "SetEnv returning %s", EchoObject(res)); return res; } /* end SetEnv */
/* Terminate a connection: - Close the socket - Remove associated edge and tell other connections about it if report = true - Check if we need to retry making an outgoing connection - Deactivate the host */ void terminate_connection(connection_t *c, bool report) { if(c->status.remove) return; ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Closing connection with %s (%s)", c->name, c->hostname); c->status.remove = true; c->status.active = false; if(c->node) c->node->connection = NULL; if(c->socket) closesocket(c->socket); if(c->edge) { if(report && !tunnelserver) send_del_edge(broadcast, c->edge); edge_del(c->edge); /* Run MST and SSSP algorithms */ graph(); /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */ if(report && !c->node->status.reachable) { edge_t *e; e = lookup_edge(c->node, myself); if(e) { if(!tunnelserver) send_del_edge(broadcast, e); edge_del(e); } } } /* Check if this was our outgoing connection */ if(c->outgoing) { retry_outgoing(c->outgoing); c->outgoing = NULL; } free(c->outbuf); c->outbuf = NULL; c->outbuflen = 0; c->outbufsize = 0; c->outbufstart = 0; }
void InitTime(void) { time_t raw_time; struct tm *now; FULL_CHAR buff[20]; OBJECT par, tmp, sym, env; OBJECT tag, second, minute, hour, weekday, monthday, yearday, month, year, century, dst; debug0(DTK, D, "InitTime()"); /* define @Moment symbol with its host of named parameters */ MomentSym = load(KW_MOMENT, LOCAL, StartSym); tag = load(KW_TAG, NPAR, MomentSym); second = load(KW_SECOND, NPAR, MomentSym); minute = load(KW_MINUTE, NPAR, MomentSym); hour = load(KW_HOUR, NPAR, MomentSym); monthday = load(KW_DAY, NPAR, MomentSym); month = load(KW_MONTH, NPAR, MomentSym); year = load(KW_YEAR, NPAR, MomentSym); century = load(KW_CENTURY, NPAR, MomentSym); weekday = load(KW_WEEKDAY, NPAR, MomentSym); yearday = load(KW_YEARDAY, NPAR, MomentSym); dst = load(KW_DAYLIGHTSAVING, NPAR, MomentSym); /* get current time and convert to ASCII */ if( time(&raw_time) == -1 ) Error(35, 1, "unable to obtain the current time", WARN, no_fpos); now = localtime(&raw_time); StringCopy(time_string, AsciiToFull(asctime(now))); time_string[StringLength(time_string) - 1] = '\0'; /* start of current_moment */ New(current_moment, CLOSURE); actual(current_moment) = MomentSym; /* attach its many parameters */ add_par("%s", KW_NOW, tag); add_par("%.2d", now->tm_sec, second); add_par("%.2d", now->tm_min, minute); add_par("%.2d", now->tm_hour, hour); add_par("%d", now->tm_mday, monthday); add_par("%d", now->tm_mon + 1, month); add_par("%.2d", now->tm_year % 100, year); add_par("%d", (now->tm_year+1900) / 100, century); add_par("%d", now->tm_wday + 1, weekday); add_par("%d", now->tm_yday, yearday); add_par("%d", now->tm_isdst, dst); /* add a null environment */ New(env, ENV); AttachEnv(env, current_moment); debug0(DTK, D, "InitTime() returning."); debug0(DTK, DD, "current_moment ="); ifdebug(DTK, DD, DebugObject(current_moment)); } /* end InitTime */
static bool write_packet(vpn_packet_t *packet) { ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s", packet->len, device_info); if(write(device_fd, packet->data, packet->len) < 0) { logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno)); return false; } device_total_out += packet->len; return true; }
bool status_h(connection_t *c) { int statusno; char statusstring[MAX_STRING_SIZE]; if(sscanf(c->buffer, "%*d %d " MAX_STRING, &statusno, statusstring) != 2) { logger(LOG_ERR, "Got bad %s from %s (%s)", "STATUS", c->name, c->hostname); return false; } ifdebug(STATUS) logger(LOG_NOTICE, "Status message from %s (%s): %d: %s", c->name, c->hostname, statusno, statusstring); return true; }
static void CatAdjustSize(OBJECT x, FULL_LENGTH *b, FULL_LENGTH *f, BOOLEAN ratm, OBJECT y, int dim) { OBJECT link; OBJECT pg, prec_def, sg, sd; FULL_LENGTH beffect, feffect, seffect; int side; int bb = 0, ff = 0; /* initial values unused */ debug6(DSA, DD, "CatAdjustSize(%s x, %s, %s, %s, %s y, %s)", Image(type(x)), EchoLength(*b), EchoLength(*f), bool(ratm), Image(type(y)), dimen(dim)); debug2(DSA,DD, "x(%s,%s) =", EchoLength(back(x,dim)), EchoLength(fwd(x,dim))); ifdebug(DSA, DD, DebugObject(x)); debug2(DSA,DD, "y(%s,%s) =", EchoLength(back(y,dim)), EchoLength(fwd(y,dim))); ifdebug(DSA, DD, DebugObject(y)); /* DO_ADJUST ACAT is a special case because adjustment affects its size */ if( dim==COLM && type(y)==ACAT && display_style(save_style(y)) == DO_ADJUST ) { back(x, dim) = *b; fwd(x, dim) = *f; *b = back(y, dim); *f = fwd(y, dim); debug2(DSA, DD, "CatAdjustSize ACAT %s,%s", EchoLength(*b), EchoLength(*f)); return; } link = UpDim(x, dim); SetNeighbours(link, ratm, &pg, &prec_def, &sg, &sd, &side); { ifdebug(DSA, DD, if( pg != nilobj && mode(gap(pg)) == NO_MODE ) { debug1(DSA, DD, "NO_MODE gap pg, is_indefinite(x) == %s, y =", bool(is_indefinite(type(x))) ); ifdebug(DSA, DD, DebugObject(y)); } if( sg != nilobj && mode(gap(sg)) == NO_MODE ) { debug1(DSA, DD, "NO_MODE gap sg, is_indefinite(x) == %s, y =", bool(is_indefinite(type(x))) ); ifdebug(DSA, DD, DebugObject(y)); } ); }
bool write_packet(vpn_packet_t *packet) { long lenout; ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s", packet->len, device_info); if(!WriteFile (device_handle, packet->data, packet->len, &lenout, NULL)) { logger(LOG_ERR, "Error while writing to %s %s: %s", device_info, device, winerror(GetLastError())); return false; } device_total_out += packet->len; return true; }
static void do_outgoing_pipe(connection_t *c, char *command) { #ifndef HAVE_MINGW int fd[2]; if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) { logger(LOG_ERR, "Could not create socketpair: %s\n", strerror(errno)); return; } if(fork()) { c->socket = fd[0]; close(fd[1]); ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Using proxy %s", command); return; } close(0); close(1); close(fd[0]); dup2(fd[1], 0); dup2(fd[1], 1); close(fd[1]); // Other filedescriptors should be closed automatically by CLOEXEC char *host = NULL; char *port = NULL; sockaddr2str(&c->address, &host, &port); setenv("REMOTEADDRESS", host, true); setenv("REMOTEPORT", port, true); setenv("NODE", c->name, true); setenv("NAME", myself->name, true); if(netname) setenv("NETNAME", netname, true); int result = system(command); if(result < 0) logger(LOG_ERR, "Could not execute %s: %s\n", command, strerror(errno)); else if(result) logger(LOG_ERR, "%s exited with non-zero status %d", command, result); exit(result); #else logger(LOG_ERR, "Proxy type exec not supported on this platform!"); return; #endif }
void LoadScopeSnapshot(OBJECT ss) { OBJECT link, x, sym; BOOLEAN tmp; assert( type(ss) == ACAT, "LoadScopeSnapshot: type(ss)!" ); PushScope(StartSym, FALSE, FALSE); for( link = LastDown(ss); link != ss; link = PrevDown(link) ) { Child(x, link); assert( type(x) == SCOPE_SNAPSHOT, "LoadScopeSnapshot: type(x)!" ); Child(sym, Down(x)); PushScope(sym, ss_npars_only(x), ss_vis_only(x)); body_ok[scope_top-1] = ss_body_ok(x); } tmp = suppress_visible; suppress_visible = ss_suppress(ss); ss_suppress(ss) = tmp; debug0(DST, D, "after LoadScopeSnapshot, scope is:") ifdebug(DST, D, DebugScope()); } /* end LoadScopeSnapshot */
bool error_h(connection_t *c) { int err; char errorstring[MAX_STRING_SIZE]; if(sscanf(c->buffer, "%*d %d " MAX_STRING, &err, errorstring) != 2) { logger(LOG_ERR, "Got bad %s from %s (%s)", "ERROR", c->name, c->hostname); return false; } ifdebug(ERROR) logger(LOG_NOTICE, "Error message from %s (%s): %d: %s", c->name, c->hostname, err, errorstring); terminate_connection(c, c->status.active); return true; }
bool send_meta(connection_t *c, const char *buffer, int length) { int outlen; int result; if(!c) { logger(LOG_ERR, "send_meta() called with NULL pointer!"); abort(); } ifdebug(META) logger(LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length, c->name, c->hostname); if(!c->outbuflen) c->last_flushed_time = now; /* Find room in connection's buffer */ if(length + c->outbuflen > c->outbufsize) { c->outbufsize = length + c->outbuflen; c->outbuf = xrealloc(c->outbuf, c->outbufsize); } if(length + c->outbuflen + c->outbufstart > c->outbufsize) { memmove(c->outbuf, c->outbuf + c->outbufstart, c->outbuflen); c->outbufstart = 0; } /* Add our data to buffer */ if(c->status.encryptout) { result = EVP_EncryptUpdate(c->outctx, (unsigned char *)c->outbuf + c->outbufstart + c->outbuflen, &outlen, (unsigned char *)buffer, length); if(!result || outlen < length) { logger(LOG_ERR, "Error while encrypting metadata to %s (%s): %s", c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL)); return false; } else if(outlen > length) { logger(LOG_EMERG, "Encrypted data too long! Heap corrupted!"); abort(); } c->outbuflen += outlen; } else { memcpy(c->outbuf + c->outbufstart + c->outbuflen, buffer, length); c->outbuflen += length; } return true; }
bool read_packet(vpn_packet_t *packet) { int lenin; if((lenin = read(sp[0], packet->data, MTU)) <= 0) { logger(LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno)); return false; } packet->len = lenin; device_total_in += packet->len; ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len, device_info); return true; }
void retry_outgoing(outgoing_t *outgoing) { outgoing->timeout += 5; if(outgoing->timeout > maxtimeout) outgoing->timeout = maxtimeout; if(outgoing->event) event_del(outgoing->event); outgoing->event = new_event(); outgoing->event->handler = (event_handler_t) setup_outgoing_connection; outgoing->event->time = now + outgoing->timeout; outgoing->event->data = outgoing; event_add(outgoing->event); ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Trying to re-establish outgoing connection in %d seconds", outgoing->timeout); }
static void PDF_CoordScale(float hfactor, float vfactor) { #if DEBUG_ON char buff[20]; #endif ifdebug(DPF, D, sprintf(buff, "%.3f, %.3f", hfactor, vfactor)); debug1(DPF, D, "CoordScale(%s)", buff); if ( (fabs(hfactor - 1.0) > 0.01) || (fabs(vfactor - 1.0) > 0.01) ) { #if 1 PDFPage_Scale(out_fp, hfactor, vfactor); #else char temp_str[64]; sprintf(temp_str, "%.2f 0 0 %.2f 0 0 cm\n", hfactor, vfactor); PDFPage_Write(out_fp, temp_str); #endif } cpexists = FALSE; debug0(DPF, D, "CoordScale returning."); } /* end PDF_CoordScale */
void setup_outgoing_connection(outgoing_t *outgoing) { connection_t *c; node_t *n; outgoing->event = NULL; n = lookup_node(outgoing->name); if(n) if(n->connection) { ifdebug(CONNECTIONS) logger(LOG_INFO, "Already connected to %s", outgoing->name); n->connection->outgoing = outgoing; return; } c = new_connection(); c->name = xstrdup(outgoing->name); c->outcipher = myself->connection->outcipher; c->outdigest = myself->connection->outdigest; c->outmaclength = myself->connection->outmaclength; c->outcompression = myself->connection->outcompression; init_configuration(&c->config_tree); read_connection_config(c); outgoing->cfg = lookup_config(c->config_tree, "Address"); if(!outgoing->cfg) { logger(LOG_ERR, "No address specified for %s", c->name); free_connection(c); return; } c->outgoing = outgoing; c->last_ping_time = now; connection_add(c); do_outgoing_connection(c); }
/* accept a new tcp connect and create a new connection */ bool handle_new_meta_connection(int sock) { connection_t *c; sockaddr_t sa; int fd; socklen_t len = sizeof(sa); fd = accept(sock, &sa.sa, &len); if(fd < 0) { logger(LOG_ERR, "Accepting a new connection failed: %s", sockstrerror(sockerrno)); return false; } sockaddrunmap(&sa); c = new_connection(); c->name = xstrdup("<unknown>"); c->outcipher = myself->connection->outcipher; c->outdigest = myself->connection->outdigest; c->outmaclength = myself->connection->outmaclength; c->outcompression = myself->connection->outcompression; c->address = sa; c->hostname = sockaddr2hostname(&sa); c->socket = fd; c->last_ping_time = now; ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection from %s", c->hostname); configure_tcp(c); connection_add(c); c->allow_request = ID; send_id(c); return true; }
int AttachGalley(OBJECT hd, OBJECT *inners, OBJECT *suspend_pt) { OBJECT hd_index; /* the index of hd in the enclosing galley */ OBJECT hd_inners; /* inner galleys of hd, if unsized */ OBJECT dest; /* the target @Galley hd empties into */ OBJECT dest_index; /* the index of dest */ OBJECT target; /* the target indefinite containing dest */ OBJECT target_index; /* the index of target */ OBJECT target_galley; /* the body of target, made into a galley */ OBJECT tg_inners; /* inner galleys of target_galley */ BOOLEAN need_precedes = FALSE;/* true if destination lies before galley */ OBJECT recs; /* list of recursive definite objects */ OBJECT link, y = nilobj; /* for scanning through the components of hd */ CONSTRAINT c; /* temporary variable holding a constraint */ OBJECT env, n1, tmp, zlink, z, sym; /* placeholders and temporaries */ BOOLEAN was_sized; /* true if sized(hd) initially */ int dim; /* the galley direction */ FULL_LENGTH perp_back, perp_fwd; OBJECT why, junk; debug2(DGA, D, "[ AttachGalley(Galley %s into %s)", SymName(actual(hd)), SymName(whereto(hd))); ifdebug(DGA, DD, DebugGalley(hd, nilobj, 4)); assert( Up(hd) != hd, "AttachGalley: no index!" ); Parent(hd_index, Up(hd)); assert( type(hd_index) == UNATTACHED, "AttachGalley: not UNATTACHED!" ); hd_inners = tg_inners = nilobj; was_sized = sized(hd); dim = gall_dir(hd); for(;;) { /*************************************************************************/ /* */ /* Search for a destination for hd. If hd is unsized, search for */ /* inner galleys preceding it first of all, then for receptive objects */ /* following it, possibly in inner galleys. If no luck, exit. */ /* If hd is sized, search only for receptive objects in the current */ /* galley below the current spot, and fail if cannot find any. */ /* */ /*************************************************************************/ sym = whereto(hd); if( sized(hd) ) { /* sized galley case: search on from current spot */ target_index = SearchGalley(Up(hd_index), sym, TRUE, FALSE, TRUE, TRUE); if( target_index == nilobj ) { /* search failed to find any new target, so kill the galley */ for( link = Down(hd); link != hd; link = NextDown(link) ) { Child(y, link); if( type(y) == SPLIT ) Child(y, DownDim(y, dim)); if( is_definite(type(y)) ) break; } if( link != hd ) Error(19, 1, "galley %s deleted from here (no target)", WARN, &fpos(y), SymName(actual(hd))); if( hd_inners != nilobj ) DisposeObject(hd_inners), hd_inners=nilobj; if( tg_inners != nilobj ) DisposeObject(tg_inners), tg_inners=nilobj; KillGalley(hd, FALSE); *inners = nilobj; debug0(DGA, D, "] AttachGalley returning ATTACH_KILLED"); return ATTACH_KILLED; } else if( actual(actual(target_index)) == InputSym ) { /* search found input object, so suspend on that */ DeleteNode(hd_index); Link(target_index, hd); *inners = nilobj; debug0(DGA, D, "] AttachGalley returning ATTACH_INPUT"); return ATTACH_INPUT; } } else /* unsized galley, either backwards or normal */ { if( foll_or_prec(hd) == GALL_PREC ) { target_index= SearchGalley(Up(hd_index), sym, FALSE, TRUE,TRUE,FALSE); need_precedes = FALSE; } else { target_index = SearchGalley(Up(hd_index), sym, FALSE,TRUE,FALSE,FALSE); need_precedes = (target_index != nilobj); if( target_index == nilobj ) target_index = SearchGalley(Up(hd_index), sym, TRUE,TRUE,TRUE,FALSE); } /* if no luck, exit without error */ if( target_index == nilobj ) { *inners = nilobj; debug0(DGA, D, "] AttachGalley returning ATTACH_NOTARGET"); return ATTACH_NOTARGET; } } assert( type(target_index) == RECEPTIVE, "AttachGalley: target_index!" ); target = actual(target_index); assert( type(target) == CLOSURE, "AttachGalley: target!" ); /* set target_galley to the expanded value of target */ debug1(DYY, D, "[ EnterErrorBlock(FALSE) (expanding target %s)", SymName(actual(target))); EnterErrorBlock(FALSE); New(target_galley, HEAD); force_gall(target_galley) = FALSE; enclose_obj(target_galley) = limiter(target_galley) = nilobj; ClearHeaders(target_galley); opt_components(target_galley) = opt_constraints(target_galley) = nilobj; gall_dir(target_galley) = external_hor(target) ? COLM : ROWM; FposCopy(fpos(target_galley), fpos(target)); actual(target_galley) = actual(target); whereto(target_galley) = ready_galls(target_galley) = nilobj; foll_or_prec(target_galley) = GALL_FOLL; must_expand(target_galley) = FALSE; sized(target_galley) = FALSE; /* get perpendicular constraint (none if horizontal galley) */ if( dim == ROWM ) { Constrained(target, &c, 1-dim, &junk); if( !constrained(c) ) Error(19, 2, "receptive symbol %s has unconstrained width", FATAL, &fpos(target), SymName(actual(target))); debug2(DSC, DD, "Constrained( %s, 1-dim ) = %s", EchoObject(target), EchoConstraint(&c)); if( !FitsConstraint(0, 0, c) ) { debug0(DGA, D, " reject: target_galley horizontal constraint is -1"); y = nilobj; goto REJECT; } } else /* actually unused */ SetConstraint(c, MAX_FULL_LENGTH, MAX_FULL_LENGTH, MAX_FULL_LENGTH); debug1(DGA, DDD, " expanding %s", EchoObject(target)); tmp = CopyObject(target, no_fpos); Link(target_galley, tmp); env = DetachEnv(tmp); debug4(DGM, D, " external_ver(%s) = %s, external_hor(%s) = %s", SymName(actual(target)), bool(external_ver(target)), SymName(actual(target)), bool(external_hor(target))); SizeGalley(target_galley, env, external_ver(target) || external_hor(target), threaded(target), non_blocking(target_index), trigger_externs(target_index), &save_style(target), &c, whereto(hd), &dest_index, &recs, &tg_inners, enclose_obj(hd) != nilobj ? CopyObject(enclose_obj(hd), no_fpos):nilobj); debug1(DGA, DD, " SizeGalley tg_inners: %s", DebugInnersNames(tg_inners)); if( recs != nilobj ) ExpandRecursives(recs); dest = actual(dest_index); if( underline(dest) == UNDER_UNDEF ) underline(dest) = UNDER_OFF; /* verify that hd satisfies any horizontal constraint on dest */ if( dim == ROWM ) { debug1(DGA, DDD, " checking hor fit of hd in %s",SymName(actual(dest))); Constrained(dest, &c, 1-dim, &junk); debug3(DSC, DD, "Constrained( %s, %s ) = %s", EchoObject(dest), dimen(1-dim), EchoConstraint(&c)); assert( constrained(c), "AttachGalley: dest unconstrained!" ); if( !FitsConstraint(0, 0, c) ) { debug0(DGA, D, " reject: hd horizontal constraint is -1"); y = nilobj; goto REJECT; } } /* manifest and size the galley if not done yet */ if( !sized(hd) ) { debug2(DYY, D, "[ EnterErrorBlock(TRUE) (sizing galley %s into %s)", SymName(actual(hd)), SymName(whereto(hd))); EnterErrorBlock(TRUE); n1 = nilobj; Child(y, Down(hd)); env = DetachEnv(y); /*** threaded() only defined in ROWM case SizeGalley(hd, env, TRUE, threaded(dest), non_blocking(target_index), TRUE, &save_style(dest), &c, nilobj, &n1, &recs, &hd_inners); *** */ SizeGalley(hd, env, TRUE, dim == ROWM ? threaded(dest) : FALSE, non_blocking(target_index), TRUE, &save_style(dest), &c, nilobj, &n1, &recs, &hd_inners, nilobj); debug1(DGA,DD," SizeGalley hd_inners: %s", DebugInnersNames(hd_inners)); if( recs != nilobj ) ExpandRecursives(recs); if( need_precedes ) /* need an ordering constraint */ { OBJECT index1, index2; New(index1, PRECEDES); New(index2, FOLLOWS); blocked(index2) = FALSE; tmp = MakeWord(WORD, STR_EMPTY, no_fpos); Link(index1, tmp); Link(index2, tmp); Link(Up(hd_index), index1); Link(Down(hd), index2); debug0(DGA, D, " inserting PRECEDES and FOLLOWS"); } LeaveErrorBlock(TRUE); debug0(DYY, D, "] LeaveErrorBlock(TRUE) (finished sizing galley)"); } if( dim == ROWM ) { if( !FitsConstraint(back(hd, 1-dim), fwd(hd, 1-dim), c) ) { debug3(DGA, D, " reject: hd %s,%s does not fit target_galley %s", EchoLength(back(hd, 1-dim)), EchoLength(fwd(hd, 1-dim)), EchoConstraint(&c)); Error(19, 3, "too little horizontal space for galley %s at %s", WARN, &fpos(hd), SymName(actual(hd)), SymName(actual(dest))); goto REJECT; } } /* check status of first component of hd */ debug0(DGA, DDD, " now ready to attach; hd ="); ifdebug(DGA, DDD, DebugObject(hd)); for( link = Down(hd); link != hd; link = NextDown(link) ) { Child(y, link); debug1(DGA, DDD, " examining %s", EchoIndex(y)); if( type(y) == SPLIT ) Child(y, DownDim(y, dim)); switch( type(y) ) { case EXPAND_IND: case SCALE_IND: case COVER_IND: case GALL_PREC: case GALL_FOLL: case GALL_FOLL_OR_PREC: case GALL_TARG: case CROSS_PREC: case CROSS_FOLL: case CROSS_FOLL_OR_PREC: case CROSS_TARG: case PAGE_LABEL_IND: break; case PRECEDES: case UNATTACHED: if( was_sized ) { /* SizeGalley was not called, so hd_inners was not set by it */ if( hd_inners == nilobj ) New(hd_inners, ACAT); Link(hd_inners, y); } break; case RECEPTIVE: goto SUSPEND; case RECEIVING: goto SUSPEND; case FOLLOWS: Child(tmp, Down(y)); if( Up(tmp) == LastUp(tmp) ) { link = pred(link, CHILD); debug0(DGA, DD, " disposing FOLLOWS"); DisposeChild(NextDown(link)); break; } Parent(tmp, Up(tmp)); assert(type(tmp) == PRECEDES, "Attach: PRECEDES!"); switch( CheckComponentOrder(tmp, target_index) ) { case CLEAR: DeleteNode(tmp); link = pred(link, CHILD); DisposeChild(NextDown(link)); break; case PROMOTE: break; case BLOCK: debug0(DGA, DD, "CheckContraint: BLOCK"); goto SUSPEND; case CLOSE: debug0(DGA, D, " reject: CheckContraint"); goto REJECT; } break; case GAP_OBJ: underline(y) = underline(dest); if( !join(gap(y)) ) seen_nojoin(hd) = TRUE; break; case BEGIN_HEADER: case END_HEADER: case SET_HEADER: case CLEAR_HEADER: /* do nothing until actually promoted out of here */ underline(y) = underline(dest); break; case CLOSURE: case CROSS: case FORCE_CROSS: case NULL_CLOS: case PAGE_LABEL: underline(y) = underline(dest); break; case WORD: case QWORD: case ONE_COL: case ONE_ROW: case WIDE: case HIGH: case HSHIFT: case VSHIFT: case HMIRROR: case VMIRROR: case HSCALE: case VSCALE: case HCOVER: case VCOVER: case HCONTRACT: case VCONTRACT: case HLIMITED: case VLIMITED: case HEXPAND: case VEXPAND: case START_HVSPAN: case START_HSPAN: case START_VSPAN: case HSPAN: case VSPAN: case ROTATE: case BACKGROUND: case SCALE: case KERN_SHRINK: case INCGRAPHIC: case SINCGRAPHIC: case PLAIN_GRAPHIC: case GRAPHIC: case LINK_SOURCE: case LINK_DEST: case LINK_DEST_NULL: case LINK_URL: case ACAT: case HCAT: case VCAT: case ROW_THR: case COL_THR: underline(y) = underline(dest); if( dim == ROWM ) { /* make sure y is not joined to a target below (vertical only) */ for( zlink = NextDown(link); zlink != hd; zlink = NextDown(zlink) ) { Child(z, zlink); switch( type(z) ) { case RECEPTIVE: if( non_blocking(z) ) { zlink = PrevDown(zlink); DeleteNode(z); } else { y = z; goto SUSPEND; } break; case RECEIVING: if( non_blocking(z) ) { zlink = PrevDown(zlink); while( Down(z) != z ) { Child(tmp, Down(y)); if( opt_components(tmp) != nilobj ) { DisposeObject(opt_components(tmp)); opt_components(tmp) = nilobj; debug3(DOG, D, "AttachGalley(%s) de-optimizing %s %s", SymName(actual(hd)), SymName(actual(tmp)), "(join)"); } DetachGalley(tmp); KillGalley(tmp, FALSE); } DeleteNode(z); } else { y = z; goto SUSPEND; } break; case GAP_OBJ: if( !join(gap(z)) ) zlink = PrevDown(hd); break; default: break; } } /* if HCAT, try vertical hyphenation (vertical galleys only) */ if( type(y) == HCAT ) VerticalHyphenate(y); } /* check availability of parallel space for the first component */ why = nilobj; Constrained(dest, &c, dim, &why); debug3(DGF, DD, " dest parallel Constrained(%s, %s) = %s", EchoObject(dest), dimen(dim), EchoConstraint(&c)); if( !FitsConstraint(back(y, dim), fwd(y, dim), c) ) { BOOLEAN scaled; /* if forcing galley doesn't fit, try scaling first component */ scaled = FALSE; if( force_gall(hd) && size(y, dim) > 0 ) { int scale_factor; scale_factor = ScaleToConstraint(back(y,dim), fwd(y,dim), &c); if( scale_factor > 0.5 * SF ) { char num1[20], num2[20]; sprintf(num1, "%.1fc", (float) size(y, dim) / CM); sprintf(num2, "%.1fc", (float) bfc(c) / CM); if( dim == ROWM ) Error(19, 4, "%s object too high for %s space; %s inserted", WARN, &fpos(y), num1, num2, KW_SCALE); else Error(19, 5, "%s object too wide for %s space; %s inserted", WARN, &fpos(y), num1, num2, KW_SCALE); y = InterposeScale(y, scale_factor, dim); scaled = TRUE; } } /* otherwise we must reject, and warn the user */ if( !scaled ) { char num1[20], num2[20]; debug3(DGA, D, " reject: vsize %s,%s in %s; y=", EchoLength(back(y, dim)), EchoLength(fwd(y, dim)), EchoConstraint(&c)); ifdebug(DGA, D, DebugObject(y)); if( size(y, dim) > 0 ) { sprintf(num1, "%.1fc", (float) size(y, dim) / CM); sprintf(num2, "%.1fc", (float) bfc(c) / CM); if( dim == ROWM ) Error(19, 12, "%s object too high for %s space; will try elsewhere", WARN, &fpos(y), num1, num2); else Error(19, 13, "%s object too wide for %s space; will try elsewhere", WARN, &fpos(y), num1, num2); } goto REJECT; } } /* check availability of perpendicular space for first component */ if( dim == ROWM ) { perp_back = back(hd, 1-dim); perp_fwd = fwd(hd, 1-dim); } else { perp_back = back(y, 1-dim); perp_fwd = fwd(y, 1-dim); } Constrained(dest, &c, 1-dim, &junk); debug3(DGF, DD, " dest perpendicular Constrained(%s, %s) = %s", EchoObject(dest), dimen(1-dim), EchoConstraint(&c)); if( !FitsConstraint(perp_back, perp_fwd, c) ) { BOOLEAN scaled; /* if forcing galley doesn't fit, try scaling first component */ scaled = FALSE; if( force_gall(hd) && perp_back + perp_fwd > 0 ) { int scale_factor; scale_factor = ScaleToConstraint(perp_back, perp_fwd, &c); if( scale_factor > 0.5 * SF ) { char num1[20], num2[20]; sprintf(num1, "%.1fc", (float) (perp_back + perp_fwd) / CM); sprintf(num2, "%.1fc", (float) bfc(c) / CM); if( 1-dim == ROWM ) Error(19, 6, "%s object too high for %s space; %s inserted", WARN, &fpos(y), num1, num2, KW_SCALE); else Error(19, 7, "%s object too wide for %s space; %s inserted", WARN, &fpos(y), num1, num2, KW_SCALE); y = InterposeScale(y, scale_factor, 1-dim); scaled = TRUE; } } /* otherwise we must reject, and warn the user */ if( !scaled ) { debug3(DGA, D, " reject: vsize %s,%s in %s; y=", EchoLength(perp_back), EchoLength(perp_fwd), EchoConstraint(&c)); ifdebug(DGA, D, DebugObject(y)); goto REJECT; } } /* dest seems OK, so perform its size adjustments */ debug0(DSA, D, "calling AdjustSize from AttachGalley (a)"); AdjustSize(dest, back(y, dim), fwd(y, dim), dim); debug0(DSA, D, "calling AdjustSize from AttachGalley (b)"); AdjustSize(dest, perp_back, perp_fwd, 1-dim); /* now check parallel space for target_galley in target */ Constrained(target, &c, dim, &why); debug3(DGF, DD, " target parallel Constrained(%s, %s) = %s", EchoObject(target), dimen(dim), EchoConstraint(&c)); Child(z, LastDown(target_galley)); /* works in all cases? */ assert( !is_index(type(z)), "AttachGalley: is_index(z)!" ); assert( back(z, dim)>=0 && fwd(z, dim)>=0, "AttachGalley: z size!" ); if( !FitsConstraint(back(z, dim), fwd(z, dim), c) ) { BOOLEAN scaled; debug2(DGA, D, " why = %d %s", (int) why, EchoObject(why)); debug2(DGA, D, " limiter = %d %s", (int) limiter(hd), EchoObject(limiter(hd))); /* if forcing galley doesn't fit, try scaling z */ scaled = FALSE; if( force_gall(hd) && size(z, dim) > 0 && limiter(hd) != why ) { int scale_factor; scale_factor = ScaleToConstraint(back(z,dim), fwd(z,dim), &c); if( scale_factor > 0.5 * SF ) { char num1[20], num2[20]; sprintf(num1, "%.1fc", (float) size(z, dim) / CM); sprintf(num2, "%.1fc", (float) bfc(c) / CM); if( dim == ROWM ) Error(19, 8, "%s object too high for %s space; %s inserted", WARN, &fpos(y), num1, num2, KW_SCALE); else Error(19, 9, "%s object too wide for %s space; %s inserted", WARN, &fpos(y), num1, num2, KW_SCALE); z = InterposeWideOrHigh(z, dim); z = InterposeScale(z, scale_factor, dim); scaled = TRUE; } } if( !scaled ) { char num1[20], num2[20]; limiter(hd) = why; debug3(DGA, D, " set limiter(%s) = %d %s", SymName(actual(hd)), (int) limiter(hd), EchoObject(limiter(hd))); debug3(DGA, D, " reject: size was %s,%s in %s; y =", EchoLength(back(z, dim)), EchoLength(fwd(z, dim)), EchoConstraint(&c)); ifdebug(DGA, D, DebugObject(y)); if( size(z, dim) > 0 ) { sprintf(num1, "%.1fc", (float) size(z, dim) / CM); sprintf(num2, "%.1fc", (float) bfc(c) / CM); if( dim == ROWM ) Error(19, 14, "%s object too high for %s space; will try elsewhere", WARN, &fpos(y), num1, num2); else Error(19, 15, "%s object too wide for %s space; will try elsewhere", WARN, &fpos(y), num1, num2); } goto REJECT; } } limiter(hd) = why; debug3(DGA, D, " set limiter(%s) = %d %s", SymName(actual(hd)), (int) limiter(hd), EchoObject(limiter(hd))); /* now check perpendicular space for target_galley in target */ Constrained(target, &c, 1-dim, &junk); debug3(DGF, DD, " target perpendicular Constrained(%s, %s) = %s", EchoObject(target), dimen(1-dim), EchoConstraint(&c)); Child(z, LastDown(target_galley)); /* works in all cases? */ assert( !is_index(type(z)), "AttachGalley: is_index(z)!" ); assert( back(z, 1-dim)>=0 && fwd(z, 1-dim)>=0, "AttachGalley: z size (perpendicular)!" ); if( !FitsConstraint(back(z, 1-dim), fwd(z, 1-dim), c) ) { BOOLEAN scaled; /* if forcing galley doesn't fit, try scaling z */ scaled = FALSE; if( force_gall(hd) && size(z, 1-dim) > 0 ) { int scale_factor; scale_factor = ScaleToConstraint(back(z,1-dim), fwd(z,1-dim), &c); if( scale_factor > 0.5 * SF ) { char num1[20], num2[20]; sprintf(num1, "%.1fc", (float) size(z, 1-dim) / CM); sprintf(num2, "%.1fc", (float) bfc(c) / CM); if( 1-dim == ROWM ) Error(19, 10, "%s object too high for %s space; %s inserted", WARN, &fpos(y), num1, num2, KW_SCALE); else Error(19, 11, "%s object too wide for %s space; %s inserted", WARN, &fpos(y), num1, num2, KW_SCALE); z = InterposeWideOrHigh(z, 1-dim); z = InterposeScale(z, scale_factor, 1-dim); scaled = TRUE; } } if( !scaled ) { debug3(DGA, D, " reject: size was %s,%s in %s; y =", EchoLength(back(z, 1-dim)), EchoLength(fwd(z, 1-dim)), EchoConstraint(&c)); ifdebug(DGA, D, DebugObject(y)); goto REJECT; } } /* target seems OK, so adjust sizes and accept */ if( external_hor(target) ) { /* don't adjust any sizes, none to adjust */ debug0(DSA, D, "not calling AdjustSize from AttachGalley (c)"); } else if( external_ver(target) ) { /* adjust perp size only, to galley size */ debug0(DSA, D, "calling AdjustSize from AttachGalley (d)"); AdjustSize(target, back(target_galley, 1-dim), fwd(target_galley, 1-dim), 1-dim); } else { /* adjust both directions, using z (last component) */ Child(z, LastDown(target_galley)); debug0(DSA, D, "AttachGalley AdjustSize using z ="); ifdebug(DSA, D, DebugObject(z)); debug0(DSA, D, "calling AdjustSize from AttachGalley (e)"); AdjustSize(target, back(z, dim), fwd(z, dim), dim); debug0(DSA, D, "calling AdjustSize from AttachGalley (f)"); AdjustSize(target, back(z, 1-dim), fwd(z, 1-dim), 1-dim); } goto ACCEPT; default: assert1(FALSE, "AttachGalley:", Image(type(y))); break; } /* end switch */ } /* end for */ /* null galley: promote whole galley without expanding the target */ debug0(DGA, D, " null galley"); if( tg_inners != nilobj ) DisposeObject(tg_inners), tg_inners = nilobj; DisposeObject(target_galley); LeaveErrorBlock(FALSE); debug0(DYY, D, "] LeaveErrorBlock(FALSE) (null galley)"); /* kill off any null objects within the galley, then transfer it */ /* don't use Promote() since it does extra unwanted things here */ for( link = Down(hd); link != hd; link = NextDown(link) ) { Child(y, link); switch( type(y) ) { case GAP_OBJ: case CLOSURE: case CROSS: case FORCE_CROSS: case NULL_CLOS: case PAGE_LABEL: link = PrevDown(link); debug1(DGA, D, " null galley, disposing %s", Image(type(y))); DisposeChild(NextDown(link)); break; default: break; } } TransferLinks(NextDown(hd), hd, Up(target_index)); /* attach hd temporarily to target_index */ MoveLink(Up(hd), target_index, PARENT); assert( type(hd_index) == UNATTACHED, "AttachGalley: type(hd_index)!" ); DeleteNode(hd_index); /* return; only hd_inners needs to be flushed now */ *inners = hd_inners; debug0(DGA, D, "] AttachGalley returning ATTACH_NULL"); return ATTACH_NULL; REJECT: /* reject first component */ /* debug1(DGA, D, " reject %s", EchoObject(y)); */ debug0(DGA, D, " reject first component"); LeaveErrorBlock(TRUE); debug0(DYY, D, "] LeaveErrorBlock(TRUE) (REJECT)"); if( tg_inners != nilobj ) DisposeObject(tg_inners), tg_inners = nilobj; DisposeObject(target_galley); if( foll_or_prec(hd) == GALL_PREC && !sized(hd) ) { /* move to just before the failed target */ MoveLink(Up(hd_index), Up(target_index), PARENT); } else { /* move to just after the failed target */ MoveLink(Up(hd_index), NextDown(Up(target_index)), PARENT); } continue; SUSPEND: /* suspend at first component */ debug1(DGA, D, " suspend %s", EchoIndex(y)); blocked(y) = TRUE; LeaveErrorBlock(FALSE); debug0(DYY, D, "] LeaveErrorBlock(FALSE) (SUSPEND)"); if( tg_inners != nilobj ) DisposeObject(tg_inners), tg_inners = nilobj; DisposeObject(target_galley); MoveLink(Up(hd_index), Up(target_index), PARENT); if( was_sized ) { /* nothing new to flush if suspending and already sized */ if( hd_inners != nilobj ) DisposeObject(hd_inners), hd_inners=nilobj; *inners = nilobj; } else { /* flush newly discovered inners if not sized before */ *inners = hd_inners; } debug0(DGA, D, "] AttachGalley returning ATTACH_SUSPEND"); *suspend_pt = y; return ATTACH_SUSPEND; ACCEPT: /* accept first component; now committed to the attach */ debug3(DGA, D, " accept %s %s %s", Image(type(y)), EchoObject(y), EchoFilePos(&fpos(y))); LeaveErrorBlock(TRUE); debug0(DYY, D, "] LeaveErrorBlock(TRUE) (ACCEPT)"); /* attach hd to dest */ MoveLink(Up(hd), dest_index, PARENT); assert( type(hd_index) == UNATTACHED, "AttachGalley: type(hd_index)!" ); DeleteNode(hd_index); /* move first component of hd into dest */ /* nb Interpose must be done after all AdjustSize calls */ if( dim == ROWM && !external_ver(dest) ) Interpose(dest, VCAT, hd, y); else if( dim == COLM && !external_hor(dest) ) { Interpose(dest, ACAT, y, y); Parent(junk, Up(dest)); assert( type(junk) == ACAT, "AttachGalley: type(junk) != ACAT!" ); StyleCopy(save_style(junk), save_style(dest)); adjust_cat(junk) = padjust(save_style(junk)); } debug1(DGS, D, "calling Promote(hd, %s) from AttachGalley/ACCEPT", link == hd ? "hd" : "NextDown(link)"); Promote(hd, link == hd ? hd : NextDown(link), dest_index, TRUE); /* move target_galley into target */ /* nb Interpose must be done after all AdjustSize calls */ if( !(external_ver(target) || external_hor(target)) ) { Child(z, LastDown(target_galley)); Interpose(target, VCAT, z, z); } debug0(DGS, D, "calling Promote(target_galley) from AttachGalley/ACCEPT"); Promote(target_galley, target_galley, target_index, TRUE); DeleteNode(target_galley); assert(Down(target_index)==target_index, "AttachGalley: target_ind"); if( blocked(target_index) ) blocked(dest_index) = TRUE; DeleteNode(target_index); /* return; both tg_inners and hd_inners need to be flushed now; */ /* if was_sized, hd_inners contains the inners of the first component; */ /* otherwise it contains the inners of all components, from SizeGalley */ if( tg_inners == nilobj ) *inners = hd_inners; else if( hd_inners == nilobj ) *inners = tg_inners; else { TransferLinks(Down(hd_inners), hd_inners, tg_inners); DeleteNode(hd_inners); *inners = tg_inners; } debug0(DGA, D, "] AttachGalley returning ATTACH_ACCEPT"); ifdebug(DGA, D, if( dim == COLM && !external_hor(dest) ) { OBJECT z; Parent(z, Up(dest)); debug2(DGA, D, " COLM dest_encl on exit = %s %s", Image(type(z)), EchoObject(z)); } ) return ATTACH_ACCEPT; } /* end for */
bool add_edge_h(connection_t *c) { edge_t *e; node_t *from, *to; char from_name[MAX_STRING_SIZE]; char to_name[MAX_STRING_SIZE]; char to_address[MAX_STRING_SIZE]; char to_port[MAX_STRING_SIZE]; sockaddr_t address; uint32_t options; int weight; if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %x %d", from_name, to_name, to_address, to_port, &options, &weight) != 6) { logger(LOG_ERR, "Got bad %s from %s (%s)", "ADD_EDGE", c->name, c->hostname); return false; } /* Check if names are valid */ if(!check_id(from_name) || !check_id(to_name)) { logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name, c->hostname, "invalid name"); return false; } if(seen_request(c->buffer)) return true; /* Lookup nodes */ from = lookup_node(from_name); to = lookup_node(to_name); if(tunnelserver && from != myself && from != c->node && to != myself && to != c->node) { /* ignore indirect edge registrations for tunnelserver */ ifdebug(PROTOCOL) logger(LOG_WARNING, "Ignoring indirect %s from %s (%s)", "ADD_EDGE", c->name, c->hostname); return true; } if(!from) { from = new_node(); from->name = xstrdup(from_name); node_add(from); } if(!to) { to = new_node(); to->name = xstrdup(to_name); node_add(to); } /* Convert addresses */ address = str2sockaddr(to_address, to_port); /* Check if edge already exists */ e = lookup_edge(from, to); if(e) { if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) { if(from == myself) { ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry", "ADD_EDGE", c->name, c->hostname); send_add_edge(c, e); return true; } else { ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) which does not match existing entry", "ADD_EDGE", c->name, c->hostname); edge_del(e); graph(); } } else return true; } else if(from == myself) { ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for ourself which does not exist", "ADD_EDGE", c->name, c->hostname); contradicting_add_edge++; e = new_edge(); e->from = from; e->to = to; send_del_edge(c, e); free_edge(e); return true; } e = new_edge(); e->from = from; e->to = to; e->address = address; e->options = options; e->weight = weight; edge_add(e); /* Tell the rest about the new edge */ if(!tunnelserver) forward_request(c); /* Run MST before or after we tell the rest? */ graph(); return true; }
int main2(int argc, char **argv) { InitializeCriticalSection(&mutex); EnterCriticalSection(&mutex); #endif if(!detach()) return 1; #ifdef HAVE_MLOCKALL /* Lock all pages into memory if requested. * This has to be done after daemon()/fork() so it works for child. * No need to do that in parent as it's very short-lived. */ if(do_mlock && mlockall(MCL_CURRENT | MCL_FUTURE) != 0) { logger(LOG_ERR, "System call `%s' failed: %s", "mlockall", strerror(errno)); return 1; } #endif /* Setup sockets and open device. */ if(!setup_network()) goto end; /* Initiate all outgoing connections. */ try_outgoing_connections(); /* Change process priority */ char *priority = 0; if(get_config_string(lookup_config(config_tree, "ProcessPriority"), &priority)) { if(!strcasecmp(priority, "Normal")) { if (setpriority(NORMAL_PRIORITY_CLASS) != 0) { logger(LOG_ERR, "System call `%s' failed: %s", "setpriority", strerror(errno)); goto end; } } else if(!strcasecmp(priority, "Low")) { if (setpriority(BELOW_NORMAL_PRIORITY_CLASS) != 0) { logger(LOG_ERR, "System call `%s' failed: %s", "setpriority", strerror(errno)); goto end; } } else if(!strcasecmp(priority, "High")) { if (setpriority(HIGH_PRIORITY_CLASS) != 0) { logger(LOG_ERR, "System call `%s' failed: %s", "setpriority", strerror(errno)); goto end; } } else { logger(LOG_ERR, "Invalid priority `%s`!", priority); goto end; } } /* drop privileges */ if (!drop_privs()) goto end; /* Start main loop. It only exits when tinc is killed. */ status = main_loop(); /* Shutdown properly. */ ifdebug(CONNECTIONS) dump_device_stats(); close_network_connections(); end: logger(LOG_NOTICE, "Terminating"); #ifndef HAVE_MINGW remove_pid(pidfilename); #endif EVP_cleanup(); ENGINE_cleanup(); CRYPTO_cleanup_all_ex_data(); ERR_remove_state(0); ERR_free_strings(); exit_configuration(&config_tree); free_names(); return status; }
bool del_edge_h(connection_t *c) { edge_t *e; char from_name[MAX_STRING_SIZE]; char to_name[MAX_STRING_SIZE]; node_t *from, *to; if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) { logger(LOG_ERR, "Got bad %s from %s (%s)", "DEL_EDGE", c->name, c->hostname); return false; } /* Check if names are valid */ if(!check_id(from_name) || !check_id(to_name)) { logger(LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_EDGE", c->name, c->hostname, "invalid name"); return false; } if(seen_request(c->buffer)) return true; /* Lookup nodes */ from = lookup_node(from_name); to = lookup_node(to_name); if(tunnelserver && from != myself && from != c->node && to != myself && to != c->node) { /* ignore indirect edge registrations for tunnelserver */ ifdebug(PROTOCOL) logger(LOG_WARNING, "Ignoring indirect %s from %s (%s)", "DEL_EDGE", c->name, c->hostname); return true; } if(!from) { ifdebug(PROTOCOL) logger(LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree", "DEL_EDGE", c->name, c->hostname); return true; } if(!to) { ifdebug(PROTOCOL) logger(LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree", "DEL_EDGE", c->name, c->hostname); return true; } /* Check if edge exists */ e = lookup_edge(from, to); if(!e) { ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) which does not appear in the edge tree", "DEL_EDGE", c->name, c->hostname); return true; } if(e->from == myself) { ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for ourself", "DEL_EDGE", c->name, c->hostname); contradicting_del_edge++; send_add_edge(c, e); /* Send back a correction */ return true; } /* Tell the rest about the deleted edge */ if(!tunnelserver) forward_request(c); /* Delete the edge */ edge_del(e); /* Run MST before or after we tell the rest? */ graph(); /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */ if(!to->status.reachable) { e = lookup_edge(to, myself); if(e) { if(!tunnelserver) send_del_edge(broadcast, e); edge_del(e); } } return true; }
static RETSIGTYPE ignore_signal_handler(int a) { ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Ignored signal %d (%s)", a, strsignal(a)); }