eagi_proxy.c File Reference

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <pthread.h>

Include dependency graph for eagi_proxy.c:

Go to the source code of this file.

Defines

#define BUFSIZE   1024
#define COMMAND_PORT   8417
#define SEND_ENVIORNMENT
#define SIGNAL_PORT   8418
#define WINBUF_NUM   2400
#define WINSIZE   400

Functions

int connect_to_host (char *host, int port)
void finalize ()
int main ()
void read_full (int file, char *buffer, int num)
int read_some (int file, char *buffer, int size)
void * readSignal (void *ptr)
void * readStdin (void *ptr)
void setnonblocking (int desc)
int write_amap (int file, char *buffer, int num)
void write_buf (int file, char *buffer, int num)

Variables

char * be
char * bs
char buf [BUFSIZE]
int command_desc
pthread_mutex_t command_mutex
char connected = 1
char * end
pthread_t signal_thread
int speech_desc
pthread_t stdin_thread
char * winbuf
char window [WINSIZE]


Define Documentation

#define BUFSIZE   1024

Definition at line 65 of file eagi_proxy.c.

Referenced by main(), and readStdin().

#define COMMAND_PORT   8417

Definition at line 60 of file eagi_proxy.c.

Referenced by main().

#define SEND_ENVIORNMENT

Definition at line 61 of file eagi_proxy.c.

#define SIGNAL_PORT   8418

Definition at line 59 of file eagi_proxy.c.

Referenced by main().

#define WINBUF_NUM   2400

Definition at line 71 of file eagi_proxy.c.

Referenced by main().

#define WINSIZE   400

Definition at line 68 of file eagi_proxy.c.

Referenced by main(), and readSignal().


Function Documentation

int connect_to_host ( char *  host,
int  port 
)

Definition at line 217 of file eagi_proxy.c.

References desc, EXIT_FAILURE, and gethostbyname.

Referenced by main().

00218 {
00219    int address;
00220    struct hostent* host_entity;
00221    int res,desc;
00222    int opts;
00223    struct sockaddr_in host;
00224    
00225 
00226    /* get address */ 
00227    if(!strcmp(name,"localhost"))
00228       address=htonl(2130706433); /*127.0.0.1*/
00229    else
00230    {
00231       address=inet_addr(name); /* check if it's an IP that's written in the string */
00232       if(address==(in_addr_t)-1)
00233       {
00234          host_entity = gethostbyname(name); /* search for the host under this name */
00235    
00236          if(!host_entity)
00237          {
00238             fprintf(stderr,"EAGI proxy: Wrong address!\n"); /* can't find anything*/
00239             return -1;
00240          }
00241          address=*((int*)host_entity->h_addr);
00242       }
00243    }
00244 
00245    desc=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
00246    if(desc<0)
00247    {
00248       fprintf(stderr,"EAGI proxy: Cannot create socket!\n");
00249       return -1;
00250    }
00251 
00252    memset((void*)&host,0,sizeof(struct sockaddr_in));
00253    
00254    host.sin_family=AF_INET;
00255    host.sin_port=htons(port);
00256    host.sin_addr.s_addr=address;
00257    
00258    res=connect(desc,(struct sockaddr*)&host,sizeof(host));
00259    if(res<0)
00260    {
00261       fprintf(stderr,"EAGI proxy: Cannot connect!\n");
00262       return -1;
00263    }
00264 
00265    /* set to non-blocking mode */
00266    opts = fcntl(desc,F_GETFL);
00267    if (opts < 0) {
00268       perror("fcntl(F_GETFL)");
00269       exit(EXIT_FAILURE);
00270    }
00271    opts = (opts | O_NONBLOCK);
00272    if (fcntl(desc,F_SETFL,opts) < 0) {
00273       perror("fcntl(F_SETFL)");
00274       exit(EXIT_FAILURE);
00275    }
00276 
00277 
00278    return desc;
00279 }

void finalize (  ) 

Definition at line 154 of file eagi_proxy.c.

References command_desc, free, speech_desc, and winbuf.

Referenced by main().

00155 {
00156    close(command_desc);
00157    close(speech_desc);
00158    free(winbuf);  
00159 }

int main (  ) 

Definition at line 107 of file eagi_proxy.c.

References be, bs, buf, BUFSIZE, command_desc, command_mutex, COMMAND_PORT, connect_to_host(), connected, end, finalize(), malloc, NULL, pthread_create, pthread_mutex_init, pthread_mutex_lock, pthread_mutex_unlock, read_some(), readSignal(), readStdin(), SIGNAL_PORT, signal_thread, speech_desc, stdin_thread, winbuf, WINBUF_NUM, and WINSIZE.

