File: conf.c

package info (click to toggle)
netw-ib-ox-ag 5.36.0-1.2
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 20,128 kB
  • ctags: 19,282
  • sloc: ansic: 177,182; tcl: 10,705; makefile: 2,785; sh: 1,050; cpp: 900
file content (258 lines) | stat: -rw-r--r-- 7,653 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* This file is part of Netwib. Read and honor its license.
*/

/*-------------------------------------------------------------*/
netwib_priv_conf_t netwib_priv_conf;
netwib_priv_mutex netwib_priv_conf_mut;
netwib_bool netwib_priv_conf_needtobeupdated = NETWIB_TRUE;

/*-------------------------------------------------------------*/
static netwib_err netwib_priv_conft_init(netwib_priv_conf_t *pct)
{
  netwib_er(netwib_ring_init(&netwib_conf_devices_erase, NULL, &pct->pdevices));
  netwib_er(netwib_ring_init(&netwib_conf_ip_erase, NULL, &pct->pip));
  netwib_er(netwib_ring_init(&netwib_conf_arpcache_erase, NULL, &pct->parpcache));
  netwib_er(netwib_ring_init(&netwib_conf_routes_erase, NULL, &pct->proutes));
  return(NETWIB_ERR_OK);
}

/*-------------------------------------------------------------*/
static netwib_err netwib_priv_conft_close(netwib_priv_conf_t *pct)
{
  netwib_er(netwib_ring_close(&pct->pdevices, NETWIB_TRUE));
  netwib_er(netwib_ring_close(&pct->pip, NETWIB_TRUE));
  netwib_er(netwib_ring_close(&pct->parpcache, NETWIB_TRUE));
  netwib_er(netwib_ring_close(&pct->proutes, NETWIB_TRUE));
  return(NETWIB_ERR_OK);
}

/*-------------------------------------------------------------*/
static netwib_err netwib_priv_conft_add_confwork(netwib_priv_confwork *pcw,
                                                 netwib_priv_conf_t *pc)
{
  netwib_priv_confwork_devices *pcwd;
  netwib_priv_confwork_ip *pcwi;
  netwib_priv_confwork_arpcache *pcwa;
  netwib_priv_confwork_routes *pcwr;
  netwib_conf_devices *pcd;
  netwib_conf_ip *pci;
  netwib_conf_arpcache *pca;
  netwib_conf_routes *pcr;
  netwib_ring_index *pringindex;
  netwib_err ret;

  netwib_er(netwib_ring_index_init(pcw->pdevices, &pringindex));
  ret = NETWIB_ERR_OK;
  while (NETWIB_TRUE) {
    ret = netwib_ring_index_next(pringindex, (netwib_ptr*)&pcwd);
    if (ret != NETWIB_ERR_OK) {
      if (ret == NETWIB_ERR_DATAEND) ret = NETWIB_ERR_OK;
      break;
    }
    ret = netwib_conf_devices_init(&pcd);
    if (ret != NETWIB_ERR_OK) {
      break;
    }
    pcd->devnum = pcwd->devnum;
    netwib_er(netwib_buf_append_buf(&pcwd->device, &pcd->device));
    netwib_er(netwib_buf_append_buf(&pcwd->deviceeasy, &pcd->deviceeasy));
    pcd->mtu = pcwd->mtu;
    pcd->hwtype = pcwd->hwtype;
    pcd->eth = pcwd->eth;
    ret = netwib_conf_devices_add(pc, pcd);
    if (ret != NETWIB_ERR_OK) {
      break;
    }
  }
  netwib_er(netwib_ring_index_close(&pringindex));
  if (ret != NETWIB_ERR_OK) {
    return(ret);
  }

  netwib_er(netwib_ring_index_init(pcw->pip, &pringindex));
  ret = NETWIB_ERR_OK;
  while (NETWIB_TRUE) {
    ret = netwib_ring_index_next(pringindex, (netwib_ptr*)&pcwi);
    if (ret != NETWIB_ERR_OK) {
      if (ret == NETWIB_ERR_DATAEND) ret = NETWIB_ERR_OK;
      break;
    }
    ret = netwib_conf_ip_init(&pci);
    if (ret != NETWIB_ERR_OK) {
      break;
    }
    pci->devnum = pcwi->devnum;
    pci->ip = pcwi->ip;
    pci->mask = pcwi->mask;
    pci->prefix = pcwi->prefix;
    pci->ispointtopoint = pcwi->ispointtopoint;
    pci->pointtopointip = pcwi->pointtopointip;
    ret = netwib_conf_ip_add(pc, pci);
    if (ret != NETWIB_ERR_OK) {
      break;
    }
  }
  netwib_er(netwib_ring_index_close(&pringindex));
  if (ret != NETWIB_ERR_OK) {
    return(ret);
  }

  netwib_er(netwib_ring_index_init(pcw->parpcache, &pringindex));
  ret = NETWIB_ERR_OK;
  while (NETWIB_TRUE) {
    ret = netwib_ring_index_next(pringindex, (netwib_ptr*)&pcwa);
    if (ret != NETWIB_ERR_OK) {
      if (ret == NETWIB_ERR_DATAEND) ret = NETWIB_ERR_OK;
      break;
    }
    ret = netwib_conf_arpcache_init(&pca);
    if (ret != NETWIB_ERR_OK) {
      break;
    }
    pca->devnum = pcwa->devnum;
    pca->eth = pcwa->eth;
    pca->ip = pcwa->ip;
    ret = netwib_conf_arpcache_addi(pc, pca);
    if (ret != NETWIB_ERR_OK) {
      break;
    }
  }
  netwib_er(netwib_ring_index_close(&pringindex));
  if (ret != NETWIB_ERR_OK) {
    return(ret);
  }

  netwib_er(netwib_ring_index_init(pcw->proutes, &pringindex));
  ret = NETWIB_ERR_OK;
  while (NETWIB_TRUE) {
    ret = netwib_ring_index_next(pringindex, (netwib_ptr*)&pcwr);
    if (ret != NETWIB_ERR_OK) {
      if (ret == NETWIB_ERR_DATAEND) ret = NETWIB_ERR_OK;
      break;
    }
    ret = netwib_conf_routes_init(&pcr);
    if (ret != NETWIB_ERR_OK) {
      break;
    }
    pcr->devnum = pcwr->devnum;
    pcr->dst = pcwr->dst;
    pcr->mask = pcwr->mask;
    pcr->prefix = pcwr->prefix;
    pcr->srcset = pcwr->srcset;
    pcr->src = pcwr->src;
    pcr->gwset = pcwr->gwset;
    pcr->gw = pcwr->gw;
    pcr->metric = pcwr->metric;
    ret = netwib_conf_routes_add(pc, pcr);
    if (ret != NETWIB_ERR_OK) {
      break;
    }
  }
  netwib_er(netwib_ring_index_close(&pringindex));
  if (ret != NETWIB_ERR_OK) {
    return(ret);
  }

  return(NETWIB_ERR_OK);
}

