Revision 1a8337f2
b/lib/daemon.py | ||
---|---|---|
1 | 1 |
# |
2 | 2 |
# |
3 | 3 |
|
4 |
# Copyright (C) 2006, 2007, 2008 Google Inc. |
|
4 |
# Copyright (C) 2006, 2007, 2008, 2010 Google Inc.
|
|
5 | 5 |
# |
6 | 6 |
# This program is free software; you can redistribute it and/or modify |
7 | 7 |
# it under the terms of the GNU General Public License as published by |
... | ... | |
100 | 100 |
return False |
101 | 101 |
|
102 | 102 |
|
103 |
def FormatAddress(family, address): |
|
104 |
"""Format a client's address |
|
105 |
|
|
106 |
@type family: integer |
|
107 |
@param family: socket family (one of socket.AF_*) |
|
108 |
@type address: family specific (usually tuple) |
|
109 |
@param address: address, as reported by this class |
|
110 |
|
|
111 |
""" |
|
112 |
if family == socket.AF_INET and len(address) == 2: |
|
113 |
return "%s:%d" % address |
|
114 |
elif family == socket.AF_UNIX and len(address) == 3: |
|
115 |
return "pid=%s, uid=%s, gid=%s" % address |
|
116 |
else: |
|
117 |
return str(address) |
|
118 |
|
|
119 |
|
|
120 | 103 |
class AsyncStreamServer(GanetiBaseAsyncoreDispatcher): |
121 | 104 |
"""A stream server to use with asyncore. |
122 | 105 |
|
... | ... | |
159 | 142 |
# is passed in from accept anyway |
160 | 143 |
client_address = netutils.GetSocketCredentials(connected_socket) |
161 | 144 |
logging.info("Accepted connection from %s", |
162 |
FormatAddress(self.family, client_address)) |
|
145 |
netutils.FormatAddress(self.family, client_address))
|
|
163 | 146 |
self.handle_connection(connected_socket, client_address) |
164 | 147 |
|
165 | 148 |
def handle_connection(self, connected_socket, client_address): |
... | ... | |
290 | 273 |
|
291 | 274 |
def close_log(self): |
292 | 275 |
logging.info("Closing connection from %s", |
293 |
FormatAddress(self.family, self.peer_address)) |
|
276 |
netutils.FormatAddress(self.family, self.peer_address))
|
|
294 | 277 |
self.close() |
295 | 278 |
|
296 | 279 |
# this method is overriding an asyncore.dispatcher method |
b/lib/http/client.py | ||
---|---|---|
28 | 28 |
|
29 | 29 |
from ganeti import http |
30 | 30 |
from ganeti import compat |
31 |
from ganeti import netutils |
|
31 | 32 |
|
32 | 33 |
|
33 | 34 |
class HttpClientRequest(object): |
... | ... | |
104 | 105 |
"""Returns the full URL for this requests. |
105 | 106 |
|
106 | 107 |
""" |
108 |
if netutils.IPAddress.IsValid(self.host): |
|
109 |
family = netutils.IPAddress.GetAddressFamily(self.host) |
|
110 |
address = netutils.FormatAddress(family, (self.host, self.port)) |
|
111 |
else: |
|
112 |
address = "%s:%s" % (self.host, self.port) |
|
107 | 113 |
# TODO: Support for non-SSL requests |
108 |
return "https://%s:%s%s" % (self.host, self.port, self.path)
|
|
114 |
return "https://%s%s" % (address, self.path)
|
|
109 | 115 |
|
110 | 116 |
@property |
111 | 117 |
def identity(self): |
b/lib/netutils.py | ||
---|---|---|
472 | 472 |
address_int = (address_int << 16) + int(part or '0', 16) |
473 | 473 |
|
474 | 474 |
return address_int |
475 |
|
|
476 |
def FormatAddress(family, address): |
|
477 |
"""Format a socket address |
|
478 |
|
|
479 |
@type family: integer |
|
480 |
@param family: socket family (one of socket.AF_*) |
|
481 |
@type address: family specific (usually tuple) |
|
482 |
@param address: address, as reported by this class |
|
483 |
|
|
484 |
""" |
|
485 |
if family == socket.AF_UNIX and len(address) == 3: |
|
486 |
return "pid=%s, uid=%s, gid=%s" % address |
|
487 |
|
|
488 |
if family in (socket.AF_INET, socket.AF_INET6) and len(address) == 2: |
|
489 |
host, port = address |
|
490 |
if family == socket.AF_INET6: |
|
491 |
res = "[%s]" % host |
|
492 |
else: |
|
493 |
res = host |
|
494 |
|
|
495 |
if port is not None: |
|
496 |
res += ":%s" % port |
|
497 |
|
|
498 |
return res |
|
499 |
|
|
500 |
raise errors.ParameterError(family, address) |
b/lib/rapi/client.py | ||
---|---|---|
35 | 35 |
|
36 | 36 |
import logging |
37 | 37 |
import simplejson |
38 |
import socket |
|
38 | 39 |
import urllib |
39 | 40 |
import threading |
40 | 41 |
import pycurl |
... | ... | |
265 | 266 |
self._curl_config_fn = curl_config_fn |
266 | 267 |
self._curl_factory = curl_factory |
267 | 268 |
|
268 |
self._base_url = "https://%s:%s" % (host, port) |
|
269 |
try: |
|
270 |
socket.inet_pton(socket.AF_INET6, host) |
|
271 |
address = "[%s]:%s" % (host, port) |
|
272 |
except socket.error: |
|
273 |
address = "%s:%s" % (host, port) |
|
274 |
|
|
275 |
self._base_url = "https://%s" % address |
|
269 | 276 |
|
270 | 277 |
if username is not None: |
271 | 278 |
if password is None: |
b/test/ganeti.netutils_unittest.py | ||
---|---|---|
385 | 385 |
_BaseTcpPingDeafTest.tearDown(self) |
386 | 386 |
|
387 | 387 |
|
388 |
class TestFormatAddress(unittest.TestCase): |
|
389 |
"""Testcase for FormatAddress""" |
|
390 |
|
|
391 |
def testFormatAddressUnixSocket(self): |
|
392 |
res1 = netutils.FormatAddress(socket.AF_UNIX, ("12352", 0, 0)) |
|
393 |
self.assertEqual(res1, "pid=12352, uid=0, gid=0") |
|
394 |
|
|
395 |
def testFormatAddressIP4(self): |
|
396 |
res1 = netutils.FormatAddress(socket.AF_INET, ("127.0.0.1", 1234)) |
|
397 |
self.assertEqual(res1, "127.0.0.1:1234") |
|
398 |
res2 = netutils.FormatAddress(socket.AF_INET, ("192.0.2.32", None)) |
|
399 |
self.assertEqual(res2, "192.0.2.32") |
|
400 |
|
|
401 |
def testFormatAddressIP6(self): |
|
402 |
res1 = netutils.FormatAddress(socket.AF_INET6, ("::1", 1234)) |
|
403 |
self.assertEqual(res1, "[::1]:1234") |
|
404 |
res2 = netutils.FormatAddress(socket.AF_INET6, ("::1", None)) |
|
405 |
self.assertEqual(res2, "[::1]") |
|
406 |
res2 = netutils.FormatAddress(socket.AF_INET6, ("2001:db8::beef", "80")) |
|
407 |
self.assertEqual(res2, "[2001:db8::beef]:80") |
|
408 |
|
|
409 |
def testInvalidFormatAddress(self): |
|
410 |
self.assertRaises(errors.ParameterError, |
|
411 |
netutils.FormatAddress, None, ("::1", None)) |
|
412 |
self.assertRaises(errors.ParameterError, |
|
413 |
netutils.FormatAddress, socket.AF_INET, "127.0.0.1") |
|
414 |
self.assertRaises(errors.ParameterError, |
|
415 |
netutils.FormatAddress, socket.AF_INET, ("::1")) |
|
416 |
|
|
417 |
|
|
388 | 418 |
if __name__ == "__main__": |
389 | 419 |
testutils.GanetiTestProgram() |
Also available in: Unified diff