/* http_post_simple - do a POST to an HTTP URL and return the values Copyright GPL 2003 by Mike Chirico <mchirico@brainsquash.org> A few things to note with this example: .. note the \r\n .. it needs a carrage return line feed strcpy(sendline,"POST /test.php HTTP/1.0\r\n"); strcat(sendline,"Host: souptonuts.sourceforge.net\r\n"); souptonuts.sourceforge.net ip address is 66.35.250.209 these need to match for it to work. strcat(sendline,"Content-length: 34\r\n\r\n"); strcat(sendline,"mode=login&user=test&password=test\r\n"); Content-length, 34, is the length of the string "mode=login&user=test&password=test" if you add your own string... you need to adjust this value. References: http://tecfa.unige.ch/moo/book2/node93.html [root@third-fl-71 root]# tcpdump src port 8081 tcpdump: listening on eth0 10:57:24.356112 192.168.1.155.tproxy > third-fl-71.localdomain.32878: S 525851741:525851741(0) ack 1480974760 win 5792 <mss 1460,sackOK,timestamp 82790685 17692834,nop,wscale 0> (DF) 10:57:24.358147 192.168.1.155.tproxy > third-fl-71.localdomain.32878: . ack 130 win 6432 <nop,nop,timestamp 82790685 17692834> (DF) 10:57:24.681157 192.168.1.155.tproxy > third-fl-71.localdomain.32878: . 1:1449(1448) ack 130 win 6432 <nop,nop,timestamp 82790717 17692834> (DF) 10:57:24.683207 192.168.1.155.tproxy > third-fl-71.localdomain.32878: . 1449:2897(1448) ack 130 win 6432 <nop,nop,timestamp 82790717 17692834> (DF) 10:57:24.686681 192.168.1.155.tproxy > third-fl-71.localdomain.32878: P 2897:4345(1448) ack 130 win 6432 <nop,nop,timestamp 82790717 17692867> (DF) 10:57:24.688116 192.168.1.155.tproxy > third-fl-71.localdomain.32878: FP 4345:5079(734) ack 130 win 6432 <nop,nop,timestamp 82790717 17692867> (DF) 10:57:24.702899 192.168.1.155.tproxy > third-fl-71.localdomain.32878: . ack 131 win 6432 <nop,nop,timestamp 82790718 17692868> (DF) Note the size of n that is returned [chirico@third-fl-71 spider]$ ./test > out 1448 1448 1448 734 */ #include <arpa/inet.h> #include <assert.h> #include <errno.h> #include <netinet/in.h> #include <signal.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/wait.h> #include <unistd.h> #define SA struct sockaddr #define MAXLINE 4096 /* Following could be derived from SOMAXCONN in <sys/socket.h>, but many kernels still #define it as 5, while actually supporting many more */ #define LISTENQ 1024 /* 2nd argument to listen() */ void process_http(int sockfd){ char sendline[MAXLINE], recvline[MAXLINE]; ssize_t n; // strcpy(sendline,"POST /chirico/test.php HTTP/1.0\r\n"); strcpy(sendline,"GET /test.php HTTP/1.0\r\n"); strcat(sendline,"Connection: Keep-Alive\r\n"); strcat(sendline,"Pragma: no-cache\r\n"); strcat(sendline,"Host: 192.168.1.155\r\n"); strcat(sendline,"Accept: www/source\r\n"); strcat(sendline,"Accept: text/html\r\n\r\n"); write(sockfd, sendline, strlen(sendline)); while ( ( n = read(sockfd, recvline, MAXLINE)) != 0 ) { recvline[n]='\0'; printf("%s",recvline); } } int main(void) { int sockfd; struct sockaddr_in servaddr; sockfd = socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(8081); inet_pton(AF_INET, "192.168.1.155", &servaddr.sin_addr); connect(sockfd, (SA *) &servaddr, sizeof(servaddr)); process_http(sockfd); exit(0); }
Linux System Admin Tips: There are over 200 Linux tips and tricks in this article. That is over 100 pages covering everything from NTP, setting up 2 IP address on one NIC, sharing directories among several users, putting running jobs in the background, find out who is doing what on your system by examining open sockets and the ps command, how to watch a file, how to prevent even root from deleting a file, tape commands, setting up cron jobs, using rsync, using screen conveniently with emacs, how to kill every process for a user, security tips and a lot more. These tip grow weekly. The above link will download the text version for easy grep searching. There is also an html version here.
Breaking Firewalls with OpenSSH and PuTTY: If the system administrator deliberately filters out all traffic except port 22 (ssh), to a single server, it is very likely that you can still gain access other computers behind the firewall. This article shows how remote Linux and Windows users can gain access to firewalled samba, mail, and http servers. In essence, it shows how openSSH and Putty can be used as a VPN solution for your home or workplace.
MySQL Tips and Tricks: Find out who is doing what in MySQL and how to kill the process, create binary log files, connect, create and select with Perl and Java, remove duplicates in a table with the index command, rollback and how to apply, merging several tables into one, updating foreign keys, monitor port 3306 with the tcpdump command, creating a C API, complex selects, and much more.
Create a Live Linux CD - BusyBox and OpenSSH Included: These steps will show you how to create a functioning Linux system, with the latest 2.6 kernel compiled from source, and how to integrate the BusyBox utilities including the installation of DHCP. Plus, how to compile in the OpenSSH package on this CD based system. On system boot-up a filesystem will be created and the contents from the CD will be uncompressed and completely loaded into RAM -- the CD could be removed at this point for boot-up on a second computer. The remaining functioning system will have full ssh capabilities. You can take over any PC assuming, of course, you have configured the kernel with the appropriate drivers and the PC can boot from a CD. This tutorial steps you through the whole processes.
SQLite Tutorial : This article explores the power and simplicity of sqlite3, first by starting with common commands and triggers, then the attach statement with the union operation is introduced in a way that allows multiple tables, in separate databases, to be combined as one virtual table, without the overhead of copying or moving data. Next, the simple sign function and the amazingly powerful trick of using this function in SQL select statements to solve complex queries with a single pass through the data is demonstrated, after making a brief mathematical case for how the sign function defines the absolute value and IF conditions.
The Lemon Parser Tutorial: This article explains how to build grammars and programs using the lemon parser, which is faster than yacc. And, unlike yacc, it is thread safe.
How to Compile the 2.6 kernel for Red Hat 9 and 8.0 and get Fedora Updates: This is a step by step tutorial on how to compile the 2.6 kernel from source.
Virtual Filesystem: Building A Linux Filesystem From An Ordinary File. You can take a disk file, format it as ext2, ext3, or reiser filesystem and then mount it, just like a physical drive. Yes, it then possible to read and write files to this newly mounted device. You can also copy the complete filesystem, since it is just a file, to another computer. If security is an issue, read on. This article will show you how to encrypt the filesystem, and mount it with ACL (Access Control Lists), which give you rights beyond the traditional read (r) write (w) and execute (x) for the 3 user groups file, owner and other.
Working With Time: What? There are 61 seconds in a minute? We can go back in time? We still tell time by the sun?
Mike Chirico, a father of triplets (all girls) lives outside of
Philadelphia, PA, USA. He has worked with Linux since 1996, has a Masters
in Computer Science and Mathematics from Villanova University, and has
worked in computer-related jobs from Wall Street to the University of
Pennsylvania. His hero is Paul Erdos, a brilliant number theorist who was
known for his open collaboration with others.
Mike's notes page is souptonuts. For
open source consulting needs, please send an email to
mchirico@gmail.com. All consulting work must include a donation to
SourceForge.net.