2022-02-23 20:38:22 +03:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2022 by Claudio Cambra <claudio.cambra@nextcloud.com>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
|
* for more details.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
2022-12-23 19:52:16 +03:00
|
|
|
|
|
2022-02-23 20:38:22 +03:00
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
#import "LocalSocketClient.h"
|
|
|
|
|
|
|
|
|
|
@interface LocalSocketClient()
|
|
|
|
|
{
|
|
|
|
|
NSString* _socketPath;
|
2022-12-23 20:24:05 +03:00
|
|
|
|
id<LineProcessor> _lineProcessor;
|
2022-12-23 19:52:16 +03:00
|
|
|
|
|
|
|
|
|
int _sock;
|
|
|
|
|
dispatch_queue_t _localSocketQueue;
|
|
|
|
|
dispatch_source_t _readSource;
|
|
|
|
|
dispatch_source_t _writeSource;
|
|
|
|
|
NSMutableData* _inBuffer;
|
|
|
|
|
NSMutableData* _outBuffer;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2022-02-23 20:38:22 +03:00
|
|
|
|
@implementation LocalSocketClient
|
|
|
|
|
|
2022-12-23 20:24:05 +03:00
|
|
|
|
- (instancetype)initWithSocketPath:(NSString*)socketPath
|
|
|
|
|
lineProcessor:(id<LineProcessor>)lineProcessor
|
2022-02-23 20:38:22 +03:00
|
|
|
|
{
|
2022-12-27 02:18:00 +03:00
|
|
|
|
NSLog(@"Initiating local socket client pointing to %@", socketPath);
|
2022-02-23 20:38:22 +03:00
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
|
|
if(self) {
|
2022-12-23 19:52:16 +03:00
|
|
|
|
_socketPath = socketPath;
|
|
|
|
|
_lineProcessor = lineProcessor;
|
2022-02-23 20:38:22 +03:00
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
_sock = -1;
|
|
|
|
|
_localSocketQueue = dispatch_queue_create("localSocketQueue", DISPATCH_QUEUE_SERIAL);
|
2022-02-23 20:38:22 +03:00
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
_inBuffer = [NSMutableData data];
|
|
|
|
|
_outBuffer = [NSMutableData data];
|
2022-02-23 20:38:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL)isConnected
|
|
|
|
|
{
|
2022-12-23 19:52:16 +03:00
|
|
|
|
NSLog(@"Checking is connected: %@", _sock != -1 ? @"YES" : @"NO");
|
|
|
|
|
return _sock != -1;
|
2022-02-23 20:38:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)start
|
|
|
|
|
{
|
|
|
|
|
if([self isConnected]) {
|
|
|
|
|
NSLog(@"Socket client already connected. Not starting.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct sockaddr_un localSocketAddr;
|
2022-12-23 19:52:16 +03:00
|
|
|
|
unsigned long socketPathByteCount = [_socketPath lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; // add 1 for the NUL terminator char
|
2022-02-23 20:38:22 +03:00
|
|
|
|
int maxByteCount = sizeof(localSocketAddr.sun_path);
|
|
|
|
|
|
|
|
|
|
if(socketPathByteCount > maxByteCount) {
|
|
|
|
|
// LOG THAT THE SOCKET PATH IS TOO LONG HERE
|
2022-12-23 19:52:16 +03:00
|
|
|
|
NSLog(@"Socket path '%@' is too long: maximum socket path length is %i, this path is of length %lu", _socketPath, maxByteCount, socketPathByteCount);
|
2022-02-23 20:38:22 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NSLog(@"Opening local socket...");
|
|
|
|
|
|
|
|
|
|
// LOG THAT THE SOCKET IS BEING OPENED HERE
|
2022-12-23 19:52:16 +03:00
|
|
|
|
_sock = socket(AF_LOCAL, SOCK_STREAM, 0);
|
2022-02-23 20:38:22 +03:00
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
if(_sock == -1) {
|
2022-02-23 20:38:22 +03:00
|
|
|
|
NSLog(@"Cannot open socket: '%@'", [self strErr]);
|
|
|
|
|
[self restart];
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
NSLog(@"Local socket opened. Connecting to '%@' ...", _socketPath);
|
2022-02-23 20:38:22 +03:00
|
|
|
|
|
|
|
|
|
localSocketAddr.sun_family = AF_LOCAL & 0xff;
|
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
const char* pathBytes = [_socketPath UTF8String];
|
2022-02-23 20:38:22 +03:00
|
|
|
|
strcpy(localSocketAddr.sun_path, pathBytes);
|
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
int connectionStatus = connect(_sock, (struct sockaddr*)&localSocketAddr, sizeof(localSocketAddr));
|
2022-02-23 20:38:22 +03:00
|
|
|
|
|
|
|
|
|
if(connectionStatus == -1) {
|
2022-12-23 19:52:16 +03:00
|
|
|
|
NSLog(@"Could not connect to '%@': '%@'", _socketPath, [self strErr]);
|
2022-02-23 20:38:22 +03:00
|
|
|
|
[self restart];
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
int flags = fcntl(_sock, F_GETFL, 0);
|
2022-02-23 20:38:22 +03:00
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
if(fcntl(_sock, F_SETFL, flags | O_NONBLOCK) == -1) {
|
2022-02-23 20:38:22 +03:00
|
|
|
|
NSLog(@"Could not set socket to non-blocking mode: '%@'", [self strErr]);
|
|
|
|
|
[self restart];
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NSLog(@"Connected to socket. Setting up dispatch sources...");
|
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
_readSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, _sock, 0, _localSocketQueue);
|
|
|
|
|
dispatch_source_set_event_handler(_readSource, ^(void){ [self readFromSocket]; });
|
|
|
|
|
dispatch_source_set_cancel_handler(_readSource, ^(void){
|
|
|
|
|
self->_readSource = nil;
|
2022-02-23 20:38:22 +03:00
|
|
|
|
[self closeConnection];
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
_writeSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_WRITE, _sock, 0, _localSocketQueue);
|
|
|
|
|
dispatch_source_set_event_handler(_writeSource, ^(void){ [self writeToSocket]; });
|
|
|
|
|
dispatch_source_set_cancel_handler(_writeSource, ^(void){
|
|
|
|
|
self->_writeSource = nil;
|
2022-02-23 20:38:22 +03:00
|
|
|
|
[self closeConnection];
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// These dispatch sources are suspended upon creation.
|
|
|
|
|
// We resume the writeSource when we actually have something to write, suspending it again once our outBuffer is empty.
|
|
|
|
|
// We start the readSource now.
|
|
|
|
|
|
|
|
|
|
NSLog(@"Starting to read from socket");
|
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
dispatch_resume(_readSource);
|
2022-02-23 20:38:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)restart
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Restarting connection to socket.");
|
|
|
|
|
[self closeConnection];
|
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^(void){
|
|
|
|
|
[NSTimer scheduledTimerWithTimeInterval:5 repeats:NO block:^(NSTimer* timer) {
|
|
|
|
|
[self start];
|
|
|
|
|
}];
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)closeConnection
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Closing connection.");
|
2022-05-12 12:50:03 +03:00
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
if(_readSource) {
|
2022-06-15 13:06:41 +03:00
|
|
|
|
// Since dispatch_source_cancel works asynchronously, if we deallocate the dispatch source here then we can
|
|
|
|
|
// cause a crash. So instead we strongly hold a reference to the read source and deallocate it asynchronously
|
|
|
|
|
// with the handler.
|
2022-12-23 19:52:16 +03:00
|
|
|
|
__block dispatch_source_t previousReadSource = _readSource;
|
|
|
|
|
dispatch_source_set_cancel_handler(_readSource, ^{
|
2022-06-15 13:06:41 +03:00
|
|
|
|
previousReadSource = nil;
|
|
|
|
|
});
|
2022-12-23 19:52:16 +03:00
|
|
|
|
dispatch_source_cancel(_readSource);
|
2022-06-15 13:06:41 +03:00
|
|
|
|
// The readSource is still alive due to the other reference and will be deallocated by the cancel handler
|
2022-12-23 19:52:16 +03:00
|
|
|
|
_readSource = nil;
|
2022-05-12 12:50:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
if(_writeSource) {
|
2022-06-15 13:06:41 +03:00
|
|
|
|
// Same deal with the write source
|
2022-12-23 19:52:16 +03:00
|
|
|
|
__block dispatch_source_t previousWriteSource = _writeSource;
|
|
|
|
|
dispatch_source_set_cancel_handler(_writeSource, ^{
|
2022-06-15 13:06:41 +03:00
|
|
|
|
previousWriteSource = nil;
|
|
|
|
|
});
|
2022-12-23 19:52:16 +03:00
|
|
|
|
dispatch_source_cancel(_writeSource);
|
|
|
|
|
_writeSource = nil;
|
2022-05-12 12:50:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
[_inBuffer setLength:0];
|
|
|
|
|
[_outBuffer setLength: 0];
|
2022-02-23 20:38:22 +03:00
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
if(_sock != -1) {
|
|
|
|
|
close(_sock);
|
|
|
|
|
_sock = -1;
|
2022-02-23 20:38:22 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*)strErr
|
|
|
|
|
{
|
|
|
|
|
int err = errno;
|
|
|
|
|
const char *errStr = strerror(err);
|
|
|
|
|
NSString *errorStr = [NSString stringWithUTF8String:errStr];
|
|
|
|
|
|
|
|
|
|
if([errorStr length] == 0) {
|
|
|
|
|
return errorStr;
|
|
|
|
|
} else {
|
|
|
|
|
return [NSString stringWithFormat:@"Unknown error code: %i", err];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 01:44:40 +03:00
|
|
|
|
- (void)sendMessage:(NSString *)message
|
2022-02-23 20:38:22 +03:00
|
|
|
|
{
|
2022-12-23 19:52:16 +03:00
|
|
|
|
dispatch_async(_localSocketQueue, ^(void) {
|
2022-02-23 20:38:22 +03:00
|
|
|
|
if(![self isConnected]) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-12-29 01:44:40 +03:00
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
BOOL writeSourceIsSuspended = [self->_outBuffer length] == 0;
|
2022-12-29 01:44:40 +03:00
|
|
|
|
|
|
|
|
|
[self->_outBuffer appendData:[message dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
|
|
|
|
|
|
|
NSLog(@"Writing to out buffer: '%@'", message);
|
2022-12-23 19:52:16 +03:00
|
|
|
|
NSLog(@"Out buffer now %li bytes", [self->_outBuffer length]);
|
2022-12-29 01:44:40 +03:00
|
|
|
|
|
2022-02-23 20:38:22 +03:00
|
|
|
|
if(writeSourceIsSuspended) {
|
|
|
|
|
NSLog(@"Resuming write dispatch source.");
|
2022-12-23 19:52:16 +03:00
|
|
|
|
dispatch_resume(self->_writeSource);
|
2022-02-23 20:38:22 +03:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 01:44:40 +03:00
|
|
|
|
- (void)askOnSocket:(NSString *)path query:(NSString *)verb
|
|
|
|
|
{
|
|
|
|
|
NSString *line = [NSString stringWithFormat:@"%@:%@\n", verb, path];
|
|
|
|
|
[self sendMessage:line];
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 20:38:22 +03:00
|
|
|
|
- (void)writeToSocket
|
|
|
|
|
{
|
|
|
|
|
if(![self isConnected]) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
if([_outBuffer length] == 0) {
|
2022-02-23 20:38:22 +03:00
|
|
|
|
NSLog(@"Empty out buffer, suspending write dispatch source.");
|
2022-12-23 19:52:16 +03:00
|
|
|
|
dispatch_suspend(_writeSource);
|
2022-02-23 20:38:22 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
NSLog(@"About to write %li bytes from outbuffer to socket.", [_outBuffer length]);
|
2022-02-23 20:38:22 +03:00
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
long bytesWritten = write(_sock, [_outBuffer bytes], [_outBuffer length]);
|
|
|
|
|
char lineWritten[[_outBuffer length]];
|
|
|
|
|
memcpy(lineWritten, [_outBuffer bytes], [_outBuffer length]);
|
2022-05-18 17:08:12 +03:00
|
|
|
|
NSLog(@"Wrote %li bytes to socket. Line written was: '%@'", bytesWritten, [NSString stringWithUTF8String:lineWritten]);
|
2022-02-23 20:38:22 +03:00
|
|
|
|
|
|
|
|
|
if(bytesWritten == 0) {
|
|
|
|
|
// 0 means we reached "end of file" and thus the socket was closed. So let's restart it
|
|
|
|
|
NSLog(@"Socket was closed. Restarting...");
|
|
|
|
|
[self restart];
|
|
|
|
|
} else if(bytesWritten == -1) {
|
|
|
|
|
int err = errno; // Make copy before it gets nuked by something else
|
|
|
|
|
|
|
|
|
|
if(err == EAGAIN || err == EWOULDBLOCK) {
|
|
|
|
|
// No free space in the OS' buffer, nothing to do here
|
|
|
|
|
NSLog(@"No free space in OS buffer. Ending write.");
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
NSLog(@"Error writing to local socket: '%@'", [self strErr]);
|
|
|
|
|
[self restart];
|
|
|
|
|
}
|
|
|
|
|
} else if(bytesWritten > 0) {
|
2022-12-23 19:52:16 +03:00
|
|
|
|
[_outBuffer replaceBytesInRange:NSMakeRange(0, bytesWritten) withBytes:NULL length:0];
|
2022-02-23 20:38:22 +03:00
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
NSLog(@"Out buffer cleared. Now count is %li bytes.", [_outBuffer length]);
|
2022-02-23 20:38:22 +03:00
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
if([_outBuffer length] == 0) {
|
2022-02-23 20:38:22 +03:00
|
|
|
|
NSLog(@"Out buffer has been emptied, suspending write dispatch source.");
|
2022-12-23 19:52:16 +03:00
|
|
|
|
dispatch_suspend(_writeSource);
|
2022-02-23 20:38:22 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)askForIcon:(NSString*)path isDirectory:(BOOL)isDirectory;
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Asking for icon.");
|
|
|
|
|
|
|
|
|
|
NSString *verb;
|
|
|
|
|
if(isDirectory) {
|
|
|
|
|
verb = @"RETRIEVE_FOLDER_STATUS";
|
|
|
|
|
} else {
|
|
|
|
|
verb = @"RETRIEVE_FILE_STATUS";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[self askOnSocket:path query:verb];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)readFromSocket
|
|
|
|
|
{
|
|
|
|
|
if(![self isConnected]) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NSLog(@"Reading from socket.");
|
|
|
|
|
|
|
|
|
|
int bufferLength = BUF_SIZE / 2;
|
|
|
|
|
char buffer[bufferLength];
|
|
|
|
|
|
|
|
|
|
while(true) {
|
2022-12-23 19:52:16 +03:00
|
|
|
|
long bytesRead = read(_sock, buffer, bufferLength);
|
2022-02-23 20:38:22 +03:00
|
|
|
|
|
|
|
|
|
NSLog(@"Read %li bytes from socket.", bytesRead);
|
|
|
|
|
|
|
|
|
|
if(bytesRead == 0) {
|
|
|
|
|
// 0 means we reached "end of file" and thus the socket was closed. So let's restart it
|
|
|
|
|
NSLog(@"Socket was closed. Restarting...");
|
|
|
|
|
[self restart];
|
|
|
|
|
return;
|
|
|
|
|
} else if(bytesRead == -1) {
|
|
|
|
|
int err = errno;
|
|
|
|
|
if(err == EAGAIN) {
|
|
|
|
|
NSLog(@"No error and no data. Stopping.");
|
|
|
|
|
return; // No error, no data, so let's stop
|
|
|
|
|
} else {
|
|
|
|
|
NSLog(@"Error reading from local socket: '%@'", [self strErr]);
|
|
|
|
|
[self closeConnection];
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-12-23 19:52:16 +03:00
|
|
|
|
[_inBuffer appendBytes:buffer length:bytesRead];
|
2022-02-23 20:38:22 +03:00
|
|
|
|
[self processInBuffer];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)processInBuffer
|
|
|
|
|
{
|
2022-12-23 19:52:16 +03:00
|
|
|
|
NSLog(@"Processing in buffer. In buffer length %li", [_inBuffer length]);
|
2022-02-23 20:38:22 +03:00
|
|
|
|
UInt8 separator[] = {0xa}; // Byte value for "\n"
|
|
|
|
|
while(true) {
|
2022-12-23 19:52:16 +03:00
|
|
|
|
NSRange firstSeparatorIndex = [_inBuffer rangeOfData:[NSData dataWithBytes:separator length:1] options:0 range:NSMakeRange(0, [_inBuffer length])];
|
2022-02-23 20:38:22 +03:00
|
|
|
|
|
|
|
|
|
if(firstSeparatorIndex.location == NSNotFound) {
|
|
|
|
|
NSLog(@"No separator found. Stopping.");
|
|
|
|
|
return; // No separator, nope out
|
|
|
|
|
} else {
|
2022-12-23 19:52:16 +03:00
|
|
|
|
unsigned char *buffer = [_inBuffer mutableBytes];
|
2022-02-23 20:38:22 +03:00
|
|
|
|
buffer[firstSeparatorIndex.location] = 0; // Add NULL terminator, so we can use C string methods
|
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
NSString *newLine = [NSString stringWithUTF8String:[_inBuffer bytes]];
|
2022-02-23 20:38:22 +03:00
|
|
|
|
|
2022-12-23 19:52:16 +03:00
|
|
|
|
[_inBuffer replaceBytesInRange:NSMakeRange(0, firstSeparatorIndex.location + 1) withBytes:NULL length:0];
|
|
|
|
|
[_lineProcessor process:newLine];
|
2022-02-23 20:38:22 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|