]> granicus.if.org Git - strace/blob - net.c
Use printnum_int64 instead of print_loff_t
[strace] / net.c
1 /*
2  * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3  * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5  * Copyright (c) 1996-2000 Wichert Akkerman <wichert@cistron.nl>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "defs.h"
32 #include <sys/stat.h>
33 #include <sys/socket.h>
34 #include <sys/uio.h>
35 #include <sys/un.h>
36 #include <netinet/in.h>
37 #ifdef HAVE_NETINET_TCP_H
38 # include <netinet/tcp.h>
39 #endif
40 #ifdef HAVE_NETINET_UDP_H
41 # include <netinet/udp.h>
42 #endif
43 #ifdef HAVE_NETINET_SCTP_H
44 # include <netinet/sctp.h>
45 #endif
46 #include <arpa/inet.h>
47 #include <net/if.h>
48 #include <asm/types.h>
49 #if defined(__GLIBC__)
50 # include <netipx/ipx.h>
51 #else
52 # include <linux/ipx.h>
53 #endif
54
55 #if defined(HAVE_LINUX_NETLINK_H)
56 # include <linux/netlink.h>
57 #endif
58 #if defined(HAVE_LINUX_IF_PACKET_H)
59 # include <linux/if_packet.h>
60 #endif
61 #if defined(HAVE_LINUX_ICMP_H)
62 # include <linux/icmp.h>
63 #endif
64 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
65 # include <bluetooth/bluetooth.h>
66 # include <bluetooth/hci.h>
67 # include <bluetooth/l2cap.h>
68 # include <bluetooth/rfcomm.h>
69 # include <bluetooth/sco.h>
70 #endif
71 #ifndef PF_UNSPEC
72 # define PF_UNSPEC AF_UNSPEC
73 #endif
74
75 #include "xlat/domains.h"
76 #include "xlat/addrfams.h"
77 #include "xlat/socktypes.h"
78 #include "xlat/sock_type_flags.h"
79 #ifndef SOCK_TYPE_MASK
80 # define SOCK_TYPE_MASK 0xf
81 #endif
82
83 #include "xlat/socketlayers.h"
84
85 #include "xlat/inet_protocols.h"
86
87 #ifdef PF_NETLINK
88 # if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
89 #  define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
90 # endif
91 # include "xlat/netlink_protocols.h"
92 #endif
93
94 #if defined(HAVE_BLUETOOTH_BLUETOOTH_H)
95 # include "xlat/bt_protocols.h"
96 #endif
97
98 #include "xlat/msg_flags.h"
99
100 #if defined(AF_PACKET) /* from e.g. linux/if_packet.h */
101 # include "xlat/af_packet_types.h"
102 #endif
103
104 static void
105 print_ifindex(unsigned int ifindex)
106 {
107 #ifdef HAVE_IF_INDEXTONAME
108         char buf[IFNAMSIZ + 1];
109
110         if (if_indextoname(ifindex, buf)) {
111                 tprints("if_nametoindex(");
112                 print_quoted_string(buf, sizeof(buf), QUOTE_0_TERMINATED);
113                 tprints(")");
114                 return;
115         }
116 #endif
117         tprintf("%u", ifindex);
118 }
119
120 void
121 printsock(struct tcb *tcp, long addr, int addrlen)
122 {
123         union {
124                 char pad[128];
125                 struct sockaddr sa;
126                 struct sockaddr_in sin;
127                 struct sockaddr_un sau;
128 #ifdef HAVE_INET_NTOP
129                 struct sockaddr_in6 sa6;
130 #endif
131 #if defined(AF_IPX)
132                 struct sockaddr_ipx sipx;
133 #endif
134 #ifdef AF_PACKET
135                 struct sockaddr_ll ll;
136 #endif
137 #ifdef AF_NETLINK
138                 struct sockaddr_nl nl;
139 #endif
140 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
141                 struct sockaddr_hci hci;
142                 struct sockaddr_l2 l2;
143                 struct sockaddr_rc rc;
144                 struct sockaddr_sco sco;
145 #endif
146         } addrbuf;
147         char string_addr[100];
148
149         if (addrlen < 2 || addrlen > (int) sizeof(addrbuf))
150                 addrlen = sizeof(addrbuf);
151
152         memset(&addrbuf, 0, sizeof(addrbuf));
153         if (umoven_or_printaddr(tcp, addr, addrlen, addrbuf.pad))
154                 return;
155         addrbuf.pad[sizeof(addrbuf.pad) - 1] = '\0';
156
157         tprints("{sa_family=");
158         printxval(addrfams, addrbuf.sa.sa_family, "AF_???");
159         tprints(", ");
160
161         switch (addrbuf.sa.sa_family) {
162         case AF_UNIX:
163                 if (addrlen == 2) {
164                         tprints("NULL");
165                 } else if (addrbuf.sau.sun_path[0]) {
166                         tprints("sun_path=");
167                         print_quoted_string(addrbuf.sau.sun_path,
168                                             sizeof(addrbuf.sau.sun_path) + 1,
169                                             QUOTE_0_TERMINATED);
170                 } else {
171                         tprints("sun_path=@");
172                         print_quoted_string(addrbuf.sau.sun_path + 1,
173                                             sizeof(addrbuf.sau.sun_path),
174                                             QUOTE_0_TERMINATED);
175                 }
176                 break;
177         case AF_INET:
178                 tprintf("sin_port=htons(%u), sin_addr=inet_addr(\"%s\")",
179                         ntohs(addrbuf.sin.sin_port), inet_ntoa(addrbuf.sin.sin_addr));
180                 break;
181 #ifdef HAVE_INET_NTOP
182         case AF_INET6:
183                 inet_ntop(AF_INET6, &addrbuf.sa6.sin6_addr, string_addr, sizeof(string_addr));
184                 tprintf("sin6_port=htons(%u), inet_pton(AF_INET6, \"%s\", &sin6_addr), sin6_flowinfo=%u",
185                                 ntohs(addrbuf.sa6.sin6_port), string_addr,
186                                 addrbuf.sa6.sin6_flowinfo);
187 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
188                 tprints(", sin6_scope_id=");
189 #if defined IN6_IS_ADDR_LINKLOCAL && defined IN6_IS_ADDR_MC_LINKLOCAL
190                 if (IN6_IS_ADDR_LINKLOCAL(&addrbuf.sa6.sin6_addr)
191                     || IN6_IS_ADDR_MC_LINKLOCAL(&addrbuf.sa6.sin6_addr))
192                         print_ifindex(addrbuf.sa6.sin6_scope_id);
193                 else
194 #endif
195                         tprintf("%u", addrbuf.sa6.sin6_scope_id);
196 #endif /* HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID */
197                 break;
198 #endif
199 #if defined(AF_IPX)
200         case AF_IPX:
201                 {
202                         int i;
203                         tprintf("sipx_port=htons(%u), ",
204                                         ntohs(addrbuf.sipx.sipx_port));
205                         /* Yes, I know, this does not look too
206                          * strace-ish, but otherwise the IPX
207                          * addresses just look monstrous...
208                          * Anyways, feel free if you don't like
209                          * this way.. :)
210                          */
211                         tprintf("%08lx:", (unsigned long)ntohl(addrbuf.sipx.sipx_network));
212                         for (i = 0; i < IPX_NODE_LEN; i++)
213                                 tprintf("%02x", addrbuf.sipx.sipx_node[i]);
214                         tprintf("/[%02x]", addrbuf.sipx.sipx_type);
215                 }
216                 break;
217 #endif /* AF_IPX */
218 #ifdef AF_PACKET
219         case AF_PACKET:
220                 {
221                         int i;
222                         tprintf("proto=%#04x, if%d, pkttype=",
223                                         ntohs(addrbuf.ll.sll_protocol),
224                                         addrbuf.ll.sll_ifindex);
225                         printxval(af_packet_types, addrbuf.ll.sll_pkttype, "PACKET_???");
226                         tprintf(", addr(%d)={%d, ",
227                                         addrbuf.ll.sll_halen,
228                                         addrbuf.ll.sll_hatype);
229                         for (i = 0; i < addrbuf.ll.sll_halen; i++)
230                                 tprintf("%02x", addrbuf.ll.sll_addr[i]);
231                 }
232                 break;
233
234 #endif /* AF_PACKET */
235 #ifdef AF_NETLINK
236         case AF_NETLINK:
237                 tprintf("pid=%d, groups=%08x", addrbuf.nl.nl_pid, addrbuf.nl.nl_groups);
238                 break;
239 #endif /* AF_NETLINK */
240 #if defined(AF_BLUETOOTH) && defined(HAVE_BLUETOOTH_BLUETOOTH_H)
241         case AF_BLUETOOTH:
242                 tprintf("{sco_bdaddr=%02X:%02X:%02X:%02X:%02X:%02X} or "
243                         "{rc_bdaddr=%02X:%02X:%02X:%02X:%02X:%02X, rc_channel=%d} or "
244                         "{l2_psm=htobs(%d), l2_bdaddr=%02X:%02X:%02X:%02X:%02X:%02X, l2_cid=htobs(%d)} or "
245                         "{hci_dev=htobs(%d)}",
246                         addrbuf.sco.sco_bdaddr.b[0], addrbuf.sco.sco_bdaddr.b[1],
247                         addrbuf.sco.sco_bdaddr.b[2], addrbuf.sco.sco_bdaddr.b[3],
248                         addrbuf.sco.sco_bdaddr.b[4], addrbuf.sco.sco_bdaddr.b[5],
249                         addrbuf.rc.rc_bdaddr.b[0], addrbuf.rc.rc_bdaddr.b[1],
250                         addrbuf.rc.rc_bdaddr.b[2], addrbuf.rc.rc_bdaddr.b[3],
251                         addrbuf.rc.rc_bdaddr.b[4], addrbuf.rc.rc_bdaddr.b[5],
252                         addrbuf.rc.rc_channel,
253                         btohs(addrbuf.l2.l2_psm), addrbuf.l2.l2_bdaddr.b[0],
254                         addrbuf.l2.l2_bdaddr.b[1], addrbuf.l2.l2_bdaddr.b[2],
255                         addrbuf.l2.l2_bdaddr.b[3], addrbuf.l2.l2_bdaddr.b[4],
256                         addrbuf.l2.l2_bdaddr.b[5], btohs(addrbuf.l2.l2_cid),
257                         btohs(addrbuf.hci.hci_dev));
258                 break;
259 #endif /* AF_BLUETOOTH && HAVE_BLUETOOTH_BLUETOOTH_H */
260         /* AF_AX25 AF_APPLETALK AF_NETROM AF_BRIDGE AF_AAL5
261         AF_X25 AF_ROSE etc. still need to be done */
262
263         default:
264                 tprints("sa_data=");
265                 print_quoted_string(addrbuf.sa.sa_data,
266                                     sizeof(addrbuf.sa.sa_data), 0);
267                 break;
268         }
269         tprints("}");
270 }
271
272 #ifdef HAVE_SENDMSG
273 # ifndef SCM_SECURITY
274 #  define SCM_SECURITY 0x03
275 # endif
276 # include "xlat/scmvals.h"
277
278 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
279 struct cmsghdr32 {
280         uint32_t cmsg_len;
281         int cmsg_level;
282         int cmsg_type;
283 };
284 #endif
285
286 typedef union {
287         char *ptr;
288         struct cmsghdr *cmsg;
289 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
290         struct cmsghdr32 *cmsg32;
291 #endif
292 } union_cmsghdr;
293
294 static bool
295 print_scm_rights(struct tcb *tcp, size_t cmsg_size, char *ptr, size_t cmsg_len)
296 {
297         if (cmsg_size + sizeof(int) > cmsg_len)
298                 return false;
299
300         int *fds = (int *) (ptr + cmsg_size);
301         bool seen = false;
302
303         tprints(", [");
304         while ((char *) fds < (ptr + cmsg_len)) {
305                 if (seen)
306                         tprints(", ");
307                 else
308                         seen = true;
309                 printfd(tcp, *fds++);
310         }
311         tprints("]}");
312         return true;
313 }
314
315 static bool
316 print_scm_creds(struct tcb *tcp, size_t cmsg_size, char *ptr, size_t cmsg_len)
317 {
318         if (cmsg_size + sizeof(struct ucred) > cmsg_len)
319                 return false;
320
321         const struct ucred *uc = (void *) (ptr + cmsg_size);
322
323         tprintf(", {pid=%u, uid=%u, gid=%u}}",
324                 (unsigned) uc->pid, (unsigned) uc->uid, (unsigned) uc->gid);
325         return true;
326 }
327
328 static bool
329 print_scm_security(struct tcb *tcp, size_t cmsg_size, char *ptr, size_t cmsg_len)
330 {
331         if (cmsg_size + sizeof(char) > cmsg_len)
332                 return false;
333
334         const char *label = (const char *) (ptr + cmsg_size);
335         const size_t label_len = cmsg_len - cmsg_size;
336
337         tprints(", ");
338         print_quoted_string(label, label_len, 0);
339         tprints("}");
340
341         return true;
342 }
343
344 static void
345 printcmsghdr(struct tcb *tcp, unsigned long addr, size_t len)
346 {
347         const size_t cmsg_size =
348 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
349                 (current_wordsize < sizeof(long)) ? sizeof(struct cmsghdr32) :
350 #endif
351                         sizeof(struct cmsghdr);
352
353         char *buf = len < cmsg_size ? NULL : malloc(len);
354         if (!buf || umoven(tcp, addr, len, buf) < 0) {
355                 tprints(", msg_control=");
356                 printaddr(addr);
357                 free(buf);
358                 return;
359         }
360
361         union_cmsghdr u = { .ptr = buf };
362
363         tprints(", [");
364         while (len >= cmsg_size) {
365                 size_t cmsg_len =
366 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
367                         (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_len :
368 #endif
369                                 u.cmsg->cmsg_len;
370                 int cmsg_level =
371 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
372                         (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_level :
373 #endif
374                                 u.cmsg->cmsg_level;
375                 int cmsg_type =
376 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
377                         (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_type :
378 #endif
379                                 u.cmsg->cmsg_type;
380
381                 if (u.ptr != buf)
382                         tprints(", ");
383                 tprintf("{cmsg_len=%lu, cmsg_level=", (unsigned long) cmsg_len);
384                 printxval(socketlayers, cmsg_level, "SOL_???");
385                 tprints(", cmsg_type=");
386
387                 if (cmsg_len > len)
388                         cmsg_len = len;
389
390                 if (cmsg_level == SOL_SOCKET) {
391                         printxval(scmvals, cmsg_type, "SCM_???");
392                         switch (cmsg_type) {
393                         case SCM_RIGHTS:
394                                 if (print_scm_rights(tcp, cmsg_size, u.ptr, cmsg_len))
395                                         goto next_cmsg;
396                                 break;
397                         case SCM_CREDENTIALS:
398                                 if (print_scm_creds(tcp, cmsg_size, u.ptr, cmsg_len))
399                                         goto next_cmsg;
400                                 break;
401                         case SCM_SECURITY:
402                                 if (print_scm_security(tcp, cmsg_size, u.ptr, cmsg_len))
403                                         goto next_cmsg;
404                                 break;
405                         }
406                 } else {
407                         tprintf("%u", cmsg_type);
408                 }
409                 tprints(", ...}");
410 next_cmsg:
411                 if (cmsg_len < cmsg_size) {
412                         len -= cmsg_size;
413                         break;
414                 }
415                 cmsg_len = (cmsg_len + current_wordsize - 1) &
416                         (size_t) ~(current_wordsize - 1);
417                 if (cmsg_len >= len) {
418                         len = 0;
419                         break;
420                 }
421                 u.ptr += cmsg_len;
422                 len -= cmsg_len;
423         }
424         if (len)
425                 tprints(", ...");
426         tprints("]");
427         free(buf);
428 }
429
430 static void
431 do_msghdr(struct tcb *tcp, struct msghdr *msg, unsigned long data_size)
432 {
433         tprintf("{msg_name(%d)=", msg->msg_namelen);
434         printsock(tcp, (long)msg->msg_name, msg->msg_namelen);
435
436         tprintf(", msg_iov(%lu)=", (unsigned long)msg->msg_iovlen);
437         tprint_iov_upto(tcp, (unsigned long)msg->msg_iovlen,
438                    (unsigned long)msg->msg_iov, 1, data_size);
439
440 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
441         tprintf(", msg_controllen=%lu", (unsigned long)msg->msg_controllen);
442         if (msg->msg_controllen)
443                 printcmsghdr(tcp, (unsigned long) msg->msg_control,
444                              msg->msg_controllen);
445         tprints(", msg_flags=");
446         printflags(msg_flags, msg->msg_flags, "MSG_???");
447 #else /* !HAVE_STRUCT_MSGHDR_MSG_CONTROL */
448         tprintf("msg_accrights=%#lx, msg_accrightslen=%u",
449                 (unsigned long) msg->msg_accrights, msg->msg_accrightslen);
450 #endif /* !HAVE_STRUCT_MSGHDR_MSG_CONTROL */
451         tprints("}");
452 }
453
454 struct msghdr32 {
455         uint32_t /* void* */    msg_name;
456         uint32_t /* socklen_t */msg_namelen;
457         uint32_t /* iovec* */   msg_iov;
458         uint32_t /* size_t */   msg_iovlen;
459         uint32_t /* void* */    msg_control;
460         uint32_t /* size_t */   msg_controllen;
461         uint32_t /* int */      msg_flags;
462 };
463 struct mmsghdr32 {
464         struct msghdr32         msg_hdr;
465         uint32_t /* unsigned */ msg_len;
466 };
467
468 #ifndef HAVE_STRUCT_MMSGHDR
469 struct mmsghdr {
470         struct msghdr msg_hdr;
471         unsigned msg_len;
472 };
473 #endif
474
475 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
476 static void
477 copy_from_msghdr32(struct msghdr *to_msg, struct msghdr32 *from_msg32)
478 {
479         to_msg->msg_name       = (void*)(long)from_msg32->msg_name;
480         to_msg->msg_namelen    =              from_msg32->msg_namelen;
481         to_msg->msg_iov        = (void*)(long)from_msg32->msg_iov;
482         to_msg->msg_iovlen     =              from_msg32->msg_iovlen;
483         to_msg->msg_control    = (void*)(long)from_msg32->msg_control;
484         to_msg->msg_controllen =              from_msg32->msg_controllen;
485         to_msg->msg_flags      =              from_msg32->msg_flags;
486 }
487 #endif
488
489 static bool
490 extractmsghdr(struct tcb *tcp, long addr, struct msghdr *msg)
491 {
492 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
493         if (current_wordsize == 4) {
494                 struct msghdr32 msg32;
495
496                 if (umove(tcp, addr, &msg32) < 0)
497                         return false;
498                 copy_from_msghdr32(msg, &msg32);
499         } else
500 #endif
501         if (umove(tcp, addr, msg) < 0)
502                 return false;
503         return true;
504 }
505
506 static bool
507 extractmmsghdr(struct tcb *tcp, long addr, unsigned int idx, struct mmsghdr *mmsg)
508 {
509 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
510         if (current_wordsize == 4) {
511                 struct mmsghdr32 mmsg32;
512
513                 addr += sizeof(struct mmsghdr32) * idx;
514                 if (umove(tcp, addr, &mmsg32) < 0)
515                         return false;
516
517                 copy_from_msghdr32(&mmsg->msg_hdr, &mmsg32.msg_hdr);
518                 mmsg->msg_len = mmsg32.msg_len;
519         } else
520 #endif
521         {
522                 addr += sizeof(*mmsg) * idx;
523                 if (umove(tcp, addr, mmsg) < 0)
524                         return false;
525         }
526         return true;
527 }
528
529 static void
530 printmsghdr(struct tcb *tcp, long addr, unsigned long data_size)
531 {
532         struct msghdr msg;
533
534         if (verbose(tcp) && extractmsghdr(tcp, addr, &msg))
535                 do_msghdr(tcp, &msg, data_size);
536         else
537                 printaddr(addr);
538 }
539
540 void
541 dumpiov_in_msghdr(struct tcb *tcp, long addr)
542 {
543         struct msghdr msg;
544
545         if (extractmsghdr(tcp, addr, &msg))
546                 dumpiov(tcp, msg.msg_iovlen, (long)msg.msg_iov);
547 }
548
549 static void
550 printmmsghdr(struct tcb *tcp, long addr, unsigned int idx, unsigned long msg_len)
551 {
552         struct mmsghdr mmsg;
553
554         if (extractmmsghdr(tcp, addr, idx, &mmsg)) {
555                 tprints("{");
556                 do_msghdr(tcp, &mmsg.msg_hdr, msg_len ? msg_len : mmsg.msg_len);
557                 tprintf(", %u}", mmsg.msg_len);
558         }
559         else
560                 printaddr(addr);
561 }
562
563 static void
564 decode_mmsg(struct tcb *tcp, unsigned long msg_len)
565 {
566         /* mmsgvec */
567         if (syserror(tcp)) {
568                 printaddr(tcp->u_arg[1]);
569         } else {
570                 unsigned int len = tcp->u_rval;
571                 unsigned int i;
572
573                 tprints("{");
574                 for (i = 0; i < len; ++i) {
575                         if (i)
576                                 tprints(", ");
577                         printmmsghdr(tcp, tcp->u_arg[1], i, msg_len);
578                 }
579                 tprints("}");
580         }
581         /* vlen */
582         tprintf(", %u, ", (unsigned int) tcp->u_arg[2]);
583         /* flags */
584         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
585 }
586
587 void
588 dumpiov_in_mmsghdr(struct tcb *tcp, long addr)
589 {
590         unsigned int len = tcp->u_rval;
591         unsigned int i;
592         struct mmsghdr mmsg;
593
594         for (i = 0; i < len; ++i) {
595                 if (extractmmsghdr(tcp, addr, i, &mmsg)) {
596                         tprintf(" = %lu buffers in vector %u\n",
597                                 (unsigned long)mmsg.msg_hdr.msg_iovlen, i);
598                         dumpiov(tcp, mmsg.msg_hdr.msg_iovlen,
599                                 (long)mmsg.msg_hdr.msg_iov);
600                 }
601         }
602 }
603 #endif /* HAVE_SENDMSG */
604
605 /*
606  * low bits of the socket type define real socket type,
607  * other bits are socket type flags.
608  */
609 static void
610 tprint_sock_type(int flags)
611 {
612         const char *str = xlookup(socktypes, flags & SOCK_TYPE_MASK);
613
614         if (str) {
615                 tprints(str);
616                 flags &= ~SOCK_TYPE_MASK;
617                 if (!flags)
618                         return;
619                 tprints("|");
620         }
621         printflags(sock_type_flags, flags, "SOCK_???");
622 }
623
624 SYS_FUNC(socket)
625 {
626         printxval(domains, tcp->u_arg[0], "PF_???");
627         tprints(", ");
628         tprint_sock_type(tcp->u_arg[1]);
629         tprints(", ");
630         switch (tcp->u_arg[0]) {
631         case PF_INET:
632 #ifdef PF_INET6
633         case PF_INET6:
634 #endif
635                 printxval(inet_protocols, tcp->u_arg[2], "IPPROTO_???");
636                 break;
637 #ifdef PF_IPX
638         case PF_IPX:
639                 /* BTW: I don't believe this.. */
640                 tprints("[");
641                 printxval(domains, tcp->u_arg[2], "PF_???");
642                 tprints("]");
643                 break;
644 #endif /* PF_IPX */
645 #ifdef PF_NETLINK
646         case PF_NETLINK:
647                 printxval(netlink_protocols, tcp->u_arg[2], "NETLINK_???");
648                 break;
649 #endif
650 #if defined(PF_BLUETOOTH) && defined(HAVE_BLUETOOTH_BLUETOOTH_H)
651         case PF_BLUETOOTH:
652                 printxval(bt_protocols, tcp->u_arg[2], "BTPROTO_???");
653                 break;
654 #endif
655         default:
656                 tprintf("%lu", tcp->u_arg[2]);
657                 break;
658         }
659
660         return RVAL_DECODED;
661 }
662
663 SYS_FUNC(bind)
664 {
665         printfd(tcp, tcp->u_arg[0]);
666         tprints(", ");
667         printsock(tcp, tcp->u_arg[1], tcp->u_arg[2]);
668         tprintf(", %lu", tcp->u_arg[2]);
669
670         return RVAL_DECODED;
671 }
672
673 SYS_FUNC(listen)
674 {
675         printfd(tcp, tcp->u_arg[0]);
676         tprints(", ");
677         tprintf("%lu", tcp->u_arg[1]);
678
679         return RVAL_DECODED;
680 }
681
682 static int
683 do_sockname(struct tcb *tcp, int flags_arg)
684 {
685         if (entering(tcp)) {
686                 printfd(tcp, tcp->u_arg[0]);
687                 tprints(", ");
688                 return 0;
689         }
690
691         int len;
692         if (!tcp->u_arg[2] || !verbose(tcp) || syserror(tcp) ||
693             umove(tcp, tcp->u_arg[2], &len) < 0) {
694                 printaddr(tcp->u_arg[1]);
695                 tprints(", ");
696                 printaddr(tcp->u_arg[2]);
697         } else {
698                 printsock(tcp, tcp->u_arg[1], len);
699                 tprintf(", [%d]", len);
700         }
701
702         if (flags_arg >= 0) {
703                 tprints(", ");
704                 printflags(sock_type_flags, tcp->u_arg[flags_arg],
705                            "SOCK_???");
706         }
707         return 0;
708 }
709
710 SYS_FUNC(accept)
711 {
712         do_sockname(tcp, -1);
713         return RVAL_FD;
714 }
715
716 SYS_FUNC(accept4)
717 {
718         do_sockname(tcp, 3);
719         return RVAL_FD;
720 }
721
722 SYS_FUNC(send)
723 {
724         printfd(tcp, tcp->u_arg[0]);
725         tprints(", ");
726         printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
727         tprintf(", %lu, ", tcp->u_arg[2]);
728         /* flags */
729         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
730
731         return RVAL_DECODED;
732 }
733
734 SYS_FUNC(sendto)
735 {
736         printfd(tcp, tcp->u_arg[0]);
737         tprints(", ");
738         printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
739         tprintf(", %lu, ", tcp->u_arg[2]);
740         /* flags */
741         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
742         /* to address */
743         tprints(", ");
744         printsock(tcp, tcp->u_arg[4], tcp->u_arg[5]);
745         /* to length */
746         tprintf(", %lu", tcp->u_arg[5]);
747
748         return RVAL_DECODED;
749 }
750
751 #ifdef HAVE_SENDMSG
752
753 SYS_FUNC(sendmsg)
754 {
755         printfd(tcp, tcp->u_arg[0]);
756         tprints(", ");
757         printmsghdr(tcp, tcp->u_arg[1], (unsigned long) -1L);
758         /* flags */
759         tprints(", ");
760         printflags(msg_flags, tcp->u_arg[2], "MSG_???");
761
762         return RVAL_DECODED;
763 }
764
765 SYS_FUNC(sendmmsg)
766 {
767         if (entering(tcp)) {
768                 /* sockfd */
769                 printfd(tcp, tcp->u_arg[0]);
770                 tprints(", ");
771                 if (!verbose(tcp)) {
772                         tprintf("%#lx, %u, ",
773                                 tcp->u_arg[1], (unsigned int) tcp->u_arg[2]);
774                         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
775                 }
776         } else {
777                 if (verbose(tcp))
778                         decode_mmsg(tcp, (unsigned long) -1L);
779         }
780         return 0;
781 }
782
783 #endif /* HAVE_SENDMSG */
784
785 SYS_FUNC(recv)
786 {
787         if (entering(tcp)) {
788                 printfd(tcp, tcp->u_arg[0]);
789                 tprints(", ");
790         } else {
791                 if (syserror(tcp))
792                         printaddr(tcp->u_arg[1]);
793                 else
794                         printstr(tcp, tcp->u_arg[1], tcp->u_rval);
795
796                 tprintf(", %lu, ", tcp->u_arg[2]);
797                 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
798         }
799         return 0;
800 }
801
802 SYS_FUNC(recvfrom)
803 {
804         int fromlen;
805
806         if (entering(tcp)) {
807                 printfd(tcp, tcp->u_arg[0]);
808                 tprints(", ");
809         } else {
810                 if (syserror(tcp)) {
811                         tprintf("%#lx, %lu, %lu, %#lx, %#lx",
812                                 tcp->u_arg[1], tcp->u_arg[2], tcp->u_arg[3],
813                                 tcp->u_arg[4], tcp->u_arg[5]);
814                         return 0;
815                 }
816                 /* buf */
817                 printstr(tcp, tcp->u_arg[1], tcp->u_rval);
818                 /* len */
819                 tprintf(", %lu, ", tcp->u_arg[2]);
820                 /* flags */
821                 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
822                 /* from address, len */
823                 tprints(", ");
824                 if (!tcp->u_arg[4] || !tcp->u_arg[5] ||
825                     umove(tcp, tcp->u_arg[5], &fromlen) < 0) {
826                         printaddr(tcp->u_arg[4]);
827                         tprints(", ");
828                         printaddr(tcp->u_arg[5]);
829                         return 0;
830                 }
831                 printsock(tcp, tcp->u_arg[4], tcp->u_arg[5]);
832                 /* from length */
833                 tprintf(", [%u]", fromlen);
834         }
835         return 0;
836 }
837
838 #ifdef HAVE_SENDMSG
839
840 SYS_FUNC(recvmsg)
841 {
842         if (entering(tcp)) {
843                 printfd(tcp, tcp->u_arg[0]);
844                 tprints(", ");
845         } else {
846                 if (syserror(tcp))
847                         printaddr(tcp->u_arg[1]);
848                 else
849                         printmsghdr(tcp, tcp->u_arg[1], tcp->u_rval);
850                 /* flags */
851                 tprints(", ");
852                 printflags(msg_flags, tcp->u_arg[2], "MSG_???");
853         }
854         return 0;
855 }
856
857 SYS_FUNC(recvmmsg)
858 {
859         /* +5 chars are for "left " prefix */
860         static char str[5 + TIMESPEC_TEXT_BUFSIZE];
861
862         if (entering(tcp)) {
863                 printfd(tcp, tcp->u_arg[0]);
864                 tprints(", ");
865                 if (verbose(tcp)) {
866                         sprint_timespec(str, tcp, tcp->u_arg[4]);
867                         /* Abusing tcp->auxstr as temp storage.
868                          * Will be used and freed on syscall exit.
869                          */
870                         tcp->auxstr = xstrdup(str);
871                 } else {
872                         tprintf("%#lx, %ld, ", tcp->u_arg[1], tcp->u_arg[2]);
873                         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
874                         tprints(", ");
875                         print_timespec(tcp, tcp->u_arg[4]);
876                 }
877                 return 0;
878         } else {
879                 if (verbose(tcp)) {
880                         decode_mmsg(tcp, 0);
881                         /* timeout on entrance */
882                         tprintf(", %s", tcp->auxstr ? tcp->auxstr : "{...}");
883                         free((void *) tcp->auxstr);
884                         tcp->auxstr = NULL;
885                 }
886                 if (syserror(tcp))
887                         return 0;
888                 if (tcp->u_rval == 0) {
889                         tcp->auxstr = "Timeout";
890                         return RVAL_STR;
891                 }
892                 if (!verbose(tcp))
893                         return 0;
894                 /* timeout on exit */
895                 sprint_timespec(stpcpy(str, "left "), tcp, tcp->u_arg[4]);
896                 tcp->auxstr = str;
897                 return RVAL_STR;
898         }
899 }
900
901 #endif /* HAVE_SENDMSG */
902
903 #include "xlat/shutdown_modes.h"
904
905 SYS_FUNC(shutdown)
906 {
907         printfd(tcp, tcp->u_arg[0]);
908         tprints(", ");
909         printxval(shutdown_modes, tcp->u_arg[1], "SHUT_???");
910
911         return RVAL_DECODED;
912 }
913
914 SYS_FUNC(getsockname)
915 {
916         return do_sockname(tcp, -1);
917 }
918
919 static int
920 do_pipe(struct tcb *tcp, int flags_arg)
921 {
922         if (exiting(tcp)) {
923                 if (syserror(tcp)) {
924                         printaddr(tcp->u_arg[0]);
925                 } else {
926 #ifdef HAVE_GETRVAL2
927                         if (flags_arg < 0)
928                                 tprintf("[%lu, %lu]", tcp->u_rval, getrval2(tcp));
929                         else
930 #endif
931                                 printpair_int(tcp, tcp->u_arg[0], "%u");
932                 }
933                 if (flags_arg >= 0) {
934                         tprints(", ");
935                         printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
936                 }
937         }
938         return 0;
939 }
940
941 SYS_FUNC(pipe)
942 {
943         return do_pipe(tcp, -1);
944 }
945
946 SYS_FUNC(pipe2)
947 {
948         return do_pipe(tcp, 1);
949 }
950
951 SYS_FUNC(socketpair)
952 {
953         if (entering(tcp)) {
954                 printxval(domains, tcp->u_arg[0], "PF_???");
955                 tprints(", ");
956                 tprint_sock_type(tcp->u_arg[1]);
957                 tprintf(", %lu", tcp->u_arg[2]);
958         } else {
959                 tprints(", ");
960                 printpair_int(tcp, tcp->u_arg[3], "%u");
961         }
962         return 0;
963 }
964
965 #include "xlat/sockoptions.h"
966 #include "xlat/sockipoptions.h"
967 #include "xlat/sockipv6options.h"
968 #include "xlat/sockipxoptions.h"
969 #include "xlat/sockrawoptions.h"
970 #include "xlat/sockpacketoptions.h"
971 #include "xlat/socksctpoptions.h"
972 #include "xlat/socktcpoptions.h"
973
974 static void
975 print_sockopt_fd_level_name(struct tcb *tcp, int fd, int level, int name)
976 {
977         printfd(tcp, fd);
978         tprints(", ");
979         printxval(socketlayers, level, "SOL_??");
980         tprints(", ");
981
982         switch (level) {
983         case SOL_SOCKET:
984                 printxval(sockoptions, name, "SO_???");
985                 break;
986         case SOL_IP:
987                 printxval(sockipoptions, name, "IP_???");
988                 break;
989         case SOL_IPV6:
990                 printxval(sockipv6options, name, "IPV6_???");
991                 break;
992         case SOL_IPX:
993                 printxval(sockipxoptions, name, "IPX_???");
994                 break;
995         case SOL_PACKET:
996                 printxval(sockpacketoptions, name, "PACKET_???");
997                 break;
998         case SOL_TCP:
999                 printxval(socktcpoptions, name, "TCP_???");
1000                 break;
1001         case SOL_SCTP:
1002                 printxval(socksctpoptions, name, "SCTP_???");
1003                 break;
1004         case SOL_RAW:
1005                 printxval(sockrawoptions, name, "RAW_???");
1006                 break;
1007
1008                 /* Other SOL_* protocol levels still need work. */
1009
1010         default:
1011                 tprintf("%u", name);
1012         }
1013
1014         tprints(", ");
1015 }
1016
1017 #ifdef SO_LINGER
1018 static void
1019 print_linger(struct tcb *tcp, long addr, int len)
1020 {
1021         struct linger linger;
1022
1023         if (len != sizeof(linger) ||
1024             umove(tcp, addr, &linger) < 0) {
1025                 printaddr(addr);
1026                 return;
1027         }
1028
1029         tprintf("{onoff=%d, linger=%d}",
1030                 linger.l_onoff,
1031                 linger.l_linger);
1032 }
1033 #endif /* SO_LINGER */
1034
1035 #ifdef SO_PEERCRED
1036 static void
1037 print_ucred(struct tcb *tcp, long addr, int len)
1038 {
1039         struct ucred uc;
1040
1041         if (len != sizeof(uc) ||
1042             umove(tcp, addr, &uc) < 0) {
1043                 printaddr(addr);
1044         } else {
1045                 tprintf("{pid=%u, uid=%u, gid=%u}",
1046                         (unsigned) uc.pid,
1047                         (unsigned) uc.uid,
1048                         (unsigned) uc.gid);
1049         }
1050 }
1051 #endif /* SO_PEERCRED */
1052
1053 #ifdef PACKET_STATISTICS
1054 static void
1055 print_tpacket_stats(struct tcb *tcp, long addr, int len)
1056 {
1057         struct tpacket_stats stats;
1058
1059         if (len != sizeof(stats) ||
1060             umove(tcp, addr, &stats) < 0) {
1061                 printaddr(addr);
1062         } else {
1063                 tprintf("{packets=%u, drops=%u}",
1064                         stats.tp_packets,
1065                         stats.tp_drops);
1066         }
1067 }
1068 #endif /* PACKET_STATISTICS */
1069
1070 #ifdef ICMP_FILTER
1071 # include "xlat/icmpfilterflags.h"
1072
1073 static void
1074 print_icmp_filter(struct tcb *tcp, long addr, int len)
1075 {
1076         struct icmp_filter      filter;
1077
1078         if (len != sizeof(filter) ||
1079             umove(tcp, addr, &filter) < 0) {
1080                 printaddr(addr);
1081                 return;
1082         }
1083
1084         tprints("~(");
1085         printflags(icmpfilterflags, ~filter.data, "ICMP_???");
1086         tprints(")");
1087 }
1088 #endif /* ICMP_FILTER */
1089
1090 static void
1091 print_getsockopt(struct tcb *tcp, int level, int name, long addr, int len)
1092 {
1093         if (addr && verbose(tcp))
1094         switch (level) {
1095         case SOL_SOCKET:
1096                 switch (name) {
1097 #ifdef SO_LINGER
1098                 case SO_LINGER:
1099                         print_linger(tcp, addr, len);
1100                         goto done;
1101 #endif
1102 #ifdef SO_PEERCRED
1103                 case SO_PEERCRED:
1104                         print_ucred(tcp, addr, len);
1105                         goto done;
1106 #endif
1107                 }
1108                 break;
1109
1110         case SOL_PACKET:
1111                 switch (name) {
1112 #ifdef PACKET_STATISTICS
1113                 case PACKET_STATISTICS:
1114                         print_tpacket_stats(tcp, addr, len);
1115                         goto done;
1116 #endif
1117                 }
1118                 break;
1119
1120         case SOL_RAW:
1121                 switch (name) {
1122 #ifdef ICMP_FILTER
1123                 case ICMP_FILTER:
1124                         print_icmp_filter(tcp, addr, len);
1125                         goto done;
1126 #endif
1127                 }
1128                 break;
1129         }
1130
1131         /* default arg printing */
1132
1133         if (verbose(tcp)) {
1134                 if (len == sizeof(int)) {
1135                         printnum_int(tcp, addr, "%d");
1136                 } else {
1137                         printstr(tcp, addr, len);
1138                 }
1139         } else {
1140                 printaddr(addr);
1141         }
1142 done:
1143         tprintf(", [%d]", len);
1144 }
1145
1146 SYS_FUNC(getsockopt)
1147 {
1148         if (entering(tcp)) {
1149                 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
1150                                             tcp->u_arg[1], tcp->u_arg[2]);
1151         } else {
1152                 int len;
1153
1154                 if (syserror(tcp) || umove(tcp, tcp->u_arg[4], &len) < 0) {
1155                         tprintf("%#lx, %#lx",
1156                                 tcp->u_arg[3], tcp->u_arg[4]);
1157                 } else {
1158                         print_getsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
1159                                          tcp->u_arg[3], len);
1160                 }
1161         }
1162         return 0;
1163 }
1164
1165 #ifdef IP_ADD_MEMBERSHIP
1166 static void
1167 print_mreq(struct tcb *tcp, long addr, unsigned int len)
1168 {
1169         struct ip_mreq mreq;
1170
1171         if (len < sizeof(mreq)) {
1172                 printstr(tcp, addr, len);
1173                 return;
1174         }
1175         if (umove_or_printaddr(tcp, addr, &mreq))
1176                 return;
1177
1178         tprints("{imr_multiaddr=inet_addr(");
1179         print_quoted_string(inet_ntoa(mreq.imr_multiaddr),
1180                             16, QUOTE_0_TERMINATED);
1181         tprints("), imr_interface=inet_addr(");
1182         print_quoted_string(inet_ntoa(mreq.imr_interface),
1183                             16, QUOTE_0_TERMINATED);
1184         tprints(")}");
1185 }
1186 #endif /* IP_ADD_MEMBERSHIP */
1187
1188 #ifdef IPV6_ADD_MEMBERSHIP
1189 static void
1190 print_mreq6(struct tcb *tcp, long addr, unsigned int len)
1191 {
1192         struct ipv6_mreq mreq;
1193
1194         if (len < sizeof(mreq))
1195                 goto fail;
1196
1197         if (umove_or_printaddr(tcp, addr, &mreq))
1198                 return;
1199
1200 #ifdef HAVE_INET_NTOP
1201         const struct in6_addr *in6 = &mreq.ipv6mr_multiaddr;
1202         char address[INET6_ADDRSTRLEN];
1203
1204         if (!inet_ntop(AF_INET6, in6, address, sizeof(address)))
1205                 goto fail;
1206
1207         tprints("{ipv6mr_multiaddr=inet_pton(");
1208         print_quoted_string(address, sizeof(address), QUOTE_0_TERMINATED);
1209         tprints("), ipv6mr_interface=");
1210         print_ifindex(mreq.ipv6mr_interface);
1211         tprints("}");
1212         return;
1213 #endif /* HAVE_INET_NTOP */
1214
1215 fail:
1216         printstr(tcp, addr, len);
1217 }
1218 #endif /* IPV6_ADD_MEMBERSHIP */
1219
1220 #ifdef MCAST_JOIN_GROUP
1221 static void
1222 print_group_req(struct tcb *tcp, long addr, int len)
1223 {
1224         struct group_req greq;
1225
1226         if (len != sizeof(greq) ||
1227             umove(tcp, addr, &greq) < 0) {
1228                 printaddr(addr);
1229                 return;
1230         }
1231
1232         union {
1233                 struct sockaddr *sa;
1234                 struct sockaddr_in *sin;
1235 #ifdef HAVE_INET_NTOP
1236                 struct sockaddr_in6 *sin6;
1237 #endif
1238         } a = { .sa = (struct sockaddr *) &greq.gr_group };
1239 #ifdef HAVE_INET_NTOP
1240         char str[INET6_ADDRSTRLEN];
1241 #endif
1242
1243         tprintf("{gr_interface=%u, gr_group={sa_family=", greq.gr_interface);
1244         printxval(addrfams, a.sa->sa_family, "AF_???");
1245
1246         switch (a.sa->sa_family) {
1247         case AF_INET:
1248                 tprintf(", sin_port=htons(%u), sin_addr=inet_addr(\"%s\")}}",
1249                         ntohs(a.sin->sin_port),
1250                         inet_ntoa(a.sin->sin_addr));
1251                 return;
1252 #ifdef HAVE_INET_NTOP
1253         case AF_INET6:
1254                 if (!inet_ntop(AF_INET6, &a.sin6->sin6_addr, str, sizeof(str)))
1255                         break;
1256                 tprintf(", sin6_port=htons(%u)"
1257                         ", inet_pton(AF_INET6, \"%s\", &sin6_addr)}}",
1258                         ntohs(a.sin6->sin6_port), str);
1259                 return;
1260 #endif /* HAVE_INET_NTOP */
1261         }
1262
1263         tprints(", sa_data=");
1264         print_quoted_string(a.sa->sa_data, sizeof(a.sa->sa_data), 0);
1265         tprintf("}}");
1266
1267 }
1268 #endif /* MCAST_JOIN_GROUP */
1269
1270 #ifdef PACKET_RX_RING
1271 static void
1272 print_tpacket_req(struct tcb *tcp, long addr, int len)
1273 {
1274         struct tpacket_req req;
1275
1276         if (len != sizeof(req) ||
1277             umove(tcp, addr, &req) < 0) {
1278                 printaddr(addr);
1279         } else {
1280                 tprintf("{block_size=%u, block_nr=%u, "
1281                         "frame_size=%u, frame_nr=%u}",
1282                         req.tp_block_size,
1283                         req.tp_block_nr,
1284                         req.tp_frame_size,
1285                         req.tp_frame_nr);
1286         }
1287 }
1288 #endif /* PACKET_RX_RING */
1289
1290 #ifdef PACKET_ADD_MEMBERSHIP
1291 # include "xlat/packet_mreq_type.h"
1292
1293 static void
1294 print_packet_mreq(struct tcb *tcp, long addr, int len)
1295 {
1296         struct packet_mreq mreq;
1297
1298         if (len != sizeof(mreq) ||
1299             umove(tcp, addr, &mreq) < 0) {
1300                 printaddr(addr);
1301         } else {
1302                 unsigned int i;
1303
1304                 tprintf("{mr_ifindex=%u, mr_type=", mreq.mr_ifindex);
1305                 printxval(packet_mreq_type, mreq.mr_type, "PACKET_MR_???");
1306                 tprintf(", mr_alen=%u, mr_address=", mreq.mr_alen);
1307                 if (mreq.mr_alen > ARRAY_SIZE(mreq.mr_address))
1308                         mreq.mr_alen = ARRAY_SIZE(mreq.mr_address);
1309                 for (i = 0; i < mreq.mr_alen; ++i)
1310                         tprintf("%02x", mreq.mr_address[i]);
1311                 tprints("}");
1312         }
1313 }
1314 #endif /* PACKET_ADD_MEMBERSHIP */
1315
1316 static void
1317 print_setsockopt(struct tcb *tcp, int level, int name, long addr, int len)
1318 {
1319         if (addr && verbose(tcp))
1320         switch (level) {
1321         case SOL_SOCKET:
1322                 switch (name) {
1323 #ifdef SO_LINGER
1324                 case SO_LINGER:
1325                         print_linger(tcp, addr, len);
1326                         goto done;
1327 #endif
1328                 }
1329                 break;
1330
1331         case SOL_IP:
1332                 switch (name) {
1333 #ifdef IP_ADD_MEMBERSHIP
1334                 case IP_ADD_MEMBERSHIP:
1335                 case IP_DROP_MEMBERSHIP:
1336                         print_mreq(tcp, addr, len);
1337                         goto done;
1338 #endif /* IP_ADD_MEMBERSHIP */
1339 #ifdef MCAST_JOIN_GROUP
1340                 case MCAST_JOIN_GROUP:
1341                 case MCAST_LEAVE_GROUP:
1342                         print_group_req(tcp, addr, len);
1343                         goto done;
1344 #endif /* MCAST_JOIN_GROUP */
1345                 }
1346                 break;
1347
1348         case SOL_IPV6:
1349                 switch (name) {
1350 #ifdef IPV6_ADD_MEMBERSHIP
1351                 case IPV6_ADD_MEMBERSHIP:
1352                 case IPV6_DROP_MEMBERSHIP:
1353 # ifdef IPV6_JOIN_ANYCAST
1354                 case IPV6_JOIN_ANYCAST:
1355 # endif
1356 # ifdef IPV6_LEAVE_ANYCAST
1357                 case IPV6_LEAVE_ANYCAST:
1358 # endif
1359                         print_mreq6(tcp, addr, len);
1360                         goto done;
1361 #endif /* IPV6_ADD_MEMBERSHIP */
1362                 }
1363                 break;
1364
1365         case SOL_PACKET:
1366                 switch (name) {
1367 #ifdef PACKET_RX_RING
1368                 case PACKET_RX_RING:
1369 # ifdef PACKET_TX_RING
1370                 case PACKET_TX_RING:
1371 # endif
1372                         print_tpacket_req(tcp, addr, len);
1373                         goto done;
1374 #endif /* PACKET_RX_RING */
1375 #ifdef PACKET_ADD_MEMBERSHIP
1376                 case PACKET_ADD_MEMBERSHIP:
1377                 case PACKET_DROP_MEMBERSHIP:
1378                         print_packet_mreq(tcp, addr, len);
1379                         goto done;
1380 #endif /* PACKET_ADD_MEMBERSHIP */
1381                 }
1382                 break;
1383
1384         case SOL_RAW:
1385                 switch (name) {
1386 #ifdef ICMP_FILTER
1387                 case ICMP_FILTER:
1388                         print_icmp_filter(tcp, addr, len);
1389                         goto done;
1390 #endif
1391                 }
1392                 break;
1393         }
1394
1395         /* default arg printing */
1396
1397         if (verbose(tcp)) {
1398                 if (len == sizeof(int)) {
1399                         printnum_int(tcp, addr, "%d");
1400                 } else {
1401                         printstr(tcp, addr, len);
1402                 }
1403         } else {
1404                 printaddr(addr);
1405         }
1406 done:
1407         tprintf(", %d", len);
1408 }
1409
1410 SYS_FUNC(setsockopt)
1411 {
1412         print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
1413                                     tcp->u_arg[1], tcp->u_arg[2]);
1414         print_setsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
1415                          tcp->u_arg[3], tcp->u_arg[4]);
1416
1417         return RVAL_DECODED;
1418 }