[BACK]Return to sm1Socket.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / plugin

Annotation of OpenXM/src/kan96xx/plugin/sm1Socket.c, Revision 1.22

1.22    ! takayama    1: /* $OpenXM: OpenXM/src/kan96xx/plugin/sm1Socket.c,v 1.21 2015/10/10 11:29:46 takayama Exp $ */
1.1       maekawa     2: /* msg0s.c */
                      3: #include <stdio.h>
1.20      ohara       4: #include <string.h>
1.1       maekawa     5: #include <sys/types.h>
                      6: #include <sys/socket.h>
                      7: #include <sys/time.h>
                      8: #include <netinet/in.h>
                      9: #include <netdb.h>
1.17      takayama   10: #include <errno.h>
1.21      takayama   11: #include <stdlib.h>
1.22    ! takayama   12: #include <unistd.h>
1.1       maekawa    13:
                     14: #include "datatype.h"
                     15: #include "stackm.h"
                     16: #include "extern.h"
                     17: #include "sm1Socket.h"
                     18:
                     19: extern int Quiet;
1.14      takayama   20: static int Post_debug=0;
1.1       maekawa    21: static void errorMsg1s(char *s);
1.8       takayama   22: static int getContentLength(char *s);
                     23: static int getReceivedContentLength(char *s);