00108 {
00109    int ret;
00110    
00111    atexit(finalize);
00112 
00113    setlinebuf(stdin);
00114    setlinebuf(stdout);
00115 
00116    winbuf=(char*)malloc(WINSIZE*WINBUF_NUM);
00117    end=winbuf+WINSIZE*WINBUF_NUM;
00118    bs=be=winbuf;
00119 
00120    speech_desc=connect_to_host("localhost",SIGNAL_PORT);
00121    if(speech_desc<0) 
00122    {
00123       perror("signal socket");
00124       return -1;
00125    }
00126 
00127 
00128    command_desc=connect_to_host("localhost",COMMAND_PORT);
00129    if(command_desc<0) 
00130    {
00131       perror("command socket");
00132       return -1;
00133    }
00134 
00135    pthread_mutex_init(&command_mutex,NULL);
00136    pthread_create(&stdin_thread,NULL,readStdin,NULL);
00137    pthread_create(&signal_thread,NULL,readSignal,NULL);
00138 
00139    while(connected)
00140    {  
00141       pthread_mutex_lock(&command_mutex);
00142       ret=read_some(command_desc,buf,BUFSIZE);
00143       pthread_mutex_unlock(&command_mutex);
00144       if(ret>0)
00145       {
00146          buf[ret]=0;
00147          printf("%s",buf);       
00148       }
00149    }
00150 
00151    return 0;
00152 }

void read_full ( int  file,
char *  buffer,
int  num 
)

Definition at line 200 of file eagi_proxy.c.

References connected, and errno.

Referenced by readSignal().

00201 {
00202    int count,pos=0;
00203 
00204    while(num)
00205    {
00206       count=read(file,buffer+pos,num);
00207       if(count==0 || (count<0 && errno!=EAGAIN))
00208       {
00209          connected=0;
00210          return;
00211       }
00212       num-=count;
00213       pos+=count;
00214    }
00215 }

int read_some ( int  file,
char *  buffer,
int  size 
)

Definition at line 281 of file eagi_proxy.c.

References c, connected, and errno.

Referenced by main().

00282 {
00283    unsigned char c;
00284    int res,i=0;
00285 
00286    for(;;)
00287    {
00288       res=read(desc,&c,1);
00289       if(res<1)
00290       {
00291          if(errno!=EAGAIN)
00292          {
00293             perror("Error reading");
00294             connected=0;
00295          }  
00296          break;
00297       }
00298       if(res==0) 
00299       {
00300          connected=0;
00301          break;
00302       }
00303       
00304       buffer[i]=c;
00305       i++;
00306    }
00307 
00308    return i;
00309 }

void * readSignal ( void *  ptr  ) 

Definition at line 188 of file eagi_proxy.c.

References connected, NULL, read_full(), speech_desc, window, WINSIZE, and write_buf().

Referenced by main().

00189 {
00190    while(connected)
00191    {
00192       read_full(3,window,WINSIZE);
00193       write_buf(speech_desc,window,WINSIZE);
00194    }
00195 
00196    pthread_exit(NULL);
00197 }

void * readStdin ( void *  ptr  ) 

Definition at line 161 of file eagi_proxy.c.

References buf, BUFSIZE, command_desc, command_mutex, connected, NULL, pthread_mutex_lock, pthread_mutex_unlock, and write_buf().

Referenced by main().

00162 {
00163    while(1)/*read enviornment*/
00164    {
00165       fgets(buf,BUFSIZE,stdin);
00166       #ifdef SEND_ENVIORNMENT
00167          pthread_mutex_lock(&command_mutex);
00168          write_buf(command_desc,buf,strlen(buf));
00169          pthread_mutex_unlock(&command_mutex);
00170       #endif
00171       if(feof(stdin) || buf[0]=='\n') 
00172       {
00173          break;
00174       }
00175    }
00176 
00177    while(connected)
00178    {
00179       fgets(buf,BUFSIZE,stdin);
00180       pthread_mutex_lock(&command_mutex);
00181       write_buf(command_desc,buf,strlen(buf));
00182       pthread_mutex_unlock(&command_mutex);
00183    }
00184 
00185    pthread_exit(NULL);
00186 }

void setnonblocking ( int  desc  ) 

Definition at line 402 of file eagi_proxy.c.

