void write_file(FILE *fp, int size) { TLS_ISSUES tls_issues = {0}; TLS_CONTEXT ctx; int sock, posFile = 0; printf("tls_client connecting\n"); sock = init_tls_client(tls_issues, &ctx); printf("tls_client initialized\n"); if (sock < 0) exit (1); tls_setblocking(sock, 0); rewind(fp); while (posFile < size) { int posBuffer = 0; char buf[150000]; while (posBuffer < sizeof(buf)) { int bytes; bytes = read(fileno(fp), buf + posBuffer, sizeof(buf)); if (bytes == 0) break; if (bytes < 0) { perror("read"); } else { printf("writing %d buffer\n", bytes); tls_write_buffer(ctx, buf, bytes); printf("wrote %d buffer\n", bytes); posBuffer += bytes; posFile += bytes; } if (posFile >= size) break; } } printf("wrote total %d bytes\n", posFile); }
int main (int argc, char *argv []) { /* main for osdp-tcp-client */ int c1; int done_tls; fd_set exceptfds; fd_set readfds; int nfds; const sigset_t sigmask; int status; int status_io; int status_sock; int status_tls; struct timespec timeout; int tls_current_length; fd_set writefds; int ufd; status = ST_OK; status = initialize (&config, argc, argv); if (status EQUALS ST_OK) { status = local_socket_setup (&ufd); }; if (status EQUALS ST_OK) { done_tls = 0; // assume not done unless some bad status status = init_tls_client (); // for "phase 1" authentication, kludge it by sending a passphrase // send the passphrase to authenticate status = send_osdp_data (&context, (unsigned char *)specified_passphrase, plmax); if (status != ST_OK) done_tls = 1; if (status EQUALS 0) { request_immediate_poll = 0; while (!done_tls) { fflush (stdout); fflush (stderr); fflush (context.log); { // look for file descriptor activity nfds = 0; FD_ZERO (&readfds); FD_ZERO (&writefds); FD_ZERO (&exceptfds); FD_SET (ufd, &readfds); FD_SET (current_sd, &readfds); nfds = ufd+1; if (current_sd > ufd) nfds = current_sd + 1; timeout.tv_sec = 0; timeout.tv_nsec = 100000000; status_sock = pselect (nfds, &readfds, &writefds, &exceptfds, &timeout, &sigmask); if (status_sock > 0) { // check for command input (unix socket activity pokes us to check) if (FD_ISSET (ufd, &readfds)) { char cmdbuf [2]; char gratuitous_data [2] = {C_OSDP_MARK, 0x00};; /* send a benign "message" up the line so that the other knows we're active. If the othere end is the CP this will motivate it to generate an osdp_POLL. */ status = send_osdp_data (&context, (unsigned char *)gratuitous_data, 1); if (status != ST_OK) done_tls = 1; c1 = accept (ufd, NULL, NULL); if (c1 != -1) { status_io = read (c1, cmdbuf, sizeof (cmdbuf)); if (status_io > 0) { fprintf (stderr, "cmd buf %02x%02x\n", cmdbuf [0], cmdbuf [1]); close (c1); status = process_current_command (); if (status EQUALS ST_OK) preserve_current_command (); status = ST_OK; }; }; }; if (FD_ISSET (current_sd, &readfds)) { status = read_tcp_stream (&context, current_sd, &request_immediate_poll); }; }; // idle processing if (status_sock EQUALS 0) { if ((context.role EQUALS OSDP_ROLE_CP) && context.authenticated) { if (osdp_timeout (&context, &last_time_check) || request_immediate_poll) { status = background (&context); request_immediate_poll = 0; }; }; }; } if (0) { status = ST_OK; // assume tls read was ok for starters tls_current_length = status_tls; if (status_tls EQUALS 0) status = ST_OSDP_TLS_CLOSED; if (status_tls < 0) status = ST_OSDP_TLS_ERROR; if (status EQUALS ST_OK) { if (context.verbosity > 4) fprintf (stderr, "%d bytes received via TLS:\n", status_tls); // append buffer to osdp buffer if (context.authenticated) { // while first not SOM skip until SOM int i; int done; int current_length; i = 0; current_length = tls_current_length; done = 0; while (!done) { if (buffer [i] != C_SOM) { i++; current_length --; } else { memcpy (osdp_buf.buf + osdp_buf.next, buffer+i, current_length); osdp_buf.next = osdp_buf.next + current_length; status = ST_NET_INPUT_READY; done = 1; }; if (i EQUALS tls_current_length) done = 1; } }; } }; if (status != ST_OK) { if (status != ST_NET_INPUT_READY) { fprintf (stderr, "status %d\n", status); done_tls = 1; }; }; // if there was input, process the message if (status EQUALS ST_NET_INPUT_READY) { if (status != ST_OK) status = process_osdp_input (&osdp_buf); // if it's too short so far it'll be 'serial_in' so ignore that if (status EQUALS ST_SERIAL_IN) status = ST_OK; }; if (status != ST_OK) { done_tls = 1; }; } /* not done dls */; } /* tls initialized */; }; if (status != ST_OK) fprintf (stderr, "osdp-tls return status %d\n", status); return (status); } /* main for osdp-net-client */