1 /* 2 * Routines for controlled update/initialization of request structures. 3 * 4 * request_init() initializes its argument. Pointers and string-valued members 5 * are initialized to zero, to indicate that no lookup has been attempted. 6 * 7 * request_set() adds information to an already initialized request structure. 8 * 9 * Both functions take a variable-length name-value list. 10 * 11 * Diagnostics are reported through syslog(3). 12 * 13 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands. 14 * 15 * $FreeBSD: src/contrib/tcp_wrappers/update.c,v 1.2 2000/02/03 10:27:00 shin Exp $ 16 */ 17 18 /* System libraries */ 19 20 #include <stdarg.h> 21 #include <stdio.h> 22 #include <syslog.h> 23 #include <string.h> 24 #include <unistd.h> 25 26 /* Local stuff. */ 27 28 #include "tcpd.h" 29 30 /* request_fill - request update engine */ 31 32 static struct request_info *request_fill(request, ap) 33 struct request_info *request; 34 va_list ap; 35 { 36 int key; 37 char *ptr; 38 39 while ((key = va_arg(ap, int)) > 0) { 40 switch (key) { 41 default: 42 tcpd_warn("request_fill: invalid key: %d", key); 43 return (request); 44 case RQ_FILE: 45 request->fd = va_arg(ap, int); 46 continue; 47 case RQ_CLIENT_SIN: 48 #ifdef INET6 49 request->client->sin = va_arg(ap, struct sockaddr *); 50 #else 51 request->client->sin = va_arg(ap, struct sockaddr_in *); 52 #endif 53 continue; 54 case RQ_SERVER_SIN: 55 #ifdef INET6 56 request->server->sin = va_arg(ap, struct sockaddr *); 57 #else 58 request->server->sin = va_arg(ap, struct sockaddr_in *); 59 #endif 60 continue; 61 62 /* 63 * All other fields are strings with the same maximal length. 64 */ 65 66 case RQ_DAEMON: 67 ptr = request->daemon; 68 break; 69 case RQ_USER: 70 ptr = request->user; 71 break; 72 case RQ_CLIENT_NAME: 73 ptr = request->client->name; 74 break; 75 case RQ_CLIENT_ADDR: 76 ptr = request->client->addr; 77 break; 78 case RQ_SERVER_NAME: 79 ptr = request->server->name; 80 break; 81 case RQ_SERVER_ADDR: 82 ptr = request->server->addr; 83 break; 84 } 85 STRN_CPY(ptr, va_arg(ap, char *), STRING_LENGTH); 86 } 87 return (request); 88 } 89 90 /* request_init - initialize request structure */ 91 92 struct request_info *request_init(struct request_info *request, ...) 93 { 94 static struct request_info default_info; 95 struct request_info *r; 96 va_list ap; 97 98 /* 99 * Initialize data members. We do not assign default function pointer 100 * members, to avoid pulling in the whole socket module when it is not 101 * really needed. 102 */ 103 va_start(ap, request); 104 *request = default_info; 105 request->fd = -1; 106 strcpy(request->daemon, unknown); 107 sprintf(request->pid, "%d", getpid()); 108 request->client->request = request; 109 request->server->request = request; 110 r = request_fill(request, ap); 111 va_end(ap); 112 return (r); 113 } 114 115 /* request_set - update request structure */ 116 117 struct request_info *request_set(struct request_info *request, ...) 118 { 119 struct request_info *r; 120 va_list ap; 121 122 va_start(ap, request); 123 r = request_fill(request, ap); 124 va_end(ap); 125 return (r); 126 } 127