3 * Copyright 1989 by the Massachusetts Institute of Technology.
5 * For copying and distribution information, please see the file
8 * Simple UDP-based server application. For demonstration.
9 * This program performs no useful function.
14 RCSID("$Id: simple_server.c,v 1.11 1999/11/13 06:29:24 assar Exp $");
19 fprintf (stderr, "Usage: %s [-p port] [-s service] [-t srvtab]\n",
25 main(int argc, char **argv)
27 char service[SNAME_SZ];
28 char instance[INST_SZ];
30 char srvtab[MaxPathLen];
31 struct sockaddr_in addr, otheraddr;
40 des_key_schedule sched;
42 set_progname (argv[0]);
43 strlcpy (service, SAMPLE_SERVICE, sizeof(service));
44 strlcpy (instance, "*", sizeof(instance));
48 while ((c = getopt (argc, argv, "p:s:t:")) != -1)
53 sp = getservbyname (optarg, "udp");
57 port = htons(atoi(optarg));
61 strlcpy (service, optarg, sizeof(service));
64 strlcpy (srvtab, optarg, sizeof(srvtab));
72 port = k_getportbyname (SAMPLE_SERVICE, "udp", htons(SAMPLE_PORT));
74 memset (&addr, 0, sizeof(addr));
75 addr.sin_family = AF_INET;
78 sock = socket (AF_INET, SOCK_DGRAM, 0);
82 if (bind (sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
85 /* GET KRB_MK_REQ MESSAGE */
87 i = read(sock, ktxt->dat, MAX_KTXT_LEN);
91 printf("Received %d bytes\n", i);
94 /* Check authentication info */
95 i = krb_rd_req(ktxt, service, instance, 0, &ad, "");
97 errx (1, "krb_rd_req: %s", krb_get_err_text(i));
98 printf("Got authentication info from %s%s%s@%s\n", ad.pname,
99 *ad.pinst ? "." : "", ad.pinst, ad.prealm);
101 /* GET KRB_MK_SAFE MESSAGE */
103 /* use "recvfrom" so we know client's address */
104 len = sizeof(otheraddr);
105 i = recvfrom(sock, ktxt->dat, MAX_KTXT_LEN, 0,
106 (struct sockaddr *)&otheraddr, &len);
109 printf("Received %d bytes\n", i);
111 /* Verify the checksummed message */
112 i = krb_rd_safe(ktxt->dat, i, &ad.session, &otheraddr,
115 errx (1, "krb_rd_safe: %s", krb_get_err_text(i));
116 printf("Safe message is: %s\n", msg_data.app_data);
118 /* NOW GET ENCRYPTED MESSAGE */
121 memset(sched, 0, sizeof(sched));
123 /* need key schedule for session key */
124 des_key_sched(&ad.session, sched);
127 /* use "recvfrom" so we know client's address */
128 len = sizeof(otheraddr);
129 i = recvfrom(sock, ktxt->dat, MAX_KTXT_LEN, 0,
130 (struct sockaddr *)&otheraddr, &len);
133 printf("Received %d bytes\n", i);
134 i = krb_rd_priv(ktxt->dat, i, sched, &ad.session, &otheraddr,
137 errx (1, "krb_rd_priv: %s", krb_get_err_text(i));
138 printf("Decrypted message is: %s\n", msg_data.app_data);