1/*
2  Copyright (C) 2000-2005 SKYRIX Software AG
3
4  This file is part of SOPE.
5
6  SOPE is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License as published by the
8  Free Software Foundation; either version 2, or (at your option) any
9  later version.
10
11  SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12  WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14  License for more details.
15
16  You should have received a copy of the GNU Lesser General Public
17  License along with SOPE; see the file COPYING.  If not, write to the
18  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19  02111-1307, USA.
20*/
21
22#include "NGNetUtilities.h"
23#include "NGInternetSocketAddress.h"
24#include "NGLocalSocketAddress.h"
25#include "common.h"
26
27id<NGSocketAddress> NGSocketAddressFromString(NSString *_string) {
28  const unsigned char *cstr = (unsigned char *)[_string cString];
29  if (cstr == NULL)         return nil;
30  if ([_string length] < 1) return nil;
31
32  {
33    const unsigned char *tmp = (unsigned char *)index((char *)cstr, ':');
34
35    if (tmp) { // INET socket
36      NSString *hostName = nil;
37
38      if (((tmp - cstr) == 1) && (*cstr == '*'))
39        hostName = nil; // wildcard host
40      else {
41        hostName = [NSString stringWithCString:(char *)cstr
42			     length:(tmp - cstr)];
43      }
44
45      // check what comes after colon
46      if (isdigit(tmp[1])) {
47        // a port
48        int port = atoi((char *)tmp + 1);
49        return [NGInternetSocketAddress addressWithPort:port onHost:hostName];
50      }
51      else {
52        // a service or 'auto' for auto-assigned ports
53        const unsigned char *tmp2;
54        NSString *protocol = @"tcp";
55        NSString *service;
56
57	tmp2 = (unsigned char *)index((char *)(tmp + 1), '/');
58        tmp++;
59
60        if (tmp2 == NULL)
61          service  = [NSString stringWithCString:(char *)tmp];
62        else {
63          service  = [NSString stringWithCString:(char *)tmp
64			       length:(tmp2 - tmp)];
65          protocol = [NSString stringWithCString:(char *)(tmp2 + 1)];
66        }
67
68        if ([service isEqualToString:@"auto"])
69          return [NGInternetSocketAddress addressWithPort:0
70                                          onHost:hostName];
71
72        return [NGInternetSocketAddress addressWithService:service
73                                        onHost:hostName
74                                        protocol:protocol];
75      }
76    }
77
78#if !defined(WIN32)
79    if ([_string isAbsolutePath])
80      return [NGLocalSocketAddress addressWithPath:_string];
81#endif
82  }
83  return nil;
84}
85