int Proxy_connect(Proxy *conn) { Node *rhdr = NULL, *rmsg = NULL; Node *imsg = NULL; bstring header = NULL; int rc = 0; assert_mem(conn); CryptState_init(); // create the peer crypto we need conn->state = CryptState_create(conn->client.name, conn->client.key); check_err(conn->state, CRYPTO, "Failed to initialize cryptography subsystem."); conn->state->data = conn; log(INFO, "Connecting to %s:%s as %s expecting server named %s", bdata(conn->host.host), bdata(conn->host.port), bdata(conn->client.name), bdata(conn->host.name)); // connect to the hub conn->host.fd = tcp_client((char *)bdata(conn->host.host), (char *)bdata(conn->host.port)); check_err(conn->host.fd >= 0, NET, "Failed to connect to Hub."); rmsg = readnodes(conn->host.fd, &rhdr); check_err(!rmsg && rhdr, PEER_ESTABLISH, "Incorrectly formatted initial message from Hub."); // generate first response imsg = CryptState_initiator_start(conn->state, rhdr, &header, Proxy_confirm_key); Node_destroy(rhdr); rhdr = NULL; check_err(imsg, PEER_ESTABLISH, "Failed to construct response to Hub's first message."); rc = write_header_node(conn->host.fd, header, imsg); Node_destroy(imsg); imsg = NULL; bdestroy(header); header = NULL; check_err(rc > 0, PEER_ESTABLISH, "Failed sending response to Hub's first message."); rmsg = readnodes(conn->host.fd, &rhdr); check_err(rmsg && rhdr, PEER_ESTABLISH, "Invalid response from hub."); header = Node_bstr(rhdr, 1); Node_destroy(rhdr); rhdr = NULL; rc = CryptState_initiator_process_response(conn->state, header, rmsg); check_err(rc, PEER_ESTABLISH, "Failed to process receiver 2nd message."); Node_destroy(rmsg); bdestroy(header); header = NULL; rmsg = NULL; imsg = CryptState_initiator_send_final(conn->state, &header); check_err(imsg, PEER_ESTABLISH, "Failed to generate final message."); rc = write_header_node(conn->host.fd, header, imsg); check_err(rc > 0, PEER_ESTABLISH, "Failed to write final message to hub."); check_err(Proxy_final_verify(conn), PEER_VERIFY, "Final verify of Hub info failed."); // remember, rc>0 is how we make sure that everything went ok during processing ensure(if(imsg) Node_destroy(imsg); if(rmsg) Node_destroy(rmsg); if(rhdr) Node_destroy(rhdr); if(header) bdestroy(header); return rc > 0); }
Node *Proxy_hub_recv(Proxy *conn, Node **header) { Node *msg = NULL, *packet = NULL; bstring hdata = NULL; assert_mem(header); assert_mem(conn); packet = readnodes(conn->host.fd, header); check_err(packet && *header, HUB_IO, "Failed to read hub message."); hdata = Node_bstr(*header, 1); check_err(hdata, STACKISH, "Failed to convert stackish header from hub into a string for decrypt."); msg = CryptState_decrypt_node(conn->state, &conn->state->me.skey, hdata, packet); check_err(msg, CRYPTO, "Failed to decrypt payload from hub."); ensure(Node_destroy(packet); bdestroy(hdata); return msg); }
int Proxy_configure(Proxy *conn) { int rc = 0; Node *host = NULL; Node *client = NULL; // read the configuration stackish from the client log(INFO, "waiting for initial config"); host = readnodes(conn->client.read_fd, &client); check_err(host && client, CONFIG, "Received an invalid host and client configuration."); rc = Node_decons(host, 1, "[bsssw", &conn->host.key, &conn->host.name, &conn->host.port, &conn->host.host, "host"); check_err(rc, CONFIG, "Invalid configuration format for host: [sssbw"); rc = Node_decons(client, 1, "[bsw", &conn->client.key, &conn->client.name, "client"); check_err(rc, CONFIG, "Invalid configuration format for client: [sbw"); // set any keys that are 0 length to NULL so they are generated if(blength(conn->host.key) == 0) { log(WARN, "No expected host key given, will accept anything."); bdestroy(conn->host.key); conn->host.key = NULL; } if(blength(conn->client.key) == 0) { log(WARN, "No client key given, will generate one."); bdestroy(conn->client.key); conn->client.key = NULL; } ensure(if(host) Node_destroy(host); if(client) Node_destroy(client); return rc); }
/*============================================================================*/ int main (int argc, char **argv) { /* printf("argc=%d\n", argc); */ #ifdef PROOL if(argc==3) { exitif(strlen(argv[1])>195, "unical: sorry input file name too long.", NULL); strcpy(inname, argv[1]); exitif(strlen(argv[1])>195, "unical: sorry output file name too long.", NULL); strcpy(outname, argv[2]); printf("unical1, Copyright(C) 2011 Bernhardi \n"); printf("some modif. by prool, 2015. www.prool.kharkov.org\n"); printf("unical comes with ABSOLUTELY NO WARRANTY. This is free\n"); printf("software, and you are welcome to redistribute it under\n"); printf("certain conditions, see http://www.gnu.org/licenses/gpl.html\n\n"); } else { printf("unical: usage: unical inputfile outputfile\n"); exit(0); } #else if(argc==2) { exitif(strlen(argv[1])>195, "unical: sorry file name too long.", NULL); strcpy(problem, argv[1]); full=0; } else if(argc==3) { exitif(strncmp(argv[1],"-f",2)!=0, "unical: command not understood.", NULL); exitif(strlen(argv[2])>195, "unical: sorry file name too long.", NULL); strcpy(problem, argv[2]); full=1; } else { printf("unical: usage: unical [-f] unv_filename (without extension .unv)\n"); exit(0); } printf("unical1, Copyright(C) 2011 Bernhardi \n"); printf("unical comes with ABSOLUTELY NO WARRANTY. This is free\n"); printf("software, and you are welcome to redistribute it under\n"); printf("certain conditions, see http://www.gnu.org/licenses/gpl.html\n\n"); strcpy(inname, problem); strcat(inname, ".unv"); #endif #ifdef PROOL process_e(inname); // convert exponent letter D+00 to E+00 strcpy(inname,TMPNAME); #endif printf("unical: Reading from file %s\n",inname); fid = fopen(inname,"r"); exitif(fid==0,"Error reading from file ",inname); countuni(); /* determine number of elements, nodes, etc. */ printf("unical: maxnode=%d, sumele=%d\n", maxnode, sumele); allocate(); /* allocate large arrays. */ rewind(fid); readnodes(); /* read nodes: coordinates. */ rewind(fid); readelements(); /* read element connectivities. */ rewind(fid); readgroups(); /* read group information. */ /* close universal file */ printf("unical: closing universal file %s\n", inname); ret = fclose(fid); exitif(ret < 0,"while attempting to close file ",inname); #ifndef PROOL strcpy(outname, problem); strcat(outname, ".inp"); #endif printf("unical: writing to file %s\n",outname); fid = fopen(outname,"w"); exitif(fid==0,"Error opening file ",outname); writemesh(); /* write node point and element data to abaqus/calculix input file. */ surface(); /* process groups for boundary conditions. */ #ifndef PROOL /* write standard ABAQUS procedure calls: *frequency step. */ fprintf(fid,"*material, name=steel\n"); fprintf(fid,"*elastic\n"); fprintf(fid,"209000.0, 0.3\n"); fprintf(fid,"*density\n"); fprintf(fid,"7.85e-09\n"); fprintf(fid,"*step\n"); fprintf(fid,"*frequency\n"); fprintf(fid,"12\n"); fprintf(fid,"*nodefile\n"); fprintf(fid,"u\n"); fprintf(fid,"*elfile, output=3D\n"); fprintf(fid,"s, e\n"); fprintf(fid,"*endstep\n"); #endif /* close abaqus/calculix input file */ printf("unical: closing file %s\n", outname); ret = fclose(fid); exitif(ret < 0,"while attempting to close file ",outname); printf("unical: It was a pleasure serving you. Bye. \n"); printf("unical: returning to system. \n"); #ifdef PROOL if (remove(TMPNAME)) {printf("can't delete tmp file `%s'\n",TMPNAME); exit(1);} #endif exit(0); }
Node *Proxy_listener_recv(Proxy *conn, Node **header) { Node *body = readnodes(conn->client.read_fd, header); return body; }