/* * TODO: It's not an orthogonal design: If I modify some part of the code to * send a protocol message, the response must be handled here. */ void listener_task(int listen_port) { int listenfd; struct sockaddr_in peeraddr; socklen_t peeraddr_len; listenfd = bind_listener(listen_port); peeraddr_len = sizeof(peeraddr); while (1) { char *message = recv_message(listenfd, (SA *) &peeraddr, &peeraddr_len); message += 4; /* Skip length */ if (strncmp(message, AUTH_CAN, strlen(AUTH_CAN)) == 0) { printf("Received AUTH_CAN\n"); auth_accept(listenfd, (SA *) &peeraddr, peeraddr_len); printf("Sent auth accept\n"); } else { printf("%s\n", message); } message -= 4; /* Unskip length */ free(message); } }
int main(int argc, char *argv[]) { struct link *link, *master; char *subject = 0, *type = 0; time_t stoptime; char line[1024]; signed char c; int portnum = 30000; char *hostname = 0; int timeout = 30; debug_config(argv[0]); static struct option long_options[] = { {"auth", required_argument, 0, 'a'}, {"port", required_argument, 0, 'p'}, {"host", required_argument, 0, 'r'}, {"help", required_argument, 0, 'h'}, {"debug-file", required_argument, 0, 'o'}, {"debug-rotate-max", required_argument, 0, 'O'}, {"debug", required_argument, 0, 'd'}, {0,0,0,0} }; while((c = getopt_long(argc, argv, "a:p:r:d:o:O:", long_options, NULL)) > -1) { switch (c) { case 'p': portnum = atoi(optarg); break; case 'h': show_help(argv[0]); exit(0); break; case 'r': hostname = optarg; break; case 'd': debug_flags_set(optarg); break; case 'o': debug_config_file(optarg); break; case 'O': debug_config_file_size(string_metric_parse(optarg)); break; case 'a': if(!auth_register_byname(optarg)) fatal("couldn't register %s authentication", optarg); break; default: show_use(argv[0]); exit(1); break; } } if(hostname) { char addr[LINK_ADDRESS_MAX]; stoptime = time(0) + timeout; if(!domain_name_cache_lookup(hostname, addr)) fatal("unknown host name: %s", hostname); link = link_connect(addr, portnum, stoptime); if(!link) fatal("couldn't connect to %s:%d: %s", hostname, portnum, strerror(errno)); if(auth_assert(link, &type, &subject, stoptime)) { printf("server thinks I am %s %s\n", type, subject); if(link_readline(link, line, sizeof(line), stoptime)) { printf("got message: %s\n", line); } else { printf("lost connection!\n"); } } else { printf("unable to authenticate.\n"); } link_close(link); } else { stoptime = time(0) + timeout; master = link_serve(portnum); if(!master) fatal("couldn't serve port %d: %s\n", portnum, strerror(errno)); while(time(0) < stoptime) { link = link_accept(master, stoptime); if(!link) continue; if(auth_accept(link, &type, &subject, stoptime)) { time_t t = time(0); link_putfstring(link, "Hello %s:%s, it is now %s", stoptime, type, subject, ctime(&t)); /* ctime ends with newline */ } else { printf("couldn't auth accept\n"); } link_close(link); } } return 0; }