00403 {
00404    int opts;
00405    
00406    opts = fcntl(desc,F_GETFL);
00407    if(opts < 0)
00408    {
00409       perror("fcntl(F_GETFL)");
00410       exit(-1);
00411    }
00412 
00413    opts = (opts | O_NONBLOCK );
00414    if(fcntl(desc,F_SETFL,opts) < 0)
00415    {
00416       perror("fcntl(F_SETFL)");
00417       exit(-1);
00418    }
00419 }

int write_amap ( int  file,
char *  buffer,
int  num 
)

Definition at line 382 of file eagi_proxy.c.

References connected, and errno.

Referenced by write_buf().

00383 {
00384    int ret;
00385    ret=write(desc,buf,size);
00386    if(ret<0)
00387    {
00388       if(errno!=EAGAIN) 
00389       {
00390          perror("Error writing");
00391          connected=0;
00392       }
00393       return 0;
00394    }
00395    if(ret==0)
00396       connected=0;
00397 
00398    return ret;
00399 }

void write_buf ( int  file,
char *  buffer,
int  num 
)

Definition at line 312 of file eagi_proxy.c.

References be, bs, end, winbuf, and write_amap().

Referenced by AST_TEST_DEFINE(), audiohook_read_frame_both(), readSignal(), and readStdin().

00313 {
00314    int ret;
00315 
00316    /*NOTE: AMAP -> as much as possible */
00317 
00318    if(be!=bs)/* if data left in buffer */
00319    {
00320       if(be>bs)/* if buffer not split */
00321       {
00322          ret=write_amap(desc,bs,be-bs);/* write AMAP */
00323          bs+=ret;/* shift the start of the buffer */
00324       }
00325       else/* if buffer is split */
00326       {
00327          ret=write_amap(desc,bs,end-bs);/* write higher part first */
00328          if(ret==end-bs)/* if wrote whole of the higher part */
00329          {
00330             ret=write_amap(desc,winbuf,be-winbuf);/* write lower part */
00331             bs=winbuf+ret;/* shift start to new position */
00332          }
00333          else bs+=ret;/* if not wrote whole of higher part, only shift start */
00334       }
00335    }
00336 
00337    if(be==bs)/* if buffer is empty now */
00338    {
00339       ret=write_amap(desc,buf,size);/* write AMAP of the new data */
00340       buf+=ret;/* shift start of new data */
00341       size-=ret;/* lower size of new data */
00342    }
00343 
00344    if(size)/* if new data still remains unsent */
00345    {
00346       if(be>=bs)/* if data not split */
00347       {
00348          if(size>end-be)/* if new data size doesn't fit higher end */
00349          {
00350             size-=end-be;/* reduce new data size by the higher end size */
00351             memcpy(be,buf,end-be);/* copy to higher end */
00352             be=winbuf;/* shift end to begining of buffer */
00353             buf+=end-be;/* shift start of new data */
00354          }
00355          else/* if new data fits the higher end */
00356          {
00357             memcpy(be,buf,size);/* copy to higher end */
00358             be+=size;/* shift end by size */
00359             if(be>=end)/* if end goes beyond the buffer */
00360                be=winbuf;/* restart */
00361             size=0;/* everything copied */
00362          }
00363       }
00364 
00365       if(size)/* if new data still remains */
00366       {
00367          if(size>=bs-be)/* if new data doesn't fit between end and start */
00368          {
00369             fprintf(stderr,"Buffer overflow!\n");
00370             size=bs-be-1;/* reduce the size that we can copy */
00371          }
00372 
00373          if(size)/* if we can copy anything */
00374          {
00375             memcpy(be,buf,size);/* copy the new data between end and start */
00376             be+=size;/* shift end by size */
00377          }
00378       }
00379    }
00380 }


Variable Documentation

char * be

Definition at line 73 of file eagi_proxy.c.

Referenced by hashtab_compare_strings(), main(), and write_buf().

char * bs

char buf[BUFSIZE]

Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 66 of file eagi_proxy.c.

