siproc

A primitive SIP client that spawns processes for each call
git clone git://xatko.vsos.ethz.ch/siproc.git
Log | Files | Refs

commit 855a293c3eb0a0bbd9af82b9a834934369677c0a
parent 0ac0b323d46e2d358af39f2321f5891ee77f3e23
Author: Dominik Schmidt <dominik@schm1dt.ch>
Date:   Wed, 24 Jul 2019 00:03:03 +0200

Reformat source code

Employing uncrustify

Diffstat:
src/siproc.cpp | 591++++++++++++++++++++++++++++++++++++++++---------------------------------------
1 file changed, 298 insertions(+), 293 deletions(-)

diff --git a/src/siproc.cpp b/src/siproc.cpp @@ -9,55 +9,58 @@ struct EV{ int efd; - + EV(){ - efd=epoll_create1(0); - if(efd<0){ + efd = epoll_create1(0); + if(efd < 0){ throw "Could not create epoll fd"; } } - + ~EV(){ close(efd); } - + void ev_add(int fd, void *data){ - struct epoll_event ev={0}; + struct epoll_event ev = {0}; + ev.events = EPOLLIN; ev.data.ptr = data; epoll_ctl(efd, EPOLL_CTL_ADD, fd, &ev); } - + void ev_del(int fd){ epoll_ctl(efd, EPOLL_CTL_DEL, fd, NULL); } - - void* wait(int timeout){ - struct epoll_event ev={0}; - ev.data.u64=0; - int ret=epoll_wait(efd, &ev, 1, timeout); - if(ret<0){ + + void *wait(int timeout){ + struct epoll_event ev = {0}; + + ev.data.u64 = 0; + int ret = epoll_wait(efd, &ev, 1, timeout); + if(ret < 0){ throw "Could not wait on epollfd"; } else if(ret == 0){ return NULL; } - else{ + else { return ev.data.ptr; } } - - void* wait(){ + + void *wait(){ return wait(-1); } }; ssize_t readall(int fd, char **buffer, size_t *buffer_length){ - ssize_t ret=0; - size_t offset=0; - while((ret = read(fd, (*buffer+offset), *buffer_length-offset)) == *buffer_length-offset){ + ssize_t ret = 0; + size_t offset = 0; + + while((ret = read(fd, (*buffer + offset), *buffer_length - offset)) == *buffer_length - offset){ *buffer_length *= 2; - *buffer = (char*)realloc(*buffer, *buffer_length); + *buffer = (char *)realloc(*buffer, *buffer_length); offset += ret; } if(ret == -1){ @@ -68,15 +71,13 @@ ssize_t readall(int fd, char **buffer, size_t *buffer_length){ return offset + ret; } -EV ev_table=EV(); +EV ev_table = EV(); using namespace pj; class MyAccount; -class MyCall : public Call -{ - +class MyCall : public Call{ FILE *c_stdin; int c_stdout; MyAccount *myacc; @@ -85,239 +86,246 @@ class MyCall : public Call size_t line_length; AudioMediaPlayer player; AudioMediaRecorder recorder; -public: - MyCall(Account &acc, int call_id = PJSUA_INVALID_ID) : Call(acc, call_id){ - c_stdin = NULL; - c_stdout = -1; - child = -1; - line = (char*)malloc(256); - line_length = 256; - } - - ~MyCall(){ - if(line!= NULL){ - free(line); - line=NULL; - line_length=0; - } - if (child != -1){ - fork_off(); + public: + MyCall(Account &acc, int call_id = PJSUA_INVALID_ID) : Call(acc, call_id){ + c_stdin = NULL; + c_stdout = -1; + child = -1; + line = (char *)malloc(256); + line_length = 256; } - } - - AudioMedia& playdev(){ - //return Endpoint::instance().audDevManager().getPlaybackDevMedia(); - CallInfo ci = getInfo(); - AudioMedia *aud_med = NULL; - - // Find out which media index is the audio - for (unsigned i=0; i<ci.media.size(); ++i) { - if (ci.media[i].type == PJMEDIA_TYPE_AUDIO) { - aud_med = (AudioMedia *)getMedia(i); - break; + + ~MyCall(){ + if(line != NULL){ + free(line); + line = NULL; + line_length = 0; + } + if(child != -1){ + fork_off(); } } - return *aud_med; - } - - void fork_off(){ - if(child == -1){ - return; - } - PJ_LOG(3, ("MyCall", "Shutting down child process")); - ev_table.ev_del(c_stdout); - fclose(c_stdin); - close(c_stdout); - c_stdin = NULL; - c_stdout = -1; - kill(child, SIGTERM); - int statloc; - waitpid(child, &statloc, 0); - child = -1; - } - - void fork_on(char **args); - - virtual void onCallState(OnCallStateParam &prm){ - CallInfo ci = getInfo(); - if (ci.state == PJSIP_INV_STATE_DISCONNECTED) { - fork_off(); - delete this; - return; - } - } - - virtual void onCallMediaState(OnCallMediaStateParam &prm){ - - } - - int command(const char *cmd, ...){ - if(c_stdin==NULL){ - return -1; + AudioMedia& playdev(){ + //return Endpoint::instance().audDevManager().getPlaybackDevMedia(); + CallInfo ci = getInfo(); + AudioMedia *aud_med = NULL; + + // Find out which media index is the audio + for(unsigned i = 0; i < ci.media.size(); ++i){ + if(ci.media[i].type == PJMEDIA_TYPE_AUDIO){ + aud_med = (AudioMedia *)getMedia(i); + break; + } + } + + return *aud_med; } - int ret; - va_list ap; - va_start(ap, cmd); - ret = vfprintf(c_stdin, cmd, ap); - va_end(ap); - fflush(c_stdin); - return ret; - } - - virtual void onDtmfDigit(OnDtmfDigitParam &prm){ - command("DTMF %s\n", prm.digit.c_str()); - } - - virtual void onInstantMessage(OnInstantMessageParam &prm){ - command("MESSAGE %s\n", prm.msgBody.c_str()); - } - - void cmd_hangup(){ - // Just hangup for now - CallOpParam op; - op.statusCode = PJSIP_SC_DECLINE; - hangup(op); - } - - void cmd_answer(){ - CallOpParam prm; - prm.statusCode = PJSIP_SC_OK; - answer(prm); - } - - void cmd_dtmf(char *args){ - dialDtmf(args); - } - - void cmd_message(char *msg){ - SendInstantMessageParam prm=SendInstantMessageParam(); - prm.content=std::string(msg); - sendInstantMessage(prm); - } - - void cmd_play(char *path){ - AudioMedia& play_dev_med=playdev(); - try { - player.createPlayer(path, PJMEDIA_FILE_NO_LOOP); - player.startTransmit(play_dev_med); + + void fork_off(){ + if(child == -1){ + return; + } + PJ_LOG(3, ("MyCall", "Shutting down child process")); + ev_table.ev_del(c_stdout); + fclose(c_stdin); + close(c_stdout); + c_stdin = NULL; + c_stdout = -1; + kill(child, SIGTERM); + int statloc; + waitpid(child, &statloc, 0); + child = -1; } - catch(Error& err){ - //std::cerr << err <<std::endl; + + void fork_on(char **args); + + virtual void onCallState(OnCallStateParam &prm){ + CallInfo ci = getInfo(); + + if(ci.state == PJSIP_INV_STATE_DISCONNECTED){ + fork_off(); + delete this; + return; + } } - } - - void cmd_record(char *path){ - AudioMedia& play_dev_med=playdev(); - try { - player.createPlayer(path, PJMEDIA_FILE_NO_LOOP); - player.startTransmit(play_dev_med); + + virtual void onCallMediaState(OnCallMediaStateParam &prm){ } - catch(Error& err){ - //std::cerr << err <<std::endl; + + int command(const char *cmd, ...){ + if(c_stdin == NULL){ + return -1; + } + int ret; + va_list ap; + va_start(ap, cmd); + ret = vfprintf(c_stdin, cmd, ap); + va_end(ap); + fflush(c_stdin); + return ret; } - } - - void cmd_stop(char *path){ - AudioMedia& play_dev_med=playdev(); - player.stopTransmit(play_dev_med); - } - void cmd_ringing(){ - CallOpParam prm; - prm.statusCode = PJSIP_SC_RINGING; - answer(prm); - } - - - void command_machine(char *command){ - char *args = strstr(command, " "); - if(args != NULL){ - *args++ = '\0'; + virtual void onDtmfDigit(OnDtmfDigitParam &prm){ + command("DTMF %s\n", prm.digit.c_str()); } - if(strcmp(command, "HANGUP")==0){ - cmd_hangup(); + + virtual void onInstantMessage(OnInstantMessageParam &prm){ + command("MESSAGE %s\n", prm.msgBody.c_str()); } - else if(strcmp(command, "ANSWER")==0){ - cmd_answer(); + + void cmd_hangup(){ + // Just hangup for now + CallOpParam op; + + op.statusCode = PJSIP_SC_DECLINE; + hangup(op); } - else if(strcmp(command, "DTMF")==0){ - cmd_dtmf(args); + + void cmd_answer(){ + CallOpParam prm; + + prm.statusCode = PJSIP_SC_OK; + answer(prm); } - else if(strcmp(command, "MESSAGE")==0){ - cmd_message(args); + + void cmd_dtmf(char *args){ + dialDtmf(args); } - else if(strcmp(command, "PLAY")==0){ - cmd_play(args); + + void cmd_message(char *msg){ + SendInstantMessageParam prm = SendInstantMessageParam(); + + prm.content = std::string(msg); + sendInstantMessage(prm); } - else if(strcmp(command, "STOP")==0){ - cmd_stop(args); + + void cmd_play(char *path){ + AudioMedia& play_dev_med = playdev(); + + try { + player.createPlayer(path, PJMEDIA_FILE_NO_LOOP); + player.startTransmit(play_dev_med); + } + catch(Error& err){ + //std::cerr << err <<std::endl; + } } - else if(strcmp(command, "RECORD")==0){ - cmd_record(args); + + void cmd_record(char *path){ + AudioMedia& play_dev_med = playdev(); + + try { + player.createPlayer(path, PJMEDIA_FILE_NO_LOOP); + player.startTransmit(play_dev_med); + } + catch(Error& err){ + //std::cerr << err <<std::endl; + } } - else if(strcmp(command, "RINGING")==0){ - cmd_ringing(); + + void cmd_stop(char *path){ + AudioMedia& play_dev_med = playdev(); + + player.stopTransmit(play_dev_med); } - } - - void handle_line(){ - PJ_LOG(3, ("MyCall", "Handling line")); - ssize_t ret=readall(c_stdout, &line, &line_length); - printf("Read %lu bytes\n", ret); - if(ret<0){ - throw "Error reading from stdout"; + + void cmd_ringing(){ + CallOpParam prm; + + prm.statusCode = PJSIP_SC_RINGING; + answer(prm); } - else if(ret == 0){ - PJ_LOG(3, ("MyCall", "Child closed stdout, killing it")); - fork_off(); + + + void command_machine(char *command){ + char *args = strstr(command, " "); + + if(args != NULL){ + *args++ = '\0'; + } + if(strcmp(command, "HANGUP") == 0){ + cmd_hangup(); + } + else if(strcmp(command, "ANSWER") == 0){ + cmd_answer(); + } + else if(strcmp(command, "DTMF") == 0){ + cmd_dtmf(args); + } + else if(strcmp(command, "MESSAGE") == 0){ + cmd_message(args); + } + else if(strcmp(command, "PLAY") == 0){ + cmd_play(args); + } + else if(strcmp(command, "STOP") == 0){ + cmd_stop(args); + } + else if(strcmp(command, "RECORD") == 0){ + cmd_record(args); + } + else if(strcmp(command, "RINGING") == 0){ + cmd_ringing(); + } } - else{ - char *begin = line; - while(begin){ - char *end=strstr(begin, "\n"); - if(end != NULL){ - *end='\0'; + + void handle_line(){ + PJ_LOG(3, ("MyCall", "Handling line")); + ssize_t ret = readall(c_stdout, &line, &line_length); + printf("Read %lu bytes\n", ret); + if(ret < 0){ + throw "Error reading from stdout"; + } + else if(ret == 0){ + PJ_LOG(3, ("MyCall", "Child closed stdout, killing it")); + fork_off(); + } + else { + char *begin = line; + while(begin){ + char *end = strstr(begin, "\n"); + if(end != NULL){ + *end = '\0'; + } + command_machine(begin); + begin = end; } - command_machine(begin); - begin=end; } } - - } }; -class MyAccount : public Account -{ -public: - char **args; - MyAccount(char **args) { - this->args=args; - } - ~MyAccount(){ - } - - virtual void onIncomingCall(OnIncomingCallParam &iprm){ - MyCall *call = new MyCall(*this, iprm.callId); - call->fork_on(args); - } +class MyAccount : public Account{ + public: + char **args; + MyAccount(char **args) { + this->args = args; + } + ~MyAccount(){ + } + + virtual void onIncomingCall(OnIncomingCallParam &iprm){ + MyCall *call = new MyCall(*this, iprm.callId); + + call->fork_on(args); + } }; void MyCall::fork_on(char **args){ PJ_LOG(4, ("MyCall", "Setting up Fork")); - int pipes_stdin[2],pipes_stdout[2]; - - if(pipe(pipes_stdin)!=0){ + int pipes_stdin[2], pipes_stdout[2]; + + if(pipe(pipes_stdin) != 0){ throw "Pipe creation failed"; } - - if(pipe(pipes_stdout)!=0){ + + if(pipe(pipes_stdout) != 0){ throw "Pipe creation failed"; } c_stdin = fdopen(pipes_stdin[1], "w"); setvbuf(c_stdin, NULL, _IOLBF, 256); c_stdout = pipes_stdout[0]; - + CallInfo ci = getInfo(); PJ_LOG(4, ("MyCall", "Calling fork")); pid_t pid = fork(); @@ -338,9 +346,9 @@ void MyCall::fork_on(char **args){ } exit(1); } - else{ + else { PJ_LOG(3, ("MyCall", "Fork successfull")); - child=pid; + child = pid; close(pipes_stdin[0]); close(pipes_stdout[1]); ev_table.ev_add(pipes_stdout[0], this); @@ -348,101 +356,98 @@ void MyCall::fork_on(char **args){ } void usage(){ - fprintf(stderr, -"Usage: siproc <executable> [args...]\n\n" -"Make sure to define the following environment variables:\n" -"\t* SIPROC_USERNAME:\tThe username used for authentication, e.g. Foo\n" -"\t* SIPROC_PASSWORD:\tThe password used for authentication, e.g. Bar\n" -"\t* SIPROC_REGISTRAR_URI:\tThe server to connect to, e.g. \"sip:fritz.box\"\n" -"\t* SIPROC_ID_URI:\tThe ID URI of your account, e.g. \"Foo Baz <sip:Foo@fritz.box>\"\n" -"\nThanks for riding siproc!\n" -); + fprintf(stderr, + "Usage: siproc <executable> [args...]\n\n" + "Make sure to define the following environment variables:\n" + "\t* SIPROC_USERNAME:\tThe username used for authentication, e.g. Foo\n" + "\t* SIPROC_PASSWORD:\tThe password used for authentication, e.g. Bar\n" + "\t* SIPROC_REGISTRAR_URI:\tThe server to connect to, e.g. \"sip:fritz.box\"\n" + "\t* SIPROC_ID_URI:\tThe ID URI of your account, e.g. \"Foo Baz <sip:Foo@fritz.box>\"\n" + "\nThanks for riding siproc!\n" + ); } int main(int argc, char **argv){ - - - char *user,*password,*idUri,*reguri; - - if(!(user = getenv("SIPROC_USERNAME"))){ + char *user, *password, *idUri, *reguri; + + if(!(user = getenv("SIPROC_USERNAME"))){ fprintf(stderr, "SIPROC_USERNAME not in environment variables\n\n"); usage(); return 1; } - if(!(password = getenv("SIPROC_PASSWORD"))){ + if(!(password = getenv("SIPROC_PASSWORD"))){ fprintf(stderr, "SIPROC_PASSWORD not in environment variables\n\n"); usage(); return 1; } - - if(!(reguri = getenv("SIPROC_REGISTRAR_URI"))){ + + if(!(reguri = getenv("SIPROC_REGISTRAR_URI"))){ fprintf(stderr, "SIPROC_REGISTRAR_URI not in environment variables\n\n"); usage(); return 1; } - - if(!(idUri = getenv("SIPROC_ID_URI"))){ + + if(!(idUri = getenv("SIPROC_ID_URI"))){ fprintf(stderr, "SIPROC_ID_URI not in environment variables\n\n"); usage(); return 1; } - + try{ - - Endpoint ep; - - ep.libCreate(); - EpConfig ep_cfg; - ep.libInit(ep_cfg); - AudDevManager &adm = ep.audDevManager(); - adm.setNullDev(); - - TransportConfig tcfg; - tcfg.port = 5060; - try { - ep.transportCreate(PJSIP_TRANSPORT_UDP, tcfg); - } catch (Error &err) { - std::cerr << err.info() << std::endl; - return 1; - } - - ep.libStart(); - - - AccountConfig acfg; - acfg.idUri = idUri; - acfg.regConfig.registrarUri = reguri; - //acfg.regConfig.registrarUri = "sip:fritz.box"; - //AuthCredInfo cred("digest", "*", "Dominik9", 0, "12345678"); - AuthCredInfo cred("digest", "*", user, 0, password); - acfg.sipConfig.authCreds.push_back( cred ); - - MyAccount *acc = new MyAccount(&argv[1]); - acc->create(acfg); - - char *line=NULL; - size_t line_length=0; - ev_table.ev_add(STDIN_FILENO, &line_length); - while(void *ptr=ev_table.wait()){ - if(ptr == &line_length){ - ssize_t bytes = getline(&line, &line_length, stdin); - if(bytes > 0){ - line[--bytes]='\0'; - printf("Got line with length %lu:\n", bytes); - fwrite(line, 1, bytes, stdout); - if(strncmp(line, "QUIT", line_length)==0){ - break; + Endpoint ep; + + ep.libCreate(); + EpConfig ep_cfg; + ep.libInit(ep_cfg); + AudDevManager &adm = ep.audDevManager(); + adm.setNullDev(); + + TransportConfig tcfg; + tcfg.port = 5060; + try { + ep.transportCreate(PJSIP_TRANSPORT_UDP, tcfg); + } catch(Error &err){ + std::cerr << err.info() << std::endl; + return 1; + } + + ep.libStart(); + + + AccountConfig acfg; + acfg.idUri = idUri; + acfg.regConfig.registrarUri = reguri; + //acfg.regConfig.registrarUri = "sip:fritz.box"; + //AuthCredInfo cred("digest", "*", "Dominik9", 0, "12345678"); + AuthCredInfo cred("digest", "*", user, 0, password); + acfg.sipConfig.authCreds.push_back(cred); + + MyAccount *acc = new MyAccount(&argv[1]); + acc->create(acfg); + + char *line = NULL; + size_t line_length = 0; + ev_table.ev_add(STDIN_FILENO, &line_length); + while(void *ptr = ev_table.wait()){ + if(ptr == &line_length){ + ssize_t bytes = getline(&line, &line_length, stdin); + if(bytes > 0){ + line[--bytes] = '\0'; + printf("Got line with length %lu:\n", bytes); + fwrite(line, 1, bytes, stdout); + if(strncmp(line, "QUIT", line_length) == 0){ + break; + } } } + else { + MyCall *c = (MyCall *)ptr; + c->handle_line(); + } } - else{ - MyCall* c=(MyCall*)ptr; - c->handle_line(); - } - } - delete acc; + delete acc; } - catch(char const* c){ + catch(char const *c){ printf("EXCEPTION: %s\n", c); }