summaryrefslogtreecommitdiffstats
path: root/src/astmanproxy.c
blob: 3c976e66da4f4c78eb21c2d4b4257610ef00cb98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/* Asterisk Manager Proxy
   Copyright (c) 2005 David C. Troy <dave@popvox.com>

   This program is free software, distributed under the terms of
   the GNU General Public License.

*/

#include "astmanproxy.h"

extern int LoadHandlers( void );
extern void ReadConfig( void );
extern void ReadPerms( void );
extern FILE *OpenLogfile( void );
extern int SetProcUID( void );

extern void *proxyaction_do(char *proxyaction, struct message *m, struct mansession *s);
extern void *ProxyLogin(struct mansession *s, struct message *m);
extern void *ProxyLogoff(struct mansession *s);
extern int ValidateAction(struct message *m, struct mansession *s, int inbound);

int ConnectAsterisk(struct mansession *s);

struct proxyconfig pc;
struct mansession *sessions = NULL;
struct iohandler *iohandlers = NULL;

pthread_mutex_t sessionlock;
pthread_mutex_t serverlock;
pthread_mutex_t userslock;
pthread_mutex_t loglock;
pthread_mutex_t debuglock;
static int asock = -1;
FILE *proxylog;
int debug = 0;

void hup(int sig) {
    if (proxylog) {
       fflush(proxylog);
       fclose(proxylog);
    }
    proxylog = OpenLogfile();
    logmsg("Received HUP -- reopened log");
    ReadPerms();
    logmsg("Received HUP -- reread permissions");
}

void leave(int sig) {
    struct mansession *c;
    struct message sm, cm;
    struct iohandler *io;
    struct ast_server *srv;
    char iabuf[INET_ADDRSTRLEN];

    /* Message to send to servers */
    memset(&sm, 0, sizeof(struct message));
    AddHeader(&sm, "Action: Logoff");

    /* Message to send to clients */
    memset(&cm, 0, sizeof(struct message));
    AddHeader(&cm, PROXY_SHUTDOWN);

    if (debug)
        debugmsg("Notifying and closing sessions");
    pthread_mutex_lock (&sessionlock);
    while (sessions) {
        c = sessions;
        sessions = sessions->next;

        if (c->server) {
            if (debug)
               debugmsg("asterisk@%s: closing session", ast_inet_ntoa(iabuf, sizeof(iabuf), c->sin.sin_addr));
            c->output->write(c, &sm);
            logmsg("Shutdown, closed server %s", ast_inet_ntoa(iabuf, sizeof(iabuf), c->sin.sin_addr));
        } else {
            if (debug)
               debugmsg("client@%s: closing session", ast_inet_ntoa(iabuf, sizeof(iabuf), c->sin.sin_addr));
            c->output->write(c, &cm);
            logmsg("Shutdown, closed client %s", ast_inet_ntoa(iabuf, sizeof(iabuf), c->sin.sin_addr));
        }
        close_sock(c->fd);	/* close tcp & ssl socket */
        pthread_mutex_destroy(&c->lock);
        free(c);
    }
    pthread_mutex_unlock (&sessionlock);

    /* unload server list */
    while (pc.serverlist) {
        srv = pc.serverlist;
        pc.serverlist = srv->next;
        if (debug)
            debugmsg("asterisk@%s: forgetting", srv->ast_host);
        free(srv);
    }

    if (debug)
        debugmsg("Closing listener socket");
    close_sock(asock);		/* close tcp & ssl socket */

    /* unload io handlers */
    while (iohandlers) {
        io = iohandlers;
        iohandlers = iohandlers->next;
        if (debug)
            debugmsg("unloading: %s", io->formatname);
        dlclose(io->dlhandle);
        free(io);
    }

    if(debug)
        debugmsg("Done!\n");
    logmsg("Proxy stopped; shutting down.");

    fclose(proxylog);
    pthread_mutex_destroy(&sessionlock);
    pthread_mutex_destroy(&loglock);
    pthread_mutex_destroy(&debuglock);
    exit(sig);
}

void Version( void )
{
    printf("astmanproxy: Version %s, (C) David C. Troy\n", PROXY_VERSION);
    return;
}

void Usage( void )
{
    printf("Usage: astmanproxy [-d|-h|-v]\n");
    printf(" -d : Start in Debug Mode\n");
    printf(" -h : Displays this message\n");
    printf(" -v : Displays version information\n");
    printf("Start with no options to run as daemon\n");
    return;
}

