I would like to create Email client working with IMAP protocol. I'm using Apple's Network framework to create TCP connection with imap.gmail.com.
Spoiler! I used the official tutorial(Implementing netcat with Network Framework).
I have very simple interface in my app:
- Email placeholder
- Password placeholder
- Login button
All methods wrote in ViewController.m
When I press Login button:
- (IBAction)loginButtonPressed:(id)sender {
const char *hostname = "imap.gmail.com";
const char *port = "imaps";
NSString *tempFileTemplate = [NSTemporaryDirectory() stringByAppendingPathComponent:@"myapptempfile.XXXXXX"];
const char *tempFileTemplateCString = [tempFileTemplate fileSystemRepresentation];
char *tempFileNameCString = (char *)malloc(strlen(tempFileTemplateCString) + 1);
strcpy(tempFileNameCString, tempFileTemplateCString);
dispatch_fd_t fd = mkstemp(tempFileNameCString); // Creating temp file descriptor
free(tempFileNameCString);
NSFileHandle *tempFileHandle = [[NSFileHandle alloc] initWithFileDescriptor:fd closeOnDealloc:NO]; // Creating tempFile to use this file descriptor
nw_parameters_t parameters = nw_parameters_create_secure_tcp(NW_PARAMETERS_DEFAULT_CONFIGURATION, NW_PARAMETERS_DEFAULT_CONFIGURATION);
nw_endpoint_t endpoint = nw_endpoint_create_host(hostname, port);
nw_connection_t connection = nw_connection_create(endpoint, parameters);
[self start_connection:connection: tempFileHandle: fd]; // Start connection
}
Someone will think that it's it not necessary to pass "fd" variable to my connection method. And I guess you are be absolutely right, but I had some problems with file descriptors and it was my last try to understand what's going on.
Then I try to start connection and all is right there.
- (void)start_connection:(nw_connection_t)connection :(NSFileHandle *)file :(dispatch_fd_t)fd {
nw_connection_set_queue(connection, dispatch_get_main_queue());
nw_connection_set_state_changed_handler(connection, ^(nw_connection_state_t state, nw_error_t error) {
switch (state) {
case nw_connection_state_waiting:
NSLog(@"waiting");
break;
case nw_connection_state_failed:
NSLog(@"failed");
break;
case nw_connection_state_ready:
NSLog(@"connection is ready");
[self receive_loop:connection: file :fd];
break;
case nw_connection_state_cancelled:
NSLog(@"connection is cancelled");
break;
default:
break;
}
});
nw_connection_start(connection);
}
My console in Xcode just displays: "connection is ready".
As you can see, we call "receive_loop method with 3 arguments", let's take a look at this method:
- (void)receive_loop:(nw_connection_t)connection :(NSFileHandle *)file :(dispatch_fd_t)fd {
nw_connection_receive(connection, 1, UINT32_MAX, ^(dispatch_data_t content, nw_content_context_t context, bool is_complete, nw_error_t receive_error) {
dispatch_block_t schedule_next_receive = ^{
if (is_complete &&
context != NULL && nw_content_context_get_is_final(context)) {
exit(0);
}
if (receive_error == NULL) {
[self receive_loop:connection: file :fd];
} else {
NSLog(@"Error");
}
};
if (content != NULL) {
dispatch_write(fd, content, dispatch_get_main_queue(), ^(__unused dispatch_data_t _Nullable data, int stdout_error) {
if (stdout_error != 0) {
NSLog(@"Error in receive loop");
} else {
NSString* str = @"00000001 LOGIN zurgsrushmber@gmail.com mypasswordhere";
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
[file writeData:data];
[self send_loop:connection: file :fd];
schedule_next_receive();
}
});
} else {
// No content, so directly schedule the next receive
schedule_next_receive();
}
});
}
And now the most interesting thing: At first, I used STDOUT_FILENO instead of "fd" or (very important) "file.fileDescriptor" in my dispath_write() function. Well, when I was using STDOUT_FILENO my console just printing something like that "* OK Gimap ready for requests from 109.252.29.37 m5mb106667441ljg" As you can see, I'm trying to write it in my "fd" or in "file" variables (both doesn't work, I have "NULL" after trying to read fd or file).
So, that's how I try to send info, but it doesn't matter because I can't write to my fd, so I can't send the data to the imap.gmail.com...
My send_loop method:
- (void)send_loop:(nw_connection_t)connection :(NSFileHandle *)file :(dispatch_fd_t)fd {
dispatch_read(fd, 8192, dispatch_get_main_queue(), ^(dispatch_data_t _Nonnull read_data, int stdin_error) {
if (stdin_error != 0) {
// … error logging …
} else if (read_data == NULL) {
// … handle end of file …
} else {
nw_connection_send(connection, read_data, NW_CONNECTION_DEFAULT_MESSAGE_CONTEXT, true, ^(nw_error_t _Nullable error) {
if (error != NULL) {
// … error logging …
} else {
NSLog(@"send loop %@", (NSData *)read_data);
[self send_loop:connection: file :fd];
}
});
}
});
}
Someone help me please!
UPDATE: receive_loop method works properly, I understood it, when opened tmp folder and saw my file with content: "* OK Gimap ready for requests from 109.252.29.37 m5mb106667441ljg", but I can't read this file with send_loop method...