1/* @(#) parsing functions for udpxy
2 *
3 * Copyright 2008-2011 Pavel V. Cherenkov (pcherenkov@gmail.com)
4 *
5 *  This file is part of udpxy.
6 *
7 *  udpxy is free software: you can redistribute it and/or modify
8 *  it under the terms of the GNU General Public License as published by
9 *  the Free Software Foundation, either version 3 of the License, or
10 *  (at your option) any later version.
11 *
12 *  udpxy is distributed in the hope that it will be useful,
13 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *  GNU General Public License for more details.
16 *
17 *  You should have received a copy of the GNU General Public License
18 *  along with udpxy.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <string.h>
22#include <sys/types.h>
23#include <stdlib.h>
24#include <stdio.h>
25#include <errno.h>
26
27#include <sys/socket.h>
28#include <netinet/in.h>
29#include <arpa/inet.h>
30
31#include <assert.h>
32
33#include "rparse.h"
34#include "mtrace.h"
35
36/* parse and copy parameters of HTTP GET request into
37 * request buffer
38 *
39 * @param src       buffer with raw source data
40 * @param srclen    length of raw data
41 * @param request   destination buffer for the request
42 * @param rqlen     length of request buffer on entry
43 *                  length of request on exit
44 *
45 * @return 0 if success: request buffer gets populated,
46 *         non-zero if error
47 */
48int
49get_request( const char* src, size_t srclen,
50             char* request, size_t* rqlen )
51{
52    const char HEAD[] = "GET /";
53    char* p = NULL;
54    const char* EOD = src + srclen - 1;
55    size_t n = 0;
56    const char SPACE[] = " ";
57
58    p = strstr( src, HEAD );
59    if( NULL == p ) return 1;   /* no header */
60
61    p += (sizeof(HEAD) - 1);
62    if( p >= EOD)               /* no request */
63        return 2;
64
65    n = strcspn( p, " " );
66    if( (SPACE[0] != p[n]) || ((p + n) > EOD) || (n >= *rqlen) ) /* overflow */
67        return 3;
68
69    (void) strncpy( request, p, n );
70    request[ n ] = '\0';
71
72    *rqlen = n;
73
74    return 0;
75}
76
77
78/* parse (GET) request into command and parameters (options)
79 * c-strings
80 *
81 * @param s         source c-string
82 * @param cmd       buffer for the parsed command c-string
83 * @param clen      length of command buffer
84 * @param opt       buffer for the parsed options c-string
85 * @param optlen    length of options buffer
86 * @param tail      buffer for tail (whatever is beyond options)
87 * @param tlen      length of tail buffer
88 *
89 * @return 0 if success: cmd and opt get get populated
90 *         non-zero if an error ocurred
91 */
92int
93parse_param( const char* s, size_t slen,
94             char* cmd,     size_t clen,
95             char* opt,     size_t optlen,
96             char* tail,    size_t tlen)
97{
98    const char DLM = '/';
99    size_t i, j, n = 0;
100
101    assert( s && cmd && (clen > (size_t)0) && opt && (optlen > (size_t)0) );
102
103    *cmd = *opt = '\0';
104    if( (size_t)0 == slen ) return 0;   /* empty source */
105
106    /* request ::= [DLM] cmd [DLM opt] */
107
108    /* skip leading delimiter */
109    i = ( DLM == s[0] ) ? 1 : 0;
110
111    /* copy into cmd until next delimiter or EOD */
112    for( j = 0; (i < slen) && (j < clen) && s[i] && (s[i] != DLM); ) {
113        cmd[j++] = s[i++];
114    }
115    if( j >= clen ) return EOVERFLOW;
116    cmd[ j ] = '\0';
117
118    /* skip dividing delimiter */
119    if( DLM == s[i] )
120        ++i;
121
122    /* over the edge yet? */
123    if (i >= slen)
124        return 0;
125
126    /* look for '?' separating options from tail */
127    n = strcspn(s + i, "?");
128    if (n < optlen) {
129        (void) strncpy( opt, s + i, n );
130        opt[n] = '\0';
131    }
132    else
133        return EOVERFLOW;
134
135    i += n;
136    if (i >= slen) return 0;
137
138    if (tail && tlen > 0) {
139        (void) strncpy(tail, s + i, tlen);
140        tail[tlen - 1] = '\0';
141    }
142
143    return 0;
144}
145
146
147
148/* parse options of upd-relay command into IP address
149 * and port
150 *
151 * @param opt       options string
152 * @param addr      destination for address string
153 * @param addrlen   length of address string buffer
154 * @param port      port to populate
155 *
156 * @return 0 if success: inaddr and port get populated
157 *         non-zero if error
158 */
159int
160parse_udprelay( const char*  opt, size_t optlen,
161                char* addr,       size_t addrlen,
162                uint16_t* port )
163{
164    int rc = 1;
165    size_t n;
166    int pval;
167
168    const char* SEP = ":%~+-^";
169    const int MAX_PORT = 65535;
170
171    #define MAX_OPTLEN 512
172    char s[ MAX_OPTLEN ];
173
174    assert( opt && addr && addrlen && port );
175
176    (void) strncpy( s, opt, MAX_OPTLEN );
177    s[ MAX_OPTLEN - 1 ] = '\0';
178    do {
179        n = strcspn( s, SEP );
180        if( !n || n >= optlen ) break;
181        s[n] = '\0';
182
183        strncpy( addr, s, addrlen );
184        addr[ addrlen - 1 ] ='\0';
185
186        ++n;
187        pval = atoi( s + n );
188        if( (pval > 0) && (pval < MAX_PORT) ) {
189            *port = (uint16_t)pval;
190        }
191        else {
192            rc = 3;
193            break;
194        }
195
196        rc = 0;
197    }
198    while(0);
199
200    return rc;
201}
202
203/* __EOF__ */
204
205