Log In
New Account
  
 
Home My Page Project Tree Code Snippets Project Openings NUTSS
 
 
Summary Tracker Lists CVS Files
 

CVS | Administration

Annotation of libnutss/examples/nutssd.c, revision 1.2

1.1       saikat      1: /**
1.2     ! saikat      2:  * $Id: nutssd.c,v 1.1.1.1 2007/04/24 11:42:45 saikat Exp $
1.1       saikat      3:  * Copyright (C) 2007  Saikat Guha <saikat@cs.cornell.edu>
                      4:  *
                      5:  * This program is free software; you can redistribute it and/or
                      6:  * modify it under the terms of the GNU General Public License
                      7:  * as published by the Free Software Foundation; either version 2
                      8:  * of the License, or (at your option) any later version.
                      9:  *
                     10:  * This program is distributed in the hope that it will be useful,
                     11:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
                     12:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     13:  * GNU General Public License for more details.
                     14:  *
                     15:  * You should have received a copy of the GNU General Public License
                     16:  * along with this program; if not, write to the Free Software
                     17:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
                     18:  **/
                     19:
                     20: #include <stdio.h>
                     21: #include <stdlib.h>
                     22: #include <string.h>
1.2     ! saikat     23: #include <unistd.h>
        !            24: #include <sys/types.h>
        !            25: #include <sys/stat.h>
        !            26: #include <fcntl.h>
        !            27: #include <signal.h>
1.1       saikat     28: #include <semaphore.h>
                     29: #include <string.h>
                     30: #include <errno.h>
                     31:
                     32: #define _GNU_SOURCE
                     33: #include <getopt.h>
                     34:
                     35: #include "nutss.h"
                     36:
                     37: sem_t sem;
                     38:
                     39: void usage(char *argv0);
                     40:
                     41: void
                     42: usage(char *argv0)
                     43: {
                     44:     printf("Usage:\n"
                     45:         "\t%s [OPTIONS] [SERVICES] user@domain\n"
                     46:         "\n"
                     47:         "Services:\n"
                     48:         "  --stuntd,-s\t\tEnable STUNT server\n"
                     49:         "  --turnd,-t\t\tEnable TURN server\n"
                     50:         "  --swaplinksd,-w\tEnable Swaplinks server\n"
                     51:         "\n"
                     52:         "Options:\n"
                     53:         "  --registrar,-r server:port\tUse sip registrar at server\n"
1.2     ! saikat     54:         "  --password,-p pass\t\tUse password for registering with proxy\n"
        !            55:         "  --nofork,-n\t\t\tDon't fork\n",
1.1       saikat     56:         "nutssd");
                     57:     exit(1);
                     58: }
                     59:
1.2     ! saikat     60: void signal_handler(int sig)
        !            61: {
        !            62:     switch(sig) {
        !            63:         case SIGHUP:
        !            64:             break;
        !            65:         case SIGTERM:
        !            66:             exit(0);
        !            67:             break;
        !            68:     }
        !            69: }
        !            70:
1.1       saikat     71: int
                     72: main(int argc, char **argv)
                     73: {
1.2     ! saikat     74:     int c, nofork = 0, l = -1;
1.1       saikat     75:     int idx = 0;
                     76:     char *tmp;
                     77:     static int one = 1;
                     78:     static struct option opts[] = {
                     79:         {"stuntd", 0, 0, 's'},
                     80:         {"turnd", 0, 0, 't'},
                     81:         {"swaplinksd", 0, 0, 'w'},
                     82:         {"registrar", 1, 0, 'r'},
                     83:         {"password", 1, 0, 'p'},
1.2     ! saikat     84:         {"nofork", 0, 0, 'n'},
        !            85:         {"help", 0, 0, 'h'},
        !            86:         {"logfile", 1, 0, 'l'},
1.1       saikat     87:         {0, 0, 0, 0}
                     88:     };
                     89:
                     90:     nutss_config_set(NUTSS_CONFIG_OPT_BIND, &one, sizeof(one));
                     91:     while (1) {
1.2     ! saikat     92:         c = getopt_long(argc, argv, "hp:r:l:stwn", opts, &idx);
1.1       saikat     93:
                     94:         if (c == -1)
                     95:             break;
                     96:
                     97:         switch (c) {
                     98:             case 's':
                     99:                 nutss_config_set(NUTSS_CONFIG_OPT_STUNTD, &one,
                    100:                     sizeof(one));
                    101:                 break;
                    102:             case 't':
                    103:                 nutss_config_set(NUTSS_CONFIG_OPT_TURND, &one, sizeof(one));
                    104:                 break;
                    105:             case 'w':
                    106:                 nutss_config_set(NUTSS_CONFIG_OPT_SWAPLINKSD, &one,
                    107:                     sizeof(one));
                    108:                 break;
                    109:             case 'r':
                    110:                 nutss_config_set(NUTSS_CONFIG_SIGPROXY, optarg,
                    111:                     strlen(optarg) + 1);
                    112:                 break;
                    113:             case 'p':
                    114:                 nutss_config_set(NUTSS_CONFIG_PROXYPASSWORD, optarg,
                    115:                     strlen(optarg) + 1);
                    116:                 break;
1.2     ! saikat    117:             case 'n':
        !           118:                 nofork = 1;
        !           119:                 break;
        !           120:             case 'l':
        !           121:                 if (l != -1)
        !           122:                     close(l);
        !           123:                 l = open(optarg, O_RDWR|O_APPEND|O_CREAT|O_SYNC, S_IRUSR|S_IWUSR);
        !           124:                 break;
1.1       saikat    125:             case 'h':
                    126:             default:
                    127:                 usage(argv[0]);
                    128:         }
                    129:     }
                    130:
                    131:     if (optind >= argc)
                    132:         usage(argv[0]);
                    133:
                    134:     tmp = strchr(argv[optind], '@');
                    135:     if (!tmp)
                    136:         usage(argv[0]);
                    137:     tmp[0] = '\0';
                    138:     tmp++;
                    139:     nutss_config_set(NUTSS_CONFIG_USERNAME, argv[optind],
                    140:         strlen(argv[optind]) + 1);
                    141:     nutss_config_set(NUTSS_CONFIG_PROXYUSERNAME, argv[optind],
                    142:         strlen(argv[optind]) + 1);
                    143:     nutss_config_set(NUTSS_CONFIG_DOMAINNAME, tmp, strlen(tmp) + 1);
                    144:     optind++;
                    145:
1.2     ! saikat    146:     if (!nofork && daemon(0,0)) {
        !           147:         perror("nutssd");
        !           148:         exit(1);
        !           149:     }
        !           150:
        !           151:     if (l != -1) {
        !           152:         // Connect stdin and stdout to log file
        !           153:         dup2(l,1);
        !           154:         dup2(l,2);
        !           155:         close(l);
        !           156:     }
        !           157:
        !           158:     signal(SIGHUP, signal_handler);
        !           159:     signal(SIGTERM, signal_handler);
        !           160:
1.1       saikat    161:     sem_init(&sem, 0, 0);
                    162:     nutss_init();
                    163:     while (sem_wait(&sem) == -1 && errno == EINTR);
                    164:
1.2     ! saikat    165:     exit(0);
1.1       saikat    166: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>
 

GForge.cis.cornell.edu is brought to you by

Cornell Computing and Information Science


Powered By GForge Collaborative Development Environment