~ubuntu-branches/ubuntu/maverick/eucalyptus/maverick

« back to all changes in this revision

Viewing changes to net/dhcp.c

  • Committer: Bazaar Package Importer
  • Author(s): Dave Walker (Daviey)
  • Date: 2010-07-21 17:27:10 UTC
  • mfrom: (1.1.38 upstream)
  • Revision ID: james.westby@ubuntu.com-20100721172710-7xv07dmdqgivc3t9
Tags: 2.0~bzr1211-0ubuntu1
* New major upstream version merge, 2.0 (r1211).
* debian/patches/:
  - 01-wsdl-stubs.patch, debian/wsdl.md5sums: wsdl stubs updated.
  - 02-Makefile.patch: Updated to reflect new code layout.
  - 07-local_support_euca_conf-in.patch: Updated to reflect new code layout.
  - 08-ubuntu-default-networking.patch: Refreshed.
  - 09-small-128-192MB.patch: Updated to point to new location.
  - 10-disable-iscsi.patch: Refreshed.
  - 11-state-cleanup-memleakfix.patch: Removed, fixed upstream.
  - 15-fix-default-ramdisk.patch: Updated to point to new location.
  - 16-kvm_libvirt_xml_default_use_kvm.patch: Updated to reflect changes.
  - 17-fix_walrus_OOM_errors.patch: Removed, fixed upstream.
  - 18-priv_security.patch: Updated to reflect upstream changes.
  - 20-brute-force-webui.patch: Updated to reflect upstream changes. 
  - 21-eucalyptus-1.7-with-gwt-1.6.4.patch: New patch, allows 
    eucalyptus-1.7 to be built against gwt 1.6.4. Based on patch courtesy 
    of Dmitrii Zagorodnov, upstream. (LP: #597330)
* debian/eucalyptus-java-common.links: 
  - Changed symlink for groovy, point to groovy.all.jar, making compatiable 
    with groovy versions >1.7. (LP: #595421)
  - Added ant.jar & jetty-rewrite-handler.jar as they are now required.
* debian/control
  - & debian/build-jars: Added libjavassist-java and libjetty-extra-java as 
    build dependencies.
  - Added libjetty-extra-java as a dependency of eucalyptus-java-common
* The binary resulting jar's have been renamed from eucalyptus-*-1.6.2.jar
  to eucalyptus-*-main.jar:    
  - debian/eucalyptus-cc.upstart
  - debian/eucalyptus-cloud.install
  - debian/eucalyptus-common.eucalyptus.upstart
  - debian/eucalyptus-java-common.install
  - debian/eucalyptus-network.upstart
  - debian/eucalyptus-sc.install
  - debian/eucalyptus-walrus.install
* debian/eucalyptus-java-common.install: New upstream jars that have been
  installed:
  - eucalyptus-db-hsqldb-ext-main.jar
  - eucalyptus-component-main.jar
* debian/control:
  - Updated Standards Version to 3.8.4 (no change)
  - Updated the upstream Homepage to: http://open.eucalyptus.com/
  - Changed Vcs-Bzr to reflect new location of Ubuntu hosted development branch.
  - Made the Build Dependency of groovy and the binary eucalyptus-java-common
    package depend on version >=1.7.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Copyright (c) 2009  Eucalyptus Systems, Inc.    
3
 
 
4
 
This program is free software: you can redistribute it and/or modify
5
 
it under the terms of the GNU General Public License as published by 
6
 
the Free Software Foundation, only version 3 of the License.  
7
 
 
8
 
This file is distributed in the hope that it will be useful, but WITHOUT
9
 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11
 
for more details.  
12
 
 
13
 
You should have received a copy of the GNU General Public License along
14
 
with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 
16
 
Please contact Eucalyptus Systems, Inc., 130 Castilian
17
 
Dr., Goleta, CA 93101 USA or visit <http://www.eucalyptus.com/licenses/> 
18
 
if you need additional information or have any questions.
19
 
 
20
 
This file may incorporate work covered under the following copyright and
21
 
permission notice:
22
 
 
23
 
  Software License Agreement (BSD License)
24
 
 
25
 
  Copyright (c) 2008, Regents of the University of California
26
 
  
27
 
 
28
 
  Redistribution and use of this software in source and binary forms, with
29
 
  or without modification, are permitted provided that the following
30
 
  conditions are met:
31
 
 
32
 
    Redistributions of source code must retain the above copyright notice,
33
 
    this list of conditions and the following disclaimer.
34
 
 
35
 
    Redistributions in binary form must reproduce the above copyright
36
 
    notice, this list of conditions and the following disclaimer in the
37
 
    documentation and/or other materials provided with the distribution.
38
 
 
39
 
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
40
 
  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41
 
  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
42
 
  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
43
 
  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44
 
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
45
 
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
46
 
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
47
 
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
48
 
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
49
 
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. USERS OF
50
 
  THIS SOFTWARE ACKNOWLEDGE THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE
51
 
  LICENSED MATERIAL, COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS
52
 
  SOFTWARE, AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
53
 
  IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA, SANTA
54
 
  BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY, WHICH IN
55
 
  THE REGENTS’ DISCRETION MAY INCLUDE, WITHOUT LIMITATION, REPLACEMENT
56
 
  OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO IDENTIFIED, OR
57
 
  WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT NEEDED TO COMPLY WITH
58
 
  ANY SUCH LICENSES OR RIGHTS.
59
 
*/
60
 
#include <stdio.h>
61
 
#include <stdlib.h>
62
 
#include <string.h> /* strdup */
63
 
#include <sys/types.h> /* open */
64
 
#include <sys/stat.h> /* open */
65
 
#include <fcntl.h>  /* open */
66
 
#include <errno.h> /* perror */
67
 
#include "misc.h"
68
 
#include "dhcp.h"
69
 
 
70
 
int initDHCP(dhcpConfig *dhcpconfig, char *path, char *deamon, char *pubmode, char *pubInterface, char *pubSubnet, char *pubSubnetMask, char *pubBroadcastAddress, char *pubRouter,char *pubDNS, char *pubRangeMin, char *pubRangeMax, char *privSubnet, char *privSubnetMask, char *privBroadcastAddress) {
71
 
  char newpath[1024], cmd[1024];
72
 
  
73
 
  if (!dhcpconfig) {
74
 
    return(1);
75
 
  }
76
 
  
77
 
  if (!dhcpconfig->initialized) {
78
 
    
79
 
    bzero(dhcpconfig, sizeof(dhcpConfig));
80
 
  
81
 
    if (pubmode) {
82
 
      strncpy(dhcpconfig->pubmode, pubmode, 32);
83
 
    } else {
84
 
      strncpy(dhcpconfig->pubmode, "SYSTEM", 32);
85
 
    }
86
 
    
87
 
    if (!path) {
88
 
      path = strdup("");
89
 
    }
90
 
    
91
 
    snprintf(newpath, 1024, "%s/euca-dhcp.conf", path);
92
 
    snprintf(cmd, 1024, "mkdir -p %s; touch %s", path, newpath);
93
 
    system(cmd);
94
 
    strncpy(dhcpconfig->config, newpath, 1024);
95
 
    
96
 
    snprintf(newpath, 1024, "%s/euca-dhcp.pid", path);
97
 
    snprintf(cmd, 1024, "mkdir -p %s; touch %s", path, newpath);
98
 
    system(cmd);
99
 
    strncpy(dhcpconfig->pidfile, newpath, 1024);
100
 
    
101
 
    snprintf(newpath, 1024, "%s/euca-dhcp.leases", path);
102
 
    snprintf(cmd, 1024, "mkdir -p %s; touch %s", path, newpath);
103
 
    system(cmd);
104
 
    strncpy(dhcpconfig->leases, newpath, 1024);
105
 
    
106
 
    snprintf(newpath, 1024, "%s/euca-dhcp.trace", path);
107
 
    snprintf(cmd, 1024, "mkdir -p %s; touch %s", path, newpath);
108
 
    system(cmd);
109
 
    strncpy(dhcpconfig->trace, newpath, 1024);
110
 
    
111
 
    if (deamon) {
112
 
      strncpy(dhcpconfig->deamon, deamon, 32);
113
 
    } else {
114
 
      strncpy(dhcpconfig->deamon, "/usr/sbin/dhcpd", 32);
115
 
    }
116
 
    
117
 
    if (privSubnet) {
118
 
      strncpy(dhcpconfig->privSubnet, privSubnet, 32);
119
 
    } else {
120
 
      strncpy(dhcpconfig->privSubnet, "192.168.0.0", 32);
121
 
    }
122
 
    
123
 
    if (privSubnetMask) {
124
 
      strncpy(dhcpconfig->privSubnetMask, privSubnetMask, 32);
125
 
    } else {
126
 
      strncpy(dhcpconfig->privSubnetMask, "255.255.0.0", 32);
127
 
    }
128
 
    
129
 
    if (privBroadcastAddress) {
130
 
      strncpy(dhcpconfig->privBroadcastAddress, privBroadcastAddress, 32);
131
 
    } else {
132
 
      strncpy(dhcpconfig->privBroadcastAddress, "192.168.255.255", 32);
133
 
    }
134
 
    
135
 
    if (!strcmp(dhcpconfig->pubmode, "STATIC") || !strcmp(dhcpconfig->pubmode, "DYNAMIC")) { 
136
 
      if (pubInterface) {
137
 
        strncpy(dhcpconfig->pubInterface, pubInterface, 32);
138
 
      } else {
139
 
        strncpy(dhcpconfig->pubInterface, "eth0", 32);
140
 
      }
141
 
      
142
 
      if (pubSubnet) {
143
 
        strncpy(dhcpconfig->pubSubnet, pubSubnet, 32);
144
 
      } else {
145
 
        strncpy(dhcpconfig->pubSubnet, "10.0.0.0", 32);
146
 
      }
147
 
      
148
 
      if (pubSubnetMask) {
149
 
        strncpy(dhcpconfig->pubSubnetMask, pubSubnetMask, 32);
150
 
      } else {
151
 
        strncpy(dhcpconfig->pubSubnetMask, "255.0.0.0", 32);
152
 
      }
153
 
      
154
 
      if (pubBroadcastAddress) {
155
 
        strncpy(dhcpconfig->pubBroadcastAddress, pubBroadcastAddress, 32);
156
 
      } else {
157
 
        strncpy(dhcpconfig->pubBroadcastAddress, "10.255.255.255", 32);
158
 
      }
159
 
      
160
 
      if (pubRouter) {
161
 
        strncpy(dhcpconfig->pubRouter, pubRouter, 32);
162
 
      } else {
163
 
        strncpy(dhcpconfig->pubRouter, "10.1.1.1", 32);
164
 
      }
165
 
      
166
 
      if (pubDNS) {
167
 
        strncpy(dhcpconfig->pubDNS, pubDNS, 32);
168
 
      } else {
169
 
        strncpy(dhcpconfig->pubDNS, "10.1.1.1", 32);
170
 
      }
171
 
      
172
 
      
173
 
      if (pubRangeMin) {
174
 
        strncpy(dhcpconfig->pubRangeMin, pubRangeMin, 32);
175
 
      } else {
176
 
        strncpy(dhcpconfig->pubRangeMin, "10.128.1.1", 32);
177
 
      }
178
 
      
179
 
      if (pubRangeMax) {
180
 
        strncpy(dhcpconfig->pubRangeMax, pubRangeMax, 32);
181
 
      } else {
182
 
        strncpy(dhcpconfig->pubRangeMax, "10.128.1.254", 32);
183
 
      }
184
 
    }
185
 
    
186
 
    writeDHCPConfig(dhcpconfig);
187
 
    dhcpconfig->initialized=1;
188
 
  }
189
 
}
190
 
      
191
 
int writeDHCPConfig(dhcpConfig *dhcpconfig) {
192
 
  FILE *fp;
193
 
  int i;
194
 
 
195
 
  fp = fopen(dhcpconfig->config, "w");
196
 
  if (fp == NULL) {
197
 
    return(1);
198
 
  }
199
 
  
200
 
  fprintf(fp, "# automatically generated config file for DHCP server\ndefault-lease-time 1200;\nmax-lease-time 1200;\nddns-update-style none;\n\n");
201
 
  
202
 
  fprintf(fp, "subnet %s netmask %s {\n  option subnet-mask %s;\n  option broadcast-address %s;\n}\n", dhcpconfig->privSubnet, dhcpconfig->privSubnetMask, dhcpconfig->privSubnetMask, dhcpconfig->privBroadcastAddress);
203
 
 
204
 
  logprintf("public dhcp mode set to %s\n", dhcpconfig->pubmode);
205
 
  
206
 
  if (!strcmp(dhcpconfig->pubmode, "STATIC") || !strcmp(dhcpconfig->pubmode, "DYNAMIC")) { 
207
 
    fprintf(fp, "subnet %s netmask %s {\n  option subnet-mask %s;\n  option broadcast-address %s;\n  option domain-name-servers %s;\n  option routers %s;\n", dhcpconfig->pubSubnet, dhcpconfig->pubSubnetMask, dhcpconfig->pubSubnetMask, dhcpconfig->pubBroadcastAddress, dhcpconfig->pubDNS, dhcpconfig->pubRouter);
208
 
    
209
 
    if (!strcmp(dhcpconfig->pubmode, "DYNAMIC")) {
210
 
      fprintf(fp, "  range %s %s;\n", dhcpconfig->pubRangeMin, dhcpconfig->pubRangeMax);
211
 
    }
212
 
    fprintf(fp, "}\n");
213
 
  }
214
 
  
215
 
  for (i=0; i<2048; i++) {
216
 
    char *mac, *ip;
217
 
    
218
 
    if (dhcpconfig->macs[i][0] != '\0') {
219
 
      mac = dhcpconfig->macs[i];
220
 
      ip = dhcpconfig->ips[i];
221
 
      fprintf(fp, "\nhost node-%s {\n  hardware ethernet %s;\n  fixed-address %s;\n}\n", ip, mac, ip);
222
 
    }
223
 
    
224
 
  }
225
 
  fclose (fp);
226
 
  
227
 
  return(0);
228
 
}
229
 
 
230
 
int addDHCPDev(dhcpConfig *dhcpconfig, char *dev) {
231
 
  int i, done=0, found;
232
 
  
233
 
  if (!dhcpconfig || !dev) {
234
 
    return(1);
235
 
  }
236
 
  
237
 
  found=0;
238
 
  for (i=0; i<128 && !done; i++) {
239
 
    if (!found && dhcpconfig->etherdevs[i][0] == '\0') {
240
 
      found=i;
241
 
    } else if (!strncmp(dhcpconfig->etherdevs[i], dev, 32)) {
242
 
      done++;
243
 
    }
244
 
  }
245
 
  
246
 
  if (found && !done) {
247
 
    strncpy(dhcpconfig->etherdevs[found], dev, 32);
248
 
  }
249
 
  kickDHCPDaemon(dhcpconfig);
250
 
  return(0);
251
 
}
252
 
 
253
 
int delDHCPDev(dhcpConfig *dhcpconfig, char *dev) {
254
 
  int i, done=0;
255
 
  
256
 
  if (!dhcpconfig || !dev) {
257
 
    return(1);
258
 
  }
259
 
  
260
 
  for (i=0; i<128 && !done; i++) {
261
 
    if (!strncmp(dhcpconfig->etherdevs[i], dev, 32)) {
262
 
      bzero(dhcpconfig->etherdevs[i], 32);
263
 
      done++;
264
 
    }
265
 
  }
266
 
  
267
 
  kickDHCPDaemon(dhcpconfig);
268
 
  return(0);
269
 
}
270
 
 
271
 
int addDHCPHost(dhcpConfig *dhcpconfig, char *hostmac, char *hostip) {
272
 
  int i, done=0;
273
 
  
274
 
  if (!dhcpconfig || !hostmac || !hostip) {
275
 
    return(1);
276
 
  }
277
 
  
278
 
  for (i=0; i<2048 && !done; i++) {
279
 
    if (dhcpconfig->macs[i][0] == '\0') {
280
 
      strncpy(dhcpconfig->macs[i], hostmac, 32);
281
 
      strncpy(dhcpconfig->ips[i], hostip, 32);
282
 
      done++;
283
 
    }
284
 
  }
285
 
  kickDHCPDaemon(dhcpconfig);
286
 
  //  writeDHCPConfig(dhcpconfig);
287
 
  return(0);
288
 
}
289
 
 
290
 
int delDHCPHost(dhcpConfig *dhcpconfig, char *hostmac, char *hostip) {
291
 
  int i, done=0;
292
 
  
293
 
  for (i=0; i<2048 && !done; i++) {
294
 
    if ((!hostmac || !strncmp(dhcpconfig->macs[i], hostmac, 32)) && (!hostip || !strncmp(dhcpconfig->ips[i], hostip, 32))) {
295
 
      bzero(dhcpconfig->macs[i], 32);
296
 
      bzero(dhcpconfig->ips[i], 32);
297
 
      done++;
298
 
    }
299
 
  }
300
 
 
301
 
  kickDHCPDaemon(dhcpconfig);
302
 
  //  writeDHCPConfig(dhcpconfig);
303
 
  return(0);
304
 
}
305
 
 
306
 
int kickDHCPDaemon(dhcpConfig *dhcpconfig) {
307
 
  char dstring [512] = "";
308
 
  char buf [512];
309
 
  int rc, i;
310
 
  
311
 
  if (dhcpconfig->pubInterface) {
312
 
    snprintf(dstring, 512, "%s", dhcpconfig->pubInterface);
313
 
  }
314
 
  
315
 
  rc = writeDHCPConfig(dhcpconfig);
316
 
  if (rc) {
317
 
    logprintf ("failed to (re)create %s\n", dhcpconfig->config);
318
 
    return(1);
319
 
  }
320
 
  
321
 
  for (i=0; i<128; i++) {
322
 
    if (dhcpconfig->etherdevs[i][0] != '\0') {
323
 
      strncat (dstring, " ", 512);
324
 
      strncat (dstring, dhcpconfig->etherdevs[i], 32);
325
 
    }
326
 
  }
327
 
 
328
 
  /* force dhcpd to reload the conf */
329
 
  snprintf (buf, 512, "rm -f %s", dhcpconfig->trace);
330
 
  logprintf ("executing: %s\n", buf);
331
 
  system (buf);
332
 
  
333
 
  snprintf (buf, 512, "kill `cat %s`", dhcpconfig->pidfile);
334
 
  logprintf ("executing: %s\n", buf);
335
 
  system (buf);
336
 
  
337
 
  snprintf (buf, 512, "%s -cf %s -lf %s -pf %s -tf %s %s", 
338
 
            dhcpconfig->deamon, dhcpconfig->config, dhcpconfig->leases, dhcpconfig->pidfile, dhcpconfig->trace, dstring);
339
 
  logprintf ("executing: %s\n", buf);
340
 
  rc = system (buf);
341
 
  logprintf("\tRC from cmd: %d\n", rc);
342
 
 
343
 
  return(0);
344
 
}