1.6       takayama   24: #define MAX_LISTEN_QUEUE 3
1.1       maekawa    25:
                     26: /* [(sm1.socket) (open) [optional integer port, optional string name] ] extension ; */
                     27: struct object KsocketOpen(struct object obj) {
                     28:   char serverName[1024];
                     29:   int portNumber;
                     30:   struct object rob = NullObject;
                     31:   struct hostent *myhost;
                     32:   struct sockaddr_in me;
                     33:   int s_waiting;
                     34:   int on;
                     35:   int tt;
1.14      takayama   36:   extern int Post_debug;
                     37:
                     38:   if ((char *)getenv("OXWEB_DEBUG") != NULL) {
                     39:        Post_debug = 1;
                     40:   }
1.1       maekawa    41:
                     42:   if (obj.tag != Sarray) {
                     43:     errorMsg1s("KsocketOpen([optional integer,optional string name])");
                     44:   }
                     45:   strcpy(serverName,"localhost");
                     46:   portNumber = 0;
                     47:   if (getoaSize(obj) >= 1) {
                     48:     if ((getoa(obj,0)).tag != Sinteger) {
                     49:       errorMsg1s("KsocketOpen([optional integer,optional string name]), the first argument must be an integer.");
                     50:     }
                     51:     portNumber = KopInteger(getoa(obj,0));
                     52:   }
                     53:   if (getoaSize(obj) >= 2) {
                     54:     if ((getoa(obj,1)).tag != Sdollar) {
                     55:       errorMsg1s("KsocketOpen([optional integer,optional string name]), the second argument must be a string.");
                     56:     }
                     57:     if (strlen(KopString(getoa(obj,1))) > 1023) {
                     58:       errorMsg1s("Too long server name");
                     59:     }
                     60:     strcpy(serverName,KopString(getoa(obj,1)));
                     61:   }
                     62:
                     63:
                     64:   /* fprintf(stderr,"Hello from open.\n"); */
                     65:   if ((myhost = gethostbyname(serverName)) == NULL) {
1.18      takayama   66:     perror("gethostbyname"); errorMsg1s("Bad server name.");
1.1       maekawa    67:   }
                     68:   bzero((char *)&me,sizeof(me));
                     69:   me.sin_family = AF_INET;
                     70:   me.sin_port = htons(portNumber);
                     71:   bcopy(myhost->h_addr,
1.3       takayama   72:         &me.sin_addr,myhost->h_length);
1.1       maekawa    73:
                     74:   if ((s_waiting = socket(AF_INET,SOCK_STREAM,0)) < 0) {
1.18      takayama   75:     perror("socket"); errorMsg1s("Socket allocation is failed.");
1.1       maekawa    76:   }
                     77:
                     78:   on=1; setsockopt(s_waiting,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
                     79:   /* important */
                     80:   if (bind(s_waiting,(struct sockaddr *) &me,sizeof(me)) == -1) {
                     81:     fprintf(stderr,"Bind error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
1.18      takayama   82:     perror("bind"); errorMsg1s("cannot bind");
1.1       maekawa    83:   }
                     84:
                     85:   tt = sizeof(me);
                     86:   if (getsockname(s_waiting,(struct sockaddr *)&me,&tt) < 0) {
                     87:     fprintf(stderr,"getsockname error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
1.18      takayama   88:     perror("getsockname"); errorMsg1s("cannot getsockname");
1.1       maekawa    89:   }
                     90:
                     91:
1.6       takayama   92:   if (listen(s_waiting,MAX_LISTEN_QUEUE) < 0) {
1.18      takayama   93:     perror("listen"); errorMsg1s("Listen failed");
1.1       maekawa    94:   }
                     95:   /*
1.3       takayama   96:       fprintf(stderr,"Done the initialization. port =%d\n",ntohs(me.sin_port));
                     97:       fprintf(stderr,"socket id = %d",accept(s_waiting,NULL,NULL)); */
1.1       maekawa    98:   rob = newObjectArray(2);
                     99:   putoa(rob,0,KpoInteger(s_waiting));
                    100:   putoa(rob,1,KpoInteger(ntohs(me.sin_port)));
                    101:   return(rob);
                    102:
                    103: }
                    104:
                    105: /* [ integer port, optional string host default localhost]
                    106:    [ integer socketid, integer port ] */
                    107: struct object KsocketConnect(struct object obj) {
                    108:   char serverName[1024];
                    109:   int portNumber;
                    110:   struct hostent *servhost;
                    111:   struct sockaddr_in server;
                    112:   int socketid;
                    113:   struct object robj = NullObject;
                    114:
                    115:   if (obj.tag != Sarray) {
                    116:     errorMsg1s("KsocketConnect( [ integer port, optional string host default localhost])");
                    117:   }
                    118:   strcpy(serverName,"localhost");
                    119:   if (getoaSize(obj) < 1) {
                    120:     errorMsg1s("You need to specify port number.\n");
                    121:   }
                    122:   if (getoaSize(obj) >= 1) {
                    123:     if ((getoa(obj,0)).tag != Sinteger) {
                    124:       errorMsg1s("KsocketConnect([integer port,optional string host]), the first argument must be an integer.");
                    125:     }
                    126:     portNumber = KopInteger(getoa(obj,0));
                    127:   }
                    128:   if (getoaSize(obj) >= 2) {
                    129:     if ((getoa(obj,1)).tag != Sdollar) {
                    130:       errorMsg1s("KsocketConnect([integer port,optional string host]), the second argument must be a string.");
                    131:     }
                    132:     if (strlen(KopString(getoa(obj,1))) > 1023) {
                    133:       errorMsg1s("Too long server name");
                    134:     }
                    135:     strcpy(serverName,KopString(getoa(obj,1)));
                    136:   }
                    137:
                    138:
                    139:   if ((servhost = gethostbyname(serverName)) == NULL) {
1.18      takayama  140:     perror("gethostbyname"); errorMsg1s("bad server name.\n");
1.1       maekawa   141:   }
                    142:   bzero((char *)&server,sizeof(server));
                    143:   server.sin_family = AF_INET;
                    144:   server.sin_port = htons(portNumber);
                    145:   bcopy(servhost->h_addr,
1.3       takayama  146:         (char *)&server.sin_addr,servhost->h_length);
1.1       maekawa   147:
                    148:   if ((socketid = socket(AF_INET,SOCK_STREAM,0)) <0) {
1.18      takayama  149:     perror("socket"); errorMsg1s("socket allocation is failed.\n");
1.1       maekawa   150:   }
                    151:   if (!Quiet) {
1.22    ! takayama  152:     fprintf(stderr,"Trying to connect port %d, ip=%lx\n",ntohs(server.sin_port),(long) server.sin_addr.s_addr);
1.1       maekawa   153:   }
                    154:   if (connect(socketid,(struct sockaddr *)&server,sizeof(server)) == -1) {
1.18      takayama  155:     perror("connect"); errorMsg1s("cannot connect");
1.1       maekawa   156:   }
                    157:   /* fprintf(stderr,"connected.\n"); */
                    158:   robj = newObjectArray(2);
                    159:   putoa(robj,0,KpoInteger(socketid));
                    160:   putoa(robj,1,KpoInteger(portNumber));
                    161:   return(robj);
                    162: }
                    163:
                    164: /* [ integer socketid ]
                    165:    [ integer newsocketid ] */
                    166: struct object KsocketAccept(struct object obj) {
1.19      takayama  167:   struct object obj1 = OINIT;
                    168:   struct object obj2 = OINIT;
                    169:   struct object robj = OINIT;
1.1       maekawa   170:   int s, news;
                    171:
                    172:   if (obj.tag != Sarray) {
                    173:     errorMsg1s("KsocketAccept([integer socketid])");
                    174:   }
                    175:   if (getoaSize(obj) < 1) {
                    176:     errorMsg1s("KsocketAccept([integer socketid])");
                    177:   }
                    178:   obj1 = getoa(obj,0);
                    179:   if (obj1.tag != Sinteger ) {
                    180:     errorMsg1s("KsocketAccept([integer socketid]), argument must be integer.");
                    181:   }
                    182:   s = KopInteger(obj1);
                    183:   if ((news = accept(s,NULL,NULL)) < 0) {
1.15      takayama  184:     fprintf(stderr,"Error in accept. Retrying (KsocketAccept).\n");
                    185:     /* Code added for strange behavior on cygwin. */
                    186:     if ((news = accept(s,NULL,NULL)) < 0) {
1.18      takayama  187:       perror("accept"); errorMsg1s("Error in accept. Retry failed.");
1.15      takayama  188:     }
1.1       maekawa   189:   }
                    190:   if (close(s) < 0) {
1.18      takayama  191:     perror("close"); errorMsg1s("Error in closing the old socket.");
1.1       maekawa   192:   }
                    193:   robj = newObjectArray(1);
                    194:   putoa(robj,0,KpoInteger(news));
                    195:   return(robj);
                    196: }
                    197:
1.13      takayama  198: /* [ integer socketid ]
                    199:    [ integer newsocketid ] */
                    200: /* It does not close the listening socket. You can call it as
                    201:    ls = open.
                    202:    fd=accept2(ls).  close(fd).
                    203:    fd=accept2(ls).  close(fd).
                    204:    ....
                    205:    */
                    206: struct object KsocketAccept2(struct object obj) {
1.19      takayama  207:   struct object obj1 = OINIT;
                    208:   struct object obj2 = OINIT;
                    209:   struct object robj = OINIT;
1.13      takayama  210:   int s, news;
                    211:
                    212:   if (obj.tag != Sarray) {
                    213:     errorMsg1s("KsocketAccept([integer socketid])");
                    214:   }
                    215:   if (getoaSize(obj) < 1) {
                    216:     errorMsg1s("KsocketAccept([integer socketid])");
                    217:   }
                    218:   obj1 = getoa(obj,0);
                    219:   if (obj1.tag != Sinteger ) {
                    220:     errorMsg1s("KsocketAccept([integer socketid]), argument must be integer.");
                    221:   }
                    222:   s = KopInteger(obj1);
                    223:   if ((news = accept(s,NULL,NULL)) < 0) {
1.15      takayama  224:     fprintf(stderr,"Error in accept. Retrying (KsocketAccept2).\n");
                    225:     /* Code added for strange behavior on cygwin. */
                    226:     if ((news = accept(s,NULL,NULL)) < 0) {
1.18      takayama  227:       perror("accept"); errorMsg1s("Error in accept. Retry failed.");
1.15      takayama  228:     }
1.13      takayama  229:   }
                    230:   if (close(s) < 0) {
1.18      takayama  231:     perror("close"); errorMsg1s("Error in closing the old socket.");
1.13      takayama  232:   }
                    233:   robj = newObjectArray(1);
                    234:   putoa(robj,0,KpoInteger(news));
                    235:   return(robj);
                    236: }
                    237:
1.1       maekawa   238: int KsocketSelect0(int fd,int t) {
                    239:   fd_set readfds;
                    240:   struct timeval timeout;
                    241:   FD_ZERO(&readfds);
                    242:   FD_SET(fd,&readfds);
                    243:   timeout.tv_sec = 0;
                    244:   timeout.tv_usec = (long) t;
                    245:   if (t >= 0) {
                    246:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
                    247:       /* It must be fd+1 !,  Not fd. */
                    248:       fprintf(stderr,"Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
1.18      takayama  249:       perror("select"); errorMsg1s("KsocketSelect0() : select failed.");
1.1       maekawa   250:       return(0);
                    251:     }
                    252:   }else{ /* block */
                    253:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
1.18      takayama  254:       perror("select"); errorMsg1s("KsocketSelect0() : select failed.");
1.1       maekawa   255:       fprintf(stderr,"Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
                    256:       return(0);
                    257:     }
                    258:   }
                    259:   if (FD_ISSET(fd,&readfds)) return(1);
                    260:   else return(0);
                    261: }
                    262:
                    263: /* [ integer socketid optional integer timeout default 0]
                    264:    integer true or false
                    265: */
                    266: struct object KsocketSelect(struct object obj) {
1.19      takayama  267:   struct object robj = OINIT;
                    268:   struct object ob1 = OINIT;
                    269:   struct object ob2 = OINIT;
1.1       maekawa   270:   if (obj.tag != Sarray) {
                    271:     errorMsg1s("KsocketSelect([ integer socketid optional integer timeout default 0]");
                    272:   }
                    273:   if (getoaSize(obj) < 1) {
                    274:     errorMsg1s("KsocketSelect([ integer socketid optional integer timeout default 0]");
                    275:   }
                    276:   if (getoaSize(obj) >= 1) {
                    277:     ob1 = getoa(obj,0);
                    278:     ob2 = KpoInteger(0); /* default value */
                    279:   }
                    280:   if (getoaSize(obj) >= 2) {
                    281:     ob2 = getoa(obj,1);
                    282:   }
                    283:   if (ob1.tag != Sinteger) {
                    284:     errorMsg1s("KsocketSelect([ integer socketid optional integer timeout default 0] : the first argument must be an integer.");
                    285:   }
                    286:   if (ob2.tag != Sinteger) {
                    287:     errorMsg1s("KsocketSelect([ integer socketid optional integer timeout default 0] : the second argument must be an integer.");
                    288:   }
                    289:   robj = KpoInteger( KsocketSelect0(KopInteger(ob1),KopInteger(ob2)) );
                    290:   return(robj);
                    291: }
                    292:
                    293: struct object KsocketSelectMulti(struct object obj)
1.3       takayama  294:      /* [ [integer socketid1, integer socketid2, ...]
1.1       maekawa   295:      optional integer timeout default 0]
                    296:    [ result1, result2, ....]
                    297: */
                    298: {
1.19      takayama  299:   struct object robj = OINIT;
                    300:   struct object ob1 = OINIT;
                    301:   struct object ob2 = OINIT;
                    302:   struct object ob3 = OINIT;
1.1       maekawa   303:   int size,i,fd,p,t;
                    304:   fd_set readfds;
                    305:   struct timeval timeout;
                    306:   if (obj.tag != Sarray) {
                    307:     errorMsg1s("KsocketSelectMulti([[sid1, sid2,...] optional integer timeout default 0]");
                    308:   }
                    309:   if (getoaSize(obj) < 1) {
                    310:     errorMsg1s("KsocketSelectMulti([[sid1, sid2,...] optional integer timeout default 0]");
                    311:   }
                    312:   if (getoaSize(obj) >= 1) {
                    313:     ob1 = getoa(obj,0);
                    314:     ob2 = KpoInteger(0); /* default value */
                    315:   }
                    316:   if (getoaSize(obj) >= 2) {
                    317:     ob2 = getoa(obj,1);
                    318:   }
                    319:   if (ob1.tag != Sarray) {
                    320:     errorMsg1s("KsocketSelectMulti([[sid1, sid2, ...] optional integer timeout default 0] : the first argument must be an array.");
                    321:   }
                    322:   if (ob2.tag != Sinteger) {
                    323:     errorMsg1s("KsocketSelectMulti([[sid1, sid2, ...] optional integer timeout default 0] : the second argument must be an integer.");
                    324:   }
                    325:   FD_ZERO(&readfds);
                    326:   timeout.tv_sec = 0; t =  KopInteger(ob2);
                    327:   timeout.tv_usec = (long)t;
                    328:
                    329:   size = getoaSize(ob1);
                    330:   if (size < 1) {
                    331:     errorMsg1s("KsocketSelectMulti: the first argument must be a non-empty array of integers.");
                    332:   }
                    333:   fd = 0;
                    334:
                    335:   for (i=0; i<size; i++) {
                    336:     ob3 = getoa(ob1,i);
                    337:     if (ob3.tag != Sinteger) {
                    338:       errorMsg1s("KsocketSelectMulti: the first argument must be an array of integers.");
                    339:     }
                    340:     p = KopInteger(ob3);
                    341:     if (p > fd) fd = p;
                    342:     FD_SET(p,&readfds);
                    343:     /* printf("p = %d, fd=%d",p,fd); */
                    344:   }
                    345:
                    346:   if (t >= 0) {
                    347:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
                    348:       /* It must be fd+1 !,  Not fd. */
                    349:       fprintf(stderr,"Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
1.18      takayama  350:       perror("select"); errorMsg1s("KsocketSelectMulti() : select failed.");
1.1       maekawa   351:     }
                    352:   }else{ /* block */
                    353:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
                    354:       fprintf(stderr,"Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
1.18      takayama  355:       perror("select"); errorMsg1s("KsocketSelectMulti() : (block) select failed.");
1.1       maekawa   356:     }
                    357:   }
                    358:   robj = newObjectArray(size);
                    359:   for (i=0; i<size; i++) {
                    360:     if (FD_ISSET(KopInteger(getoa(ob1,i)),&readfds)) {
                    361:       putoa(robj,i,KpoInteger(1));
                    362:     }else{
                    363:       putoa(robj,i,KpoInteger(0));
                    364:     }
                    365:   }
                    366:
                    367:   return(robj);
                    368: }
                    369:
                    370:
                    371:
                    372:
                    373: static char Data00[1024];
                    374: /* [ integer socketid ]
                    375:    string data
                    376: */
                    377: struct object KsocketRead(struct object obj) {
1.19      takayama  378:   struct object ob1 = OINIT;
1.1       maekawa   379:   struct object robj = NullObject;
                    380:   static int datasize = 1024;
                    381:   static char *data = Data00;
                    382:   char *tmp;
                    383:   char *r;
                    384:   int n;
                    385:   int socketid;
                    386:   int totalsize;
                    387:
                    388:   if (obj.tag != Sarray) {
                    389:     errorMsg1s("KsocketRead([integer socketid])");
                    390:   }
                    391:   if (getoaSize(obj) < 1) {
                    392:     errorMsg1s("KsocketRead([integer socketid])");
                    393:   }
                    394:   ob1 = getoa(obj,0);
                    395:   if (ob1.tag != Sinteger) {
                    396:     errorMsg1s("KsocketRead([integer socketid]) : the argument must be integer.");
                    397:   }
                    398:   socketid = KopInteger(ob1);
                    399:
                    400:   totalsize = 0;
                    401:
                    402:   while (KsocketSelect0(socketid,0)) {
                    403:     if (datasize - totalsize > 0) {
                    404:       n = read(socketid,data+totalsize,datasize-totalsize);
                    405:       if (n < 0) {
1.18      takayama  406:                perror("read");
1.3       takayama  407:         errorMsg1s("Read error.");
1.1       maekawa   408:       }
                    409:       if (n < datasize-totalsize) {
1.3       takayama  410:         totalsize += n;
                    411:         break;
1.1       maekawa   412:       }else{ totalsize += n; }
                    413:       if (totalsize == 0) {
1.3       takayama  414:         errorMsg1s("Select returns 1, but there is no data to read.");
1.1       maekawa   415:       }
                    416:     }else { /* increase the datasize */
1.16      takayama  417:       tmp = (char *)sGC_malloc(sizeof(char)*2*datasize);
                    418:       /*I should use sGC_malloc_atomic and free after finishing this function?*/
1.1       maekawa   419:       if (tmp == (char *)NULL) errorMsg1s("Out of Memory.");
                    420:       bcopy(data,tmp,totalsize);
                    421:       data = tmp;
                    422:       datasize = 2*datasize;
                    423:     }
                    424:   }
                    425:
1.16      takayama  426:   r = (char *)sGC_malloc(sizeof(char)*(totalsize+1));
1.1       maekawa   427:   if (r == (char *)NULL) errorMsg1s("Out of Memory.");
                    428:   bcopy(data,r,totalsize);
1.5       takayama  429:   r[totalsize] = 0;
                    430:   robj = KpoString(r);  /* BUG: it works only for reading string from TCP/IP
                    431:                                                    stream. */
1.1       maekawa   432:
                    433:   return(robj);
                    434:
                    435: }
                    436:
                    437: /* [ integer socketid, string data ]
                    438:    integer  */
                    439: struct object KsocketWrite(struct object obj) {
1.19      takayama  440:   struct object ob1 = OINIT;
                    441:   struct object ob2 = OINIT;
1.1       maekawa   442:   int socketid;
                    443:   int r;
1.18      takayama  444:   int k,k0;
                    445:   char *s;
1.1       maekawa   446:   if (obj.tag != Sarray) {
                    447:     errorMsg1s("KsocketWrite([integer socketid, string data])");
                    448:   }
                    449:   if (getoaSize(obj) < 2) {
                    450:     errorMsg1s("KsocketWrite([integer socketid, string data])");
                    451:   }
                    452:   ob1 = getoa(obj,0);
                    453:   ob2 = getoa(obj,1);
                    454:   if (ob1.tag != Sinteger) {
                    455:     errorMsg1s("KsocketWrite([integer socketid, string data]) : the first argument must be an integer.");
                    456:   }
                    457:   socketid = KopInteger(ob1);
                    458:   if (ob2.tag != Sdollar) {
                    459:     errorMsg1s("KsocketWrite([integer socketid, string data]) : the second argument must be a string.");
                    460:   }
1.18      takayama  461:   s = KopString(ob2);
                    462:   k0 = k = strlen(s);
                    463:   while (1) {
                    464:     r = write(socketid,s,k);
                    465:     if (r < 0) {
                    466:       perror("write"); errorMsg1s("KsocketWrite: write failed.");
                    467:     }
                    468:     if (r >= k) break;
                    469:     k -= r;
                    470:     s = &(s[r]);
                    471:   }
                    472:   return(KpoInteger(k0));
1.1       maekawa   473:
                    474: }
                    475: struct object KsocketClose(struct object obj) {
                    476:   int socketid;
1.19      takayama  477:   struct object ob1 = OINIT;
1.1       maekawa   478:   if (obj.tag != Sarray) {
                    479:     errorMsg1s("KsocketClose([ integer socketid ])");
                    480:   }
                    481:   if (getoaSize(obj) != 1) {
                    482:     errorMsg1s("KsocketClose([ integer socketid ])");
                    483:   }
                    484:   ob1 = getoa(obj,0);
                    485:   if (ob1.tag != Sinteger) {
                    486:     errorMsg1s("KsocketClose([ INTEGER socketid ])");
                    487:   }
                    488:   socketid = KopInteger(ob1);
                    489:   return(KpoInteger(close(socketid)));
                    490: }
                    491:
                    492:
                    493: static void errorMsg1s(char *s) {
                    494:   fprintf(stderr,"%s\n",s);
                    495:   errorKan1("msg1s.c: %s\n",s);
                    496: }
                    497:
                    498:
                    499: /**************** new 1997, 11/23 *******************/
                    500: struct object KsocketReadByte(struct object obj);
                    501: struct object KsocketWriteByte(struct object obj);
                    502:
                    503: struct object KsocketReadByte(struct object obj) {
1.19      takayama  504:   struct object ob1 = OINIT;
1.1       maekawa   505:   struct object robj = NullObject;
                    506:   char data[2];
                    507:   char *tmp;
                    508:   char *r;
                    509:   int n;
                    510:   int socketid;
                    511:
                    512:
                    513:   if (obj.tag != Sarray) {
                    514:     errorMsg1s("KsocketReadByte([integer socketid])");
                    515:   }
                    516:   if (getoaSize(obj) < 1) {
                    517:     errorMsg1s("KsocketReadByte([integer socketid])");
                    518:   }
                    519:   ob1 = getoa(obj,0);
                    520:   if (ob1.tag != Sinteger) {
                    521:     errorMsg1s("KsocketReadByte([integer socketid]) : the argument must be integer.");
                    522:   }
                    523:   socketid = KopInteger(ob1);
                    524:
                    525:
                    526:   n = read(socketid,data,1);
                    527:   if (n < 0) {
1.18      takayama  528:        perror("read");
1.1       maekawa   529:     errorMsg1s("Read error.");
                    530:     robj = KpoInteger(-1);
                    531:     return(robj);
                    532:   }
                    533:   if (n == 0) {
                    534:     errorMsg1s("Read returned without data.");
                    535:   }
                    536:
                    537:   robj = KpoInteger((int)((unsigned char)data[0]));
                    538:   return(robj);
                    539:
                    540: }
                    541:
                    542: /* [ integer socketid, int ]
                    543:    integer  */
                    544: struct object KsocketWriteByte(struct object obj) {
1.19      takayama  545:   struct object ob1 = OINIT;
                    546:   struct object ob2 = OINIT;
1.1       maekawa   547:   int socketid;
1.4       takayama  548:   int r,i,n,kk,r0;
                    549: #define DATA_SIZE 1024
                    550:   char data[DATA_SIZE];
1.1       maekawa   551:   if (obj.tag != Sarray) {
1.4       takayama  552:     errorMsg1s("KsocketWriteByte([integer socketid, int | array of int])");
1.1       maekawa   553:   }
                    554:   if (getoaSize(obj) < 2) {
1.4       takayama  555:     errorMsg1s("KsocketWriteByte([integer socketid, int | array of int])");
1.1       maekawa   556:   }
                    557:   ob1 = getoa(obj,0);
                    558:   ob2 = getoa(obj,1);
                    559:   if (ob1.tag != Sinteger) {
1.4       takayama  560:     errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : the first argument must be an integer.");
1.1       maekawa   561:   }
                    562:   socketid = KopInteger(ob1);
1.4       takayama  563:   if (ob2.tag != Sinteger && ob2.tag != Sarray) {
                    564:     errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : the second argument must be an integer or an array of integers.");
                    565:   }
                    566:   if (ob2.tag == Sinteger) {
                    567:     data[0] = KopInteger(ob2);
                    568:     r = write(socketid,data, 1);
1.18      takayama  569:     if (r < 0) {
                    570:          perror("write"); errorMsg1s("KsocketWriteByte: write error");
                    571:     }
1.4       takayama  572:   }else{
                    573:     n = getoaSize(ob2); kk = 0; r = 0;
                    574:     for (i=0; i<n; i++) {
                    575:       if (getoa(ob2,i).tag != Sinteger)
1.18      takayama  576:         errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : elements of the second argument must be integers.");
1.4       takayama  577:       data[kk] = KopInteger(getoa(ob2,i));
                    578:       kk++;
                    579:       if (kk >= DATA_SIZE) {
1.18      takayama  580:         r0 = write(socketid,data,kk);
                    581:         if (r0 < 0) { perror("write"); errorMsg1s("write failed."); }
                    582:         if (r0 != kk) {
                    583:           fprintf(stderr,"Warning: Could not write to the socket.\n");
                    584:           return(KpoInteger(r+r0));  /* bug: we should retry. */
                    585:         }
1.4       takayama  586:         r += r0;
1.18      takayama  587:         kk = 0;
1.4       takayama  588:       }
                    589:     }
                    590:     if (kk > 0) {
                    591:       r0 = write(socketid,data,kk);
1.18      takayama  592:       if (r0 < 0) { perror("write"); errorMsg1s("write failed."); }
1.4       takayama  593:       if (r0 != kk) {
1.18      takayama  594:         fprintf(stderr,"Warning: Could not write to the socket.\n");
                    595:         return(KpoInteger(r+r0));
1.4       takayama  596:       }
                    597:       r += r0;
                    598:     }
1.1       maekawa   599:   }
                    600:   return(KpoInteger(r));
                    601: }
                    602:
1.7       takayama  603: struct object KsocketReadHTTP(struct object socketObj) {
                    604:   /* Read until two empty line appears. */
1.19      takayama  605:   struct object ob = OINIT;
                    606:   struct object ob1 = OINIT;
                    607:   struct object nob = OINIT;
1.5       takayama  608:   char *s;
                    609:   char *sss;
                    610:   char *tmp;
                    611:   int i;
                    612:   int flag;
1.7       takayama  613:   int flagmax = 1;
1.5       takayama  614:   int datasize;
1.7       takayama  615:   int last;
1.8       takayama  616:   int contentLength=-1;
1.13      takayama  617:   int socketid;
1.14      takayama  618:   extern int Post_debug;
1.13      takayama  619:   nob = NullObject;
                    620:
                    621:   if (socketObj.tag != Sarray) {
                    622:     errorMsg1s("KsocketReadHTTP([integer socketid])");
                    623:   }
                    624:   if (getoaSize(socketObj) < 1) {
                    625:     errorMsg1s("KsocketReadHTTP([integer socketid])");
                    626:   }
                    627:   ob1 = getoa(socketObj,0);
                    628:   if (ob1.tag != Sinteger) {
                    629:     errorMsg1s("KsocketReadHTTP([integer socketid]) : the argument must be integer.");
                    630:   }
                    631:   socketid = KopInteger(ob1);
                    632:
                    633:   if (KsocketSelect0(socketid,-1) != 1) {
                    634:        return(nob);
                    635:   }
1.5       takayama  636:   ob = KsocketRead(socketObj);
                    637:   s = KopString(ob);
1.8       takayama  638:   if (strncmp(s,"POST",4) == 0) flagmax=2; /* for IE */
1.7       takayama  639:   else flagmax=1;
1.5       takayama  640:   flag = 0;
                    641:   for (i=strlen(s)-1; i>=0; i--) {
                    642:        if ((s[i] == '\n') && (i==0)) {
1.7       takayama  643:          ++flag;
1.5       takayama  644:        }else if ((s[i] == '\n') && (s[i-1] == '\n')) {
1.7       takayama  645:          ++flag;
1.5       takayama  646:        }else if ((s[i] == 0xd) && (s[i+1] == 0xa) && (i == 0)) {
1.7       takayama  647:          ++flag;
1.5       takayama  648:        }else if ((s[i] == 0xa) && (s[i-1] == 0xd) && (s[i+1] == 0xd) && (s[i+2] == 0xa)) {
1.7       takayama  649:          ++flag;
1.5       takayama  650:        }
                    651:   }
1.7       takayama  652:   if (flag >= flagmax) return ob;
1.5       takayama  653:   datasize = strlen(s);
                    654:   sss = s;
1.7       takayama  655:   if ((s[strlen(s)-1] == '\n') ||
                    656:       (s[strlen(s)-2] == 0xd) && (s[strlen(s)-1] == 0xa)) {
                    657:        last = 1;
                    658:   }else last = 0;
1.5       takayama  659:
1.7       takayama  660:   while (flag < flagmax) {
1.8       takayama  661:        contentLength = getContentLength(sss);
                    662:        if (contentLength != -1) {
                    663:          if (contentLength <= getReceivedContentLength(sss)) {
                    664:                break;
                    665:          }
                    666:        }
1.14      takayama  667:        if (Post_debug) {
                    668:          fprintf(stderr,"Waiting in socketReadBlock. flagmax(0d,0a)=%d, content-length=%d, received content-length=%d\n",flagmax,contentLength,getReceivedContentLength(sss));
                    669:        }
1.13      takayama  670:        if (strlen(s) == 0) {
                    671:          fprintf(stderr,"No data. Perhaps connection is closed by foreign host.\n");
                    672:          return nob;
1.5       takayama  673:        }else{
                    674:          /* for debugging. */
1.14      takayama  675:          if (Post_debug) {
                    676:                for (i=0; i<strlen(sss); i++) {
                    677:                  if ((sss[i] >= ' ') && (sss[i] < 0x7f)) {
                    678:                        fprintf(stderr,"%c",sss[i]);
                    679:                  }else{
                    680:                        fprintf(stderr,"(%3x)",sss[i]);
                    681:                        if (sss[i] == 0xa) fprintf(stderr,"\n");
                    682:                  }
                    683:
1.8       takayama  684:                }
1.14      takayama  685:                fprintf(stderr,"\n");
1.5       takayama  686:          }
                    687:        }
1.13      takayama  688:
                    689:     if (KsocketSelect0(socketid,-1) != 1) {
                    690:          return nob;
                    691:        }
1.5       takayama  692:        ob = KsocketRead(socketObj);
                    693:        s = KopString(ob);
                    694:        for (i=strlen(s)-1; i>=0; i--) {
1.7       takayama  695:          if ((s[i] == '\n') && (i==0) && last) {
                    696:                ++flag;
1.5       takayama  697:          }else if ((s[i] == '\n') && (s[i-1] == '\n')) {
1.7       takayama  698:                ++flag;
                    699:          }else if ((s[i] == 0xd) && (s[i+1] == 0xa) && (i==0) && last) {
                    700:                ++flag;
1.5       takayama  701:          }else if ((s[i] == 0xa) && (s[i-1] == 0xd) && (s[i+1] == 0xd) && (s[i+2] == 0xa)) {
1.7       takayama  702:                ++flag;
1.5       takayama  703:          }
                    704:        }
                    705:        if (datasize-1 <= strlen(sss)+strlen(s)) {
1.16      takayama  706:       tmp = (char *)sGC_malloc(sizeof(char)*2*(datasize+strlen(s))+1);
1.5       takayama  707:       if (tmp == (char *)NULL) errorMsg1s("Out of Memory.");
                    708:          strcpy(tmp,sss);
                    709:          strcat(tmp,s);
                    710:       datasize = 2*(datasize+strlen(s));
                    711:          sss = tmp;
                    712:        }else{
                    713:          strcat(sss,s);
                    714:        }
1.7       takayama  715:
                    716:        if ((s[strlen(s)-1] == '\n') ||
                    717:                (s[strlen(s)-2] == 0xd) && (s[strlen(s)-1] == 0xa)) {
                    718:          last = 1;
                    719:        }else last = 0;
                    720:
1.5       takayama  721:   }
                    722:
                    723:   return KpoString(sss);
                    724:
                    725: }
                    726:
1.1       maekawa   727: struct object Kplugin_sm1Socket(char *key,struct object obj) {
                    728:   struct object robj = NullObject;
                    729:   if (strcmp(key,"open") == 0) {
                    730:     robj = KsocketOpen(obj);
                    731:   }else if (strcmp(key,"connect") == 0) {
                    732:     robj = KsocketConnect(obj);
                    733:   }else if (strcmp(key,"accept") == 0) {
                    734:     robj = KsocketAccept(obj);
1.13      takayama  735:   }else if (strcmp(key,"accept2") == 0) {
                    736:     robj = KsocketAccept2(obj);
1.1       maekawa   737:   }else if (strcmp(key,"select") == 0) {
                    738:     robj = KsocketSelect(obj);
                    739:   }else if (strcmp(key,"mselect") == 0) {
                    740:     robj = KsocketSelectMulti(obj);
                    741:   }else if (strcmp(key,"read") == 0) {
                    742:     robj = KsocketRead(obj);
1.7       takayama  743:   }else if (strcmp(key,"readHTTP") == 0) {
                    744:     robj = KsocketReadHTTP(obj);
1.11      takayama  745:   }else if (strcmp(key,"gethostname") == 0) {
                    746:     robj = KsocketGetHostName();
                    747:   }else if (strcmp(key,"write") == 0) {
1.1       maekawa   748:     robj = KsocketWrite(obj);
                    749:   }else if (strcmp(key,"read") == 0) {
                    750:     robj = KsocketRead(obj);
                    751:   }else if (strcmp(key,"readByte") == 0) {
                    752:     robj = KsocketReadByte(obj);
                    753:   }else if (strcmp(key,"writeByte") == 0) {
                    754:     robj = KsocketWriteByte(obj);
                    755:   }else if (strcmp(key,"close") == 0) {
                    756:     robj = KsocketClose(obj);
                    757:   }else {
                    758:     errorMsg1s("Unknown tag for sm1.socket");
                    759:   }
                    760:   return(robj);
                    761: }
                    762:
                    763:
1.8       takayama  764: static int getContentLength(char *s) {
                    765:   int n;
1.10      takayama  766:   int i,j;
1.8       takayama  767:   int len = -1;
1.10      takayama  768:   char *s1 = "content-length:";
                    769:   char s0[256];
                    770:   int m;
                    771:   m = strlen(s1);
1.8       takayama  772:   n = strlen(s);
                    773:   for (i=0; i<n; i++) {
1.10      takayama  774:     strncpy(s0,&(s[i]),m+1);
                    775:     for (j=0; j<m; j++) {
                    776:       if ((s0[j] >= 'A') && (s0[j] <= 'Z')) s0[j] = s0[j]+0x20;
                    777:     }
                    778:     if (strncmp(s0,s1,strlen(s1)) == 0) {
                    779:       sscanf(&(s[i+strlen(s1)]),"%d",&len);
                    780:       break;
                    781:     }
1.8       takayama  782:   }
                    783:   return len;
                    784: }
                    785: static int getReceivedContentLength(char *s) {
                    786:   int n;
                    787:   int i;
                    788:   int start;
                    789:   start = -1;
                    790:   n = strlen(s);
                    791:   for (i=0; i<n; i++) {
                    792:        if ((s[i] == '\n') && (s[i+1] == '\n')) {
                    793:          start = i+2; break;
                    794:        }else if ((s[i] == 0xd) && (s[i+1] == 0xa) && (s[i+2] == 0xd) && (s[i+3] == 0xa)) {
                    795:          start = i+4;
                    796:        }
                    797:   }
                    798:   if (start == -1) return 0;
                    799:   return (n-start);
                    800: }
1.1       maekawa   801:
                    802:
1.11      takayama  803: struct object KsocketGetHostName(void) {
                    804:   char name[1024];
                    805:   char *s;
                    806:   struct object rob = NullObject;
                    807:   if (gethostname(name,1023) != 0) {
                    808:     return rob;
                    809:   }
1.16      takayama  810:   s = (char *)sGC_malloc(sizeof(char)*(strlen(name)+2));
1.11      takayama  811:   if (s == (char *)NULL) errorMsg1s("Out of Memory.");
                    812:   strcpy(s,name);
                    813:   return(KpoString(s));
                    814: }
1.1       maekawa   815:
                    816:
                    817:
                    818:
                    819:
                    820:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>