void destroy_session(struct mansession *s)
{
    struct mansession *cur, *prev = NULL;
    char iabuf[INET_ADDRSTRLEN];

        pthread_mutex_lock(&sessionlock);
        cur = sessions;
        while(cur) {
                if (cur == s)
                        break;
                prev = cur;
                cur = cur->next;
        }
        if (cur) {
                if (prev)
                        prev->next = cur->next;
                else
                        sessions = cur->next;
                debugmsg("Connection closed: %s", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
                close_sock(s->fd);	/* close tcp/ssl socket */
                pthread_mutex_destroy(&s->lock);
                free(s);
        } else
                debugmsg("Trying to delete non-existent session %p?\n", s);
        pthread_mutex_unlock(&sessionlock);

    /* If there are no servers and no clients, why are we here? */
    if (!sessions) {
        logmsg("Cannot connect to any servers! Leaving!");
        leave(0);
    }
}

int WriteClients(struct message *m) {
    struct mansession *c, *d;
    char *actionid;

    c = sessions;
    while (c) {
        if ( !c->server && m->hdrcount>1 && ValidateAction(m, c, 1) ) {
            if (c->autofilter && c->actionid) {
                actionid = astman_get_header(m, ACTION_ID);
                if ( !strcmp(actionid, c->actionid) ) {
                    c->output->write(c, m);
                }
            } else
                c->output->write(c, m);
            if ( c->input->autodisconnect && c->input->autodisconnect() ) {
		d = c;			/* session to disconnect */
		c = c->next;
		destroy_session(d);
		continue;
	    }
        }
        c = c->next;
    }
    return 1;
}

int WriteAsterisk(struct message *m) {
    int i;
    char outstring[MAX_LEN], *dest;
    struct mansession *s, *first;

    first = NULL;
    dest = NULL;

    s = sessions;

    dest = astman_get_header(m, "Server");
    if (debug && *dest) debugmsg("set destination: %s", dest);
    while ( s ) {
       if ( s->server && (s->connected > 0) ) {
          if ( !first )
              first = s;
          if (*dest && !strcasecmp(dest, s->server->ast_host) )
              break;
       }
       s = s->next;
    }

    if (!s)
        s = first;  

    /* Check for no servers and empty block -- Don't pester Asterisk if it is one*/
    if (!s || !s->server || (!m->hdrcount && !m->headers[0][0]) )
        return 1;

    debugmsg("writing block to %s", s->server->ast_host);

    pthread_mutex_lock(&s->lock);
    for (i=0; i<m->hdrcount; i++) {
        if (strcasecmp(m->headers[i], "Server:") ) {
            sprintf(outstring, "%s\r\n", m->headers[i]);
            ast_carefulwrite(s->fd, outstring, strlen(outstring), s->writetimeout );
        }
    }
    ast_carefulwrite(s->fd, "\r\n", 2, s->writetimeout);
    pthread_mutex_unlock(&s->lock);
    return 1;
}

void *setactionid(char *actionid, struct message *m, struct mansession *s)
{
    pthread_mutex_lock(&s->lock);
    strncpy(s->actionid, actionid, MAX_LEN);
    pthread_mutex_unlock(&s->lock);

    return 0;
}

/* Handles proxy client sessions; closely based on session_do from asterisk's manager.c */
void *session_do(struct mansession *s)
{
    struct message m;
    int res;
    char *proxyaction, *actionid, *action, *key;

    if (s->input->onconnect)
        s->input->onconnect(s, &m);

    for (;;) {
        /* Get a complete message block from input handler */
        memset(&m, 0, sizeof(struct message) );
        res = s->input->read(s, &m);
        m.session = s;

        if (res > 0) {
            /* Check for anything that requires proxy-side processing */
            if (pc.key && !s->authenticated) {
                key = astman_get_header(&m, "ProxyKey");
                if (!strcmp(key, pc.key) ) {
                   pthread_mutex_lock(&s->lock);
                   s->authenticated = 1;
                   pthread_mutex_unlock(&s->lock);
                } else
                   break;
            }

            proxyaction = astman_get_header(&m, "ProxyAction");
            actionid = astman_get_header(&m, ACTION_ID);
            action = astman_get_header(&m, "Action");
            if ( !strcasecmp(action, "Login") )
                ProxyLogin(s, &m);
            else if ( !strcasecmp(action, "Logoff") )
                ProxyLogoff(s);
            else if ( !strcasecmp(action, "Challenge") )
                ProxyChallenge(s, &m);
            else if ( !(*proxyaction == '\0') )
                proxyaction_do(proxyaction, &m, s);
            else if ( ValidateAction(&m, s ,0) ) {
                if ( !(*actionid == '\0') )
                    setactionid(actionid, &m, s);
                if ( !WriteAsterisk(&m) )
                    break;
            } else {
                SendError(s, "Action Filtered");
            }
        } else if (res < 0)
            break;
   }

   destroy_session(s);
   if (debug)
       debugmsg("Exiting session_do thread");
   pthread_exit(NULL);
   return NULL;
}

void *HandleAsterisk(struct mansession *s)
{
    struct message m;
    int res,i;
    char iabuf[INET_ADDRSTRLEN];

    if (ConnectAsterisk(s))
        goto leave;
    for (;;) {

        debugmsg("asterisk@%s: attempting read...", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
        memset(&m, 0, sizeof(struct message) );
        res = s->input->read(s, &m);
        m.session = s;

        if (res > 0) {
            if (debug) {
                for(i=0; i<m.hdrcount; i++) {
                    debugmsg("asterisk@%s got: %s", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr), m.headers[i]);
                }
            }

            if (!s->connected) {
                if ( !strcmp("Authentication accepted", astman_get_header(&m, "Message")) ) {
                    s->connected = 1;
                    if (debug)
                        debugmsg("asterisk@%s: connected successfully!", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr) );
                }
                if ( !strcmp("Authentication failed", astman_get_header(&m, "Message")) ) {
                    s->connected = -1;
                }
            }

            m.session = s;
            AddHeader(&m, "Server: %s", m.session->server->ast_host);

            if (!WriteClients(&m))
                break;
            /*  TODO: does it make any sense to abort * conn if we cannot write to clients?  I don't think so.
                Do we even get a return code back that means anything?  I don't think so.  */
        } else if (res < 0) {
            if (debug)
                debugmsg("asterisk@%s: Not connected", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
            if ( ConnectAsterisk(s) )
                break;
        }
   }

leave:
   if (debug)
      debugmsg("asterisk@%s: Giving up and exiting thread", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr) );
   destroy_session(s);
   pthread_exit(NULL);
   return NULL;
}

