David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
Andrew McGeachie | 6db89f2 | 2009-07-21 14:45:12 +0000 | [diff] [blame] | 20 | #import <Foundation/Foundation.h> |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 21 | #import "TSocketServer.h" |
| 22 | #import "TNSFileHandleTransport.h" |
| 23 | #import "TProtocol.h" |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 24 | #import "TTransportError.h" |
| 25 | |
Andrew McGeachie | 0c89571 | 2009-07-21 21:14:19 +0000 | [diff] [blame] | 26 | #import <sys/socket.h> |
| 27 | #include <netinet/in.h> |
Chris Vasselli | df3223c | 2016-06-21 16:45:39 -0700 | [diff] [blame] | 28 | #include <sys/un.h> |
Andrew McGeachie | 0c89571 | 2009-07-21 21:14:19 +0000 | [diff] [blame] | 29 | |
| 30 | |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 31 | NSString *const TSocketServerClientConnectionFinished = @"TSocketServerClientConnectionFinished"; |
| 32 | NSString *const TSocketServerProcessorKey = @"TSocketServerProcessor"; |
| 33 | NSString *const TSockerServerTransportKey = @"TSockerServerTransport"; |
| 34 | |
| 35 | |
| 36 | @interface TSocketServer () |
| 37 | |
| 38 | @property(strong, nonatomic) id<TProtocolFactory> inputProtocolFactory; |
| 39 | @property(strong, nonatomic) id<TProtocolFactory> outputProtocolFactory; |
| 40 | @property(strong, nonatomic) id<TProcessorFactory> processorFactory; |
| 41 | @property(strong, nonatomic) NSFileHandle *socketFileHandle; |
| 42 | @property(strong, nonatomic) dispatch_queue_t processingQueue; |
Chris Vasselli | df3223c | 2016-06-21 16:45:39 -0700 | [diff] [blame] | 43 | @property(strong, nonatomic) NSString *domainSocketPath; |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 44 | |
| 45 | @end |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | @implementation TSocketServer |
| 49 | |
Chris Vasselli | df3223c | 2016-06-21 16:45:39 -0700 | [diff] [blame] | 50 | -(instancetype) initWithSocket:(CFSocketRef)socket |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 51 | protocolFactory:(id <TProtocolFactory>)protocolFactory |
| 52 | processorFactory:(id <TProcessorFactory>)processorFactory; |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 53 | { |
| 54 | self = [super init]; |
| 55 | |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 56 | _inputProtocolFactory = protocolFactory; |
| 57 | _outputProtocolFactory = protocolFactory; |
| 58 | _processorFactory = processorFactory; |
| 59 | |
| 60 | dispatch_queue_attr_t processingQueueAttr = |
| 61 | dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_CONCURRENT, QOS_CLASS_BACKGROUND, 0); |
| 62 | |
| 63 | _processingQueue = dispatch_queue_create("TSocketServer.processing", processingQueueAttr); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 64 | |
Andrew McGeachie | 0c89571 | 2009-07-21 21:14:19 +0000 | [diff] [blame] | 65 | // create a socket. |
Chris Vasselli | df3223c | 2016-06-21 16:45:39 -0700 | [diff] [blame] | 66 | int fd = CFSocketGetNative(socket); |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 67 | |
Andrew McGeachie | 0c89571 | 2009-07-21 21:14:19 +0000 | [diff] [blame] | 68 | // wrap it in a file handle so we can get messages from it |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 69 | _socketFileHandle = [[NSFileHandle alloc] initWithFileDescriptor:fd |
| 70 | closeOnDealloc:YES]; |
| 71 | |
Andrew McGeachie | ddfe0c9 | 2010-02-10 01:03:01 +0000 | [diff] [blame] | 72 | // throw away our socket |
| 73 | CFSocketInvalidate(socket); |
| 74 | CFRelease(socket); |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 75 | |
| 76 | // register for notifications of accepted incoming connections |
| 77 | [[NSNotificationCenter defaultCenter] addObserver:self |
| 78 | selector:@selector(connectionAccepted:) |
| 79 | name:NSFileHandleConnectionAcceptedNotification |
| 80 | object:_socketFileHandle]; |
| 81 | |
Andrew McGeachie | 0c89571 | 2009-07-21 21:14:19 +0000 | [diff] [blame] | 82 | // tell socket to listen |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 83 | [_socketFileHandle acceptConnectionInBackgroundAndNotify]; |
| 84 | |
Chris Vasselli | df3223c | 2016-06-21 16:45:39 -0700 | [diff] [blame] | 85 | return self; |
| 86 | } |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 87 | |
Chris Vasselli | df3223c | 2016-06-21 16:45:39 -0700 | [diff] [blame] | 88 | - (id) initWithPort: (int) port |
| 89 | protocolFactory: (id <TProtocolFactory>) protocolFactory |
| 90 | processorFactory: (id <TProcessorFactory>) processorFactory |
| 91 | { |
| 92 | CFSocketRef socket = [[self class] createSocketWithPort:port]; |
| 93 | if (socket == NULL) { |
| 94 | return nil; |
| 95 | } |
| 96 | |
| 97 | if (self = [self initWithSocket:socket protocolFactory:protocolFactory processorFactory:processorFactory]) { |
| 98 | NSLog(@"TSocketServer: Listening on TCP port %d", port); |
| 99 | } |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 100 | return self; |
| 101 | } |
| 102 | |
| 103 | |
Chris Vasselli | df3223c | 2016-06-21 16:45:39 -0700 | [diff] [blame] | 104 | +(CFSocketRef) createSocketWithPort:(int)port |
| 105 | { |
| 106 | CFSocketRef socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, 0, NULL, NULL); |
| 107 | if (socket) { |
| 108 | CFSocketSetSocketFlags(socket, CFSocketGetSocketFlags(socket) & ~kCFSocketCloseOnInvalidate); |
| 109 | int fd = CFSocketGetNative(socket); |
| 110 | int yes = 1; |
| 111 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes)); |
| 112 | |
| 113 | struct sockaddr_in addr; |
| 114 | memset(&addr, 0, sizeof(addr)); |
| 115 | addr.sin_len = sizeof(addr); |
| 116 | addr.sin_family = AF_INET; |
| 117 | addr.sin_port = htons(port); |
| 118 | addr.sin_addr.s_addr = htonl(INADDR_ANY); |
| 119 | NSData *address = [NSData dataWithBytes:&addr length:sizeof(addr)]; |
| 120 | if (CFSocketSetAddress(socket, (__bridge CFDataRef)address) != kCFSocketSuccess) { |
| 121 | CFSocketInvalidate(socket); |
| 122 | CFRelease(socket); |
| 123 | NSLog(@"TSocketServer: Could not bind to address"); |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | return socket; |
| 128 | } |
| 129 | else { |
| 130 | NSLog(@"TSocketServer: No server socket"); |
| 131 | return NULL; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | - (id) initWithPath: (NSString *) path |
| 136 | protocolFactory: (id <TProtocolFactory>) protocolFactory |
| 137 | processorFactory: (id <TProcessorFactory>) processorFactory |
| 138 | { |
| 139 | _domainSocketPath = path; |
| 140 | CFSocketRef socket = [[self class] createSocketWithPath:path]; |
| 141 | if (socket == NULL) { |
| 142 | return nil; |
| 143 | } |
| 144 | |
| 145 | if (self = [self initWithSocket:socket protocolFactory:protocolFactory processorFactory:processorFactory]) { |
| 146 | NSLog(@"TSocketServer: Listening on path %@", path); |
| 147 | } |
| 148 | return self; |
| 149 | } |
| 150 | |
| 151 | + (CFSocketRef) createSocketWithPath: (NSString *) path |
| 152 | { |
| 153 | CFSocketRef socket = CFSocketCreate(kCFAllocatorDefault, PF_LOCAL, SOCK_STREAM, IPPROTO_IP, 0, NULL, NULL); |
| 154 | if (socket) { |
| 155 | CFSocketSetSocketFlags(socket, CFSocketGetSocketFlags(socket) & ~kCFSocketCloseOnInvalidate); |
| 156 | int fd = CFSocketGetNative(socket); |
| 157 | int yes = 1; |
| 158 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes)); |
| 159 | |
| 160 | size_t nullTerminatedPathLength = path.length + 1; |
| 161 | struct sockaddr_un addr; |
| 162 | if (nullTerminatedPathLength> sizeof(addr.sun_path)) { |
| 163 | NSLog(@"TSocketServer: Unable to create socket at path %@. Path is too long.", path); |
| 164 | return NULL; |
| 165 | } |
| 166 | |
| 167 | addr.sun_family = AF_LOCAL; |
| 168 | memcpy(addr.sun_path, path.UTF8String, nullTerminatedPathLength); |
| 169 | addr.sun_len = SUN_LEN(&addr); |
| 170 | |
| 171 | NSData *address = [NSData dataWithBytes:&addr length:sizeof(addr)]; |
| 172 | if (CFSocketSetAddress(socket, (__bridge CFDataRef)address) != kCFSocketSuccess) { |
| 173 | CFSocketInvalidate(socket); |
| 174 | CFRelease(socket); |
| 175 | NSLog(@"TSocketServer: Could not bind to address"); |
| 176 | return NULL; |
| 177 | } |
| 178 | |
| 179 | return socket; |
| 180 | } else { |
| 181 | NSLog(@"TSocketServer: No server socket"); |
| 182 | return NULL; |
| 183 | } |
| 184 | } |
| 185 | |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 186 | -(void) dealloc |
| 187 | { |
Jake Farrell | 6f3a526 | 2012-07-27 15:48:37 +0000 | [diff] [blame] | 188 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
Chris Vasselli | df3223c | 2016-06-21 16:45:39 -0700 | [diff] [blame] | 189 | |
| 190 | if (_domainSocketPath != nil) { |
| 191 | unlink(_domainSocketPath.UTF8String); |
| 192 | } |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 196 | -(void) connectionAccepted:(NSNotification *)notification |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 197 | { |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 198 | NSFileHandle *socket = [notification.userInfo objectForKey:NSFileHandleNotificationFileHandleItem]; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 199 | |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 200 | // Now that we have a client connected, handle request on queue |
| 201 | dispatch_async(_processingQueue, ^{ |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 202 | |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 203 | [self handleClientConnection:socket]; |
| 204 | |
| 205 | }); |
| 206 | |
| 207 | // Continue accepting connections |
| 208 | [_socketFileHandle acceptConnectionInBackgroundAndNotify]; |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 212 | -(void) handleClientConnection:(NSFileHandle *)clientSocket |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 213 | { |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 214 | @autoreleasepool { |
| 215 | |
| 216 | TNSFileHandleTransport *transport = [[TNSFileHandleTransport alloc] initWithFileHandle:clientSocket]; |
| 217 | id<TProcessor> processor = [_processorFactory processorForTransport:transport]; |
| 218 | |
| 219 | id <TProtocol> inProtocol = [_inputProtocolFactory newProtocolOnTransport:transport]; |
| 220 | id <TProtocol> outProtocol = [_outputProtocolFactory newProtocolOnTransport:transport]; |
| 221 | |
| 222 | NSError *error; |
| 223 | if (![processor processOnInputProtocol:inProtocol outputProtocol:outProtocol error:&error]) { |
| 224 | // Handle error |
| 225 | NSLog(@"Error processing request: %@", error); |
Jake Farrell | 9689d89 | 2011-12-06 01:07:17 +0000 | [diff] [blame] | 226 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 227 | |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 228 | dispatch_async(dispatch_get_main_queue(), ^{ |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 229 | |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 230 | [NSNotificationCenter.defaultCenter postNotificationName:TSocketServerClientConnectionFinished |
| 231 | object:self |
| 232 | userInfo:@{TSocketServerProcessorKey: processor, |
| 233 | TSockerServerTransportKey: transport}]; |
| 234 | }); |
| 235 | |
| 236 | } |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 239 | @end |