#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include "client.h"
//struct ip_options;
#define OPTLEN 40
#define length 8
#define optlength(opt) (sizeof(opt) + (opt).optlen)
const char * host_name = "127.0.0.1"; // local host
int port = 8000;
/*struct ip_options {
unsigned char faddr;
unsigned char optlen;
unsigned char srr;
unsigned char rr;
unsigned char ts;
unsigned char is_strictroute:1,
srr_is_hit:1,
is_changed:1,
rr_needaddr:1,
ts_needtime:1,
ts_needaddr:1;
unsigned char router_alert;
unsigned char cipso;
unsigned char __pad2;
// unsigned char __pad[7];
unsigned char __data[0];
}
*/
void init_opt(struct ip_options *o){
o->faddr = 1; // this value is only to be zero or one
o->optlen = 40; // this value can be changed , it control the length of IP_OPTIONS ,
o->srr = 0;
o->rr = 1;
o->ts = 0;
o->is_strictroute = 0;
o->srr_is_hit = 0;
o->is_changed = 0;
o->rr_needaddr = 1;
o->ts_needtime = 0;
o->ts_needaddr = 0;
o->router_alert = 0;
o->cipso = 0;
o->__pad2 = 1;
// bzero(o->__pad, sizeof(o->__pad));
return;
}
void main(int argc,char *argv[]){
int i,j;
int optlen=OPTLEN;
char optbuff[optlen];
char buf[8192];
char message[256];
int socket_descriptor;
struct sockaddr_in pin;
struct hostent *server_host_name;
const char * str = "A default test string";
char data[1024] = "I'm IP_OPTIONS!";
int datalen=strlen(data);
struct ip_options opt,opt_data ;
struct ip_options get_opt ;
int ret = 0;
char buff[]="hello!";
int buff_len;
int get_buff;
unsigned char test[length];
unsigned char *p=&test[0];
init_opt(&opt);
if(argc < 2){
printf("Usage:'test \"Any test string\"\n");
printf("we will send a default test string.\n");
}else{
str = argv[1];
}
if((server_host_name = gethostbyname(host_name))==0){
perror("Error resolving local host\n");
}
//scanf("%d",&buff);
//printf("%d\n",buff);
/***********socket**********/
bzero(&pin,sizeof(pin));
pin.sin_family = AF_INET;
pin.sin_addr.s_addr = htonl(INADDR_ANY);
pin.sin_addr.s_addr = ((struct in_addr *)(server_host_name->h_addr))->s_addr;
pin.sin_port = htons(port);
socket_descriptor = socket(AF_INET,SOCK_STREAM,0);
/***********setsockopt******/
memcpy(opt.__data,data,strlen(data)+1);
//memcpy(opt_data.__data,data,strlen(data)+1);
//opt.optlen=datalen;
/*printf("opt=%s, len=%d\n", opt.__data, opt.optlen);
if(setsockopt(socket_descriptor,IPPROTO_IP,IP_OPTIONS,&opt,opt.optlen)==-1)
{
perror("call the setsockopt");
exit(1);
}
*/
//test[0] = getchar();
test[0] = 148;
test[1] = 4;
test[2] = 'h';
test[3] = 'e';
/*test[4] = 148;
test[5] = 4;
test[6] = 'e';
test[7] = 0;
*/
//test[3] = 0;
/*test[4] = 2;
test[5] =148;
test[6] = 4;
test[7] = 2;
test[8] = 2;
test[9] = 2;
test[10] = 2;
test[11] = 2;
*/
/*for(i=2;i<38;i++)
{
test[i] = 192;
}
test[38] = 1;
*/
//for(i=j;i<40;i++)
//{
// test[i] = 1;
//}
/*for(i=0;i<length;i++)
{
printf("test[%d] = %d\n",i,test[i]);
}
*/
if(setsockopt(socket_descriptor,IPPROTO_IP,IP_OPTIONS,p,sizeof(p))==-1)
{
perror("call the setsockopt");
exit(1);
}
/*if(setsockopt(socket_descriptor,IPPROTO_IP,IP_OPTIONS,&opt_data,sizeof(opt_data))==-1)
{
perror("call the setsockopt=>opt_data");
exit(1);
}
*/
//{
// printf("call the setsockopt");
// exit(1);
//}
//optlen = optlength(opt);
//optlen=opt.optlen;
//getsockopt(socket_descriptor,IPPROTO_IP,IP_OPTIONS,&get_opt, &optlen);
//printf("opt=%s, len=%d\n", get_opt.__data, get_opt.optlen);
//putchar('\n');
//test
/*setsockopt(socket_descriptor,IPPROTO_IP,IP_OPTIONS,&buff,sizeof(buff));
buff_len=sizeof(get_buff);
getsockopt(socket_descriptor,IPPROTO_IP,IP_OPTIONS,&get_buff,&buff_len);
printf("opt=%d , len=%d \n",get_buff,buff_len);
*/
/**********connect*********/
connect(socket_descriptor,(void *)&pin,sizeof(pin));
printf("Sending message \"%s\" to server ...\n",str);
ret = send(socket_descriptor,str,strlen(str),0);
if (ret == -1 || ret == 0){
printf("Cannot send\n");
}
printf("OK send will be over!\n");
close(socket_descriptor);
}
评论0