int ConnectAsterisk(struct mansession *s) {
    char iabuf[INET_ADDRSTRLEN];
    int r = 1, res = 0;
    struct message m;

    /* Don't try to do this if auth has already failed! */
    if (s->connected < 0 )
        return 1;
    else
        s->connected = 0;

    if (debug)
        debugmsg("asterisk@%s: Connecting (u=%s, p=%s)", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr),
                 s->server->ast_user, s->server->ast_pass);

    /* Construct auth message just once */
    memset( &m, 0, sizeof(struct message) );
    AddHeader(&m, "Action: Login");
    AddHeader(&m, "Username: %s", s->server->ast_user);
    AddHeader(&m, "Secret: %s", s->server->ast_pass);
    AddHeader(&m, "Events: %s", s->server->ast_events);

    for ( ;; ) {
        if ( connect_nonb(s->fd, (struct sockaddr *) &s->sin, sizeof(s->sin), 2) < 0 ) {
            if (errno == EISCONN) {
                pthread_mutex_lock(&s->lock);
                s->fd = socket(AF_INET, SOCK_STREAM, 0);
                pthread_mutex_unlock(&s->lock);
            }
            if (debug)
                debugmsg("asterisk@%s: Connect failed, Retrying (%d) %s",
                         ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr), r, strerror(errno) );
            if (pc.maxretries && (++r>pc.maxretries) ) {
                res = 1;
                break;
            } else
                sleep(pc.retryinterval);
        } else {
            /* Send login */
            s->output->write(s, &m);
            res = 0;
            break;
        }
    }

    return res;
}

