1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
typedef struct { struct kern_ctl_reg kctl_register; kern_ctl_ref kctl_refrance; uint8_t kext_unit; bool is_connected; } KextControlEnv; KextControlEnv g_kext_ctl;
errno_t kext_control_init(void) { errno_t error = KERN_SUCCESS; bzero(&g_kext_ctl, sizeof(KextControlEnv));
strlcpy(g_kext_ctl.kctl_register.ctl_name, "your kext name", MAX_KCTL_NAME); g_kext_ctl.kctl_register.ctl_flags = CTL_FLAG_PRIVILEGED; g_kext_ctl.kctl_register.ctl_send = kext_send; g_kext_ctl.kctl_register.ctl_getopt = sock_get_opt; g_kext_ctl.kctl_register.ctl_setopt = sock_set_opt; g_kext_ctl.kctl_register.ctl_connect = kext_connect; g_kext_ctl.kctl_register.ctl_disconnect = kext_disconnect;
error = ctl_register(&g_kext_ctl.kctl_register, &g_kext_ctl.kctl_refrance); if (error != KERN_SUCCESS) { LOG_C(LOG_ERROR, "Failed to register kext controller with error code [%d].", error); }
return error; }
errno_t kext_control_uninit(void) { errno_t error = KERN_SUCCESS;
error = ctl_deregister(g_kext_ctl.kctl_refrance); if (error != KERN_SUCCESS) { LOG_C(LOG_ERROR, "Failed to deregister kext controller with error code [%d].", error); }
return error; }
errno_t sock_set_opt(kern_ctl_ref ctlref, unsigned int unit, void *info, int command, void *data, size_t len) { errno_t err_code = KERN_SUCCESS;
switch (command) { }
return err_code; }
errno_t sock_get_opt(kern_ctl_ref ctlref, unsigned int unit, void *userdata, int opt, void *data, size_t *len) { return KERN_SUCCESS; }
errno_t kext_connect(kern_ctl_ref ctlref, struct sockaddr_ctl *sac, void **unitinfo) { errno_t err_code = KERN_SUCCESS;
if (g_kext_ctl.is_connected == true) { LOG_C(LOG_ERROR, "Failed to accept the connection request for a connection already established."); err_code = EISCONN; return err_code; }
g_kext_ctl.is_connected = true; g_kext_ctl.kext_unit = sac->sc_unit; LOG_C(LOG_INFO, "Connect with client successfully.");
return err_code; }
errno_t kext_disconnect(kern_ctl_ref ctlref, unsigned int unit, void *unitinfo) { g_kext_ctl.is_connected = false; LOG_C(LOG_INFO, "Disconnect with peer successfully.");
return KERN_SUCCESS; }
|