/*-------------------------------------------------------------*/
static netwib_err netwib_priv_conft_swap(netwib_priv_conf_t *pcsrc,
                                         netwib_priv_conf_t *pcdst)
{
  netwib_ring *p;

  p = pcdst->pdevices;
  pcdst->pdevices = pcsrc->pdevices;
  pcsrc->pdevices = p;
  p = pcdst->pip;
  pcdst->pip = pcsrc->pip;
  pcsrc->pip = p;
  p = pcdst->parpcache;
  pcdst->parpcache = pcsrc->parpcache;
  pcsrc->parpcache = p;
  p = pcdst->proutes;
  pcdst->proutes = pcsrc->proutes;
  pcsrc->proutes = p;

  return(NETWIB_ERR_OK);
}


/*-------------------------------------------------------------*/
/*-------------------------------------------------------------*/

/*-------------------------------------------------------------*/
netwib_err netwib_priv_conf_rdlock(void)
{
  return(netwib_priv_mutex_rdlock(&netwib_priv_conf_mut));
}

/*-------------------------------------------------------------*/
netwib_err netwib_priv_conf_rdunlock(void)
{
  return(netwib_priv_mutex_rdunlock(&netwib_priv_conf_mut));
}

/*-------------------------------------------------------------*/
netwib_err netwib_priv_conf_wrlock(void)
{
  return(netwib_priv_mutex_wrlock(&netwib_priv_conf_mut));
}

/*-------------------------------------------------------------*/
netwib_err netwib_priv_conf_wrunlock(void)
{
  return(netwib_priv_mutex_wrunlock(&netwib_priv_conf_mut));
}

/*-------------------------------------------------------------*/
netwib_err netwib_priv_conf_init(void)
{
  netwib_priv_conf_needtobeupdated = NETWIB_TRUE;
  netwib_er(netwib_priv_conft_init(&netwib_priv_conf));
  netwib_er(netwib_priv_mutex_init(&netwib_priv_conf_mut));

  return(NETWIB_ERR_OK);
}

/*-------------------------------------------------------------*/
netwib_err netwib_priv_conf_close(void)
{
  netwib_priv_conf_needtobeupdated = NETWIB_TRUE;
  netwib_er(netwib_priv_conft_close(&netwib_priv_conf));
  netwib_er(netwib_priv_mutex_close(&netwib_priv_conf_mut));

  return(NETWIB_ERR_OK);
}

/*-------------------------------------------------------------*/
netwib_err netwib_priv_conf_update(void)
{
  netwib_priv_conf_t ct;
  netwib_priv_confwork cw;
  netwib_bool ctset;
  netwib_err ret;

  netwib_er(netwib_priv_confwork_init(&cw));

  ctset = NETWIB_FALSE;
  netwib_eg(netwib_priv_confwork_obtain(&cw, NETWIB_FALSE, NULL));
  netwib_eg(netwib_priv_conft_init(&ct));
  ctset = NETWIB_TRUE;
  netwib_eg(netwib_priv_conft_add_confwork(&cw, &ct));

  netwib_er(netwib_priv_conf_wrlock());
  ret = netwib_priv_conft_swap(&ct, &netwib_priv_conf);
  if (ret == NETWIB_ERR_OK) {
    netwib_priv_conf_needtobeupdated = NETWIB_FALSE;
  }
  netwib_er(netwib_priv_conf_wrunlock());

 netwib_gotolabel:
  if (ctset) {
    netwib_er(netwib_priv_conft_close(&ct));
  }
  netwib_er(netwib_priv_confwork_close(&cw));
  return(ret);
}