Seeking for help with C Gemini client

Paul Warren pwarren at pwarren.id.au
Wed Aug 19 12:36:18 BST 2020


G'day!

Gemini, while simple, isn't quite that simple. All protocol commands and
responses are over TLS connections, so doing a send/recv on a raw
network socket will not work!

I'm not 100% sure on the best or simplest way to get a TLS stack going
in C, I've done some bits with OpenSSL, but it was not simple!

Cheers
--
Paul



On 19/8/20 11:57 pm, - wrote:
> Hello,
> 
> I've started writing a client for Gemini protocol, but since I've never been writing networking programs, I find myself at the dead end at the moment. Please could someone help me? When I send a request and get a response, the only thing I have in response is "gemini://" and nothing else. The code is given below:
> 
> #include <arpa/inet.h>
> #include <locale.h>
> #include <netdb.h>
> #include <netinet/in.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <sys/types.h>
> #include <sys/socket.h>
> #include <unistd.h>
> 
> #define STATUS_CODE_INPUT 10
> #define STATUS_CODE_SENSITIVE_INPUT 11
> #define STATUS_CODE_SUCCESS 20
> #define STATUS_CODE_REDIRECT_TEMP 30
> #define STATUS_CODE_REDIRECT_PERM 31
> #define STATUS_CODE_TEMP_FAILURE 40
> #define STATUS_CODE_SERVER_UNAVAILABLE 41
> #define STATUS_CODE_CGI_ERROR 42
> #define STATUS_CODE_PROXY_ERROR 43
> #define STATUS_CODE_SLOW_DOWN 44
> #define STATUS_CODE_PERM_FAILURE 50
> #define STATUS_CODE_NOT_FOUND 51
> #define STATUS_CODE_GONE 52
> #define STATUS_CODE_PROXY_REQUEST_REFUSED 53
> #define STATUS_CODE_BAD_REQUEST 59
> #define STATUS_CODE_CLIENT_CERT_REQUIRED 60
> #define STATUS_CODE_CERT_NO_AUTH 61
> #define STATUS_CODE_CERT_NOT_VALID 62
> 
> #define GEMINI_PORT 1965
> 
> int main() {
> 	char *locale;
> 	locale = setlocale(LC_ALL, "");
> 	
> 	int sockfd = socket(AF_INET, SOCK_STREAM, 0);
> 	if (sockfd < 0) {
> 		perror("socket");
> 		exit(1);
> 	}
> 	char *url = malloc(sizeof(char)*1024);
> 	strcpy(url, "gemini://gemini.circumlunar.space/\r\n");
> 	char rec_buf[1029] = {0};
> 	struct hostent *host_entry;
> 	host_entry = gethostbyname(url);
> 	struct sockaddr_in addr;
> 	addr.sin_family = AF_INET;
> 	addr.sin_port = htons(GEMINI_PORT);
> 	addr.sin_addr.s_addr = htonl(INADDR_ANY);
> 	int opt = 1;
> 	if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
> 		perror("setsockopt");
> 		exit(1);
> 	}
> 	if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
> 		perror("bind");
> 		exit(2);
> 	}
> 	if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
> 		perror("connect");
> 		exit(3);
> 	}
> 	send(sockfd, url, sizeof(url), 0);
> 	recv(sockfd, rec_buf, sizeof(rec_buf), 0);
> 	printf("%s\n", rec_buf);
> 	close(sockfd);
> 	shutdown(sockfd, SHUT_RDWR);
> 	free(url);
> 	return 0;
> }
> 
> 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 1003 bytes
Desc: OpenPGP digital signature
URL: <https://lists.orbitalfox.eu/archives/gemini/attachments/20200819/d18da0bc/attachment.sig>


More information about the Gemini mailing list