int StartServer(struct ast_server *srv) {

    struct mansession *s;
    struct hostent *ast_hostent;

    char iabuf[INET_ADDRSTRLEN];
    pthread_attr_t attr;

    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

    ast_hostent = gethostbyname(srv->ast_host);
    if (!ast_hostent) {
        logmsg("Cannot resolve host %s, cannot add!", srv->ast_host);
        debugmsg("Cannot resolve host %s, cannot add!", srv->ast_host);
        return 1;
    }

    s = malloc(sizeof(struct mansession));
    if ( !s ) {
        logmsg("Failed to allocate server session: %s\n", strerror(errno));
        debugmsg("Failed to allocate server session: %s\n", strerror(errno));
        return 1;
    }

    memset(s, 0, sizeof(struct mansession));
    SetIOHandlers(s, "standard", "standard");
    s->server = srv;

    bzero((char *) &s->sin,sizeof(s->sin));
    s->sin.sin_family = AF_INET;
    memcpy( &s->sin.sin_addr.s_addr, ast_hostent->h_addr, ast_hostent->h_length );
    s->sin.sin_port = htons(atoi(s->server->ast_port));
    s->fd = socket(AF_INET, SOCK_STREAM, 0);

    pthread_mutex_lock(&sessionlock);
    s->next = sessions;
    sessions = s;
    pthread_mutex_unlock(&sessionlock);

    logmsg("Allocated Asterisk server session for %s", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
    if (debug) {
        debugmsg("asterisk@%s: Allocated server session", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
        debugmsg("Set %s input format to %s", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr), s->input->formatname);
        debugmsg("Set %s output format to %s", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr), s->output->formatname);
    }


    if (pthread_create(&s->t, &attr, (void *)HandleAsterisk, s))
        destroy_session(s);
    else
        debugmsg("launched ast %s thread!", s->server->ast_host);

    pthread_attr_destroy(&attr);
    return 0;
}

int LaunchAsteriskThreads() {

    struct ast_server *srv;

    srv = pc.serverlist;
    while (srv) {
        StartServer(srv);
        srv = srv->next;
    }
    return 0;
}

int SetIOHandlers(struct mansession *s, char *ifmt, char *ofmt)
{
    int res = 0;
    struct iohandler *io;

    io = iohandlers;
    pthread_mutex_lock(&s->lock);
    while (io) {
        if ( !strcasecmp(io->formatname, ifmt) )
            s->input = io;

        if ( !strcasecmp(io->formatname, ofmt) )
            s->output = io;

	io = io->next;
    }

    if (!s->output) {
	/* TODO: Output debug that default/first handler was used */
        s->output = iohandlers;
        res = 1;
    }

    if (!s->input) {
	/* TODO: Output debug that default/first handler was used */
        s->input = iohandlers;
        res = 1;
    }
    pthread_mutex_unlock(&s->lock);

    return res;
}

static void *accept_thread()
{
        int as;
        struct sockaddr_in sin;
        int sinlen;
        struct mansession *s;
        struct protoent *p;
        int arg = 1;
        int flags;
        pthread_attr_t attr;
        char iabuf[INET_ADDRSTRLEN];
        int is_encrypted;

        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

        for (;;) {
                sinlen = sizeof(sin);
                as = accept(asock, (struct sockaddr *)&sin, &sinlen);
                if (as < 0) {
                        logmsg("Accept returned -1: %s\n", strerror(errno));
                        continue;
                }
                p = (struct protoent *)getprotobyname("tcp");
                if( p ) {
                        if( setsockopt(as, p->p_proto, TCP_NODELAY, (char *)&arg, sizeof(arg) ) < 0 ) {
                                logmsg("Failed to set listener tcp connection to TCP_NODELAY mode: %s\n", strerror(errno));
                        }
                }

		/* SSL stuff below */
                is_encrypted = is_encrypt_request(pc.sslclhellotimeout, as);
                if (is_encrypted > 0) {
                        if (!pc.acceptencryptedconnection) {
                                if( debug )
					debugmsg("Accepting encrypted connection disabled, closing the connection \n");
                                close_sock(as);
                                continue;
                        } else {
                                if((as = saccept(as)) >= 0 ) {
					if( debug )
	                                        debugmsg("Can't accept the ssl connection, since SSL init has failed for certificate reason\n");
                                        close_sock(as);
                                        continue;
                                }
                        }
                } else if (is_encrypted == -1) {
                        logmsg("SSL version 2 is unsecure, we don't support it\n");
                        close_sock(as);
                        continue;
                }
                if ( (! pc.acceptunencryptedconnection) && (as >= 0)) {
                        logmsg("Unencrypted connections are not accepted and we received an unencrypted connection request\n");
                        close_sock(as);
                        continue;
                }
		/* SSL stuff end */

                s = malloc(sizeof(struct mansession));
                if ( !s ) {
                        logmsg("Failed to allocate listener session: %s\n", strerror(errno));
                        continue;
                }
                memset(s, 0, sizeof(struct mansession));
                memcpy(&s->sin, &sin, sizeof(sin));

                /* For safety, make sure socket is non-blocking */
                flags = fcntl(as, F_GETFL);
		fcntl(get_real_fd(as), F_SETFL, flags | O_NONBLOCK);

                pthread_mutex_init(&s->lock, NULL);
                s->fd = as;
                SetIOHandlers(s, pc.inputformat, pc.outputformat);
                s->autofilter = pc.autofilter;
                s->inputcomplete = 0;
                s->server = NULL;

                pthread_mutex_lock(&sessionlock);
                s->next = sessions;
                sessions = s;
                pthread_mutex_unlock(&sessionlock);

                logmsg("Connection received from %s", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
                if (debug) {
                   debugmsg("Connection received from %s", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
                   debugmsg("Set %s input format to %s", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr), s->input->formatname);
                   debugmsg("Set %s output format to %s", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr), s->output->formatname);
                }

                if (pthread_create(&s->t, &attr, (void *)session_do, s))
                    destroy_session(s);
        }
        pthread_attr_destroy(&attr);
        return NULL;
}

