]> err.no Git - sope/blob - sope-core/NGStreams/NGNetUtilities.m
fixed library versions for gstep-make 1.9.2
[sope] / sope-core / NGStreams / NGNetUtilities.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo 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   OGo 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 OGo; 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 // $Id$
22
23 #include "NGNetUtilities.h"
24 #include "NGInternetSocketAddress.h"
25 #include "NGLocalSocketAddress.h"
26 #include "common.h"
27
28 id<NGSocketAddress> NGSocketAddressFromString(NSString *_string) {
29   const unsigned char *cstr = [_string cString];
30   if (cstr == NULL)         return nil;
31   if ([_string length] < 1) return nil;
32
33   {
34     const unsigned char *tmp = index(cstr, ':');
35     
36     if (tmp) { // INET socket
37       NSString *hostName = nil;
38
39       if (((tmp - cstr) == 1) && (*cstr == '*'))
40         hostName = nil; // wildcard host
41       else
42         hostName = [NSString stringWithCString:cstr length:(tmp - cstr)];
43
44       // check what comes after colon
45       if (isdigit(tmp[1])) {
46         // a port
47         int port = atoi(tmp + 1);
48         return [NGInternetSocketAddress addressWithPort:port onHost:hostName];
49       }
50       else {
51         // a service or 'auto' for auto-assigned ports
52         const unsigned char *tmp2 = index((tmp + 1), '/');
53         NSString *protocol = @"tcp";
54         NSString *service;
55
56         tmp++;
57
58         if (tmp2 == NULL)
59           service  = [NSString stringWithCString:tmp];
60         else {
61           service  = [NSString stringWithCString:tmp length:(tmp2 - tmp)];
62           protocol = [NSString stringWithCString:(tmp2 + 1)];
63         }
64
65         if ([service isEqualToString:@"auto"])
66           return [NGInternetSocketAddress addressWithPort:0
67                                           onHost:hostName];
68         
69         return [NGInternetSocketAddress addressWithService:service
70                                         onHost:hostName
71                                         protocol:protocol];
72       }
73     }
74
75 #if !defined(WIN32)
76     if ([_string isAbsolutePath])
77       return [NGLocalSocketAddress addressWithPath:_string];
78 #endif
79   }
80   return nil;
81 }