Skip to content

Instantly share code, notes, and snippets.

@NSEcho
Created October 29, 2021 11:08
Show Gist options
  • Save NSEcho/ac0e84fab66c5a0794bf753bccc4cd3d to your computer and use it in GitHub Desktop.
Save NSEcho/ac0e84fab66c5a0794bf753bccc4cd3d to your computer and use it in GitHub Desktop.
Mach ports client and server
// cc client.c -o client
#include <stdio.h>
#include <stdlib.h>
#include <mach/mach.h>
#include <bootstrap.h>
#include <string.h>
#include <unistd.h>
typedef struct {
mach_msg_header_t header;
char body_str[32];
} message;
int main(void) {
int kr;
mach_port_name_t task = mach_task_self();
// Create bootstrap port
mach_port_t bootstrap_port;
kr = task_get_special_port(task, TASK_BOOTSTRAP_PORT, &bootstrap_port);
if (kr != KERN_SUCCESS) {
return -1;
}
printf("[*] Got special bootstrap port: 0x%x\n", bootstrap_port);
// Get port to send to the com.echo.macherino.as-a-service and store it in port
mach_port_t port;
kr = bootstrap_look_up(bootstrap_port, "com.echo.macherino.as-a-service", &port);
if (kr != KERN_SUCCESS) {
return -2;
}
printf("[*] Port for com.echo.macherino.as-a-service: 0x%x\n", port);
// Create the message for sending
message msg = {0};
msg.header.msgh_remote_port = port;
msg.header.msgh_bits = MACH_MSGH_BITS_SET(
MACH_MSG_TYPE_COPY_SEND,
0,
0,
0);
msg.header.msgh_id = 4;
msg.header.msgh_size = sizeof(msg);
strcpy(msg.body_str, "test message");
mach_msg_return_t ret = mach_msg(
(mach_msg_header_t *)&msg,
MACH_SEND_MSG,
sizeof(msg),
0,
MACH_PORT_NULL,
MACH_MSG_TIMEOUT_NONE,
MACH_PORT_NULL);
return ret;
}
// cc server.c -o server
#include <bootstrap.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mach/mach.h>
typedef struct {
mach_msg_header_t header;
char body_str[32];
mach_msg_trailer_t trailer;
} message;
mach_msg_return_t receive_msg(mach_port_name_t);
int main(void) {
int kr;
mach_port_t task = mach_task_self();
mach_port_name_t recv_port;
kr = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &recv_port);
if (kr != KERN_SUCCESS)
return EXIT_FAILURE;
printf("[*] Created port with MACH_PORT_RIGHT_RECEIVE: 0x%x\n", recv_port);
kr = mach_port_insert_right(task, recv_port, recv_port, MACH_MSG_TYPE_MAKE_SEND);
if (kr != KERN_SUCCESS)
return EXIT_FAILURE;
printf("[*] Added send right to the port\n");
mach_port_t bootstrap_port;
kr = task_get_special_port(task, TASK_BOOTSTRAP_PORT, &bootstrap_port);
printf("[*] Got bootstrap port: 0x%x\n", bootstrap_port);
if (kr != KERN_SUCCESS)
return EXIT_FAILURE;
kr = bootstrap_check_in(bootstrap_port, "com.echo.macherino.as-a-service", &recv_port);
if (kr != KERN_SUCCESS)
return EXIT_FAILURE;
printf("[*] Registered our service\n");
while(true) {
mach_msg_return_t ret = receive_msg(recv_port);
}
}
mach_msg_return_t receive_msg(mach_port_name_t recv_port) {
message msg = {0};
mach_msg_return_t ret = mach_msg(
(mach_msg_header_t *)&msg,
MACH_RCV_MSG,
0,
sizeof(msg),
recv_port,
MACH_MSG_TIMEOUT_NONE,
MACH_PORT_NULL);
if (ret != MACH_MSG_SUCCESS)
return ret;
printf("got message\n");
printf("\tid: %d\n", msg.header.msgh_id);
printf("\tbodys: %s\n", msg.body_str);
return MACH_MSG_SUCCESS;
}
@NSEcho
Copy link
Author

NSEcho commented Oct 29, 2021

Server:

$ ./server
[*] Created port with MACH_PORT_RIGHT_RECEIVE: 0xb03
[*] Added send right to the port
[*] Got bootstrap port: 0x707
[*] Registered our service
got message
	id: 4
	bodys: test message

Client:

$ ./client
[*] Got special bootstrap port: 0x707
[*] Port for com.echo.macherino.as-a-service: 0x1203

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment