]> granicus.if.org Git - strace/blob - net.c
43bc3cdd8a280f7a3ad2ea8e8d806a79750dcbad
[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  *      $Id$
31  */
32
33 #include "defs.h"
34
35 #include <sys/stat.h>
36 #include <sys/socket.h>
37 #include <sys/un.h>
38
39 #if defined(HAVE_SIN6_SCOPE_ID_LINUX)
40 #define in6_addr in6_addr_libc
41 #define ipv6_mreq ipv6_mreq_libc
42 #define sockaddr_in6 sockaddr_in6_libc
43 #endif
44
45 #include <netinet/in.h>
46 #ifdef HAVE_NETINET_TCP_H
47 #include <netinet/tcp.h>
48 #endif
49 #ifdef HAVE_NETINET_UDP_H
50 #include <netinet/udp.h>
51 #endif
52 #include <arpa/inet.h>
53 #include <net/if.h>
54 #if defined(LINUX)
55 #include <asm/types.h>
56 #if defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC__ + __GLIBC_MINOR__ >= 3)
57 #  include <netipx/ipx.h>
58 #else
59 #  include <linux/ipx.h>
60 #endif
61 #endif /* LINUX */
62
63 #if defined (__GLIBC__) && (((__GLIBC__ < 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 1)) || defined(HAVE_SIN6_SCOPE_ID_LINUX))
64 #if defined(HAVE_LINUX_IN6_H)
65 #if defined(HAVE_SIN6_SCOPE_ID_LINUX)
66 #undef in6_addr
67 #undef ipv6_mreq
68 #undef sockaddr_in6
69 #define in6_addr in6_addr_kernel
70 #define ipv6_mreq ipv6_mreq_kernel
71 #define sockaddr_in6 sockaddr_in6_kernel
72 #endif
73 #include <linux/in6.h>
74 #if defined(HAVE_SIN6_SCOPE_ID_LINUX)
75 #undef in6_addr
76 #undef ipv6_mreq
77 #undef sockaddr_in6
78 #define in6_addr in6_addr_libc
79 #define ipv6_mreq ipv6_mreq_libc
80 #define sockaddr_in6 sockaddr_in6_kernel
81 #endif
82 #endif
83 #endif
84
85 #if defined(HAVE_SYS_UIO_H)
86 #include <sys/uio.h>
87 #endif
88
89 #if defined(HAVE_LINUX_NETLINK_H)
90 #include <linux/netlink.h>
91 #endif
92
93 #if defined(HAVE_LINUX_IF_PACKET_H)
94 #include <linux/if_packet.h>
95 #endif
96
97 #if defined(HAVE_LINUX_ICMP_H)
98 #include <linux/icmp.h>
99 #endif
100
101 #ifndef PF_UNSPEC
102 #define PF_UNSPEC AF_UNSPEC
103 #endif
104
105 #if UNIXWARE >= 7
106 #define HAVE_SENDMSG            1               /* HACK - *FIXME* */
107 #endif
108
109 #ifdef LINUX
110 /* Under Linux these are enums so we can't test for them with ifdef. */
111 #define IPPROTO_EGP IPPROTO_EGP
112 #define IPPROTO_PUP IPPROTO_PUP
113 #define IPPROTO_IDP IPPROTO_IDP
114 #define IPPROTO_IGMP IPPROTO_IGMP
115 #define IPPROTO_RAW IPPROTO_RAW
116 #define IPPROTO_MAX IPPROTO_MAX
117 #endif
118
119 static struct xlat domains[] = {
120 #ifdef PF_AAL5
121         { PF_AAL5,      "PF_AAL5"       },
122 #endif
123 #ifdef PF_APPLETALK
124         { PF_APPLETALK, "PF_APPLETALK"  },
125 #endif
126 #ifdef PF_ASH
127         { PF_ASH,       "PF_ASH"        },
128 #endif
129 #ifdef PF_ATMPVC
130         { PF_ATMPVC,    "PF_ATMPVC"     },
131 #endif
132 #ifdef PF_ATMSVC
133         { PF_ATMSVC,    "PF_ATMSVC"     },
134 #endif
135 #ifdef PF_AX25
136         { PF_AX25,      "PF_AX25"       },
137 #endif
138 #ifdef PF_BLUETOOTH
139         { PF_BLUETOOTH, "PF_BLUETOOTH"  },
140 #endif
141 #ifdef PF_BRIDGE
142         { PF_BRIDGE,    "PF_BRIDGE"     },
143 #endif
144 #ifdef PF_DECnet
145         { PF_DECnet,    "PF_DECnet"     },
146 #endif
147 #ifdef PF_DECNET
148         { PF_DECNET,    "PF_DECNET"     },
149 #endif
150 #ifdef PF_ECONET
151         { PF_ECONET,    "PF_ECONET"     },
152 #endif
153 #ifdef PF_FILE
154         { PF_FILE,      "PF_FILE"       },
155 #endif
156 #ifdef PF_IMPLINK
157         { PF_IMPLINK,   "PF_IMPLINK"    },
158 #endif
159 #ifdef PF_INET
160         { PF_INET,      "PF_INET"       },
161 #endif
162 #ifdef PF_INET6
163         { PF_INET6,     "PF_INET6"      },
164 #endif
165 #ifdef PF_IPX
166         { PF_IPX,       "PF_IPX"        },
167 #endif
168 #ifdef PF_IRDA
169         { PF_IRDA,      "PF_IRDA"       },
170 #endif
171 #ifdef PF_ISO
172         { PF_ISO,       "PF_ISO"        },
173 #endif
174 #ifdef PF_KEY
175         { PF_KEY,       "PF_KEY"        },
176 #endif
177 #ifdef PF_UNIX
178         { PF_UNIX,      "PF_UNIX"       },
179 #endif
180 #ifdef PF_LOCAL
181         { PF_LOCAL,     "PF_LOCAL"      },
182 #endif
183 #ifdef PF_NETBEUI
184         { PF_NETBEUI,   "PF_NETBEUI"    },
185 #endif
186 #ifdef PF_NETLINK
187         { PF_NETLINK,   "PF_NETLINK"    },
188 #endif
189 #ifdef PF_NETROM
190         { PF_NETROM,    "PF_NETROM"     },
191 #endif
192 #ifdef PF_PACKET
193         { PF_PACKET,    "PF_PACKET"     },
194 #endif
195 #ifdef PF_PPPOX
196         { PF_PPPOX,     "PF_PPPOX"      },
197 #endif
198 #ifdef PF_ROSE
199         { PF_ROSE,      "PF_ROSE"       },
200 #endif
201 #ifdef PF_ROUTE
202         { PF_ROUTE,     "PF_ROUTE"      },
203 #endif
204 #ifdef PF_SECURITY
205         { PF_SECURITY,  "PF_SECURITY"   },
206 #endif
207 #ifdef PF_SNA
208         { PF_SNA,       "PF_SNA"        },
209 #endif
210 #ifdef PF_UNSPEC
211         { PF_UNSPEC,    "PF_UNSPEC"     },
212 #endif
213 #ifdef PF_WANPIPE
214         { PF_WANPIPE,   "PF_WANPIPE"    },
215 #endif
216 #ifdef PF_X25
217         { PF_X25,       "PF_X25"        },
218 #endif
219         { 0,            NULL            },
220 };
221 struct xlat addrfams[] = {
222 #ifdef AF_APPLETALK
223         { AF_APPLETALK, "AF_APPLETALK"  },
224 #endif
225 #ifdef AF_ASH
226         { AF_ASH,       "AF_ASH"        },
227 #endif
228 #ifdef AF_ATMPVC
229         { AF_ATMPVC,    "AF_ATMPVC"     },
230 #endif
231 #ifdef AF_ATMSVC
232         { AF_ATMSVC,    "AF_ATMSVC"     },
233 #endif
234 #ifdef AF_AX25
235         { AF_AX25,      "AF_AX25"       },
236 #endif
237 #ifdef AF_BLUETOOTH
238         { AF_BLUETOOTH, "AF_BLUETOOTH"  },
239 #endif
240 #ifdef AF_BRIDGE
241         { AF_BRIDGE,    "AF_BRIDGE"     },
242 #endif
243 #ifdef AF_DECnet
244         { AF_DECnet,    "AF_DECnet"     },
245 #endif
246 #ifdef AF_ECONET
247         { AF_ECONET,    "AF_ECONET"     },
248 #endif
249 #ifdef AF_FILE
250         { AF_FILE,      "AF_FILE"       },
251 #endif
252 #ifdef AF_IMPLINK
253         { AF_IMPLINK,   "AF_IMPLINK"    },
254 #endif
255 #ifdef AF_INET
256         { AF_INET,      "AF_INET"       },
257 #endif
258 #ifdef AF_INET6
259         { AF_INET6,     "AF_INET6"      },
260 #endif
261 #ifdef AF_IPX
262         { AF_IPX,       "AF_IPX"        },
263 #endif
264 #ifdef AF_IRDA
265         { AF_IRDA,      "AF_IRDA"       },
266 #endif
267 #ifdef AF_ISO
268         { AF_ISO,       "AF_ISO"        },
269 #endif
270 #ifdef AF_KEY
271         { AF_KEY,       "AF_KEY"        },
272 #endif
273 #ifdef AF_UNIX
274         { AF_UNIX,      "AF_UNIX"       },
275 #endif
276 #ifdef AF_LOCAL
277         { AF_LOCAL,     "AF_LOCAL"      },
278 #endif
279 #ifdef AF_NETBEUI
280         { AF_NETBEUI,   "AF_NETBEUI"    },
281 #endif
282 #ifdef AF_NETLINK
283         { AF_NETLINK,   "AF_NETLINK"    },
284 #endif
285 #ifdef AF_NETROM
286         { AF_NETROM,    "AF_NETROM"     },
287 #endif
288 #ifdef AF_PACKET
289         { AF_PACKET,    "AF_PACKET"     },
290 #endif
291 #ifdef AF_PPPOX
292         { AF_PPPOX,     "AF_PPPOX"      },
293 #endif
294 #ifdef AF_ROSE
295         { AF_ROSE,      "AF_ROSE"       },
296 #endif
297 #ifdef AF_ROUTE
298         { AF_ROUTE,     "AF_ROUTE"      },
299 #endif
300 #ifdef AF_SECURITY
301         { AF_SECURITY,  "AF_SECURITY"   },
302 #endif
303 #ifdef AF_SNA
304         { AF_SNA,       "AF_SNA"        },
305 #endif
306 #ifdef AF_UNSPEC
307         { AF_UNSPEC,    "AF_UNSPEC"     },
308 #endif
309 #ifdef AF_WANPIPE
310         { AF_WANPIPE,   "AF_WANPIPE"    },
311 #endif
312 #ifdef AF_X25
313         { AF_X25,       "AF_X25"        },
314 #endif
315         { 0,            NULL            },
316 };
317 static struct xlat socktypes[] = {
318         { SOCK_STREAM,  "SOCK_STREAM"   },
319         { SOCK_DGRAM,   "SOCK_DGRAM"    },
320 #ifdef SOCK_RAW
321         { SOCK_RAW,     "SOCK_RAW"      },
322 #endif
323 #ifdef SOCK_SEQPACKET
324         { SOCK_SEQPACKET,"SOCK_SEQPACKET"},
325 #endif
326 #ifdef SOCK_RDM
327         { SOCK_RDM,     "SOCK_RDM"      },
328 #endif
329 #ifdef SOCK_PACKET
330         { SOCK_PACKET,  "SOCK_PACKET"   },
331 #endif
332         { 0,            NULL            },
333 };
334 static struct xlat socketlayers[] = {
335 #if defined(SOL_IP)
336         { SOL_IP,       "SOL_IP"        },
337 #endif
338 #if defined(SOL_ICMP)
339         { SOL_ICMP,     "SOL_ICMP"      },
340 #endif
341 #if defined(SOL_TCP)
342         { SOL_TCP,      "SOL_TCP"       },
343 #endif
344 #if defined(SOL_UDP)
345         { SOL_UDP,      "SOL_UDP"       },
346 #endif
347 #if defined(SOL_IPV6)
348         { SOL_IPV6,     "SOL_IPV6"      },
349 #endif
350 #if defined(SOL_ICMPV6)
351         { SOL_ICMPV6,   "SOL_ICMPV6"    },
352 #endif
353 #if defined(SOL_RAW)
354         { SOL_RAW,      "SOL_RAW"       },
355 #endif
356 #if defined(SOL_IPX)
357         { SOL_IPX,      "SOL_IPX"       },
358 #endif
359 #if defined(SOL_IPX)
360         { SOL_IPX,      "SOL_IPX"       },
361 #endif
362 #if defined(SOL_AX25)
363         { SOL_AX25,     "SOL_AX25"      },
364 #endif
365 #if defined(SOL_ATALK)
366         { SOL_ATALK,    "SOL_ATALK"     },
367 #endif
368 #if defined(SOL_NETROM)
369         { SOL_NETROM,   "SOL_NETROM"    },
370 #endif
371 #if defined(SOL_ROSE)
372         { SOL_ROSE,     "SOL_ROSE"      },
373 #endif
374 #if defined(SOL_DECNET)
375         { SOL_DECNET,   "SOL_DECNET"    },
376 #endif
377 #if defined(SOL_X25)
378         { SOL_X25,      "SOL_X25"       },
379 #endif
380 #if defined(SOL_PACKET)
381         { SOL_PACKET,   "SOL_PACKET"    },
382 #endif
383 #if defined(SOL_ATM)
384         { SOL_ATM,      "SOL_ATM"       },
385 #endif
386 #if defined(SOL_AAL)
387         { SOL_AAL,      "SOL_AAL"       },
388 #endif
389 #if defined(SOL_IRDA)
390         { SOL_IRDA,     "SOL_IRDA"      },
391 #endif
392         { SOL_SOCKET,   "SOL_SOCKET"    },      /* Never used! */
393 };
394 /*** WARNING: DANGER WILL ROBINSON: NOTE "socketlayers" array above
395      falls into "protocols" array below!!!!   This is intended!!! ***/
396 static struct xlat protocols[] = {
397         { IPPROTO_IP,   "IPPROTO_IP"    },
398         { IPPROTO_ICMP, "IPPROTO_ICMP"  },
399         { IPPROTO_TCP,  "IPPROTO_TCP"   },
400         { IPPROTO_UDP,  "IPPROTO_UDP"   },
401 #ifdef IPPROTO_GGP
402         { IPPROTO_GGP,  "IPPROTO_GGP"   },
403 #endif
404 #ifdef IPPROTO_EGP
405         { IPPROTO_EGP,  "IPPROTO_EGP"   },
406 #endif
407 #ifdef IPPROTO_PUP
408         { IPPROTO_PUP,  "IPPROTO_PUP"   },
409 #endif
410 #ifdef IPPROTO_IDP
411         { IPPROTO_IDP,  "IPPROTO_IDP"   },
412 #endif
413 #ifdef IPPROTO_IPV6
414         { IPPROTO_IPV6, "IPPROTO_IPV6"  },
415 #endif
416 #ifdef IPPROTO_ICMPV6
417         { IPPROTO_ICMPV6,"IPPROTO_ICMPV6"},
418 #endif
419 #ifdef IPPROTO_IGMP
420         { IPPROTO_IGMP, "IPPROTO_IGMP"  },
421 #endif
422 #ifdef IPPROTO_HELLO
423         { IPPROTO_HELLO,"IPPROTO_HELLO" },
424 #endif
425 #ifdef IPPROTO_ND
426         { IPPROTO_ND,   "IPPROTO_ND"    },
427 #endif
428 #ifdef IPPROTO_RAW
429         { IPPROTO_RAW,  "IPPROTO_RAW"   },
430 #endif
431 #ifdef IPPROTO_MAX
432         { IPPROTO_MAX,  "IPPROTO_MAX"   },
433 #endif
434 #ifdef IPPROTO_IPIP
435         { IPPROTO_IPIP, "IPPROTO_IPIP"  },
436 #endif
437         { 0,            NULL            },
438 };
439 static struct xlat msg_flags[] = {
440         { MSG_OOB,      "MSG_OOB"       },
441 #ifdef MSG_DONTROUTE
442         { MSG_DONTROUTE,"MSG_DONTROUTE" },
443 #endif
444 #ifdef MSG_PEEK
445         { MSG_PEEK,     "MSG_PEEK"      },
446 #endif
447 #ifdef MSG_CTRUNC
448         { MSG_CTRUNC,   "MSG_CTRUNC"    },
449 #endif
450 #ifdef MSG_PROXY
451         { MSG_PROXY,    "MSG_PROXY"     },
452 #endif
453 #ifdef MSG_EOR
454         { MSG_EOR,      "MSG_EOR"       },
455 #endif
456 #ifdef MSG_WAITALL
457         { MSG_WAITALL,  "MSG_WAITALL"   },
458 #endif
459 #ifdef MSG_TRUNC
460         { MSG_TRUNC,    "MSG_TRUNC"     },
461 #endif
462 #ifdef MSG_CTRUNC
463         { MSG_CTRUNC,   "MSG_CTRUNC"    },
464 #endif
465 #ifdef MSG_ERRQUEUE
466         { MSG_ERRQUEUE, "MSG_ERRQUEUE"  },
467 #endif
468 #ifdef MSG_DONTWAIT
469         { MSG_DONTWAIT, "MSG_DONTWAIT"  },
470 #endif
471 #ifdef MSG_CONFIRM
472         { MSG_CONFIRM,  "MSG_CONFIRM"   },
473 #endif
474 #ifdef MSG_PROBE
475         { MSG_PROBE,    "MSG_PROBE"     },
476 #endif
477 #ifdef MSG_FIN
478         { MSG_FIN,      "MSG_FIN"       },
479 #endif
480 #ifdef MSG_SYN
481         { MSG_SYN,      "MSG_SYN"       },
482 #endif
483 #ifdef MSG_RST
484         { MSG_RST,      "MSG_RST"       },
485 #endif
486 #ifdef MSG_NOSIGNAL
487         { MSG_NOSIGNAL, "MSG_NOSIGNAL"  },
488 #endif
489 #ifdef MSG_MORE
490         { MSG_MORE,     "MSG_MORE"      },
491 #endif
492         { 0,            NULL            },
493 };
494
495 static struct xlat sockoptions[] = {
496 #ifdef SO_PEERCRED
497         { SO_PEERCRED,  "SO_PEERCRED"   },
498 #endif
499 #ifdef SO_PASSCRED
500         { SO_PASSCRED,  "SO_PASSCRED"   },
501 #endif
502 #ifdef SO_DEBUG
503         { SO_DEBUG,     "SO_DEBUG"      },
504 #endif
505 #ifdef SO_REUSEADDR
506         { SO_REUSEADDR, "SO_REUSEADDR"  },
507 #endif
508 #ifdef SO_KEEPALIVE
509         { SO_KEEPALIVE, "SO_KEEPALIVE"  },
510 #endif
511 #ifdef SO_DONTROUTE
512         { SO_DONTROUTE, "SO_DONTROUTE"  },
513 #endif
514 #ifdef SO_BROADCAST
515         { SO_BROADCAST, "SO_BROADCAST"  },
516 #endif
517 #ifdef SO_LINGER
518         { SO_LINGER,    "SO_LINGER"     },
519 #endif
520 #ifdef SO_OOBINLINE
521         { SO_OOBINLINE, "SO_OOBINLINE"  },
522 #endif
523 #ifdef SO_TYPE
524         { SO_TYPE,      "SO_TYPE"       },
525 #endif
526 #ifdef SO_ERROR
527         { SO_ERROR,     "SO_ERROR"      },
528 #endif
529 #ifdef SO_SNDBUF
530         { SO_SNDBUF,    "SO_SNDBUF"     },
531 #endif
532 #ifdef SO_RCVBUF
533         { SO_RCVBUF,    "SO_RCVBUF"     },
534 #endif
535 #ifdef SO_NO_CHECK
536         { SO_NO_CHECK,  "SO_NO_CHECK"   },
537 #endif
538 #ifdef SO_PRIORITY
539         { SO_PRIORITY,  "SO_PRIORITY"   },
540 #endif
541 #ifdef SO_ACCEPTCONN
542         { SO_ACCEPTCONN,"SO_ACCEPTCONN" },
543 #endif
544 #ifdef SO_USELOOPBACK
545         { SO_USELOOPBACK,"SO_USELOOPBACK"},
546 #endif
547 #ifdef SO_SNDLOWAT
548         { SO_SNDLOWAT,  "SO_SNDLOWAT"   },
549 #endif
550 #ifdef SO_RCVLOWAT
551         { SO_RCVLOWAT,  "SO_RCVLOWAT"   },
552 #endif
553 #ifdef SO_SNDTIMEO
554         { SO_SNDTIMEO,  "SO_SNDTIMEO"   },
555 #endif
556 #ifdef SO_RCVTIMEO
557         { SO_RCVTIMEO,  "SO_RCVTIMEO"   },
558 #endif
559 #ifdef SO_BSDCOMPAT
560         { SO_BSDCOMPAT, "SO_BSDCOMPAT"  },
561 #endif
562 #ifdef SO_REUSEPORT
563         { SO_REUSEPORT, "SO_REUSEPORT"  },
564 #endif
565 #ifdef SO_ORDREL
566         { SO_ORDREL,    "SO_ORDREL"     },
567 #endif
568 #ifdef SO_IMASOCKET
569         { SO_IMASOCKET, "SO_IMASOCKET"  },
570 #endif
571 #ifdef SO_MGMT
572         { SO_MGMT,      "SO_MGMT"       },
573 #endif
574 #ifdef SO_LISTENING
575         { SO_LISTENING, "SO_LISTENING"  },
576 #endif
577 #ifdef SO_RDWR
578         { SO_RDWR,      "SO_RDWR"       },
579 #endif
580 #ifdef SO_SEMA
581         { SO_SEMA,      "SO_SEMA"       },
582 #endif
583 #ifdef SO_PARALLELSVR
584         { SO_PARALLELSVR,"SO_PARALLELSVR"},
585 #endif
586 #ifdef SO_PROTOTYPE
587         { SO_PROTOTYPE, "SO_PROTOTYPE"  },
588 #endif
589 #ifdef SO_ALLRAW
590         { SO_ALLRAW,    "SO_ALLRAW"     },
591 #endif
592 #ifdef SO_ICS
593         { SO_ICS,       "SO_ICS"        },
594 #endif
595         { 0,            NULL            },
596 };
597
598 #if !defined (SOL_IP) && defined (IPPROTO_IP)
599 #define SOL_IP IPPROTO_IP
600 #endif
601
602 #ifdef SOL_IP
603 static struct xlat sockipoptions[] = {
604 #ifdef IP_TOS
605         { IP_TOS,               "IP_TOS"                },
606 #endif
607 #ifdef IP_TTL
608         { IP_TTL,               "IP_TTL"                },
609 #endif
610 #ifdef IP_HDRINCL
611         { IP_HDRINCL,           "IP_HDRINCL"            },
612 #endif
613 #ifdef IP_OPTIONS
614         { IP_OPTIONS,           "IP_OPTIONS"            },
615 #endif
616 #ifdef IP_ROUTER_ALERT
617         { IP_ROUTER_ALERT,      "IP_ROUTER_ALERT"       },
618 #endif
619 #ifdef IP_RECVOPTIONS
620         { IP_RECVOPTIONS,       "IP_RECVOPTIONS"        },
621 #endif
622 #ifdef IP_RECVOPTS
623         { IP_RECVOPTS,          "IP_RECVOPTS"           },
624 #endif
625 #ifdef IP_RECVRETOPTS
626         { IP_RECVRETOPTS,       "IP_RECVRETOPTS"        },
627 #endif
628 #ifdef IP_RECVDSTADDR
629         { IP_RECVDSTADDR,       "IP_RECVDSTADDR"        },
630 #endif
631 #ifdef IP_RETOPTS
632         { IP_RETOPTS,           "IP_RETOPTS"            },
633 #endif
634 #ifdef IP_PKTINFO
635         { IP_PKTINFO,           "IP_PKTINFO"            },
636 #endif
637 #ifdef IP_PKTOPTIONS
638         { IP_PKTOPTIONS,        "IP_PKTOPTIONS"         },
639 #endif
640 #ifdef IP_MTU_DISCOVER
641         { IP_MTU_DISCOVER,      "IP_MTU_DISCOVER"       },
642 #endif
643 #ifdef IP_RECVERR
644         { IP_RECVERR,           "IP_RECVERR"            },
645 #endif
646 #ifdef IP_RECVTTL
647         { IP_RECVTTL,           "IP_RECRECVTTL"         },
648 #endif
649 #ifdef IP_RECVTOS
650         { IP_RECVTOS,           "IP_RECRECVTOS"         },
651 #endif
652 #ifdef IP_MTU
653         { IP_MTU,               "IP_MTU"                },
654 #endif
655 #ifdef IP_MULTICAST_IF
656         { IP_MULTICAST_IF,      "IP_MULTICAST_IF"       },
657 #endif
658 #ifdef IP_MULTICAST_TTL
659         { IP_MULTICAST_TTL,     "IP_MULTICAST_TTL"      },
660 #endif
661 #ifdef IP_MULTICAST_LOOP
662         { IP_MULTICAST_LOOP,    "IP_MULTICAST_LOOP"     },
663 #endif
664 #ifdef IP_ADD_MEMBERSHIP
665         { IP_ADD_MEMBERSHIP,    "IP_ADD_MEMBERSHIP"     },
666 #endif
667 #ifdef IP_DROP_MEMBERSHIP
668         { IP_DROP_MEMBERSHIP,   "IP_DROP_MEMBERSHIP"    },
669 #endif
670 #ifdef IP_BROADCAST_IF
671         { IP_BROADCAST_IF,      "IP_BROADCAST_IF"       },
672 #endif
673 #ifdef IP_RECVIFINDEX
674         { IP_RECVIFINDEX,       "IP_RECVIFINDEX"        },
675 #endif
676 #ifdef IP_MSFILTER
677         { IP_MSFILTER,          "IP_MSFILTER"           },
678 #endif
679 #ifdef MCAST_MSFILTER
680         { MCAST_MSFILTER,       "MCAST_MSFILTER"        },
681 #endif
682 #ifdef IP_FREEBIND
683         { IP_FREEBIND,          "IP_FREEBIND"           },
684 #endif
685         { 0,                    NULL                    },
686 };
687 #endif /* SOL_IP */
688
689 #ifdef SOL_IPV6
690 static struct xlat sockipv6options[] = {
691 #ifdef IPV6_ADDRFORM
692         { IPV6_ADDRFORM,        "IPV6_ADDRFORM"         },
693 #endif
694 #ifdef MCAST_FILTER
695         { MCAST_FILTER,         "MCAST_FILTER"          },
696 #endif
697 #ifdef IPV6_PKTOPTIONS
698         { IPV6_PKTOPTIONS,      "IPV6_PKTOPTIONS"       },
699 #endif
700 #ifdef IPV6_MTU
701         { IPV6_MTU,             "IPV6_MTU"              },
702 #endif
703 #ifdef IPV6_V6ONLY
704         { IPV6_V6ONLY,          "IPV6_V6ONLY"           },
705 #endif
706 #ifdef IPV6_PKTINFO
707         { IPV6_PKTINFO,         "IPV6_PKTINFO"          },
708 #endif
709 #ifdef IPV6_HOPLIMIT
710         { IPV6_HOPLIMIT,        "IPV6_HOPLIMIT"         },
711 #endif
712 #ifdef IPV6_RTHDR
713         { IPV6_RTHDR,           "IPV6_RTHDR"            },
714 #endif
715 #ifdef IPV6_HOPOPTS
716         { IPV6_HOPOPTS,         "IPV6_HOPOPTS"          },
717 #endif
718 #ifdef IPV6_DSTOPTS
719         { IPV6_DSTOPTS,         "IPV6_DSTOPTS"          },
720 #endif
721 #ifdef IPV6_FLOWINFO
722         { IPV6_FLOWINFO,        "IPV6_FLOWINFO"         },
723 #endif
724 #ifdef IPV6_UNICAST_HOPS
725         { IPV6_UNICAST_HOPS,    "IPV6_UNICAST_HOPS"     },
726 #endif
727 #ifdef IPV6_MULTICAST_HOPS
728         { IPV6_MULTICAST_HOPS,  "IPV6_MULTICAST_HOPS"   },
729 #endif
730 #ifdef IPV6_MULTICAST_LOOP
731         { IPV6_MULTICAST_LOOP,  "IPV6_MULTICAST_LOOP"   },
732 #endif
733 #ifdef IPV6_MULTICAST_IF
734         { IPV6_MULTICAST_IF,    "IPV6_MULTICAST_IF"     },
735 #endif
736 #ifdef IPV6_MTU_DISCOVER
737         { IPV6_MTU_DISCOVER,    "IPV6_MTU_DISCOVER"     },
738 #endif
739 #ifdef IPV6_RECVERR
740         { IPV6_RECVERR,         "IPV6_RECVERR"          },
741 #endif
742 #ifdef IPV6_FLOWINFO_SEND
743         { IPV6_FLOWINFO_SEND,   "IPV6_FLOWINFO_SEND"    },
744 #endif
745         { 0,                    NULL                    },
746 };
747 #endif /* SOL_IPV6 */
748
749 #ifdef SOL_IPX
750 static struct xlat sockipxoptions[] = {
751         { IPX_TYPE,     "IPX_TYPE"      },
752         { 0,            NULL            },
753 };
754 #endif /* SOL_IPX */
755
756 #ifdef SOL_RAW
757 static struct xlat sockrawoptions[] = {
758 #if defined(ICMP_FILTER)
759         { ICMP_FILTER,          "ICMP_FILTER"   },
760 #endif
761         { 0,                    NULL            },
762 };
763 #endif /* SOL_RAW */
764
765 #ifdef SOL_PACKET
766 static struct xlat sockpacketoptions[] = {
767         { PACKET_ADD_MEMBERSHIP,        "PACKET_ADD_MEMBERSHIP" },
768         { PACKET_DROP_MEMBERSHIP,       "PACKET_DROP_MEMBERSHIP"},
769 #if defined(PACKET_RECV_OUTPUT)
770         { PACKET_RECV_OUTPUT,           "PACKET_RECV_OUTPUT"    },
771 #endif
772 #if defined(PACKET_RX_RING)
773         { PACKET_RX_RING,               "PACKET_RX_RING"        },
774 #endif
775 #if defined(PACKET_STATISTICS)
776         { PACKET_STATISTICS,            "PACKET_STATISTICS"     },
777 #endif
778         { 0,                            NULL                    },
779 };
780 #endif /* SOL_PACKET */
781
782 #if  !defined (SOL_TCP) && defined (IPPROTO_TCP)
783 #define SOL_TCP IPPROTO_TCP
784 #endif
785
786 #ifdef SOL_TCP
787 static struct xlat socktcpoptions[] = {
788         { TCP_NODELAY,          "TCP_NODELAY"   },
789         { TCP_MAXSEG,           "TCP_MAXSEG"    },
790 #if defined(TCP_CORK)
791         { TCP_CORK,             "TCP_CORK"      },
792 #endif
793 #if defined(TCP_KEEPIDLE)
794         { TCP_KEEPIDLE,         "TCP_KEEPIDLE" },
795 #endif
796 #if defined(TCP_KEEPINTVL)
797         { TCP_KEEPINTVL,        "TCP_KEEPINTVL" },
798 #endif
799 #if defined(TCP_KEEPCNT)
800         { TCP_KEEPCNT,          "TCP_KEEPCNT" },
801 #endif
802 #if defined(TCP_NKEEP)
803         { TCP_NKEEP,            "TCP_NKEEP"     },
804 #endif
805 #if defined(TCP_SYNCNT)
806         { TCP_SYNCNT,           "TCP_SYNCNT" },
807 #endif
808 #if defined(TCP_LINGER2)
809         { TCP_LINGER2,          "TCP_LINGER2" },
810 #endif
811 #if defined(TCP_DEFER_ACCEPT)
812         { TCP_DEFER_ACCEPT,     "TCP_DEFER_ACCEPT" },
813 #endif
814 #if defined(TCP_WINDOW_CLAMP)
815         { TCP_WINDOW_CLAMP,     "TCP_WINDOW_CLAMP" },
816 #endif
817 #if defined(TCP_INFO)
818         { TCP_INFO,             "TCP_INFO" },
819 #endif
820 #if defined(TCP_QUICKACK)
821         { TCP_QUICKACK,         "TCP_QUICKACK" },
822 #endif
823         { 0,                    NULL            },
824 };
825 #endif /* SOL_TCP */
826
827 #ifdef SOL_RAW
828 static struct xlat icmpfilterflags[] = {
829 #if defined(ICMP_ECHOREPLY)
830         { (1<<ICMP_ECHOREPLY),          "ICMP_ECHOREPLY"        },
831 #endif
832 #if defined(ICMP_DEST_UNREACH)
833         { (1<<ICMP_DEST_UNREACH),       "ICMP_DEST_UNREACH"     },
834 #endif
835 #if defined(ICMP_SOURCE_QUENCH)
836         { (1<<ICMP_SOURCE_QUENCH),      "ICMP_SOURCE_QUENCH"    },
837 #endif
838 #if defined(ICMP_REDIRECT)
839         { (1<<ICMP_REDIRECT),           "ICMP_REDIRECT"         },
840 #endif
841 #if defined(ICMP_ECHO)
842         { (1<<ICMP_ECHO),               "ICMP_ECHO"             },
843 #endif
844 #if defined(ICMP_TIME_EXCEEDED)
845         { (1<<ICMP_TIME_EXCEEDED),      "ICMP_TIME_EXCEEDED"    },
846 #endif
847 #if defined(ICMP_PARAMETERPROB)
848         { (1<<ICMP_PARAMETERPROB),      "ICMP_PARAMETERPROB"    },
849 #endif
850 #if defined(ICMP_TIMESTAMP)
851         { (1<<ICMP_TIMESTAMP),          "ICMP_TIMESTAMP"        },
852 #endif
853 #if defined(ICMP_TIMESTAMPREPLY)
854         { (1<<ICMP_TIMESTAMPREPLY),     "ICMP_TIMESTAMPREPLY"   },
855 #endif
856 #if defined(ICMP_INFO_REQUEST)
857         { (1<<ICMP_INFO_REQUEST),       "ICMP_INFO_REQUEST"     },
858 #endif
859 #if defined(ICMP_INFO_REPLY)
860         { (1<<ICMP_INFO_REPLY),         "ICMP_INFO_REPLY"       },
861 #endif
862 #if defined(ICMP_ADDRESS)
863         { (1<<ICMP_ADDRESS),            "ICMP_ADDRESS"          },
864 #endif
865 #if defined(ICMP_ADDRESSREPLY)
866         { (1<<ICMP_ADDRESSREPLY),       "ICMP_ADDRESSREPLY"     },
867 #endif
868         { 0,                            NULL                    },
869 };
870 #endif /* SOL_RAW */
871
872 #if defined(AF_PACKET) /* from e.g. linux/if_packet.h */
873 static struct xlat af_packet_types[] = {
874 #if defined(PACKET_HOST)
875         { PACKET_HOST,                  "PACKET_HOST"           },
876 #endif
877 #if defined(PACKET_BROADCAST)
878         { PACKET_BROADCAST,             "PACKET_BROADCAST"      },
879 #endif
880 #if defined(PACKET_MULTICAST)
881         { PACKET_MULTICAST,             "PACKET_MULTICAST"      },
882 #endif
883 #if defined(PACKET_OTHERHOST)
884         { PACKET_OTHERHOST,             "PACKET_OTHERHOST"      },
885 #endif
886 #if defined(PACKET_OUTGOING)
887         { PACKET_OUTGOING,              "PACKET_OUTGOING"       },
888 #endif
889 #if defined(PACKET_LOOPBACK)
890         { PACKET_LOOPBACK,              "PACKET_LOOPBACK"       },
891 #endif
892 #if defined(PACKET_FASTROUTE)
893         { PACKET_FASTROUTE,             "PACKET_FASTROUTE"      },
894 #endif
895         { 0,                            NULL                    },
896 };
897 #endif /* defined(AF_PACKET) */
898
899
900 void
901 printsock(tcp, addr, addrlen)
902 struct tcb *tcp;
903 long addr;
904 int addrlen;
905 {
906         union {
907                 char pad[128];
908                 struct sockaddr sa;
909                 struct sockaddr_in sin;
910                 struct sockaddr_un sau;
911 #ifdef HAVE_INET_NTOP
912                 struct sockaddr_in6 sa6;
913 #endif
914 #if defined(LINUX) && defined(AF_IPX)
915                 struct sockaddr_ipx sipx;
916 #endif
917 #ifdef AF_PACKET
918                 struct sockaddr_ll ll;
919 #endif
920 #ifdef AF_NETLINK
921                 struct sockaddr_nl nl;
922 #endif
923         } addrbuf;
924         char string_addr[100];
925
926         if (addr == 0) {
927                 tprintf("NULL");
928                 return;
929         }
930         if (!verbose(tcp)) {
931                 tprintf("%#lx", addr);
932                 return;
933         }
934         if ((addrlen<2) || (addrlen>sizeof(addrbuf)))
935                 addrlen=sizeof(addrbuf);
936
937         if (umoven(tcp, addr, addrlen, (char*)&addrbuf) < 0) {
938                 tprintf("{...}");
939                 return;
940         }
941
942         tprintf("{sa_family=");
943         printxval(addrfams, addrbuf.sa.sa_family, "AF_???");
944         tprintf(", ");
945
946         switch (addrbuf.sa.sa_family) {
947         case AF_UNIX:
948                 if (addrlen==2) {
949                         tprintf("<nil>");
950                 } else if (addrbuf.sau.sun_path[0]) {
951                         tprintf("path=\"%-.*s\"", addrlen-2, addrbuf.sau.sun_path);
952                 } else {
953                         tprintf("path=@%-.*s", addrlen-3, addrbuf.sau.sun_path+1);
954                 }
955                 break;
956         case AF_INET:
957                 tprintf("sin_port=htons(%u), sin_addr=inet_addr(\"%s\")",
958                         ntohs(addrbuf.sin.sin_port), inet_ntoa(addrbuf.sin.sin_addr));
959                 break;
960 #ifdef HAVE_INET_NTOP
961         case AF_INET6:
962                 inet_ntop(AF_INET6, &addrbuf.sa6.sin6_addr, string_addr, sizeof(string_addr));
963                 tprintf("sin6_port=htons(%u), inet_pton(AF_INET6, \"%s\", &sin6_addr), sin6_flowinfo=%u",
964                                 ntohs(addrbuf.sa6.sin6_port), string_addr,
965                                 addrbuf.sa6.sin6_flowinfo);
966 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
967                 {
968 #if defined(HAVE_IF_INDEXTONAME) && defined(IN6_IS_ADDR_LINKLOCAL) && defined(IN6_IS_ADDR_MC_LINKLOCAL)
969                     int numericscope = 0;
970                     if (IN6_IS_ADDR_LINKLOCAL (&addrbuf.sa6.sin6_addr)
971                             || IN6_IS_ADDR_MC_LINKLOCAL (&addrbuf.sa6.sin6_addr)) {
972                         char scopebuf[IFNAMSIZ + 1];
973
974                         if (if_indextoname (addrbuf.sa6.sin6_scope_id, scopebuf) == NULL)
975                             numericscope++;
976                         else
977                             tprintf(", sin6_scope_id=if_nametoindex(\"%s\")", scopebuf);
978                     } else
979                         numericscope++;
980
981                     if (numericscope)
982 #endif
983                         tprintf(", sin6_scope_id=%u", addrbuf.sa6.sin6_scope_id);
984                 }
985 #endif
986                     break;
987 #endif
988 #if defined(AF_IPX) && defined(linux)
989         case AF_IPX:
990                 {
991                         int i;
992                         tprintf("sipx_port=htons(%u), ",
993                                         ntohs(addrbuf.sipx.sipx_port));
994                         /* Yes, I know, this does not look too
995                          * strace-ish, but otherwise the IPX
996                          * addresses just look monstrous...
997                          * Anyways, feel free if you don't like
998                          * this way.. :)
999                          */
1000                         tprintf("%08lx:", (unsigned long)ntohl(addrbuf.sipx.sipx_network));
1001                         for (i = 0; i<IPX_NODE_LEN; i++)
1002                                 tprintf("%02x", addrbuf.sipx.sipx_node[i]);
1003                         tprintf("/[%02x]", addrbuf.sipx.sipx_type);
1004                 }
1005                 break;
1006 #endif /* AF_IPX && linux */
1007 #ifdef AF_PACKET
1008         case AF_PACKET:
1009                 {
1010                         int i;
1011                         tprintf("proto=%#04x, if%d, pkttype=",
1012                                         ntohs(addrbuf.ll.sll_protocol),
1013                                         addrbuf.ll.sll_ifindex);
1014                         printxval(af_packet_types, addrbuf.ll.sll_pkttype, "?");
1015                         tprintf(", addr(%d)={%d, ",
1016                                         addrbuf.ll.sll_halen,
1017                                         addrbuf.ll.sll_hatype);
1018                         for (i=0; i<addrbuf.ll.sll_halen; i++)
1019                                 tprintf("%02x", addrbuf.ll.sll_addr[i]);
1020                 }
1021                 break;
1022
1023 #endif /* AF_APACKET */
1024 #ifdef AF_NETLINK
1025         case AF_NETLINK:
1026                 tprintf("pid=%d, groups=%08x", addrbuf.nl.nl_pid, addrbuf.nl.nl_groups);
1027                 break;
1028 #endif /* AF_NETLINK */
1029         /* AF_AX25 AF_APPLETALK AF_NETROM AF_BRIDGE AF_AAL5
1030         AF_X25 AF_ROSE etc. still need to be done */
1031
1032         default:
1033                 tprintf("sa_data=");
1034                 printstr(tcp, (long) &((struct sockaddr *) addr)->sa_data,
1035                         sizeof addrbuf.sa.sa_data);
1036                 break;
1037         }
1038         tprintf("}");
1039 }
1040
1041 #if HAVE_SENDMSG
1042
1043 static void
1044 printmsghdr(tcp, addr)
1045 struct tcb *tcp;
1046 long addr;
1047 {
1048         struct msghdr msg;
1049
1050         if (umove(tcp, addr, &msg) < 0) {
1051                 tprintf("%#lx", addr);
1052                 return;
1053         }
1054         tprintf("{msg_name(%d)=", msg.msg_namelen);
1055         printsock(tcp, (long)msg.msg_name, msg.msg_namelen);
1056
1057         tprintf(", msg_iov(%lu)=", (unsigned long)msg.msg_iovlen);
1058         tprint_iov(tcp, msg.msg_iovlen, (long) msg.msg_iov);
1059
1060 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
1061         tprintf(", msg_controllen=%lu", (unsigned long)msg.msg_controllen);
1062         if (msg.msg_controllen)
1063                 tprintf(", msg_control=%#lx, ", (unsigned long) msg.msg_control);
1064         tprintf(", msg_flags=");
1065         if (printflags(msg_flags, msg.msg_flags)==0)
1066                 tprintf("0");
1067 #else /* !HAVE_STRUCT_MSGHDR_MSG_CONTROL */
1068         tprintf("msg_accrights=%#lx, msg_accrightslen=%u",
1069                 (unsigned long) msg.msg_accrights, msg.msg_accrightslen);
1070 #endif /* !HAVE_STRUCT_MSGHDR_MSG_CONTROL */
1071         tprintf("}");
1072 }
1073
1074 #endif /* HAVE_SENDMSG */
1075
1076 int
1077 sys_socket(tcp)
1078 struct tcb *tcp;
1079 {
1080         if (entering(tcp)) {
1081                 printxval(domains, tcp->u_arg[0], "PF_???");
1082                 tprintf(", ");
1083                 printxval(socktypes, tcp->u_arg[1], "SOCK_???");
1084                 tprintf(", ");
1085                 switch (tcp->u_arg[0]) {
1086                 case PF_INET:
1087 #ifdef PF_INET6
1088                 case PF_INET6:
1089 #endif
1090                         printxval(protocols, tcp->u_arg[2], "IPPROTO_???");
1091                         break;
1092 #ifdef PF_IPX
1093                 case PF_IPX:
1094                         /* BTW: I don't believe this.. */
1095                         tprintf("[");
1096                         printxval(domains, tcp->u_arg[2], "PF_???");
1097                         tprintf("]");
1098                         break;
1099 #endif /* PF_IPX */
1100                 default:
1101                         tprintf("%lu", tcp->u_arg[2]);
1102                         break;
1103                 }
1104         }
1105         return 0;
1106 }
1107
1108 int
1109 sys_so_socket(tcp)
1110 struct tcb *tcp;
1111 {
1112         if (entering(tcp)) {
1113                 /* not sure really what these args are... but this
1114                  * is how truss prints it
1115                  */
1116                 tprintf("%ld, %ld, %ld, ",
1117                   tcp->u_arg[0], tcp->u_arg[1], tcp->u_arg[2]);
1118                 printpath(tcp, tcp->u_arg[3]);
1119                 tprintf(", %ld", tcp->u_arg[4]);
1120         }
1121         return 0;
1122 }
1123
1124 int
1125 sys_so_socketpair(tcp)
1126 struct tcb *tcp;
1127 {
1128         if (entering(tcp)) {
1129                 /* not sure what this arg is */
1130                 tprintf("0x%lx", tcp->u_arg[0]);
1131         }
1132         return 0;
1133 }
1134
1135 int
1136 sys_bind(tcp)
1137 struct tcb *tcp;
1138 {
1139         if (entering(tcp)) {
1140                 tprintf("%ld, ", tcp->u_arg[0]);
1141                 printsock(tcp, tcp->u_arg[1], tcp->u_arg[2]);
1142                 tprintf(", %lu", tcp->u_arg[2]);
1143         }
1144         return 0;
1145 }
1146
1147 int
1148 sys_connect(tcp)
1149 struct tcb *tcp;
1150 {
1151         return sys_bind(tcp);
1152 }
1153
1154 int
1155 sys_listen(tcp)
1156 struct tcb *tcp;
1157 {
1158         if (entering(tcp)) {
1159                 tprintf("%ld, %lu", tcp->u_arg[0], tcp->u_arg[1]);
1160         }
1161         return 0;
1162 }
1163
1164 int
1165 sys_accept(tcp)
1166 struct tcb *tcp;
1167 {
1168         if (entering(tcp)) {
1169                 tprintf("%ld, ", tcp->u_arg[0]);
1170         } else if (!tcp->u_arg[2])
1171                 tprintf("%#lx, NULL", tcp->u_arg[1]);
1172         else {
1173                 if (tcp->u_arg[1] == 0 || syserror(tcp)) {
1174                         tprintf("%#lx", tcp->u_arg[1]);
1175                 } else {
1176                         printsock(tcp, tcp->u_arg[1], tcp->u_arg[2]);
1177                 }
1178                 tprintf(", ");
1179                 printnum(tcp, tcp->u_arg[2], "%lu");
1180         }
1181         return 0;
1182 }
1183
1184 int
1185 sys_send(tcp)
1186 struct tcb *tcp;
1187 {
1188         if (entering(tcp)) {
1189                 tprintf("%ld, ", tcp->u_arg[0]);
1190                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
1191                 tprintf(", %lu, ", tcp->u_arg[2]);
1192                 /* flags */
1193                 if (printflags(msg_flags, tcp->u_arg[3]) == 0)
1194                         tprintf("0");
1195         }
1196         return 0;
1197 }
1198
1199 int
1200 sys_sendto(tcp)
1201 struct tcb *tcp;
1202 {
1203         if (entering(tcp)) {
1204                 tprintf("%ld, ", tcp->u_arg[0]);
1205                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
1206                 tprintf(", %lu, ", tcp->u_arg[2]);
1207                 /* flags */
1208                 if (printflags(msg_flags, tcp->u_arg[3]) == 0)
1209                         tprintf("0");
1210                 /* to address */
1211                 tprintf(", ");
1212                 printsock(tcp, tcp->u_arg[4], tcp->u_arg[5]);
1213                 /* to length */
1214                 tprintf(", %lu", tcp->u_arg[5]);
1215         }
1216         return 0;
1217 }
1218
1219 #ifdef HAVE_SENDMSG
1220
1221 int
1222 sys_sendmsg(tcp)
1223 struct tcb *tcp;
1224 {
1225         if (entering(tcp)) {
1226                 tprintf("%ld, ", tcp->u_arg[0]);
1227                 printmsghdr(tcp, tcp->u_arg[1]);
1228                 /* flags */
1229                 tprintf(", ");
1230                 if (printflags(msg_flags, tcp->u_arg[2]) == 0)
1231                         tprintf("0");
1232         }
1233         return 0;
1234 }
1235
1236 #endif /* HAVE_SENDMSG */
1237
1238 int
1239 sys_recv(tcp)
1240 struct tcb *tcp;
1241 {
1242         if (entering(tcp)) {
1243                 tprintf("%ld, ", tcp->u_arg[0]);
1244         } else {
1245                 if (syserror(tcp))
1246                         tprintf("%#lx", tcp->u_arg[1]);
1247                 else
1248                         printstr(tcp, tcp->u_arg[1], tcp->u_rval);
1249
1250                 tprintf(", %lu, ", tcp->u_arg[2]);
1251                 if (printflags(msg_flags, tcp->u_arg[3]) == 0)
1252                         tprintf("0");
1253         }
1254         return 0;
1255 }
1256
1257 int
1258 sys_recvfrom(tcp)
1259 struct tcb *tcp;
1260 {
1261         int fromlen;
1262
1263         if (entering(tcp)) {
1264                 tprintf("%ld, ", tcp->u_arg[0]);
1265         } else {
1266                 if (syserror(tcp)) {
1267                         tprintf("%#lx, %lu, %lu, %#lx, %#lx",
1268                                 tcp->u_arg[1], tcp->u_arg[2], tcp->u_arg[3],
1269                                 tcp->u_arg[4], tcp->u_arg[5]);
1270                         return 0;
1271                 }
1272                 /* buf */
1273                 printstr(tcp, tcp->u_arg[1], tcp->u_rval);
1274                 /* len */
1275                 tprintf(", %lu, ", tcp->u_arg[2]);
1276                 /* flags */
1277                 if (printflags(msg_flags, tcp->u_arg[3]) == 0)
1278                         tprintf("0");
1279                 /* from address, len */
1280                 if (!tcp->u_arg[4] || !tcp->u_arg[5]) {
1281                         if (tcp->u_arg[4] == 0)
1282                                 tprintf(", NULL");
1283                         else
1284                                 tprintf(", %#lx", tcp->u_arg[4]);
1285                         if (tcp->u_arg[5] == 0)
1286                                 tprintf(", NULL");
1287                         else
1288                                 tprintf(", %#lx", tcp->u_arg[5]);
1289                         return 0;
1290                 }
1291                 if (umove(tcp, tcp->u_arg[5], &fromlen) < 0) {
1292                         tprintf(", {...}, [?]");
1293                         return 0;
1294                 }
1295                 tprintf(", ");
1296                 printsock(tcp, tcp->u_arg[4], tcp->u_arg[5]);
1297                 /* from length */
1298                 tprintf(", [%u]", fromlen);
1299         }
1300         return 0;
1301 }
1302
1303 #ifdef HAVE_SENDMSG
1304
1305 int
1306 sys_recvmsg(tcp)
1307 struct tcb *tcp;
1308 {
1309         if (entering(tcp)) {
1310                 tprintf("%ld, ", tcp->u_arg[0]);
1311         } else {
1312                 if (syserror(tcp) || !verbose(tcp))
1313                         tprintf("%#lx", tcp->u_arg[1]);
1314                 else
1315                         printmsghdr(tcp, tcp->u_arg[1]);
1316                 /* flags */
1317                 tprintf(", ");
1318                 if (printflags(msg_flags, tcp->u_arg[2]) == 0)
1319                         tprintf("0");
1320         }
1321         return 0;
1322 }
1323
1324 #endif /* HAVE_SENDMSG */
1325
1326 int
1327 sys_shutdown(tcp)
1328 struct tcb *tcp;
1329 {
1330         if (entering(tcp)) {
1331                 tprintf("%ld, %ld", tcp->u_arg[0], tcp->u_arg[1]);
1332                 switch (tcp->u_arg[1]) {
1333                 case 0:
1334                         tprintf("%s", " /* receive */");
1335                         break;
1336                 case 1:
1337                         tprintf("%s", " /* send */");
1338                         break;
1339                 case 2:
1340                         tprintf("%s", " /* send and receive */");
1341                         break;
1342                 }
1343         }
1344         return 0;
1345 }
1346
1347 int
1348 sys_getsockname(tcp)
1349 struct tcb *tcp;
1350 {
1351         return sys_accept(tcp);
1352 }
1353
1354 int
1355 sys_getpeername(tcp)
1356 struct tcb *tcp;
1357 {
1358         return sys_accept(tcp);
1359 }
1360
1361 int
1362 sys_pipe(tcp)
1363 struct tcb *tcp;
1364 {
1365
1366 #if defined(LINUX) && !defined(SPARC) && !defined(SPARC64) && !defined(SH) && !defined(IA64)
1367         int fds[2];
1368
1369         if (exiting(tcp)) {
1370                 if (syserror(tcp)) {
1371                         tprintf("%#lx", tcp->u_arg[0]);
1372                         return 0;
1373                 }
1374                 if (umoven(tcp, tcp->u_arg[0], sizeof fds, (char *) fds) < 0)
1375                         tprintf("[...]");
1376                 else
1377                         tprintf("[%u, %u]", fds[0], fds[1]);
1378         }
1379 #elif defined(SPARC) || defined(SPARC64) || defined(SH) || defined(SVR4) || defined(FREEBSD) || defined(IA64)
1380         if (exiting(tcp))
1381                 tprintf("[%lu, %lu]", tcp->u_rval, getrval2(tcp));
1382 #endif
1383         return 0;
1384 }
1385
1386 int
1387 sys_socketpair(tcp)
1388 struct tcb *tcp;
1389 {
1390 #ifdef LINUX
1391         int fds[2];
1392 #endif
1393
1394         if (entering(tcp)) {
1395                 printxval(domains, tcp->u_arg[0], "PF_???");
1396                 tprintf(", ");
1397                 printxval(socktypes, tcp->u_arg[1], "SOCK_???");
1398                 tprintf(", ");
1399                 switch (tcp->u_arg[0]) {
1400                 case PF_INET:
1401                         printxval(protocols, tcp->u_arg[2], "IPPROTO_???");
1402                         break;
1403 #ifdef PF_IPX
1404                 case PF_IPX:
1405                         /* BTW: I don't believe this.. */
1406                         tprintf("[");
1407                         printxval(domains, tcp->u_arg[2], "PF_???");
1408                         tprintf("]");
1409                         break;
1410 #endif /* PF_IPX */
1411                 default:
1412                         tprintf("%lu", tcp->u_arg[2]);
1413                         break;
1414                 }
1415         } else {
1416                 if (syserror(tcp)) {
1417                         tprintf(", %#lx", tcp->u_arg[3]);
1418                         return 0;
1419                 }
1420 #ifdef LINUX
1421                 if (umoven(tcp, tcp->u_arg[3], sizeof fds, (char *) fds) < 0)
1422                         tprintf(", [...]");
1423                 else
1424                         tprintf(", [%u, %u]", fds[0], fds[1]);
1425 #endif /* LINUX */
1426 #if defined(SUNOS4) || defined(SVR4) || defined(FREEBSD)
1427                 tprintf(", [%lu, %lu]", tcp->u_rval, getrval2(tcp));
1428 #endif /* SUNOS4 || SVR4 || FREEBSD */
1429         }
1430         return 0;
1431 }
1432
1433 int
1434 sys_getsockopt(tcp)
1435 struct tcb *tcp;
1436 {
1437         if (entering(tcp)) {
1438                 tprintf("%ld, ", tcp->u_arg[0]);
1439                 printxval(socketlayers, tcp->u_arg[1], "SOL_???");
1440                 tprintf (", ");
1441                 switch (tcp->u_arg[1]) {
1442                 case SOL_SOCKET:
1443                         printxval(sockoptions, tcp->u_arg[2], "SO_???");
1444                         break;
1445 #ifdef SOL_IP
1446                 case SOL_IP:
1447                         printxval(sockipoptions, tcp->u_arg[2], "IP_???");
1448                         break;
1449 #endif
1450 #ifdef SOL_IPV6
1451                 case SOL_IPV6:
1452                         printxval(sockipv6options, tcp->u_arg[2], "IPV6_???");
1453                         break;
1454 #endif
1455 #ifdef SOL_IPX
1456                 case SOL_IPX:
1457                         printxval(sockipxoptions, tcp->u_arg[2], "IPX_???");
1458                         break;
1459 #endif
1460 #ifdef SOL_PACKET
1461                 case SOL_PACKET:
1462                         printxval(sockpacketoptions, tcp->u_arg[2], "PACKET_???");
1463                         break;
1464 #endif
1465 #ifdef SOL_TCP
1466                 case SOL_TCP:
1467                         printxval(socktcpoptions, tcp->u_arg[2], "TCP_???");
1468                         break;
1469 #endif
1470
1471                 /* SOL_AX25 SOL_ROSE SOL_ATALK SOL_NETROM SOL_UDP SOL_DECNET SOL_X25
1472                  * etc. still need work */
1473                 default:
1474                         tprintf("%lu", tcp->u_arg[2]);
1475                         break;
1476                 }
1477         } else {
1478                 long len;
1479                 if (syserror(tcp) || umove (tcp, tcp->u_arg[4], &len) < 0) {
1480                         tprintf(", %#lx, %#lx",
1481                                 tcp->u_arg[3], tcp->u_arg[4]);
1482                         return 0;
1483                 }
1484
1485                 switch (tcp->u_arg[1]) {
1486                 case SOL_SOCKET:
1487                         switch (tcp->u_arg[2]) {
1488 #ifdef SO_LINGER
1489                         case SO_LINGER:
1490                                 if (len == sizeof (struct linger)) {
1491                                         struct linger linger;
1492                                         if (umove (tcp,
1493                                                    tcp->u_arg[3],
1494                                                    &linger) < 0)
1495                                                 break;
1496                                         tprintf(", {onoff=%d, linger=%d}, "
1497                                                 "[%ld]",
1498                                                 linger.l_onoff,
1499                                                 linger.l_linger,
1500                                                 len);
1501                                         return 0;
1502                                 }
1503                                 break;
1504 #endif
1505                         }
1506                         break;
1507                 }
1508
1509                 tprintf (", ");
1510                 if (len == sizeof (int)) {
1511                         printnum(tcp, tcp->u_arg[3], "%ld");
1512                 }
1513                 else {
1514                         printstr (tcp, tcp->u_arg[3], len);
1515                 }
1516                 tprintf(", [%ld]", len);
1517         }
1518         return 0;
1519 }
1520
1521 #if defined(ICMP_FILTER)
1522 static void printicmpfilter(tcp, addr)
1523 struct tcb *tcp;
1524 long addr;
1525 {
1526         struct icmp_filter      filter;
1527
1528         if (!addr) {
1529                 tprintf("NULL");
1530                 return;
1531         }
1532         if (syserror(tcp) || !verbose(tcp)) {
1533                 tprintf("%#lx", addr);
1534                 return;
1535         }
1536         if (umove(tcp, addr, &filter) < 0) {
1537                 tprintf("{...}");
1538                 return;
1539         }
1540
1541         tprintf("~(");
1542         if (printflags(icmpfilterflags, ~filter.data) == 0)
1543                 tprintf("0");
1544         tprintf(")");
1545 }
1546 #endif /* ICMP_FILTER */
1547
1548 static int
1549 printsockopt (tcp, level, name, addr, len)
1550 struct tcb *tcp;
1551 int level;
1552 int name;
1553 long addr;
1554 int len;
1555 {
1556         printxval(socketlayers, level, "SOL_??");
1557         tprintf (", ");
1558         switch (level) {
1559             case SOL_SOCKET:
1560                 printxval(sockoptions, name, "SO_???");
1561                 switch (name) {
1562 #if defined(SO_LINGER)
1563                     case SO_LINGER:
1564                         if (len == sizeof (struct linger)) {
1565                                 struct linger linger;
1566                                 if (umove (tcp, addr, &linger) < 0)
1567                                         break;
1568                                 tprintf(", {onoff=%d, linger=%d}",
1569                                         linger.l_onoff,
1570                                         linger.l_linger);
1571                                 return 0;
1572                         }
1573                         break;
1574 #endif
1575                 }
1576                 break;
1577 #ifdef SOL_IP
1578             case SOL_IP:
1579                 printxval(sockipoptions, name, "IP_???");
1580                 break;
1581 #endif
1582 #ifdef SOL_IPV6
1583             case SOL_IPV6:
1584                 printxval(sockipv6options, name, "IPV6_???");
1585                 break;
1586 #endif
1587 #ifdef SOL_IPX
1588             case SOL_IPX:
1589                 printxval(sockipxoptions, name, "IPX_???");
1590                 break;
1591 #endif
1592 #ifdef SOL_PACKET
1593             case SOL_PACKET:
1594                 printxval(sockpacketoptions, name, "PACKET_???");
1595                 /* TODO: decode packate_mreq for PACKET_*_MEMBERSHIP */
1596                 break;
1597 #endif
1598 #ifdef SOL_TCP
1599             case SOL_TCP:
1600                 printxval(socktcpoptions, name, "TCP_???");
1601                 break;
1602 #endif
1603 #ifdef SOL_RAW
1604             case SOL_RAW:
1605                 printxval(sockrawoptions, name, "RAW_???");
1606                 switch (name) {
1607 #if defined(ICMP_FILTER)
1608                     case ICMP_FILTER:
1609                         tprintf(", ");
1610                         printicmpfilter(tcp, addr);
1611                         return 0;
1612 #endif
1613                 }
1614                 break;
1615 #endif
1616
1617                 /* SOL_AX25 SOL_ATALK SOL_NETROM SOL_UDP SOL_DECNET SOL_X25
1618                  * etc. still need work  */
1619
1620             default:
1621                 tprintf("%u", name);
1622         }
1623
1624         /* default arg printing */
1625
1626         tprintf (", ");
1627
1628         if (len == sizeof (int)) {
1629                 printnum(tcp, addr, "%ld");
1630         }
1631         else {
1632                 printstr (tcp, addr, len);
1633         }
1634         return 0;
1635 }
1636
1637
1638 #ifdef HAVE_STRUCT_OPTHDR
1639
1640 void
1641 print_sock_optmgmt (tcp, addr, len)
1642 struct tcb *tcp;
1643 long addr;
1644 int len;
1645 {
1646         int c = 0;
1647         struct opthdr hdr;
1648
1649         while (len >= (int) sizeof hdr) {
1650                 if (umove(tcp, addr, &hdr) < 0) break;
1651                 if (c++) {
1652                         tprintf (", ");
1653                 }
1654                 else if (len > hdr.len + sizeof hdr) {
1655                         tprintf ("[");
1656                 }
1657                 tprintf ("{");
1658                 addr += sizeof hdr;
1659                 len -= sizeof hdr;
1660                 printsockopt (tcp, hdr.level, hdr.name, addr, hdr.len);
1661                 if (hdr.len > 0) {
1662                         addr += hdr.len;
1663                         len -= hdr.len;
1664                 }
1665                 tprintf ("}");
1666         }
1667         if (len > 0) {
1668                 if (c++) tprintf (", ");
1669                 printstr (tcp, addr, len);
1670         }
1671         if (c > 1) tprintf ("]");
1672 }
1673
1674 #endif
1675
1676 int
1677 sys_setsockopt(tcp)
1678 struct tcb *tcp;
1679 {
1680         if (entering(tcp)) {
1681                 tprintf("%ld, ", tcp->u_arg[0]);
1682                 printsockopt (tcp, tcp->u_arg[1], tcp->u_arg[2],
1683                               tcp->u_arg[3], tcp->u_arg[4]);
1684                 tprintf(", %lu", tcp->u_arg[4]);
1685         }
1686         return 0;
1687 }
1688
1689 #if UNIXWARE >= 7
1690
1691 static struct xlat sock_version[] = {
1692         { __NETLIB_UW211_SVR4,  "UW211_SVR4" },
1693         { __NETLIB_UW211_XPG4,  "UW211_XPG4" },
1694         { __NETLIB_GEMINI_SVR4, "GEMINI_SVR4" },
1695         { __NETLIB_GEMINI_XPG4, "GEMINI_XPG4" },
1696         { __NETLIB_FP1_SVR4,    "FP1_SVR4" },
1697         { __NETLIB_FP1_XPG4,    "FP1_XPG4" },
1698         { 0,            NULL            },
1699 };
1700
1701
1702 int
1703 netlib_call(tcp, func)
1704 struct tcb *tcp;
1705 int (*func) ();
1706 {
1707         if (entering(tcp)) {
1708                 int i;
1709                 printxval (sock_version, tcp->u_arg[0], "__NETLIB_???");
1710                 tprintf(", ");
1711                 --tcp->u_nargs;
1712                 for (i = 0; i < tcp->u_nargs; i++)
1713                         tcp->u_arg[i] = tcp->u_arg[i + 1];
1714                 return func (tcp);
1715
1716         }
1717
1718         return func (tcp);
1719 }
1720
1721 int
1722 sys_xsocket(tcp)
1723 struct tcb *tcp;
1724 {
1725         return netlib_call (tcp, sys_socket);
1726 }
1727
1728 int
1729 sys_xsocketpair(tcp)
1730 struct tcb *tcp;
1731 {
1732         return netlib_call (tcp, sys_socketpair);
1733 }
1734
1735 int
1736 sys_xbind(tcp)
1737 struct tcb *tcp;
1738 {
1739         return netlib_call (tcp, sys_bind);
1740 }
1741
1742 int
1743 sys_xconnect(tcp)
1744 struct tcb *tcp;
1745 {
1746         return netlib_call (tcp, sys_connect);
1747 }
1748
1749 int
1750 sys_xlisten(tcp)
1751 struct tcb *tcp;
1752 {
1753         return netlib_call (tcp, sys_listen);
1754 }
1755
1756 int
1757 sys_xaccept(tcp)
1758 struct tcb *tcp;
1759 {
1760         return netlib_call (tcp, sys_accept);
1761 }
1762
1763 int
1764 sys_xsendmsg(tcp)
1765 struct tcb *tcp;
1766 {
1767         return netlib_call (tcp, sys_sendmsg);
1768 }
1769
1770 int
1771 sys_xrecvmsg(tcp)
1772 struct tcb *tcp;
1773 {
1774         return netlib_call (tcp, sys_recvmsg);
1775 }
1776
1777 int
1778 sys_xgetsockaddr(tcp)
1779 struct tcb *tcp;
1780 {
1781         if (entering(tcp)) {
1782                 printxval (sock_version, tcp->u_arg[0], "__NETLIB_???");
1783                 tprintf(", ");
1784                 if (tcp->u_arg[1] == 0) {
1785                         tprintf ("LOCALNAME, ");
1786                 }
1787                 else if (tcp->u_arg[1] == 1) {
1788                         tprintf ("REMOTENAME, ");
1789                 }
1790                 else {
1791                         tprintf ("%ld, ", tcp->u_arg [1]);
1792                 }
1793                 tprintf ("%ld, ", tcp->u_arg [2]);
1794         }
1795         else {
1796                 if (tcp->u_arg[3] == 0 || syserror(tcp)) {
1797                         tprintf("%#lx", tcp->u_arg[3]);
1798                 } else {
1799                         printsock(tcp, tcp->u_arg[3], tcp->u_arg[4]);
1800                 }
1801                 tprintf(", ");
1802                 printnum(tcp, tcp->u_arg[4], "%lu");
1803         }
1804
1805         return 0;
1806
1807 }
1808
1809 #if 0
1810
1811 int
1812 sys_xsetsockaddr(tcp)
1813 struct tcb *tcp;
1814 {
1815         return netlib_call (tcp, sys_setsockaddr);
1816 }
1817
1818 #endif
1819
1820 int
1821 sys_xgetsockopt(tcp)
1822 struct tcb *tcp;
1823 {
1824         return netlib_call (tcp, sys_getsockopt);
1825 }
1826
1827 int
1828 sys_xsetsockopt(tcp)
1829 struct tcb *tcp;
1830 {
1831         return netlib_call (tcp, sys_setsockopt);
1832 }
1833
1834 int
1835 sys_xshutdown(tcp)
1836 struct tcb *tcp;
1837 {
1838         return netlib_call (tcp, sys_shutdown);
1839 }
1840
1841 #endif