1 /*
2 * XTI wrapper functions.
3 */
4
5 #include "unpxti.h"
6
7 #include <stdarg.h> /* ANSI C header file */
8 #include <syslog.h> /* for syslog() */
9
10 extern int daemon_proc; /* set nonzero by daemon_init() */
11
12 /*
13 * First the err_xti() function. We don't want it in ../lib/error.c,
14 * as some environments may not DefinE t_errno.
15 */
16
17 static void err_xti_doit(int, int, const char *, va_list);
18
19 void
err_xti(const char * fmt,...)20 err_xti(const char *fmt, ...)
21 {
22 va_list ap;
23
24 va_start(ap, fmt);
25 err_xti_doit(1, LOG_ERR, fmt, ap);
26 va_end(ap);
27 exit(1);
28 }
29
30 void
err_xti_ret(const char * fmt,...)31 err_xti_ret(const char *fmt, ...)
32 {
33 va_list ap;
34
35 va_start(ap, fmt);
36 err_xti_doit(1, LOG_INFO, fmt, ap);
37 va_end(ap);
38 return;
39 }
40
41 static void
err_xti_doit(int errnoflag,int level,const char * fmt,va_list ap)42 err_xti_doit(int errnoflag, int level, const char *fmt, va_list ap)
43 {
44 int errno_save, n;
45 char buf[MAXLINE];
46
47 errno_save = errno; /* value caller might want printed */
48 #ifdef HAVE_VSNPRINTF
49 vsnprintf(buf, sizeof(buf), fmt, ap);
50 #else
51 vsprintf(buf, fmt, ap);
52 #endif
53 n = strlen(buf);
54 if (errnoflag) {
55 snprintf(buf+n, sizeof(buf)-n, ": %s", t_strerror(t_errno));
56 n = strlen(buf);
57 if (t_errno == TSYSERR)
58 snprintf(buf+n, sizeof(buf)-n, ": %s", strerror(errno_save));
59 }
60 strcat(buf, "\n");
61
62 if (daemon_proc) {
63 syslog(level, buf);
64 } else {
65 fflush(stdout); /* in case stdout and stderr are the same */
66 fputs(buf, stderr);
67 fflush(stderr);
68 }
69 return;
70 }
71
72 int
Getmsg(int fd,struct strbuf * ctlp,struct strbuf * datap,int * flagsp)73 Getmsg(int fd, struct strbuf *ctlp, struct strbuf *datap, int *flagsp)
74 {
75 int rc;
76
77 if ( (rc = getmsg(fd, ctlp, datap, flagsp)) == -1)
78 err_sys("getmsg error");
79 return(rc); /* can be 0, MORECTL and/or MOREDATA */
80 }
81
82 void
Putmsg(int fd,const struct strbuf * ctlp,const struct strbuf * datap,int flags)83 Putmsg(int fd, const struct strbuf *ctlp, const struct strbuf *datap, int flags)
84 {
85 if (putmsg(fd, ctlp, datap, flags) == -1)
86 err_sys("putmsg error");
87 }
88
89 #ifdef HAVE_NETCONFIG_H
90 void *
Setnetconfig(void)91 Setnetconfig(void)
92 {
93 void *handle;
94
95 if ( (handle = setnetconfig()) == NULL)
96 err_quit("setnetconfig error");
97 return(handle);
98 }
99 #endif
100
101 #ifdef HAVE_NETCONFIG_H
102 void *
Setnetpath(void)103 Setnetpath(void)
104 {
105 void *handle;
106
107 if ( (handle = setnetpath()) == NULL)
108 err_quit("setnetpath error");
109 return(handle);
110 }
111 #endif
112
113 int
T_accept(int fd,int resfd,struct t_call * call)114 T_accept(int fd, int resfd, struct t_call *call)
115 {
116 int n;
117
118 if ( (n = t_accept(fd, resfd, call)) == -1)
119 err_xti("t_accept error");
120
121 return(n);
122 }
123
124 void *
T_alloc(int fd,int structtype,int fields)125 T_alloc(int fd, int structtype, int fields)
126 {
127 void *ptr;
128
129 if ( (ptr = t_alloc(fd, structtype, fields)) == NULL)
130 err_xti("t_alloc error");
131 return(ptr);
132 }
133
134 void
T_bind(int fd,const struct t_bind * req,struct t_bind * ret)135 T_bind(int fd, const struct t_bind *req, struct t_bind *ret)
136 {
137 if (t_bind(fd, req, ret) == -1)
138 err_xti("t_bind error");
139 }
140
141 void
T_close(int fd)142 T_close(int fd)
143 {
144 if (t_close(fd) == -1)
145 err_xti("t_close error");
146 }
147
148 void
T_connect(int fd,const struct t_call * sndcall,struct t_call * rcvcall)149 T_connect(int fd, const struct t_call *sndcall, struct t_call *rcvcall)
150 {
151 if (t_connect(fd, sndcall, rcvcall) == -1)
152 err_xti("t_connect error");
153 }
154
155 void
T_free(void * ptr,int structtype)156 T_free(void *ptr, int structtype)
157 {
158 if (t_free(ptr, structtype) == -1)
159 err_xti("t_free error");
160 }
161
162 void
T_getprotaddr(int fd,struct t_bind * bound,struct t_bind * peer)163 T_getprotaddr(int fd, struct t_bind *bound, struct t_bind *peer)
164 {
165 if (t_getprotaddr(fd, bound, peer) == -1)
166 err_xti("t_getprotaddr error");
167 }
168
169 int
T_getstate(int fd)170 T_getstate(int fd)
171 {
172 int n;
173
174 if ( (n = t_getstate(fd)) == -1)
175 err_xti("t_getstate error");
176
177 return(n);
178 }
179
180 void
T_listen(int fd,struct t_call * call)181 T_listen(int fd, struct t_call *call)
182 {
183 if (t_listen(fd, call) == -1)
184 err_xti("t_listen error");
185 }
186
187 int
T_look(int fd)188 T_look(int fd)
189 {
190 int n;
191
192 if ( (n = t_look(fd)) == -1)
193 err_xti("t_look error");
194
195 return(n);
196 }
197
198 int
T_open(const char * name,int oflag,struct t_info * info)199 T_open(const char *name, int oflag, struct t_info *info)
200 {
201 int n;
202
203 if ( (n = t_open(name, oflag, info)) == -1)
204 err_xti("t_open error for %s", name ? name : "(null ptr passed)");
205
206 return(n);
207 }
208
209 void
T_optmgmt(int fd,const struct t_optmgmt * req,struct t_optmgmt * ret)210 T_optmgmt(int fd, const struct t_optmgmt *req, struct t_optmgmt *ret)
211 {
212 if (t_optmgmt(fd, req, ret) == -1)
213 err_xti("t_optmgmt error");
214 }
215
216 int
T_rcv(int fd,void * buf,unsigned int nbytes,int * flags)217 T_rcv(int fd, void *buf, unsigned int nbytes, int *flags)
218 {
219 int n;
220
221 if ( (n = t_rcv(fd, buf, nbytes, flags)) == -1)
222 err_xti("t_rcv error");
223
224 return(n);
225 }
226
227 void
T_rcvdis(int fd,struct t_discon * discon)228 T_rcvdis(int fd, struct t_discon *discon)
229 {
230 if (t_rcvdis(fd, discon) == -1)
231 err_xti("t_rcvdis error");
232 }
233
234 void
T_rcvrel(int fd)235 T_rcvrel(int fd)
236 {
237 if (t_rcvrel(fd) == -1)
238 err_xti("t_rcvrel error");
239 }
240
241 void
T_rcvudata(int fd,struct t_unitdata * ud,int * flags)242 T_rcvudata(int fd, struct t_unitdata *ud, int *flags)
243 {
244 if (t_rcvudata(fd, ud, flags) == -1)
245 err_xti("t_rcvudata error");
246 }
247
248 void
T_rcvuderr(int fd,struct t_uderr * ud)249 T_rcvuderr(int fd, struct t_uderr *ud)
250 {
251 if (t_rcvuderr(fd, ud) == -1)
252 err_xti("t_rcvudata error");
253 }
254
255 void
T_snd(int fd,void * buf,unsigned int nbytes,int flags)256 T_snd(int fd, void *buf, unsigned int nbytes, int flags)
257 {
258 int n;
259
260 if ( (n = t_snd(fd, buf, nbytes, flags)) != nbytes)
261 err_xti("t_snd error");
262 }
263
264 void
T_sndrel(int fd)265 T_sndrel(int fd)
266 {
267 if (t_sndrel(fd) == -1)
268 err_xti("t_sndrel error");
269 }
270
271 void
T_sndudata(int fd,struct t_unitdata * ud)272 T_sndudata(int fd, struct t_unitdata *ud)
273 {
274 if (t_sndudata(fd, ud) == -1)
275 err_xti("t_sndudata error");
276 }
277
278 #ifdef HAVE_NETCONFIG_H
279 char *
Taddr2uaddr(struct netconfig * ncp,struct netbuf * np)280 Taddr2uaddr(struct netconfig *ncp, struct netbuf *np)
281 {
282 char *ptr;
283
284 if ( (ptr = taddr2uaddr(ncp, np)) == NULL)
285 return("(unknown host)");
286 return(ptr);
287 }
288 #endif
289
290 #ifdef HAVE_NETDIR_H
291 int
Xti_accept(int listenfd,struct netbuf * np,int rdwr)292 Xti_accept(int listenfd, struct netbuf *np, int rdwr)
293 {
294 int connfd;
295
296 if ( (connfd = xti_accept(listenfd, np, rdwr)) == -1)
297 err_xti("xti_accept error");
298 return(connfd);
299 }
300 #endif
301
302 void
Xti_getopt(int fd,int level,int name,void * optval,socklen_t * optlenp)303 Xti_getopt(int fd, int level, int name, void *optval, socklen_t *optlenp)
304 {
305 if (xti_getopt(fd, level, name, optval, optlenp) == -1)
306 err_xti("xti_getopt error");
307 }
308
309 char *
Xti_ntop(const struct netbuf * np)310 Xti_ntop(const struct netbuf *np)
311 {
312 char *ptr;
313
314 if ( (ptr = xti_ntop(np)) == NULL)
315 err_quit("xti_ntop error");
316 return(ptr);
317 }
318
319 char *
Xti_ntop_host(const struct netbuf * np)320 Xti_ntop_host(const struct netbuf *np)
321 {
322 char *ptr;
323
324 if ( (ptr = xti_ntop_host(np)) == NULL)
325 err_quit("xti_ntop_host error");
326 return(ptr);
327 }
328
329 void
Xti_rdwr(int fd)330 Xti_rdwr(int fd)
331 {
332 if (xti_rdwr(fd) == -1)
333 err_sys("xti_rdwr error");
334 }
335
336 void
Xti_setopt(int fd,int level,int name,void * optval,socklen_t optlen)337 Xti_setopt(int fd, int level, int name, void *optval, socklen_t optlen)
338 {
339 if (xti_setopt(fd, level, name, optval, optlen) == -1)
340 err_xti("xti_setopt error");
341 }
342