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.3

1.1       saikat      1: /**
1.3     ! saikat      2:  * $Id: nutssd.c,v 1.2 2007/05/03 13:29:44 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"
1.3     ! saikat     55:         "  --logfile,-l file\t\tSend log output to file\n"
1.2       saikat     56:         "  --nofork,-n\t\t\tDon't fork\n",
1.1       saikat     57:         "nutssd");
                     58:     exit(1);
                     59: }
                     60:
1.2       saikat     61: void signal_handler(int sig)
                     62: {
                     63:     switch(sig) {
                     64:         case SIGHUP:
                     65:             break;
                     66:         case SIGTERM:
                     67:             exit(0);
                     68:             break;
                     69:     }
                     70: }
                     71:
1.1       saikat     72: int
                     73: main(int argc, char **argv)
                     74: {
1.2       saikat     75:     int c, nofork = 0, l = -1;
1.1       saikat     76:     int idx = 0;
                     77:     char *tmp;
                     78:     static int one = 1;
                     79:     static struct option opts[] = {
                     80:         {"stuntd", 0, 0, 's'},
                     81:         {"turnd", 0, 0, 't'},
                     82:         {"swaplinksd", 0, 0, 'w'},
                     83:         {"registrar", 1, 0, 'r'},
                     84:         {"password", 1, 0, 'p'},
1.2       saikat     85:         {"nofork", 0, 0, 'n'},
                     86:         {"help", 0, 0, 'h'},
                     87:         {"logfile", 1, 0, 'l'},
1.1       saikat     88:         {0, 0, 0, 0}
                     89:     };
                     90:
                     91:     nutss_config_set(NUTSS_CONFIG_OPT_BIND, &one, sizeof(one));
                     92:     while (1) {
1.2       saikat     93:         c = getopt_long(argc, argv, "hp:r:l:stwn", opts, &idx);
1.1       saikat     94:
                     95:         if (c == -1)
                     96:             break;
                     97:
                     98:         switch (c) {
                     99:             case 's':
                    100:                 nutss_config_set(NUTSS_CONFIG_OPT_STUNTD, &one,
                    101:                     sizeof(one));
                    102:                 break;
                    103:             case 't':
                    104:                 nutss_config_set(NUTSS_CONFIG_OPT_TURND, &one, sizeof(one));
                    105:                 break;
                    106:             case 'w':
                    107:                 nutss_config_set(NUTSS_CONFIG_OPT_SWAPLINKSD, &one,
                    108:                     sizeof(one));
                    109:                 break;
                    110:             case 'r':
                    111:                 nutss_config_set(NUTSS_CONFIG_SIGPROXY, optarg,
                    112:                     strlen(optarg) + 1);
                    113:                 break;
                    114:             case 'p':
                    115:                 nutss_config_set(NUTSS_CONFIG_PROXYPASSWORD, optarg,
                    116:                     strlen(optarg) + 1);
                    117:                 break;
1.2       saikat    118:             case 'n':
                    119:                 nofork = 1;
                    120:                 break;
                    121:             case 'l':
                    122:                 if (l != -1)
                    123:                     close(l);
                    124:                 l = open(optarg, O_RDWR|O_APPEND|O_CREAT|O_SYNC, S_IRUSR|S_IWUSR);
                    125:                 break;
1.1       saikat    126:             case 'h':
                    127:             default:
                    128:                 usage(argv[0]);
                    129:         }
                    130:     }
                    131:
                    132:     if (optind >= argc)
                    133:         usage(argv[0]);
                    134:
                    135:     tmp = strchr(argv[optind], '@');
                    136:     if (!tmp)
                    137:         usage(argv[0]);
                    138:     tmp[0] = '\0';
                    139:     tmp++;
                    140:     nutss_config_set(NUTSS_CONFIG_USERNAME, argv[optind],
                    141:         strlen(argv[optind]) + 1);
                    142:     nutss_config_set(NUTSS_CONFIG_PROXYUSERNAME, argv[optind],
                    143:         strlen(argv[optind]) + 1);
                    144:     nutss_config_set(NUTSS_CONFIG_DOMAINNAME, tmp, strlen(tmp) + 1);
                    145:     optind++;
                    146:
1.2       saikat    147:     if (!nofork && daemon(0,0)) {
                    148:         perror("nutssd");
                    149:         exit(1);
                    150:     }
                    151:
                    152:     if (l != -1) {
                    153:         // Connect stdin and stdout to log file
                    154:         dup2(l,1);
                    155:         dup2(l,2);
                    156:         close(l);
                    157:     }
                    158:
                    159:     signal(SIGHUP, signal_handler);
                    160:     signal(SIGTERM, signal_handler);
                    161:
1.1       saikat    162:     sem_init(&sem, 0, 0);
                    163:     nutss_init();
                    164:     while (sem_wait(&sem) == -1 && errno == EINTR);
                    165:
1.2       saikat    166:     exit(0);
1.1       saikat    167: }

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