home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / mach / doc / unpublished / examples / randomUser.c.Z / randomUser.c
Encoding:
C/C++ Source or Header  |  1989-12-30  |  7.1 KB  |  309 lines

  1. #include "random.h"
  2. #include <mach/message.h>
  3. #include <mach/mach_types.h>
  4. #include <mach/mig_errors.h>
  5. #include <mach/msg_type.h>
  6. #if    !defined(KERNEL) && !defined(MIG_NO_STRINGS)
  7. #include <strings.h>
  8. #endif
  9. /* LINTLIBRARY */
  10.  
  11. extern port_t mig_get_reply_port();
  12. extern void mig_dealloc_reply_port();
  13.  
  14. #ifndef    mig_internal
  15. #define    mig_internal    static
  16. #endif
  17.  
  18. #ifndef    TypeCheck
  19. #define    TypeCheck 1
  20. #endif
  21.  
  22. #ifndef    UseExternRCSId
  23. #ifdef    __HC__
  24. #define    UseExternRCSId        1
  25. #endif
  26. #endif
  27.  
  28. #ifndef    UseStaticMsgType
  29. #if    !defined(__HC__) || defined(__STDC__)
  30. #define    UseStaticMsgType    1
  31. #endif
  32. #endif
  33.  
  34. #define msg_request_port    msg_remote_port
  35. #define msg_reply_port        msg_local_port
  36.  
  37. mig_external void init_random
  38. #if    (defined(__STDC__) || defined(c_plusplus))
  39.     (port_t rep_port)
  40. #else
  41.     (rep_port)
  42.     port_t rep_port;
  43. #endif
  44. {
  45. #ifdef    lint
  46.     rep_port++;
  47. #endif
  48. }
  49.  
  50. /* Routine get_random */
  51. mig_external kern_return_t get_random
  52. #if    (defined(__STDC__) || defined(c_plusplus))
  53. (
  54.     port_t server_port,
  55.     int *num
  56. )
  57. #else
  58.     (server_port, num)
  59.     port_t server_port;
  60.     int *num;
  61. #endif
  62. {
  63.     typedef struct {
  64.         msg_header_t Head;
  65.     } Request;
  66.  
  67.     typedef struct {
  68.         msg_header_t Head;
  69.         msg_type_t RetCodeType;
  70.         kern_return_t RetCode;
  71.         msg_type_t numType;
  72.         int num;
  73.     } Reply;
  74.  
  75.     union {
  76.         Request In;
  77.         Reply Out;
  78.     } Mess;
  79.  
  80.     register Request *InP = &Mess.In;
  81.     register Reply *OutP = &Mess.Out;
  82.  
  83.     msg_return_t msg_result;
  84.  
  85. #if    TypeCheck
  86.     boolean_t msg_simple;
  87. #endif    TypeCheck
  88.  
  89.     unsigned int msg_size = 24;
  90.  
  91. #if    UseStaticMsgType
  92.     static msg_type_t RetCodeCheck = {
  93.         /* msg_type_name = */        MSG_TYPE_INTEGER_32,
  94.         /* msg_type_size = */        32,
  95.         /* msg_type_number = */        1,
  96.         /* msg_type_inline = */        TRUE,
  97.         /* msg_type_longform = */    FALSE,
  98.         /* msg_type_deallocate = */    FALSE,
  99.         /* msg_type_unused = */        0
  100.     };
  101. #endif    UseStaticMsgType
  102.  
  103. #if    UseStaticMsgType
  104.     static msg_type_t numCheck = {
  105.         /* msg_type_name = */        MSG_TYPE_INTEGER_32,
  106.         /* msg_type_size = */        32,
  107.         /* msg_type_number = */        1,
  108.         /* msg_type_inline = */        TRUE,
  109.         /* msg_type_longform = */    FALSE,
  110.         /* msg_type_deallocate = */    FALSE,
  111.         /* msg_type_unused = */        0
  112.     };
  113. #endif    UseStaticMsgType
  114.  
  115.     InP->Head.msg_simple = TRUE;
  116.     InP->Head.msg_size = msg_size;
  117.     InP->Head.msg_type = MSG_TYPE_NORMAL | MSG_TYPE_RPC;
  118.     InP->Head.msg_request_port = server_port;
  119.     InP->Head.msg_reply_port = mig_get_reply_port();
  120.     InP->Head.msg_id = 500;
  121.  
  122.     msg_result = msg_rpc(&InP->Head, MSG_OPTION_NONE, sizeof(Reply), 0, 0);
  123.     if (msg_result != RPC_SUCCESS) {
  124.         if (msg_result == RCV_INVALID_PORT)
  125.             mig_dealloc_reply_port();
  126.         return msg_result;
  127.     }
  128.  
  129. #if    TypeCheck
  130.     msg_size = OutP->Head.msg_size;
  131.     msg_simple = OutP->Head.msg_simple;
  132. #endif    TypeCheck
  133.  
  134.     if (OutP->Head.msg_id != 600)
  135.         return MIG_REPLY_MISMATCH;
  136.  
  137. #if    TypeCheck
  138.     if (((msg_size != 40) || (msg_simple != TRUE)) &&
  139.         ((msg_size != sizeof(death_pill_t)) ||
  140.          (msg_simple != TRUE) ||
  141.          (OutP->RetCode == KERN_SUCCESS)))
  142.         return MIG_TYPE_ERROR;
  143. #endif    TypeCheck
  144.  
  145. #if    TypeCheck
  146. #if    UseStaticMsgType
  147.     if (* (int *) &OutP->RetCodeType != * (int *) &RetCodeCheck)
  148. #else    UseStaticMsgType
  149.     if ((OutP->RetCodeType.msg_type_inline != TRUE) ||
  150.         (OutP->RetCodeType.msg_type_longform != FALSE) ||
  151.         (OutP->RetCodeType.msg_type_name != MSG_TYPE_INTEGER_32) ||
  152.         (OutP->RetCodeType.msg_type_number != 1) ||
  153.         (OutP->RetCodeType.msg_type_size != 32))
  154. #endif    UseStaticMsgType
  155.         return MIG_TYPE_ERROR;
  156. #endif    TypeCheck
  157.  
  158.     if (OutP->RetCode != KERN_SUCCESS)
  159.         return OutP->RetCode;
  160.  
  161. #if    TypeCheck
  162. #if    UseStaticMsgType
  163.     if (* (int *) &OutP->numType != * (int *) &numCheck)
  164. #else    UseStaticMsgType
  165.     if ((OutP->numType.msg_type_inline != TRUE) ||
  166.         (OutP->numType.msg_type_longform != FALSE) ||
  167.         (OutP->numType.msg_type_name != MSG_TYPE_INTEGER_32) ||
  168.         (OutP->numType.msg_type_number != 1) ||
  169.         (OutP->numType.msg_type_size != 32))
  170. #endif    UseStaticMsgType
  171.         return MIG_TYPE_ERROR;
  172. #endif    TypeCheck
  173.  
  174.     *num /* num */ = /* *num */ OutP->num;
  175.  
  176.     return OutP->RetCode;
  177. }
  178.  
  179. /* Routine get_secret */
  180. mig_external kern_return_t get_secret
  181. #if    (defined(__STDC__) || defined(c_plusplus))
  182. (
  183.     port_t server_port,
  184.     string25 password
  185. )
  186. #else
  187.     (server_port, password)
  188.     port_t server_port;
  189.     string25 password;
  190. #endif
  191. {
  192.     typedef struct {
  193.         msg_header_t Head;
  194.     } Request;
  195.  
  196.     typedef struct {
  197.         msg_header_t Head;
  198.         msg_type_t RetCodeType;
  199.         kern_return_t RetCode;
  200.         msg_type_t passwordType;
  201.         string25 password;
  202.         char passwordPad[3];
  203.     } Reply;
  204.  
  205.     union {
  206.         Request In;
  207.         Reply Out;
  208.     } Mess;
  209.  
  210.     register Request *InP = &Mess.In;
  211.     register Reply *OutP = &Mess.Out;
  212.  
  213.     msg_return_t msg_result;
  214.  
  215. #if    TypeCheck
  216.     boolean_t msg_simple;
  217. #endif    TypeCheck
  218.  
  219.     unsigned int msg_size = 24;
  220.  
  221. #if    UseStaticMsgType
  222.     static msg_type_t RetCodeCheck = {
  223.         /* msg_type_name = */        MSG_TYPE_INTEGER_32,
  224.         /* msg_type_size = */        32,
  225.         /* msg_type_number = */        1,
  226.         /* msg_type_inline = */        TRUE,
  227.         /* msg_type_longform = */    FALSE,
  228.         /* msg_type_deallocate = */    FALSE,
  229.         /* msg_type_unused = */        0
  230.     };
  231. #endif    UseStaticMsgType
  232.  
  233. #if    UseStaticMsgType
  234.     static msg_type_t passwordCheck = {
  235.         /* msg_type_name = */        MSG_TYPE_STRING_C,
  236.         /* msg_type_size = */        200,
  237.         /* msg_type_number = */        1,
  238.         /* msg_type_inline = */        TRUE,
  239.         /* msg_type_longform = */    FALSE,
  240.         /* msg_type_deallocate = */    FALSE,
  241.         /* msg_type_unused = */        0
  242.     };
  243. #endif    UseStaticMsgType
  244.  
  245.     InP->Head.msg_simple = TRUE;
  246.     InP->Head.msg_size = msg_size;
  247.     InP->Head.msg_type = MSG_TYPE_NORMAL | MSG_TYPE_RPC;
  248.     InP->Head.msg_request_port = server_port;
  249.     InP->Head.msg_reply_port = mig_get_reply_port();
  250.     InP->Head.msg_id = 501;
  251.  
  252.     msg_result = msg_rpc(&InP->Head, MSG_OPTION_NONE, sizeof(Reply), 0, 0);
  253.     if (msg_result != RPC_SUCCESS) {
  254.         if (msg_result == RCV_INVALID_PORT)
  255.             mig_dealloc_reply_port();
  256.         return msg_result;
  257.     }
  258.  
  259. #if    TypeCheck
  260.     msg_size = OutP->Head.msg_size;
  261.     msg_simple = OutP->Head.msg_simple;
  262. #endif    TypeCheck
  263.  
  264.     if (OutP->Head.msg_id != 601)
  265.         return MIG_REPLY_MISMATCH;
  266.  
  267. #if    TypeCheck
  268.     if (((msg_size != 64) || (msg_simple != TRUE)) &&
  269.         ((msg_size != sizeof(death_pill_t)) ||
  270.          (msg_simple != TRUE) ||
  271.          (OutP->RetCode == KERN_SUCCESS)))
  272.         return MIG_TYPE_ERROR;
  273. #endif    TypeCheck
  274.  
  275. #if    TypeCheck
  276. #if    UseStaticMsgType
  277.     if (* (int *) &OutP->RetCodeType != * (int *) &RetCodeCheck)
  278. #else    UseStaticMsgType
  279.     if ((OutP->RetCodeType.msg_type_inline != TRUE) ||
  280.         (OutP->RetCodeType.msg_type_longform != FALSE) ||
  281.         (OutP->RetCodeType.msg_type_name != MSG_TYPE_INTEGER_32) ||
  282.         (OutP->RetCodeType.msg_type_number != 1) ||
  283.         (OutP->RetCodeType.msg_type_size != 32))
  284. #endif    UseStaticMsgType
  285.         return MIG_TYPE_ERROR;
  286. #endif    TypeCheck
  287.  
  288.     if (OutP->RetCode != KERN_SUCCESS)
  289.         return OutP->RetCode;
  290.  
  291. #if    TypeCheck
  292. #if    UseStaticMsgType
  293.     if (* (int *) &OutP->passwordType != * (int *) &passwordCheck)
  294. #else    UseStaticMsgType
  295.     if ((OutP->passwordType.msg_type_inline != TRUE) ||
  296.         (OutP->passwordType.msg_type_longform != FALSE) ||
  297.         (OutP->passwordType.msg_type_name != MSG_TYPE_STRING_C) ||
  298.         (OutP->passwordType.msg_type_number != 1) ||
  299.         (OutP->passwordType.msg_type_size != 200))
  300. #endif    UseStaticMsgType
  301.         return MIG_TYPE_ERROR;
  302. #endif    TypeCheck
  303.  
  304.     (void) mig_strncpy(password /* password */, /* password */ OutP->password, 25);
  305.     password /* password */[24] = '\0';
  306.  
  307.     return OutP->RetCode;
  308. }
  309.