First commit

master
Aadhavan Srinivasan 2 years ago
commit 23a94857d0

@ -0,0 +1,4 @@
main: main.c
gcc main.c -o main -L/usr/lib -lXm -lXt -lX11 -l:libXmHTML.so.1.1.10

@ -0,0 +1,104 @@
#include "easysock.h"
int create_socket(int network, char transport) {
int domain;
int type;
if (network == 4) {
domain = AF_INET;
} else if (network == 6) {
domain = AF_INET6;
} else {
return -1;
}
if (transport == 'T') {
type = SOCK_STREAM;
} else if (transport == 'U') {
type = SOCK_DGRAM;
} else {
return -1;
}
int newSock = socket(domain,type,0);
return newSock;
}
void create_addr(int network, char* address, int port,struct sockaddr* dest) {
if (network == 4) {
struct sockaddr_in listen_address;
listen_address.sin_family = AF_INET;
listen_address.sin_port = htons(port);
inet_pton(AF_INET,address,&listen_address.sin_addr);
memcpy(dest,&listen_address,sizeof(listen_address));
return;
} else if (network == 6) {
struct sockaddr_in6 listen_ipv6;
listen_ipv6.sin6_family = AF_INET6;
listen_ipv6.sin6_port = htons(port);
inet_pton(AF_INET6,address,&listen_ipv6.sin6_addr);
memcpy(dest,&listen_ipv6,sizeof(listen_ipv6));
return;
} else {
exit(202);
}
}
int create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct) {
int socket = create_socket(network,transport);
if (socket < 0) {
exit(errno);
}
create_addr(network,address,port,addr_struct);
int addrlen;
if (network == 4) {
addrlen = sizeof(struct sockaddr_in);
} else if (network == 6) {
addrlen = sizeof(struct sockaddr_in6);
} else {
exit(207);
}
/* The value of addrlen should be the size of the 'sockaddr'.
This should be set to the size of 'sockaddr_in' for IPv4, and 'sockaddr_in6' for IPv6.
See https://stackoverflow.com/questions/73707162/socket-bind-failed-with-invalid-argument-error-for-program-running-on-macos */
int i = bind (socket,addr_struct,(socklen_t)addrlen);
if (i < 0) {
exit(errno);
}
return socket;
}
int create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct) {
int socket = create_socket(network,transport);
if (socket < 0) {
exit(errno);
}
create_addr(network,address,port,remote_addr_struct);
int addrlen;
if (network == 4) {
addrlen = sizeof(struct sockaddr_in);
} else if (network == 6) {
addrlen = sizeof(struct sockaddr_in6);
} else {
exit(207);
}
/* The value of addrlen should be the size of the 'sockaddr'.
This should be set to the size of 'sockaddr_in' for IPv4, and 'sockaddr_in6' for IPv6.
See https://stackoverflow.com/questions/73707162/socket-bind-failed-with-invalid-argument-error-for-program-running-on-macos */
int i = connect(socket,remote_addr_struct,(socklen_t)addrlen);
if (i < 0) {
exit(errno);
}
return socket;
}

@ -0,0 +1,54 @@
#ifndef EASYSOCK_H_
#define EASYSOCK_H_
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
/* This function takes:
a layer 3 - network layer - integer, which must be '4' for IPv4
and 6 for IPv6; and
a layer 4 - transport layer - character, which must be 'T' for
TCP or 'U' for UDP.
It returns the created socket, or -1 if the socket creation failed.*/
int create_socket(int network, char transport);
/* This function fills in the sockaddr struct 'dest' based on the given information.
'network' is an integer that contains '4' for IPv4 or '6' for IPv6;
'address' is the address that is filled into the struct;
port is self-explanatory;
and dest is a pointer to the sockaddr struct that will be filled in.
The function exits with error code 202 if the network parameter contained neither '4'
nor '6'. */
void create_addr(int network, char* address, int port,struct sockaddr* dest);
/* This function utilizes the above two functions; it creates the socket and
_binds_ the addresses. It is used for local sockets (server sockets). Parameters are
same as above.
It prints the error returned by 'bind' if something went wrong, and exits with errno.*/
int create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct);
/* This function utilizes the same functions as 'create_local' but _connects_ to the
requested address. It is used for remote sockets (client sockets). The paramters are same
as above. This function needs an empty 'sockaddr *' structure passed to it, which it will fill.
It prints the error returned by 'connect' if something went wrong, and exits with errno.*/
int create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct);
#endif

@ -0,0 +1,67 @@
#include <Xm/Xm.h>
#include <stdio.h>
#include <stdlib.h>
#include <Xm/PushB.h>
#include <Xm/TextF.h>
#include <Xm/Form.h>
#include <XmHTML/XmHTML.h>
#include "easysock.h"
void testFunc(Widget,XtPointer,XmPushButtonCallbackStruct *);
int main(int argc,char** argv) {
Widget window,top_wid,search_bar,search,result;
XtAppContext app;
top_wid = XtVaAppInitialize(&app,
"Web Browser",
NULL,0,&argc,argv,NULL,NULL);
window = XtVaCreateManagedWidget("Main Window",
xmFormWidgetClass,
top_wid,
NULL);
search = XtVaCreateManagedWidget("Search",
xmPushButtonWidgetClass,
window,
XmNtopAttachment,XmATTACH_FORM,
XmNrightAttachment,XmATTACH_FORM,
NULL);
search_bar = XtVaCreateManagedWidget("Search Bar",
xmTextFieldWidgetClass,
window,
XmNleftAttachment,XmATTACH_FORM,
XmNtopAttachment,XmATTACH_FORM,
XmNrightAttachment,XmATTACH_WIDGET,XmNrightWidget,search,NULL);
result = XtVaCreateManagedWidget("HTML Result",
xmHTMLWidgetClass,
window,
XmNbottomAttachment,XmATTACH_FORM,
XmNrightAttachment,XmATTACH_FORM,
XmNleftAttachment,XmATTACH_FORM,
XmNtopAttachment,XmATTACH_WIDGET,XmNtopWidget,search_bar,NULL);
int val = 5;
Widget widget_array[] = {search_bar,result};
XtAddCallback(search,XmNactivateCallback,testFunc,widget_array);
XtRealizeWidget(top_wid);
XtAppMainLoop(app);
}
void testFunc(Widget w, XtPointer client_data, XmPushButtonCallbackStruct *callback_struct) {
Widget* widget_list = (Widget *)client_data;
Widget result_widget = widget_list[1];
Widget text_widget = widget_list[0];
XmHTMLTextSetString(result_widget,"");
// printf("%d\n",val);
// exit(2);
}
Loading…
Cancel
Save