/* * exchange_with_port - routine to send/recv from/to socket * Parameters: * buf -- buffer with data to send * len -- length of data * answer -- 1 (yes, I want to receive answer) and 0 (no, thanks, just send) * failures -- should be setted 0 on external calls (avoid infinite recursion) * Return value: * -1 -- something went wrong * 0 -- data successfully sent * string -- answer (caller shoud free it) */ static char * exchange_with_port(const char * buf, size_t len, int answer, char failures) { wait_for_socket(__darwintrace_fd, 1); if(send(__darwintrace_fd, buf, len, 0)==-1) { if(errno==ENOTSOCK && failures<3) { __darwintrace_fd=-1; __darwintrace_setup(); return exchange_with_port(buf, len, answer, failures+1); } return (char*)-1; } if(!answer) return 0; { size_t l=0; char * b; wait_for_socket(__darwintrace_fd, 0); recv(__darwintrace_fd, &l, sizeof(l),0); if(!l) return 0; b=(char*)malloc(l+1); b[l]=0; recv(__darwintrace_fd, b, l, 0); return b; } }
int main(int argc, char *argv[]) { optiondata opt; motparm mp; port_list * plist = &g_ports; int rv; if ( (rv = get_options(&opt, &mp, plist, argc, argv)) != 0 ) return rv; /* register interrupt trap */ signal( SIGHUP, clean_n_exit ); signal( SIGINT, clean_n_exit ); signal( SIGQUIT, clean_n_exit ); signal( SIGKILL, clean_n_exit ); signal( SIGTERM, clean_n_exit ); if ( (rv = open_incoming_socket(&opt, plist)) < 0 ) return rv; while (1) /* run until interrupt or error (consider restart?) */ { mp.nread = 0; /* reset our counter */ /* wait for AFNI to talk to us */ if ( (rv = wait_for_socket(&opt, plist, &mp)) < 0 ) { clean_n_exit(0); return rv; } if ( ! opt.no_serial ) if ( (rv = open_serial(&opt, plist)) != 0 ) return rv; /* read data while it is there */ while ( (rv = read_socket(&opt, plist, &mp)) == 0) if ( ! opt.no_serial ) send_serial(&opt, plist, &mp); close_data_ports(plist); } return 0; /* should not be reached, of course */ }
STATIC gchar* post_rest_form( CURL *curl, const form_field_t *form ) { gchar *url_host; gchar *url_path; gchar *headers; struct curl_write_buffer_t body = { .data = NULL, .size = 0 }; gchar content_length[ 10 ]; gchar *html_post; GRegex *url_split_regex; GMatchInfo *match_info; long socket_value; curl_socket_t socket; size_t bytes_sent; char response_buffer[ 1024 ]; CURLcode status; size_t bytes_received; gboolean read_error; /* Split the form action URL into host and path. */ url_split_regex = g_regex_new( "^http[s?]://([a-zA-Z0-9_\\.-]+)/(.*)$", 0, 0, NULL ); if( g_regex_match( url_split_regex, form_action, 0, &match_info ) ) { url_host = g_match_info_fetch( match_info, 1 ); url_path = g_match_info_fetch( match_info, 2 ); g_match_info_free( match_info ); g_regex_unref( url_split_regex ); /* Build the HTML post. */ if( url_host != NULL ) { /* Build the body. */ if( form_name != NULL ) { body.data = g_strconcat( form_name, "=", form_value, NULL ); body.size = strlen( body.data ); } g_slist_foreach( form->input_fields, fill_body_with_input, &body ); /* Build the headers. */ g_sprintf( content_length, "%d\n\0", (gint) body.size ); headers = g_strconcat( "POST ", url_path, " HTTP/1.1\n", "Host: ", url_host, "\n", "Content-Type: application/x-www-form-urlencoded\n", "Content-Length: ", content_length, NULL ); g_free( url_path ); g_free( url_host ); /* Combine the headers and the body. */ html_post = g_strconcat( headers, "\n", body.data, NULL ); g_free( body.data ); g_free( headers ); printf( "DEBUG: Sending:\n-------\n%s-------", html_post ); /* Prepare a connection. */ curl_easy_setopt( curl, CURLOPT_URL, form_action ); curl_easy_setopt( curl, CURLOPT_CONNECT_ONLY, 1 ); curl_easy_perform( curl ); curl_easy_getinfo( curl, CURLINFO_LASTSOCKET, &socket_value ); socket = socket_value; /* Wait for the socket to become ready for sending. */ if( wait_for_socket( socket, FALSE, 60000L ) == FALSE ) { g_free( html_post ); g_fprintf( stderr, "Timeout connecting to " "the Google REST service\n" ); } else { /* Submit the post. */ curl_easy_send( curl, html_post, strlen( html_post ), &bytes_sent ); /* To do: verify that all the bytes were sent; if not, send the remainding bytes in a second, third, ... call. */ printf( "DEBUG: Sent %d out of %d bytes\n", (int)bytes_sent, (int) strlen(html_post) ); g_free( html_post ); /* Receive the response. */ read_error = FALSE; do { /* Wait for the socket to become ready for receiving. */ if( wait_for_socket( socket, TRUE, 60000L ) == FALSE ) { read_error = TRUE; g_fprintf( stderr, "Timeout connecting to " "the Google REST service\n" ); break; } /* Copy chunks of data into html_response. */ else { status = curl_easy_recv( curl, response_buffer, sizeof( response_buffer ), &bytes_received ); if( status == CURLE_OK ) { receive_curl_response( response_buffer, bytes_received, 1, &html_response ); } } } while( ( status == CURLE_AGAIN ) && ( read_error == FALSE ) ); printf( "DEBUG: CURL code %d: %s\n", (int)status, curl_easy_strerror( status ) ); } } } } #endif #if 0 /** * Auxiliary function used by decode_user_code_response to copy a JSON * response into a local data structure. * @param node [in] Object with JSON data (unused). * @param member_name [in] Name of the JSON field. * @param member_node [in] Node with JSON data. * @param user_code_ptr [out] Pointer to the structure where the JSON * data will be stored. * @return Nothing. */ STATIC void parse_user_code_json( const gchar *member_name, JsonNode *member_node, gpointer user_code_ptr ) { struct user_code_t *user_code = user_code_ptr; SET_JSON_MEMBER( user_code, device_code ); SET_JSON_MEMBER( user_code, verification_url ); SET_JSON_MEMBER( user_code, user_code ); } #endif /** * Decode the initial OAuth2 response with device code, user code, and * verification URL. * @param user_code_response [in] Raw JSON response data. * @return \a user_code_t structure with authentication data, or \a FALSE * if the user code could not be obtained. */ #if 0 STATIC struct user_code_t* decode_user_code_response( const gchar *user_code_response ) { struct user_code_t *user_code; user_code = g_new0( struct user_code_t, 1 ); if( user_code_response != NULL ) { /* Decode the JSON response. */ decode_json_reply( user_code_response, parse_user_code_json, user_code ); /* Verify that all fields have been decoded. */ if( ( user_code->device_code == NULL ) || ( user_code->user_code == NULL ) || ( user_code->verification_url == NULL ) ) { g_free( (gchar*) user_code->device_code ); g_free( (gchar*) user_code->user_code ); g_free( (gchar*) user_code->verification_url ); g_free( user_code ); user_code = NULL; } } return( user_code ); } #endif /** * Obtain a device code for device access. The CURL session must include the * user's login to Google, which may be obtained by calling \a login_to_gmail * with the user's credentials. * @param curl [in] CURL handle. * @param client_id [in] Google Developer Client ID. * @return Device code or \a NULL if an error occurred. * Test: manual (via non-automated test). */ struct user_code_t* obtain_device_code( CURL *curl, const gchar *client_id ) { /* Note: the following code is more elegant than the code that is currently used but is disabled because a bug in Google's API system prevents devices from using the tasks scope. */ #if 0 static gchar *form_action = "https://accounts.google.com/o/oauth2/device/code"; gchar *scope = // "https://www.googleapis.com/auth/userinfo.email " // "https://www.googleapis.com/auth/userinfo.profile"; "https://www.googleapis.com/auth/tasks"; form_field_t form = { .name = NULL, .value = NULL, .action = form_action, .input_fields = NULL }; gchar *user_code_response; struct user_code_t *user_code; gboolean success; /* Create a user code request. */ add_input_to_form( &form, "client_id", client_id ); add_input_to_form( &form, "scope", scope ); /* Post the request and decode the response. */ user_code_response = post_form( curl, &form ); destroy_form_inputs( &form ); g_printf( "%s\n", user_code_response ); user_code = decode_user_code_response( user_code_response ); return( user_code ); #endif } STATIC gboolean submit_approval_form( CURL *curl, const gchar *approval_page ) { form_field_t *approval_form; const gchar *const approval_names[ ] = { "submit_access", NULL }; gchar *form_response; gboolean success; approval_form = find_form( approval_page, "https://accounts.google.com/o/oauth2/", approval_names ); if( approval_form != NULL ) { g_printf( "Submitting approval form\n" ); /* Submit the approval form. */ form_response = post_form( curl, approval_form ); destroy_form( approval_form ); g_free( form_response ); success = TRUE; } else { success = FALSE; } return( success ); }
int read_do_udp (xmmsc_vis_udp_t *t, xmmsc_visualization_t *v, short *buffer, int drawtime, unsigned int blocking) { int old; int ret; int i, size; xmmsc_vis_udp_data_t packet_d; char* packet = packet_init_data (&packet_d); xmmsc_vischunk_t data; if (blocking) { wait_for_socket (t, blocking); } ret = recv (t->socket[0], packet, packet_d.size, 0); if ((ret > 0) && (*packet_d.__unaligned_type == 'V')) { uint16_t grace; struct timeval rtv; XMMSC_VIS_UNALIGNED_READ (data, packet_d.__unaligned_data, xmmsc_vischunk_t); /* resync connection */ XMMSC_VIS_UNALIGNED_READ (grace, packet_d.__unaligned_grace, uint16_t); grace = ntohs (grace); if (grace < 1000) { if (t->grace != 0) { t->grace = 0; /* use second socket here, so vis packets don't get lost */ t->timediff = udp_timediff (v->id, t->socket[1]); } } else { t->grace = grace; } /* include the measured time difference */ rtv.tv_sec = ntohl (data.timestamp[0]); rtv.tv_usec = ntohl (data.timestamp[1]); double interim = tv2ts (&rtv); interim -= t->timediff; ts2net (data.timestamp, interim); ret = 1; } else { if (ret == 1 && *packet_d.__unaligned_type == 'K') { ret = -1; } else if (ret > -1 || xmms_socket_error_recoverable ()) { ret = 0; } else { ret = -1; } free (packet); return ret; } old = check_drawtime (net2ts (data.timestamp), drawtime); if (!old) { size = ntohs (data.size); for (i = 0; i < size; ++i) { buffer[i] = (int16_t)ntohs (data.data[i]); } } free (packet); if (!old) { return size; } return 0; }