|
|
1 /* 2 * Example NetBIOS client 3 */4 #include <stdio.h> 5 #include <fcntl.h> 6 #include <xti.h> 7 #include <sys/nb/nbtpi.h>
8 #define SRV_NAME "NB-XTI-DEMO-SERV" 9 #define RFCNBPATH "/dev/nbcots" 10 #define NBEPATH "/dev/netbeui"
11 extern int t_errno;
12 main (int argc, char *argv[]) 13 { 14 int rval; 15 int flags = 0; 16 char *netpath = RFCNBPATH; 17 int fd; 18 int nbytes; 19 struct t_call *sndcall; 20 struct nbaddr srv_addr; 21 struct { 22 unsigned short length; 23 char buf[1024]; 24 } msg;
25 if ((fd = t_open(netpath, O_RDWR, (struct t_info *) NULL)) < 0) { 26 t_error("t_open failed"); 27 exit(1); 28 }
29 if (t_bind(fd, (struct t_bind *) NULL, 30 (struct t_bind *) NULL) < 0) { 31 t_error("t_bind error"); 32 exit(2); 33 }
34 if ((sndcall = 35 (struct t_call *)t_alloc(fd, T_CALL, T_ADDR)) == NULL) { 36 t_error("t_alloc failed"); 37 exit(3); 38 }
39 sndcall->addr.len = sizeof(srv_addr); 40 sndcall->addr.buf = (char *)&srv_addr; 41 srv_addr.nb_type = NB_UNIQUE; 42 memcpy(srv_addr.nb_name, SRV_NAME, NB_NAMELEN);
43 if (t_connect(fd, sndcall, (struct t_call *) NULL) < 0) { 44 t_error("t_connect failed for fd"); 45 rval = t_look(fd); 46 if(rval) 47 fprintf(stderr, "t_look returned event %d\n", rval); 48 exit(4); 49 }
50 while ((nbytes = t_rcv(fd, (char *)&msg, sizeof(msg), &flags)) >= 51 sizeof(msg.length)) { 52 if (fwrite(msg.buf, 1, msg.length, stdout) < 0) { 53 fprintf(stderr, "fwrite failed"); 54 exit(5); 55 } 56 if (msg.length == 0) 57 break; 58 }
59 if (nbytes == sizeof(msg.length) && msg.length == 0) { 60 if (t_snddis(fd, NULL) < 0) { 61 t_error("t_snddis failed"); 62 exit(6); 63 } 64 exit(0); 65 }
66 if (nbytes < sizeof(msg.length)) { 67 fprintf(stderr, "received invalid message, hanging up"); 68 t_snddis(fd, (struct t_call *) NULL); 69 exit(7); 70 } 71 else { 72 if (t_errno == TLOOK) { 73 if (t_look(fd) == T_DISCONNECT) { 74 printf("Got T_DISCONNECT, doing t_rcvdis"); 75 if (t_rcvdis(fd, (struct t_discon *) NULL) < 0) { 76 t_error("t_rcvdis failed"); 77 exit(8); 78 } 79 exit(0); 80 } 81 } 82 else { 83 t_error("t_rcv failed"); 84 exit(9); 85 } 86 } 87 }
t_open(netpath, O_NONBLOCK | O_RDWR, (struct t_info *)NULL)