Referenced by __adsi_transmit_messages(), __ast_manager_event_multichan(), __ast_verbose_ap(), __transmit_response(), _sip_tcp_helper_thread(), acf_odbc_write(), action_add_agi_cmd(), action_bridge(), action_command(), action_redirect(), active_channels_to_str_cb(), add_exten_to_pattern_tree(), add_menu_entry(), adsi_begin_download(), adsi_end_download(), adsi_get_cpeid(), adsi_get_cpeinfo(), adsi_load(), adsi_print(), aelsub_exec(), agi_exec_full(), allocate_tdata_buffer(), alsa_read(), ami_outbound_registration_task(), ami_registrations_aor(), ami_subscription_detail(), announce_to_dial(), anti_injection(), app_exec(), append_history_va(), apply_outgoing(), ast_agi_send(), ast_cli(), ast_cli_generatornummatches(), ast_compile_ael2(), ast_device_state_changed(), ast_devstate_changed(), ast_el_read_char(), ast_fileexists(), ast_frdup(), ast_http_get_contents(), ast_http_get_json(), ast_http_get_post_vars(), ast_http_send(), ast_inet_ntoa(), ast_json_timeval(), ast_log_full(), ast_log_vsyslog(), ast_openstream_full(), ast_openvstream(), ast_parse_arg(), ast_presence_state_changed(), ast_read_image(), ast_recvchar(), ast_recvtext(), ast_remotecontrol(), ast_say_number_full_zh(), ast_sched_del(), ast_sip_contact_to_str(), ast_sip_create_ami_event(), ast_sorcery_objectset_json_create(), ast_state2str(), ast_str_get_encoded_str(), ast_term_color(), AST_TEST_DEFINE(), ast_threadstorage_get(), ast_to_camel_case_delim(), ast_translate_number_ka(), ast_udptl_read(), ast_udptl_write(), ast_websocket_read(), ast_websocket_read_string(), ast_websocket_write_string(), ast_writefile(), ast_yy_scan_bytes(), astman_append(), astman_append_json(), astman_send_error_va(), audiohook_read_frame_single(), auth_exec(), bucket_copy(), build_callid_pvt(), build_callid_registry(), build_peer(), build_regex(), build_user(), caldav_request(), callerid_feed(), callerid_feed_jp(), chan_pjsip_new(), check_auth(), check_header(), check_password(), cli_complete(), cli_console_sendtext(), compile_script(), conf_run(), config_text_file_load(), console_sendtext(), cops_getmsg(), cops_sendmsg(), copy(), corosync_show_members(), cpg_deliver_cb(), create_followme_number(), csv_log(), custom_devstate_callback(), custom_presence_callback(), deliver_file(), destroy_mysql(), dialgroup_refreshdb(), do_monitor_headset(), do_monitor_phone(), do_notify(), dtls_handler(), dtmf_info_incoming_request(), dump_prov_flags(), enc_ie_facility(), end_bridge_callback(), exchangecal_request(), export_aoc_vars(), fax_generator_generate(), fgetln(), format_ami_aor_handler(), format_ami_auth_handler(), format_ami_endpoint(), format_ami_endpoint_identify(), format_ami_endpoint_transport(), format_ami_endpoints(), format_ami_resource_lists(), format_contact_status(), function_db_delete_write(), function_ooh323_write(), g723_samples(), gen_prios(), get_line(), get_mapping_weight(), get_single_field_as_var_list(), get_transport_list(), handle_bchan(), handle_cli_config_reload(), handle_cli_indication_show(), handle_cli_misdn_show_port(), handle_cli_misdn_show_stacks(), handle_cli_mobile_cusd(), handle_cli_mobile_rfcomm(), handle_commandcomplete(), handle_commandmatchesarray(), handle_common_options(), handle_dbget(), handle_jack_audio(), handle_minivm_show_stats(), handle_msg_cb(), handle_pjsip_list_ciphers(), handle_recvtext(), handle_request_info(), handle_request_notify(), handle_request_subscribe(), handle_select_codec(), handle_show_calendar(), handle_show_hint(), handle_show_hints(), handle_show_settings(), handle_speechrecognize(), handle_t38_options(), has_destination_cb(), headers_to_vars(), http_body_discard_contents(), iCBSearch(), incoming_in_dialog_request(), jpeg_identify(), jpeg_read_image(), linear_generator(), load_config(), logger_print_normal(), main(), manager_bchannel_activate(), manager_bchannel_deactivate(), manager_log(), manager_park(), map_init_emacs(), map_init_meta(), message_template_parse_filebody(), milliwatt_generate(), misdn_digit_end(), misdn_facility_ie_handler(), misdn_lib_destroy(), misdn_lib_send_tone(), misdn_lib_tx2misdn_frm(), misdn_tx_jitter(), moh_generate(), my_distinctive_ring(), my_get_callerid(), parse_name_andor_addr(), pbx_builtin_background(), pcm_write(), pgsql_log(), phase_e_handler(), phone_mini_packet(), poll_mailbox(), powiedz(), print_group(), print_named_groups(), printdigest(), process_description_file(), process_message(), process_output(), publish_to_corosync(), queue_set_param(), read_config(), read_config_maps(), read_environment(), read_preread(), readline(), readmimefile(), readStdin(), realtime_common(), realtime_multi_mysql(), realtime_mysql(), receive_message(), reload_config(), remove_excess_lws(), rfc3326_add_reason_header(), rfc3326_use_reason_header(), rl_complete_internal(), rotate_file(), run_agi(), rx_data_to_ast_msg(), say_date_generic(), say_enumeration_full(), say_number_full(), search_directory_sub(), select_item_menu(), send_rasterisk_connect_commands(), set_bridge_peer_vars_multiparty(), set_channel_variables(), set_full_cmd(), set_insecure_flags(), setenv(), show_dialplan_helper(), silence_generator_generate(), sip_call(), sip_cli_notify(), sip_endpoints_aors_ami(), sip_parse_register_line(), sip_report_security_event(), sip_route_list(), sip_subscribe_mwi(), sla_state(), sla_stop_ringing_trunk(), sms_generate(), sms_log(), sms_writefile(), socket_read(), softmix_mixing_loop(), spandsp_fax_read(), sqlite3_escape_column_op(), sqlite3_escape_string_helper(), stack_destroy(), stasis_device_state_cb(), store_mysql(), store_pgsql(), store_tone_zone_ring_cadence(), stream_monitor(), synths_(), system_exec_helper(), tdd_feed(), te_lib_destroy(), term_echotc(), term_set(), test_jb_debug_output(), test_jb_error_output(), test_jb_warn_output(), transmit_fake_auth_response(), transmit_request_with_auth(), transport_bind_handler(), transport_create(), transport_read(), try_load_key(), tzload(), unshift_push(), update2_mysql(), update_mysql(), vm_change_password_shell(), websocket_client_handshake_get_response(), xml_copy_escape(), xml_encode_str(), xmpp_client_authenticate_digest(), and xmpp_client_receive().

