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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
// portobj.m--This creates a NSPort object that can be used in a
// NSConnection. Since we are using Unix/Internet stream sockets
// and not Mach sockets, most methods need to be overwritten.
// based on /NextDeveloper/Examples/Foundation/TCPTransport/TCPPort.m
// 1999-Feb-22 - [AFT]
#import "portobj.h"
#import <sys/types.h>
#import <sys/socket.h>
#import <netinet/in.h>
#import <netdb.h>
#define INVALID_SOCKET -1
#if !defined(__svr4__) && !defined(WIN32)
#import <libc.h>
#endif
@implementation portobj
//
//--- Useful function
//
void grreply(int peeraddr, short iport, void *reply, int lreply)
{
struct sockaddr_in repadd;
int repsock;
// Create reply socket descriptor
repsock = socket(AF_INET, SOCK_STREAM, 0);
// Create the reply address structure
memset((char *)&repadd, 0, sizeof(struct sockaddr_in));
repadd.sin_family = AF_INET;
repadd.sin_port=iport;
repadd.sin_addr.s_addr = peeraddr;
if (connect(repsock, (struct sockaddr *)&repadd, sizeof(repadd)) < 0) {
perror("connecting reply socket");
} else {
if (write(repsock, reply, lreply) < 0)
perror("writing on stream socket");
}
close(repsock);
return;
}
//
//--- Class methods -----------------------------------------------------
//
- (portobj *)initport:(unsigned short)ipn target:(dispatchobj *)adispatch
{
NSHost *myhost;
struct sockaddr_in addr;
struct hostent *ahost;
char *ctmp;
int fd;
int itmp, ltmp;
// Create socket descriptor
fd = socket(PF_INET, SOCK_STREAM, 0);
if (fd == INVALID_SOCKET) {
NSLog(@"socket call failed");
return nil;
}
// Create the address structure
ltmp = sizeof(struct sockaddr_in);
memset((char *)&addr, 0, ltmp);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(ipn);
// Bind address structure to socket descriptor
if (bind(fd, (struct sockaddr *)&addr, ltmp) < 0) {
NSLog(@"bind failed");
close(fd);
return nil;
}
// Passively listen for connections.
listen(fd, 5);
// Create an NSFileHandle object to do the waiting
portFile = [[NSFileHandle alloc] initWithFileDescriptor:fd];
mydispatch = adispatch;
// This tells portFile to alert us for connection attempts.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(acceptNotification:)
name:NSFileHandleConnectionAcceptedNotification
object:portFile];
// Create the hostobj to manage the list of hosts that are allowed
// to connect to us.
hostlist=[hostobj new];
[hostlist initwithdispatch:self];
// Add "localhost" to list.
ctmp="localhost";
ahost = gethostbyname(ctmp);
if (ahost == 0) {
fprintf(stderr, "%s: unknown host", ctmp);
} else {
memcpy(&itmp, ahost->h_addr, 4);
[hostlist addhost:itmp];
}
// Get the actual IP address of local machine and add.
myhost = [NSHost currentHost];
if ( myhost != nil ) {
itmp = inet_addr([[myhost address] cString]);
[hostlist addhost:itmp];
}
return self;
}
- (NSMenu *) gethostmenu
{
return [hostlist gethostmenu];
}
- (void) removehost:(id) sender
{
[hostlist removehost:sender];
return;
}
//
//--- methods needed by runloop -----------------------------------------
//
- (void)addConnection:(NSConnection *)conn
toRunLoop:(NSRunLoop *)runLoop
forMode:(NSString *)mode
{
portobj *listener = [conn receivePort];
portobj *sender = [conn sendPort];
[listener->portFile acceptConnectionInBackgroundAndNotify];
if (listener != sender) {
[sender->portFile readInBackgroundAndNotify];
}
return;
}
- (void)removeConnection:(NSConnection *)conn
fromRunLoop:(NSRunLoop *)runLoop
forMode:(NSString *)mode {
// Since addConnection allocates no objects, we have none to release.
}
- (void)acceptNotification:(NSNotification *)note {
NSFileHandle *msgFile;
NSConnection *connection;
portobj *sp;
struct sockaddr_in peeradd;
int msgsock, itmp;
// Get the file handle info for the incoming messages
msgFile = [[note userInfo]
objectForKey:NSFileHandleNotificationFileHandleItem];
// We now have all data from the Notification. Allow another connection.
[portFile acceptConnectionInBackgroundAndNotify];
if (!msgFile) {
NSLog(@"** no socket in notification info %@", [note userInfo]);
return;
}
// Find out who is trying to connect with us.
msgsock=[msgFile fileDescriptor];
itmp = sizeof(peeradd);
if (getpeername(msgsock, (struct sockaddr *)&peeradd, &itmp)) {
perror("getting socket name");
}
if ( ![hostlist queryhost:peeradd.sin_addr.s_addr] ) {
// User rejected the connection, close the file handle.
[msgFile closeFile];
return;
}
// Create a port to receive the messages. This will be a "sendPort".
sp = [isa alloc];
if (!sp) {
return;
}
// This tells the msgFile handle to stick around and saves the pointer
// in our local portFile variable.
sp->portFile = [msgFile retain];
sp->mydispatch = mydispatch;
sp->mypeer=peeradd.sin_addr.s_addr;
sp->istate = 0;
[sp autorelease];
[[NSNotificationCenter defaultCenter]
addObserver:sp
selector:@selector(readNotification:)
name:NSFileHandleReadCompletionNotification
object:msgFile];
connection = [[NSConnection alloc] initWithReceivePort:self sendPort:sp];
return;
}
- (void) sendoff {
// We have now read the function code, length, and data. Dispatch.
short iport;
float r1, r2;
int ibuf[4], itmp[4];
int i;
switch (ifunc) {
case 1:
memcpy(&iport, cbuf, 2);
[mydispatch getwind:&itmp[0] by:&itmp[1]
color:&itmp[3] scale:&itmp[2]];
for (i=0; i<4; i++) {
ibuf[i]=htonl(itmp[i]);
}
grreply(mypeer, iport, &ibuf, 16);
break;
case 2:
[mydispatch beginp];
break;
case 3:
[mydispatch pscode:cbuf];
break;
case 4:
memcpy(&iport, cbuf, 2);
[mydispatch cursorat:&r1 and:&r2 char:&itmp[2]];
ibuf[0] = htonl( (int) r1 );
ibuf[1] = htonl( (int) r2 );
ibuf[2] = htonl( itmp[2] );
grreply(mypeer, iport, &ibuf, 12);
break;
case 5:
[mydispatch flushpg];
break;
case 6:
[mydispatch endp];
break;
default:
printf("portobj--Unknown function code= %d\n",ifunc);
break;
} /* end switch (ifunc) */
}
- (void)readNotification:(NSNotification *)note {
NSData *data;
NSRange substring;
int lentot, ioff;
unsigned char c1buf;
data = [[note userInfo] objectForKey:NSFileHandleNotificationDataItem];
lentot=[data length];
if ( lentot<0 ) {
perror("reading stream message");
return;
} else if (lentot == 0) {
// Nothing more to read. Close file handle and exit.
[portFile closeFile];
return;
}
// Set the next read going.
[portFile readInBackgroundAndNotify];
ioff=0;
do {
switch (istate) {
case 0:
// Read function code
nread = 0;
substring.location=ioff;
substring.length=1;
[data getBytes:&c1buf range:substring];
ioff=ioff+substring.length;
ifunc=c1buf;
istate = 1;
break;
case 1:
// Read length
substring.location=ioff;
substring.length=1;
[data getBytes:&c1buf range:substring];
ioff=ioff+substring.length;
ilen=c1buf;
if ( ilen==0 ) {
// No data package to read, dispatch now.
istate = 0;
//printf("ifunc=%i, ilen=%i\n",ifunc,ilen);
[self sendoff];
} else {
// Next read the data.
istate = 2;
}
break;
case 2:
// Read data. There are two input cases, either cbuf is empty or
// it contains part of a previous read. Both cases are handled
// by starting cbuf at position nread. Likewise there are two output
// cases, either we finish the read or we don't.
substring.location=ioff;
if ( ilen-nread <= lentot-ioff ) {
// Can finish the read. State will change.
substring.length = ilen-nread;
istate = 0;
} else {
// Incomplete read, so no state change.
substring.length = lentot-ioff;
}
[data getBytes:&cbuf[nread] range:substring];
ioff=ioff+substring.length;
nread = nread+substring.length;
if ( istate==0 ) {
//printf("ifunc=%i, ilen=%i\n",ifunc,ilen);
[self sendoff];
}
} /* end switch (state) */
} while ( ioff<lentot );
return;
}
//- (NSString *)description {
// return [NSString stringWithFormat:@"<portobj portFile %d>", [portFile fileDescriptor]];
//}
@end
|