Changeset 1937


Ignore:
Timestamp:
Apr 22, 2008 6:32:53 PM (16 years ago)
Author:
bennylp
Message:

Added ability to send custom headers in sipstateless sample

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip-apps/src/samples/sipstateless.c

    r1405 r1937  
    4848/* What response code to be sent (default is 501/Not Implemented) */ 
    4949static int code = PJSIP_SC_NOT_IMPLEMENTED; 
     50 
     51/* Additional header list */ 
     52struct pjsip_hdr hdr_list; 
     53 
     54/* usage() */ 
     55static void usage(void) 
     56{ 
     57    puts("Usage:"); 
     58    puts("  sipstateless [code] [-H HDR] .."); 
     59    puts(""); 
     60    puts("Options:"); 
     61    puts("  code     SIP status code to send (default: 501/Not Implemented"); 
     62    puts("  -H HDR   Specify additional headers to send with response"); 
     63    puts("           This option may be specified more than once."); 
     64    puts("           Example:"); 
     65    puts("              -H 'Expires: 300' -H 'Contact: <sip:localhost>'");  
     66} 
    5067 
    5168 
     
    5976        pjsip_endpt_respond_stateless( sip_endpt, rdata,  
    6077                                       code, NULL, 
    61                                        NULL, NULL); 
     78                                       &hdr_list, NULL); 
    6279    } 
    6380    return PJ_TRUE; 
     
    7390{ 
    7491    pj_caching_pool cp; 
     92    pj_pool_t *pool = NULL; 
    7593    pjsip_module mod_app = 
    7694    { 
     
    89107        NULL,                       /* on_tsx_state()           */ 
    90108    }; 
    91  
    92  
     109    int c; 
    93110    pj_status_t status; 
    94111     
    95     if (argc == 2) 
    96         code = atoi(argv[1]); 
    97  
    98112    /* Must init PJLIB first: */ 
    99113    status = pj_init(); 
     
    107121    /* Must create a pool factory before we can allocate any memory. */ 
    108122    pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); 
    109  
    110123 
    111124    /* Create global endpoint: */ 
     
    122135    } 
    123136 
     137    /* Parse arguments */ 
     138    pj_optind = 0; 
     139    pj_list_init(&hdr_list); 
     140    while ((c=pj_getopt(argc, argv , "H:")) != -1) { 
     141        switch (c) { 
     142        case 'H': 
     143            if (pool == NULL) { 
     144                pool = pj_pool_create(&cp.factory, "sipstateless", 1000,  
     145                                      1000, NULL); 
     146            }  
     147             
     148            if (pool) { 
     149                char *name; 
     150                name = strtok(pj_optarg, ":"); 
     151                if (name == NULL) { 
     152                    puts("Error: invalid header format"); 
     153                    return 1; 
     154                } else { 
     155                    char *val = strtok(NULL, "\r\n"); 
     156                    pjsip_generic_string_hdr *h; 
     157                    pj_str_t hname, hvalue; 
     158 
     159                    hname = pj_str(name); 
     160                    hvalue = pj_str(val); 
     161 
     162                    h = pjsip_generic_string_hdr_create(pool, &hname, &hvalue); 
     163 
     164                    pj_list_push_back(&hdr_list, h); 
     165 
     166                    PJ_LOG(4,(THIS_FILE, "Header %s: %s added", name, val)); 
     167                } 
     168            } 
     169            break; 
     170        default: 
     171            puts("Error: invalid argument"); 
     172            usage(); 
     173            return 1; 
     174        } 
     175    } 
     176 
     177    if (pj_optind != argc) { 
     178        code = atoi(argv[pj_optind]); 
     179        if (code < 200 || code > 699) { 
     180            puts("Error: invalid status code"); 
     181            usage(); 
     182            return 1; 
     183        } 
     184    } 
     185 
     186    PJ_LOG(4,(THIS_FILE, "Returning %d to incoming requests", code)); 
     187 
     188 
    124189    /*  
    125190     * Add UDP transport, with hard-coded port  
Note: See TracChangeset for help on using the changeset viewer.