Definition at line 80 of file eagi_proxy.c.

Referenced by finalize(), main(), and readStdin().

pthread_mutex_t command_mutex

Definition at line 96 of file eagi_proxy.c.

Referenced by main(), and readStdin().

char connected = 1

Definition at line 82 of file eagi_proxy.c.

char* end

Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 73 of file eagi_proxy.c.

Referenced by __ast_play_and_record(), _build_port_config(), _extension_match_core(), _history_expand_command(), aco_process_config(), ari_originate_dial(), ast_bridge_transfer_attended(), ast_sip_pubsub_generate_body_content(), ast_term_init(), AST_TEST_DEFINE(), ast_udptl_init(), ast_xml_escape(), caldav_add_event(), caldav_get_events_between(), calendar_query_exec(), common_exec(), control_streamfile(), copy_via_headers(), dahdi_create_channels(), dahdi_destroy_channels(), distributor(), echo_exec(), end_bridge_callback(), ewscal_write_event(), exchangecal_get_events_between(), exchangecal_write_event(), execif_exec(), ext_cmp1(), ext_cmp_pattern_pos(), file_read(), fileexists_core(), find_table_cb(), get_ewscal_ids_for(), get_outbound_endpoint(), get_range(), get_rdnis(), get_rpid(), h264_encap(), handle_cli_dialplan_add_extension(), headers_to_vars(), icalendar_add_event(), jingle_outgoing_hook(), jingle_send_error_response(), jingle_send_session_info(), jingle_send_session_terminate(), load_module(), logger_strip_verbose_magic(), main(), misdn_cfg_update_ptp(), mpeg4_encap(), osp_convert_outin(), ospfinished_exec(), parsedoublearg(), parsefreq(), parsetime(), parsevolarg(), parsevolume(), pbx_load_config(), publish_local_bridge_message(), rl_complete_internal(), safe_scan_int(), set_config(), sip_outbound_publish_callback(), sip_websocket_callback(), strreplace(), tds_log(), tone_detect(), transmit_invite(), two_bridge_attended_transfer(), update_caldav(), update_exchangecal(), update_header(), verify_mock_cdr_record(), websocket_echo_callback(), write_buf(), and xmpp_client_service_discovery_get_hook().

pthread_t signal_thread

Definition at line 97 of file eagi_proxy.c.

Referenced by main().

Definition at line 81 of file eagi_proxy.c.

Referenced by finalize(), main(), and readSignal().

pthread_t stdin_thread

Definition at line 97 of file eagi_proxy.c.

Referenced by main().

char* winbuf

Definition at line 72 of file eagi_proxy.c.

Referenced by finalize(), main(), and write_buf().

char window[WINSIZE]

Definition at line 69 of file eagi_proxy.c.

Referenced by readSignal(), SimpleAnalysis(), and smb_pitch_shift().


Generated on Thu Apr 16 06:30:48 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6