int main(int argc, char *argv[])
{
    struct sockaddr_in serv_sock_addr, client_sock_addr; 
    int cli_addrlen;
    struct linger lingerstruct;        /* for socket reuse */
    int flag;                          /* for socket reuse */
    pid_t pid;
    char i;

    /* Figure out if we are in debug mode, handle other switches */
    while (( i = getopt( argc, argv, "dhv" ) ) != EOF )
    {
        switch( i ) {
            case 'd':
                debug = 1;
                break;
            case 'h':
                Usage();
                exit(0);
            case 'v':
                Version();
                exit(0);
            case '?':
                Usage();
                exit(1);
        }
    }


    ReadConfig();
    proxylog = OpenLogfile();
    LoadHandlers();

    if (SetProcUID()) {
        fprintf(stderr,"Cannot set user/group!  Check proc_user and proc_group config setting!\n");
        exit(1);
    }

    /* If we are not in debug mode, then fork to background */
    if (!debug) {
        if ( (pid = fork()) < 0)
            exit( 1 );
        else if ( pid > 0)
            exit( 0 );
    }

    /* Setup signal handlers */
    (void) signal(SIGINT,leave);
    (void) signal(SIGHUP,hup);
    (void) signal(SIGTERM,leave);
    (void) signal(SIGPIPE, SIG_IGN);

    /* Initialize global mutexes */
    pthread_mutex_init(&sessionlock, NULL);
    pthread_mutex_init(&userslock, NULL);
    pthread_mutex_init(&loglock, NULL);
    pthread_mutex_init(&debuglock, NULL);

    /* Read initial state for user permissions */
    ReadPerms();

    /* Initialize global client/server list */
    sessions = NULL;
    LaunchAsteriskThreads();

    /* Setup listener socket to setup new sessions... */
    if ((asock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        fprintf(stderr,"Cannot create listener socket!\n");
        exit(1);
    }
    bzero((char *) &serv_sock_addr, sizeof serv_sock_addr );
    serv_sock_addr.sin_family = AF_INET;

    if ( !strcmp(pc.listen_addr,"*") )
        serv_sock_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    else
        serv_sock_addr.sin_addr.s_addr = inet_addr( pc.listen_addr);
    serv_sock_addr.sin_port = htons((short)pc.listen_port);

    /* Set listener socket re-use options */
    setsockopt(asock, SOL_SOCKET, SO_REUSEADDR, (void *)&flag, sizeof(flag));
    lingerstruct.l_onoff = 1;
    lingerstruct.l_linger = 5;
    setsockopt(asock, SOL_SOCKET, SO_LINGER, (void *)&lingerstruct, sizeof(lingerstruct));
    
    if (bind(asock, (struct sockaddr *) &serv_sock_addr, sizeof serv_sock_addr ) < 0) {
        fprintf(stderr,"Cannot bind to listener socket!\n");
        exit(1);
    }

    listen(asock, 5);
    cli_addrlen = sizeof(client_sock_addr);
    if (debug)
        debugmsg("Listening for connections");
    logmsg("Proxy Started: Listening for connections");

    /* Launch listener thread */
    accept_thread();

    pthread_exit(NULL);
    exit(0);
}