Branch data Line data Source code
1 : : /**
2 : : * \file lib/fko_nat_access.c
3 : : *
4 : : * \brief Set/Get the spa nat access request data.
5 : : */
6 : :
7 : : /* Fwknop is developed primarily by the people listed in the file 'AUTHORS'.
8 : : * Copyright (C) 2009-2015 fwknop developers and contributors. For a full
9 : : * list of contributors, see the file 'CREDITS'.
10 : : *
11 : : * License (GNU General Public License):
12 : : *
13 : : * This program is free software; you can redistribute it and/or
14 : : * modify it under the terms of the GNU General Public License
15 : : * as published by the Free Software Foundation; either version 2
16 : : * of the License, or (at your option) any later version.
17 : : *
18 : : * This program is distributed in the hope that it will be useful,
19 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 : : * GNU General Public License for more details.
22 : : *
23 : : * You should have received a copy of the GNU General Public License
24 : : * along with this program; if not, write to the Free Software
25 : : * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 : : * USA
27 : : *
28 : : *****************************************************************************
29 : : */
30 : : #include "fko_common.h"
31 : : #include "fko.h"
32 : :
33 : : /* Set the SPA Nat Access data
34 : : */
35 : : int
36 : 323 : fko_set_spa_nat_access(fko_ctx_t ctx, const char * const msg)
37 : : {
38 : 323 : int res = FKO_SUCCESS;
39 : :
40 : : #if HAVE_LIBFIU
41 [ + - ]: 323 : fiu_return_on("fko_set_spa_nat_access_init", FKO_ERROR_CTX_NOT_INITIALIZED);
42 : : #endif
43 : :
44 : : /* Context must be initialized.
45 : : */
46 [ + + ][ + - ]: 323 : if(!CTX_INITIALIZED(ctx))
47 : : return FKO_ERROR_CTX_NOT_INITIALIZED;
48 : :
49 : : /* Gotta have a valid string.
50 : : */
51 [ + + ][ + + ]: 293 : if(msg == NULL || strnlen(msg, MAX_SPA_NAT_ACCESS_SIZE) == 0)
52 : : return(FKO_ERROR_INVALID_DATA_NAT_EMPTY);
53 : :
54 : : #if HAVE_LIBFIU
55 [ + - ]: 242 : fiu_return_on("fko_set_spa_nat_access_empty", FKO_ERROR_INVALID_DATA_NAT_EMPTY);
56 : : #endif
57 : :
58 : : /* --DSS XXX: Bail out for now. But consider just
59 : : * truncating in the future...
60 : : */
61 [ + + ]: 242 : if(strnlen(msg, MAX_SPA_NAT_ACCESS_SIZE) == MAX_SPA_NAT_ACCESS_SIZE)
62 : : return(FKO_ERROR_DATA_TOO_LARGE);
63 : :
64 : : #if HAVE_LIBFIU
65 [ + - ]: 238 : fiu_return_on("fko_set_spa_nat_access_large", FKO_ERROR_DATA_TOO_LARGE);
66 : : #endif
67 : :
68 [ + + ]: 238 : if((res = validate_nat_access_msg(msg)) != FKO_SUCCESS)
69 : : return(res);
70 : :
71 : : /* Just in case this is a subsequent call to this function. We
72 : : * do not want to be leaking memory.
73 : : */
74 [ + + ]: 175 : if(ctx->nat_access != NULL)
75 : 96 : free(ctx->nat_access);
76 : :
77 : 175 : ctx->nat_access = strdup(msg);
78 : :
79 : 175 : ctx->state |= FKO_DATA_MODIFIED;
80 : :
81 [ + - ]: 175 : if(ctx->nat_access == NULL)
82 : : return(FKO_ERROR_MEMORY_ALLOCATION);
83 : :
84 : : /* If we set the nat_access message Then we force the message_type
85 : : * as well. Technically, the message type should be set already.
86 : : * This will serve a half-protective measure.
87 : : * --DSS XXX: should do this better.
88 : : */
89 [ + + ]: 175 : if(ctx->client_timeout > 0)
90 : : {
91 [ + + ]: 85 : if(ctx->message_type != FKO_CLIENT_TIMEOUT_LOCAL_NAT_ACCESS_MSG)
92 : 59 : ctx->message_type = FKO_CLIENT_TIMEOUT_NAT_ACCESS_MSG;
93 : : }
94 : : else
95 [ + + ]: 90 : if(ctx->message_type != FKO_LOCAL_NAT_ACCESS_MSG
96 : 90 : && ctx->message_type != FKO_CLIENT_TIMEOUT_LOCAL_NAT_ACCESS_MSG)
97 : 72 : ctx->message_type = FKO_NAT_ACCESS_MSG;
98 : :
99 : : return(FKO_SUCCESS);
100 : : }
101 : :
102 : : /* Return the SPA message data.
103 : : */
104 : : int
105 : 5517 : fko_get_spa_nat_access(fko_ctx_t ctx, char **nat_access)
106 : : {
107 : :
108 : : #if HAVE_LIBFIU
109 [ + + ]: 5517 : fiu_return_on("fko_get_spa_nat_access_init", FKO_ERROR_CTX_NOT_INITIALIZED);
110 : : #endif
111 : :
112 : : /* Must be initialized
113 : : */
114 [ + + ][ + - ]: 5515 : if(!CTX_INITIALIZED(ctx))
115 : : return(FKO_ERROR_CTX_NOT_INITIALIZED);
116 : :
117 [ + + ]: 5363 : if(nat_access == NULL)
118 : : return(FKO_ERROR_INVALID_DATA);
119 : :
120 : : #if HAVE_LIBFIU
121 [ + + ]: 5295 : fiu_return_on("fko_get_spa_nat_access_val", FKO_ERROR_INVALID_DATA);
122 : : #endif
123 : :
124 : 5293 : *nat_access = ctx->nat_access;
125 : :
126 : 5293 : return(FKO_SUCCESS);
127 : : }
128 : :
129 : : /***EOF***/
|