]> granicus.if.org Git - strace/blob - sock.c
Reindent defs.h preprocessor directives
[strace] / sock.c
1 /*
2  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  *      $Id$
28  */
29
30 #include "defs.h"
31
32 #include <sys/socket.h>
33 #include <linux/sockios.h>
34 #include <arpa/inet.h>
35
36 #if defined (ALPHA) || defined(SH) || defined(SH64)
37 #ifdef HAVE_SYS_IOCTL_H
38 #include <sys/ioctl.h>
39 #elif defined(HAVE_IOCTLS_H)
40 #include <ioctls.h>
41 #endif
42 #endif
43 #include <net/if.h>
44
45 static const struct xlat iffflags[] = {
46         { IFF_UP,               "IFF_UP"                },
47         { IFF_BROADCAST,        "IFF_BROADCAST"         },
48         { IFF_DEBUG,            "IFF_DEBUG"             },
49         { IFF_LOOPBACK,         "IFF_LOOPBACK"          },
50         { IFF_POINTOPOINT,      "IFF_POINTOPOINT"       },
51         { IFF_NOTRAILERS,       "IFF_NOTRAILERS"        },
52         { IFF_RUNNING,          "IFF_RUNNING"           },
53         { IFF_NOARP,            "IFF_NOARP"             },
54         { IFF_PROMISC,          "IFF_PROMISC"           },
55         { IFF_ALLMULTI,         "IFF_ALLMULTI"          },
56         { IFF_MASTER,           "IFF_MASTER"            },
57         { IFF_SLAVE,            "IFF_SLAVE"             },
58         { IFF_MULTICAST,        "IFF_MULTICAST"         },
59         { IFF_PORTSEL,          "IFF_PORTSEL"           },
60         { IFF_AUTOMEDIA,        "IFF_AUTOMEDIA"         },
61         { 0,                    NULL                    }
62 };
63
64
65 static void
66 print_addr(struct tcb *tcp, long addr, struct ifreq *ifr)
67 {
68         if (ifr->ifr_addr.sa_family == AF_INET) {
69                 struct sockaddr_in *sinp;
70                 sinp = (struct sockaddr_in *) &ifr->ifr_addr;
71                 tprintf("inet_addr(\"%s\")", inet_ntoa(sinp->sin_addr));
72         } else
73                 printstr(tcp, addr, sizeof(ifr->ifr_addr.sa_data));
74 }
75
76 int
77 sock_ioctl(struct tcb *tcp, long code, long arg)
78 {
79         struct ifreq ifr;
80         struct ifconf ifc;
81         const char *str = NULL;
82         unsigned char *bytes;
83
84         if (entering(tcp)) {
85                 if (code == SIOCGIFCONF) {
86                         if (umove(tcp, tcp->u_arg[2], &ifc) >= 0
87                             && ifc.ifc_buf == NULL)
88                                 tprintf(", {%d -> ", ifc.ifc_len);
89                         else
90                                 tprints(", {");
91                 }
92                 return 0;
93         }
94
95         switch (code) {
96 #ifdef SIOCSHIWAT
97         case SIOCSHIWAT:
98 #endif
99 #ifdef SIOCGHIWAT
100         case SIOCGHIWAT:
101 #endif
102 #ifdef SIOCSLOWAT
103         case SIOCSLOWAT:
104 #endif
105 #ifdef SIOCGLOWAT
106         case SIOCGLOWAT:
107 #endif
108 #ifdef FIOSETOWN
109         case FIOSETOWN:
110 #endif
111 #ifdef FIOGETOWN
112         case FIOGETOWN:
113 #endif
114 #ifdef SIOCSPGRP
115         case SIOCSPGRP:
116 #endif
117 #ifdef SIOCGPGRP
118         case SIOCGPGRP:
119 #endif
120 #ifdef SIOCATMARK
121         case SIOCATMARK:
122 #endif
123                 printnum(tcp, arg, ", %#d");
124                 return 1;
125         case SIOCGIFNAME:
126         case SIOCSIFNAME:
127         case SIOCGIFINDEX:
128         case SIOCGIFADDR:
129         case SIOCSIFADDR:
130         case SIOCGIFDSTADDR:
131         case SIOCSIFDSTADDR:
132         case SIOCGIFBRDADDR:
133         case SIOCSIFBRDADDR:
134         case SIOCGIFNETMASK:
135         case SIOCSIFNETMASK:
136         case SIOCGIFFLAGS:
137         case SIOCSIFFLAGS:
138         case SIOCGIFMETRIC:
139         case SIOCSIFMETRIC:
140         case SIOCGIFMTU:
141         case SIOCSIFMTU:
142         case SIOCGIFSLAVE:
143         case SIOCSIFSLAVE:
144         case SIOCGIFHWADDR:
145         case SIOCSIFHWADDR:
146         case SIOCGIFTXQLEN:
147         case SIOCSIFTXQLEN:
148         case SIOCGIFMAP:
149         case SIOCSIFMAP:
150                 if (umove(tcp, tcp->u_arg[2], &ifr) < 0)
151                         tprintf(", %#lx", tcp->u_arg[2]);
152                 else if (syserror(tcp)) {
153                         if (code == SIOCGIFNAME || code == SIOCSIFNAME)
154                                 tprintf(", {ifr_index=%d, ifr_name=???}", ifr.ifr_ifindex);
155                         else
156                                 tprintf(", {ifr_name=\"%s\", ???}", ifr.ifr_name);
157                 } else if (code == SIOCGIFNAME || code == SIOCSIFNAME)
158                         tprintf(", {ifr_index=%d, ifr_name=\"%s\"}",
159                                 ifr.ifr_ifindex, ifr.ifr_name);
160                 else {
161                         tprintf(", {ifr_name=\"%s\", ", ifr.ifr_name);
162                         switch (code) {
163                         case SIOCGIFINDEX:
164                                 tprintf("ifr_index=%d", ifr.ifr_ifindex);
165                                 break;
166                         case SIOCGIFADDR:
167                         case SIOCSIFADDR:
168                                 str = "ifr_addr";
169                         case SIOCGIFDSTADDR:
170                         case SIOCSIFDSTADDR:
171                                 if (!str)
172                                         str = "ifr_dstaddr";
173                         case SIOCGIFBRDADDR:
174                         case SIOCSIFBRDADDR:
175                                 if (!str)
176                                         str = "ifr_broadaddr";
177                         case SIOCGIFNETMASK:
178                         case SIOCSIFNETMASK:
179                                 if (!str)
180                                         str = "ifr_netmask";
181                                 tprintf("%s={", str);
182                                 printxval(addrfams,
183                                           ifr.ifr_addr.sa_family,
184                                           "AF_???");
185                                 tprints(", ");
186                                 print_addr(tcp, ((long) tcp->u_arg[2]
187                                                  + offsetof(struct ifreq,
188                                                              ifr_addr.sa_data)),
189                                            &ifr);
190                                 tprints("}");
191                                 break;
192                         case SIOCGIFHWADDR:
193                         case SIOCSIFHWADDR:
194                                 /* XXX Are there other hardware addresses
195                                    than 6-byte MACs?  */
196                                 bytes = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
197                                 tprintf("ifr_hwaddr=%02x:%02x:%02x:%02x:%02x:%02x",
198                                         bytes[0], bytes[1], bytes[2],
199                                         bytes[3], bytes[4], bytes[5]);
200                                 break;
201                         case SIOCGIFFLAGS:
202                         case SIOCSIFFLAGS:
203                                 tprints("ifr_flags=");
204                                 printflags(iffflags, ifr.ifr_flags, "IFF_???");
205                                 break;
206                         case SIOCGIFMETRIC:
207                         case SIOCSIFMETRIC:
208                                 tprintf("ifr_metric=%d", ifr.ifr_metric);
209                                 break;
210                         case SIOCGIFMTU:
211                         case SIOCSIFMTU:
212                                 tprintf("ifr_mtu=%d", ifr.ifr_mtu);
213                                 break;
214                         case SIOCGIFSLAVE:
215                         case SIOCSIFSLAVE:
216                                 tprintf("ifr_slave=\"%s\"", ifr.ifr_slave);
217                                 break;
218                         case SIOCGIFTXQLEN:
219                         case SIOCSIFTXQLEN:
220                                 tprintf("ifr_qlen=%d", ifr.ifr_qlen);
221                                 break;
222                         case SIOCGIFMAP:
223                         case SIOCSIFMAP:
224                                 tprintf("ifr_map={mem_start=%#lx, "
225                                         "mem_end=%#lx, base_addr=%#x, "
226                                         "irq=%u, dma=%u, port=%u}",
227                                         ifr.ifr_map.mem_start,
228                                         ifr.ifr_map.mem_end,
229                                         (unsigned) ifr.ifr_map.base_addr,
230                                         (unsigned) ifr.ifr_map.irq,
231                                         (unsigned) ifr.ifr_map.dma,
232                                         (unsigned) ifr.ifr_map.port);
233                                 break;
234                         }
235                         tprints("}");
236                 }
237                 return 1;
238         case SIOCGIFCONF:
239                 if (umove(tcp, tcp->u_arg[2], &ifc) < 0) {
240                         tprints("???}");
241                         return 1;
242                 }
243                 tprintf("%d, ", ifc.ifc_len);
244                 if (syserror(tcp)) {
245                         tprintf("%lx", (unsigned long) ifc.ifc_buf);
246                 } else if (ifc.ifc_buf == NULL) {
247                         tprints("NULL");
248                 } else {
249                         int i;
250                         unsigned nifra = ifc.ifc_len / sizeof(struct ifreq);
251                         struct ifreq ifra[nifra];
252
253                         if (umoven(tcp, (unsigned long) ifc.ifc_buf,
254                                 sizeof(ifra), (char *) ifra) < 0) {
255                                 tprintf("%lx}", (unsigned long) ifc.ifc_buf);
256                                 return 1;
257                         }
258                         tprints("{");
259                         for (i = 0; i < nifra; ++i ) {
260                                 if (i > 0)
261                                         tprints(", ");
262                                 tprintf("{\"%s\", {",
263                                         ifra[i].ifr_name);
264                                 if (verbose(tcp)) {
265                                         printxval(addrfams,
266                                                   ifra[i].ifr_addr.sa_family,
267                                                   "AF_???");
268                                         tprints(", ");
269                                         print_addr(tcp, ((long) tcp->u_arg[2]
270                                                          + offsetof(struct ifreq,
271                                                                      ifr_addr.sa_data)
272                                                          + ((char *) &ifra[i]
273                                                             - (char *) &ifra[0])),
274                                                    &ifra[i]);
275                                 } else
276                                         tprints("...");
277                                 tprints("}}");
278                         }
279                         tprints("}");
280                 }
281                 tprints("}");
282                 return 1;
283         default:
284                 return 0;
285         }
286 }
287
288 int
289 sys_socketcall(struct tcb *tcp)
290 {
291         return printargs(tcp);
292 }