/* test of auth_user/server authenticate: send part */ #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include #include #include #include #include #include #include #include #include #include #include #include #define SOCK_PATH "auth_socket" int main (void) { mach_port_t rendezvous = __mach_reply_port (); mach_port_t newport; error_t err; struct msghdr msgh; struct iovec iov; int sfd, data; struct sockaddr_un addr = { 0 }; ssize_t ns; //char wbuf[30]; int cspc = CMSG_SPACE(sizeof(int)); union { struct cmsghdr cmh; char control[cspc]; } control_un; struct cmsghdr *cmhp = NULL; printf ("auth_send.c: rendezvous port = %d\n", (int)rendezvous); addr.sun_family = AF_UNIX; strncpy(addr.sun_path, SOCK_PATH, sizeof(addr.sun_path) - 1); sfd = socket (AF_UNIX, SOCK_DGRAM, 0); if (sfd == -1) { perror ("socket"); exit(EXIT_FAILURE); } printf("auth_send.c: Socket file descriptor = %d\n", sfd); err = connect (sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)); if (err == -1) { close(sfd); perror ("connect"); exit(EXIT_FAILURE); } err = mach_port_insert_right (mach_task_self (), rendezvous, rendezvous, MACH_MSG_TYPE_MAKE_SEND); printf ("auth_send.c: mach_port_insert_right() err = %d\n", err); if (err) goto out; msgh.msg_iov = &iov; msgh.msg_iovlen = 1; data = rendezvous; iov.iov_base = &data; iov.iov_len = sizeof(int); printf("auth_send.c: Sent data = %d\n", data); msgh.msg_name = NULL; msgh.msg_namelen = 0; msgh.msg_control = control_un.control; msgh.msg_controllen = sizeof(control_un.control); printf("auth_send.c: msgh.msg_controllen = %d\n", (int)msgh.msg_controllen); cmhp = CMSG_FIRSTHDR(&msgh); cmhp->cmsg_len = CMSG_LEN(sizeof(int)); cmhp->cmsg_level = SOL_SOCKET; cmhp->cmsg_type = SCM_RIGHTS; *((int *) CMSG_DATA(cmhp)) = sfd; printf("auth_send.c: Sent file descriptor: sfd = %d\n", sfd); printf("auth_send.c: cmhp->cmsg_len = %d\n", (int)cmhp->cmsg_len); printf("auth_send.c: Sending via datagram socket\n"); ns = sendmsg(sfd, &msgh, 0); if (ns == -1) { perror ("sendmsg"); exit(EXIT_FAILURE); } printf("auth_send.c: sendmsg() returned %ld bytes\n", (long) ns); err = __USEPORT (AUTH, auth_user_authenticate (port, rendezvous, MACH_MSG_TYPE_MAKE_SEND, &newport)); out: printf ("auth_send.c: newport=%d\n", (int)newport); __mach_port_deallocate (__mach_task_self (), rendezvous); __mach_port_deallocate (__mach_task_self (), newport); if (err) { printf ("auth_send.c: err = %d\n", err); exit(EXIT_FAILURE); } else exit(EXIT_SUCCESS); }