cgi_soap.c

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /* cgi_soap.cpp © Copyright 2004-2007 by Raosoft Inc. All Rights Reserved.    */
00003 /*                                                                            */
00004 /* You may use and modify this file for your own use, but may not distribute  */
00005 /* it or derivative works without the prior written consent of Raosoft, Inc.  */
00006 /*                                                                            */
00007 /* If you choose to share your modifications with Raosoft, Inc. the company   */
00008 /* will attempt to incorporate them into future versions of this file.        */
00009 /*                                                                            */
00010 /* This software is provided "as is," and Raosoft makes no warranty, express  */
00011 /* or implied, of fitness for a particular application. Every measure has been*/
00012 /* taken to anticipate risks inherent to computer networks, but we cannot     */
00013 /* guarantee safety or reliability of this program in every situation.        */
00014 /*                                                                            */
00015 /******************************************************************************/
00016 
00017 #include "cgi.h"
00018 
00019 #ifdef XP_WIN
00020 #include <winsock2.h>
00021 #else
00022 #include <sys/types.h>
00023 #include <sys/socket.h>
00024 #include <netinet/in.h>
00025 #include <arpa/inet.h>
00026 #include <netdb.h>
00027 #define INVALID_SOCKET 0xFFFFFFFF
00028 #define SOCKADDR struct sockaddr
00029 #endif
00030 
00031 int WinsockConnect(char* host, int port, unsigned int*s)
00032 {
00033  long int hostaddr = 0;
00034  unsigned long *p;
00035  struct sockaddr_in svraddr;
00036  struct hostent *he;
00037  *s = INVALID_SOCKET;
00038 
00039  if (isdigit(host[0]))        /* something like 192.168.0.1 */
00040    {
00041       hostaddr = inet_addr(host);      /* convert to 32bit unsigned int */
00042    }
00043  else                         /* something like phoenix.ttdev.com */
00044    {
00045       he = gethostbyname(host); /* convert to 32bit unsigned int */
00046       if (!he) {return 62;}
00047       p = (unsigned long*)he->h_addr_list[0];
00048       hostaddr = *p;
00049    }
00050 
00051    *s = socket(AF_INET, SOCK_STREAM, 0); /* make Internet socket */
00052 
00053    if (*s == INVALID_SOCKET) return 63;
00054 
00055    svraddr.sin_family = AF_INET;       /* Internet address family */
00056    svraddr.sin_port = htons(port);     /* port */
00057    memcpy(&svraddr.sin_addr.s_addr, &hostaddr, sizeof(INADDR_ANY)); /* host addr */
00058    memset(svraddr.sin_zero,0,sizeof(svraddr.sin_zero));
00059 
00060    if (connect(*s, (SOCKADDR*)&svraddr, sizeof(svraddr)) != 0)
00061     return 64;
00062 
00063    return 0;
00064 }
00065 
00066 int SOAPExec(char* host, int port, char* function, char* action, char* data)
00067 {
00068  unsigned int s;
00069 #ifdef XP_WIN
00070  WSADATA d;
00071 #endif
00072  int err;
00073  char temp[128];
00074 
00075 #if 0
00076 #define send(a,b,c,d) fwrite(b,c,1,stdout)
00077 #endif
00078 
00079 #ifdef XP_WIN
00080  if (WSAStartup(2, &d) != 0)     /* init winsock lib. want v2.0 */
00081   {
00082    WSACleanup();
00083    return 61;
00084   }
00085 #endif
00086 
00087  err = WinsockConnect(host, port, &s);
00088  if (!err)
00089  {
00090   send(s, "POST /", 6, 0);
00091   send(s, function, strlen(function), 0);
00092   send(s, " HTTP/1.1\r\n", 11, 0);
00093   send(s, "Host: ", 6, 0);
00094   send(s, host, strlen(host), 0);
00095   send(s, "\r\nContent-Type: text/xml; charset=\"utf-8\"\r\n", 43, 0);
00096   send(s, "Content-Length: ", 16, 0);
00097   sprintf(temp,"%d",strlen(data));  
00098 /* itoa(strlen(data), temp, 10); */
00099   send(s, temp, strlen(temp), 0);
00100   send(s, "\r\nSOAPAction: " , 14, 0);
00101   send(s, action, strlen(action), 0);
00102   send(s, "\r\n\r\n", 4, 0);
00103   send(s, data, strlen(data), 0);
00104  }
00105 
00106  if (s != INVALID_SOCKET) /* shutdown and close the socket if needed */
00107    { shutdown(s, 2);   /* shutdown both channels (read+write) */
00108 #ifdef XP_WIN
00109      closesocket(s);
00110 #else
00111      close(s);
00112 #endif
00113    }
00114 #ifdef XP_WIN
00115  WSACleanup();
00116 #endif
00117  return err;
00118 }


Raosoft, Inc.
Raosoft EZReport, EZSurvey, InterForm, RapidReport, Raosoft, and SurveyWin are registered trademarks of Raosoft, Inc. Page contents © 1996-2007 by Raosoft, Inc. You may use and modify this file for your own use, but may not distribute it or derivative works without the prior written consent of Raosoft, Inc. This software is provided "as is," and Raosoft makes no warranty, express or implied, of fitness for a particular application. Every measure has been taken to anticipate risks inherent to computer networks, but we cannot guarantee safety or reliability of this program in every situation.
Tel: 206-525-4025 (US) Email: raosoft@raosoft.com
http://www.raosoft.com/