Revision 28f34048 lib/utils.py
b/lib/utils.py | ||
---|---|---|
81 | 81 |
HEX_CHAR_RE, HEX_CHAR_RE), |
82 | 82 |
re.S | re.I) |
83 | 83 |
|
84 |
_VALID_SERVICE_NAME_RE = re.compile("^[-_.a-zA-Z0-9]{1,128}$") |
|
85 |
|
|
84 | 86 |
# Structure definition for getsockopt(SOL_SOCKET, SO_PEERCRED, ...): |
85 | 87 |
# struct ucred { pid_t pid; uid_t uid; gid_t gid; }; |
86 | 88 |
# |
... | ... | |
1155 | 1157 |
return hostname |
1156 | 1158 |
|
1157 | 1159 |
|
1160 |
def ValidateServiceName(name): |
|
1161 |
"""Validate the given service name. |
|
1162 |
|
|
1163 |
@type name: number or string |
|
1164 |
@param name: Service name or port specification |
|
1165 |
|
|
1166 |
""" |
|
1167 |
try: |
|
1168 |
numport = int(name) |
|
1169 |
except (ValueError, TypeError): |
|
1170 |
# Non-numeric service name |
|
1171 |
valid = _VALID_SERVICE_NAME_RE.match(name) |
|
1172 |
else: |
|
1173 |
# Numeric port (protocols other than TCP or UDP might need adjustments |
|
1174 |
# here) |
|
1175 |
valid = (numport >= 0 and numport < (1 << 16)) |
|
1176 |
|
|
1177 |
if not valid: |
|
1178 |
raise errors.OpPrereqError("Invalid service name '%s'" % name, |
|
1179 |
errors.ECODE_INVAL) |
|
1180 |
|
|
1181 |
return name |
|
1182 |
|
|
1183 |
|
|
1158 | 1184 |
def GetHostInfo(name=None): |
1159 | 1185 |
"""Lookup host name and raise an OpPrereqError for failures""" |
1160 | 1186 |
|
Also available in: Unified diff