summaryrefslogtreecommitdiffstats
path: root/contrib/ofed/libsdp/src/socket.c
blob: d40265b1784598d3e27f6b812e24772ace0b6a7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
  This software is available to you under a choice of one of two
  licenses.  You may choose to be licensed under the terms of the GNU
  General Public License (GPL) Version 2, available at
  <http://www.fsf.org/copyleft/gpl.html>, or the OpenIB.org BSD
  license, available in the LICENSE.TXT file accompanying this
  software.  These details are also available at
  <http://openib.org/license.html>.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.

  Copyright (c) 2004 Topspin Communications.  All rights reserved.
  Copyright (c) 2005 Mellanox Technologies Ltd.  All rights reserved.

  $Id$
*/

/*
 * system includes
 */
#if HAVE_CONFIG_H
#  include <config.h>
#endif /* HAVE_CONFIG_H */

/*
  For non-i386 systems, we pass socket() through to library code using
  dlsym() instead of trying to make the system call directly.  This
  may cause problems if this library is LD_PRELOADed before the real C
  library is available.  Eventually we may want to add the same type
  of system call assembly code as i386 has for IA64 and AMD64, but for
  now....
*/
#ifndef i386
#define _GNU_SOURCE /* Get RTLD_NEXT */
#include <dlfcn.h>
#else
#include <sys/syscall.h>
#endif

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
/*
 * SDP specific includes
 */
#include "linux/sdp_inet.h"

#if 0
#define _SDP_VERBOSE_PRELOAD
#endif

#define SOCKOP_socket           1
#define SOCKOP_bind             2
#define SOCKOP_connect          3
#define SOCKOP_listen           4
#define SOCKOP_accept           5
#define SOCKOP_getsockname      6
#define SOCKOP_getpeername      7
#define SOCKOP_socketpair       8
#define SOCKOP_send             9
#define SOCKOP_recv             10
#define SOCKOP_sendto           11
#define SOCKOP_recvfrom         12
#define SOCKOP_shutdown         13
#define SOCKOP_setsockopt       14
#define SOCKOP_getsockopt       15
#define SOCKOP_sendmsg          16
#define SOCKOP_recvmsg          17

extern char * program_invocation_name;
extern char * program_invocation_short_name;
extern char ** const environ;

/* ========================================================================= */
/*..socket -- replacment socket call. */
int socket
(
 int domain,
 int type,
 int protocol
)
{
#ifdef i386
  long  __ret;
  void *__scratch;
  int   call[3];
#endif
  char *test;
  char *inet;
  char **tenviron;

#ifdef _SDP_VERBOSE_PRELOAD
  FILE *fd;
#endif
  /*
   * check for magic enviroment variable
   */
  if ((AF_INET == domain || AF_INET6 == domain) &&
      SOCK_STREAM == type) {

    if (environ) {
      tenviron = environ;
      for (test = *tenviron; NULL != test; test = *++tenviron) {

        inet = AF_INET_STR;

        while (*inet == *test && '\0' != *inet) {

          inet++;
	  test++;
        } /* while */

        if ('\0' == *inet && '=' == *test) {

          domain = AF_INET_SDP;
          break;
        } /* if */
      } /* for */
    } /* if */
  } /* if */

#ifdef _SDP_VERBOSE_PRELOAD
  fd = fopen("/tmp/libsdp.log.txt", "a+");

  fprintf(fd, "SOCKET: <%s> domain <%d> type <%d> protocol <%d>\n",
	  program_invocation_short_name, domain, type, protocol);

  fclose(fd);
#endif

#ifdef i386
  /* Make the socket() system call directly, as described above */
  call[0] = domain;
  call[1] = type;
  call[2] = protocol;

  __asm__ __volatile__("movl %%ebx, %1\n" /* save %ebx */
                       "movl %3, %%ebx\n" /* put sockopt in %ebx as arg */
                       "int $0x80\n"      /* do syscall */
                       "movl %1, %%ebx\n" /* restore %ebx */
                       : "=a" (__ret), "=r" (__scratch)
                       : "0" (__NR_socketcall),
		       "g" (SOCKOP_socket),
		       "c" (call));
  return __ret;
#else /* i386 */
  /* Use the standard library socket() to pass through the call */
  {
    static int (*orig_socket)(int, int, int);

    if (!orig_socket) {
      orig_socket = dlsym(RTLD_NEXT, "socket");
    }

    return orig_socket(domain, type, protocol);
  }
#endif /* i386 */
} /* socket */
OpenPOWER on IntegriCloud