dbgif_requests.c
4.48 KB
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
/*
* ==========================================================================
* Copyright (c) 1993, Silicon Graphics, Inc. All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied
* or duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished
* rights reserved under the Copyright Laws of the United States.
* ==========================================================================
* Module : request handler and dispatcher
*
* Author : First Last (login)
* Owner : First Last (login)
* Reviewer : First Last (login)
*
* $Source: /root/leakn64/depot/rf/sw/bbplayer/debugger/dbgif/dbgif_requests.c,v $
* $Revision: 1.3 $
* $Date: 2002/10/29 08:22:51 $
* ==========================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#ifdef __sgi__
#include <bstring.h>
#endif
#include "rkfsn.h"
#include "dbgproto.h"
#include "dbgif.h"
#include "dbgif_services.h"
#define DEFAULT_PORT 5777 /* debugger connects here */
static char *buf = 0;
static int bufsize = 0;
void stError (const char *str)
{
perror (str);
exit (0);
}
KKHeader *stReadRequestPacket (int sock)
{
int nbytes, size;
KKHeader *request;
if (buf == 0) {
buf = (char *) malloc (BUFSIZE);
bufsize = BUFSIZE;
}
nbytes = recv (sock, buf, sizeof(KKHeader), MSG_PEEK);
if (nbytes == 0) /* close down client */
return (NULL);
else if (nbytes != sizeof (KKHeader)) { /* incomplete packet */
printf("nbytes %d\n", nbytes);
nbytes = recv (sock, buf, sizeof(KKHeader), MSG_PEEK);
if (nbytes != sizeof (KKHeader))
stError ("packet header read call failed");
}
request = (KKHeader *) buf;
if (request->length > bufsize) {
bufsize = (request->length % BUFSIZE) * BUFSIZE + BUFSIZE;
buf = (char *) realloc (buf, bufsize);
}
/* read the entire packet */
nbytes = recv (sock, buf, request->length, 0);
if (nbytes != request->length)
stError ("Unable to read request packet");
if (request->rev != TV_DBGPROTO_REV)
printf("dbgif readrequest: version mismatch expected %d, got %d\n",
TV_DBGPROTO_REV, request->rev);
return (request);
}
int stDispatchRequest (KKHeader* request)
{
if (request->code < _KK_REQ_COUNT)
(*ClientRequestDispatchTable[request->code])(request);
else
stStray(request);
return (0);
}
int stOpenTCPconnection (int port)
{
int sock;
struct sockaddr_in sockaddr_in;
struct hostent *hostent;
char hostname[MAXHOSTNAMELEN + 1];
int optval = 1;
int optlen;
/*
* create a nameless stream socket in internet domain using
* default TCP protocol
*/
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
stError ("Socket call failed");
if (gethostname (hostname, MAXHOSTNAMELEN) == -1)
stError ("gethostname call failed");
if ((hostent = gethostbyname(hostname)) == NULL)
stError ("gethostbyname call failed");
setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
setsockopt(sock, SOL_SOCKET, SO_LINGER, &optval, sizeof (optval));
#if 0
optlen = sizeof(optval);
getsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &optval, &optlen);
printf("settop opt reuseaddr %d len %d\n", optval, optlen);
optlen = sizeof(optval);
getsockopt (sock, SOL_SOCKET, SO_LINGER, &optval, &optlen);
printf("settop opt linger %d len %d\n", optval, optlen);
#endif
bzero ((char *) &sockaddr_in, sizeof(struct sockaddr_in));
bcopy (hostent->h_addr, (char *)&sockaddr_in.sin_addr, hostent->h_length);
sockaddr_in.sin_family = AF_INET;
sockaddr_in.sin_port = port;
/* bind the name to the socket */
if (bind (sock, (struct sockaddr *) &sockaddr_in,
sizeof(struct sockaddr_in)) == -1)
stError ("bind call failed");
/* listen on the socket */
if (listen (sock, MAX_BACKLOG) == -1)
stError ("listen call failed");
return (sock);
}