public
Description: Library to access lastfm via it's 2.0 api
Homepage: https://launchpad.net/liblastfm
Clone URL: git://github.com/LCID-Fire/liblastfm.git
Click here to lend your support to: liblastfm and make a donation at www.pledgie.com !
liblastfm / proxy.c
100644 764 lines (672 sloc) 17.482 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include "proxy.h"
#include "include.h"
 
EMBED_PRINT_FUNCTIONS(http_proxy, "Inprocess proxy")
 
static const guint MAX_LISTENING_BUFFER = 256;
 
/*void fd_unblock(int fd)
{
int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}*/
 
struct http_proxy
{
int in_socket;
int out_socket;
guint16 listening_port;
size_t max_threads;
GThreadPool* pool;
GThread* run_thread;
};
 
typedef struct http_proxy http_proxy;
 
static http_proxy the_proxy =
{
-1,
-1,
49152, //using only private/dynamic ports
10,
NULL,
NULL
};
 
guint16 http_proxy_get_port()
{
return the_proxy.listening_port;
}
 
GString* get_request(int fd)
{
GIOChannel* io = g_io_channel_unix_new(fd);
 
GError* error;
GIOStatus status;
 
GString* line;
 
while((status = g_io_channel_read_line_string(io, line, NULL, &error)) == G_IO_STATUS_AGAIN)
{
http_proxy_print("TRYING AGAIN");
}
 
if(error)
{
http_proxy_printerr(error->message);
return NULL;
}
 
return line;
}
 
gboolean check_socket_valid(int socket)
{
if(socket != -1)
return TRUE;
 
switch(errno)
{
case EAFNOSUPPORT:
{
         http_proxy_printerr("The implementation does not support the specified address family.");
         break;
}
case EMFILE:
{
http_proxy_printerr("No more file descriptors are available for this process.");
         break;
}
        case ENFILE:
        {
http_proxy_printerr("No more file descriptors are available for the system.");
         break;
}
case EPROTONOSUPPORT:
{
http_proxy_printerr("The protocol is not supported by the address family, or the protocol is not supported by the implementation.");
         break;
}
case EPROTOTYPE:
{
http_proxy_printerr("The socket type is not supported by the protocol.");
         break;
}
case EACCES:
{
         http_proxy_printerr("The process does not have appropriate privileges.");
         break;
}
case ENOBUFS:
{
http_proxy_printerr("Insufficient resources were available in the system to perform the operation.");
         break;
}
case ENOMEM:
{
http_proxy_printerr("Insufficient memory was available to fulfill the request.");
         break;
}
default:
{
http_proxy_printerr("Unrecognized error on socket allocation.");
         break;
}
}
 
return FALSE;
}
 
gboolean check_bind_valid(int socket, int bind)
{
if(bind != -1)
return TRUE;
 
switch(errno)
{
case EACCES:
{
http_proxy_printerr("The address is protected, and the user is not the superuser.");
break;
}
case EADDRINUSE:
{
http_proxy_printerr("The given address is already in use");
break;
}
case EBADF:
{
http_proxy_printerr("sockfd '%d' is not a valid descriptor. ", socket);
break;
}
case EINVAL:
{
http_proxy_printerr("The socket '%d' is already bound to an address", socket);
break;
}
case ENOTSOCK:
{
http_proxy_printerr("sockfd '%d' is a descriptor for a file, not a socket.", socket);
break;
}
default:
{
http_proxy_printerr("Unrecognized error '%d' on socket '%d'", errno, socket);
break;
}
}
 
return FALSE;
}
 
gboolean check_listen_valid(int listen)
{
if(listen != -1)
return TRUE;
 
switch(errno)
{
case EADDRINUSE:
{
http_proxy_printerr("Another socket is already listening on the same port.");
break;
}
case EBADF:
{
http_proxy_printerr("The argument sockfd '%d' is not a valid descriptor.", the_proxy.in_socket);
break;
}
case ENOTSOCK:
{
http_proxy_printerr("The argument sockfd '%d' is not a socket.", the_proxy.in_socket);
break;
}
case EOPNOTSUPP:
{
http_proxy_printerr("The socket '%d' is not of a type that supports the listen () operation.", the_proxy.in_socket);
break;
}
default:
{
http_proxy_printerr("Unrecognized error '%d' on listening on socket '%d'", errno, the_proxy.in_socket);
break;
}
}
 
return FALSE;
}
 
gboolean check_connect_valid(int connect)
{
if(connect != -1)
return TRUE;
 
switch(errno)
{
case EADDRNOTAVAIL:
{
http_proxy_printerr("The specified address is not available from the local machine.");
break;
}
case EAFNOSUPPORT:
{
http_proxy_printerr("The specified address is not a valid address for the address family of the specified socket.");
break;
}
case EALREADY:
{
http_proxy_printerr("A connection request is already in progress for the specified socket.");
break;
}
case EBADF:
{
http_proxy_printerr("The socket argument is not a valid file descriptor.");
break;
}
case ECONNREFUSED:
{
http_proxy_printerr("The target address was not listening for connections or refused the connection request.");
break;
}
case EINPROGRESS:
{
http_proxy_printerr("O_NONBLOCK is set for the file descriptor for the socket and the connection cannot be immediately established; the connection shall be established asynchronously.");
break;
}
case EINTR:
{
http_proxy_printerr("The attempt to establish a connection was interrupted by delivery of a signal that was caught; the connection shall be established asynchronously.");
break;
}
case EISCONN:
{
http_proxy_printerr("The specified socket is connection-mode and is already connected.");
break;
}
case ENETUNREACH:
{
http_proxy_printerr("No route to the network is present.");
break;
}
case ENOTSOCK:
{
http_proxy_printerr("The socket argument does not refer to a socket.");
break;
}
case EPROTOTYPE:
{
http_proxy_printerr("The specified address has a different type than the socket bound to the specified peer address.");
break;
}
    case ETIMEDOUT:
{
http_proxy_printerr("The attempt to connect timed out before a connection was made.");
break;
}
case EACCES:
{
http_proxy_printerr("Search permission is denied for a component of the path prefix; or write access to the named socket is denied.");
break;
}
case EADDRINUSE:
{
http_proxy_printerr("Attempt to establish a connection that uses addresses that are already in use.");
break;
}
case ECONNRESET:
{
http_proxy_printerr("Remote host reset the connection request.");
break;
}
case EHOSTUNREACH:
{
http_proxy_printerr("The destination host cannot be reached (probably because the host is down or a remote router cannot reach it).");
break;
}
case EINVAL:
{
http_proxy_printerr("The address_len argument is not a valid length for the address family; or invalid address family in the sockaddr structure.");
break;
}
case ELOOP:
{
http_proxy_printerr("More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the pathname in address.");
break;
}
case ENAMETOOLONG:
{
http_proxy_printerr("Pathname resolution of a symbolic link produced an intermediate result whose length exceeds {PATH_MAX}.");
break;
}
case ENETDOWN:
{
http_proxy_printerr("The local network interface used to reach the destination is down.");
break;
}
case ENOBUFS:
{
http_proxy_printerr("No buffer space is available.");
break;
}
case EOPNOTSUPP:
{
http_proxy_printerr("The socket is listening and cannot be connected.");
break;
}
default:
{
http_proxy_printerr("Unrecognized connect error '%d'", errno);
break;
}
}
 
return FALSE;
}
 
gboolean connect_request(int request)
{
uint16_t port;
 
struct sockaddr_in destination;
destination.sin_family = AF_INET;
destination.sin_port = htons(port);
 
/* Lookup and return the address if possible */
//ret = lookup_domain(&port_info.sin_addr, ip_addr);
 
int output = socket(AF_INET, SOCK_STREAM, 0);
 
gboolean success = check_socket_valid(output);
 
if(!success)
return FALSE;
 
/*
struct sockaddr_in bind_addr;
bind_addr.sin_family = AF_INET;
bind_addr.sin_addr.s_addr = inet_addr(config.bind_address);
ret = bind(sock_fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
*/
 
int passalong = connect(output, (struct sockaddr*)&destination, sizeof(destination));
 
success = check_connect_valid(passalong);
 
if(!success)
return FALSE;
 
/*
fd_set rset, wset;
struct timeval tv;
time_t last_access;
int ret;
double tdiff;
int maxfd = max(connptr->client_fd, connptr->server_fd) + 1;
ssize_t bytes_received;
 
socket_nonblocking(connptr->client_fd);
socket_nonblocking(connptr->server_fd);
 
last_access = time(NULL);
 
for (;;) {
FD_ZERO(&rset);
FD_ZERO(&wset);
 
tv.tv_sec =
config.idletimeout - difftime(time(NULL), last_access);
tv.tv_usec = 0;
 
if (buffer_size(connptr->sbuffer) > 0)
FD_SET(connptr->client_fd, &wset);
if (buffer_size(connptr->cbuffer) > 0)
FD_SET(connptr->server_fd, &wset);
if (buffer_size(connptr->sbuffer) < MAXBUFFSIZE)
FD_SET(connptr->server_fd, &rset);
if (buffer_size(connptr->cbuffer) < MAXBUFFSIZE)
FD_SET(connptr->client_fd, &rset);
 
ret = select(maxfd, &rset, &wset, NULL, &tv);
 
if (ret == 0) {
tdiff = difftime(time(NULL), last_access);
if (tdiff > config.idletimeout) {
log_message(LOG_INFO,
"Idle Timeout (after select) as %g > %u.",
tdiff, config.idletimeout);
return;
} else {
continue;
}
} else if (ret < 0) {
log_message(LOG_ERR,
"relay_connection: select() error \"%s\". Closing connection (client_fd:%d, server_fd:%d)",
strerror(errno), connptr->client_fd,
connptr->server_fd);
return;
} else {
// All right, something was actually selected so mark it.
last_access = time(NULL);
}
 
if (FD_ISSET(connptr->server_fd, &rset)) {
bytes_received = read_buffer(connptr->server_fd, connptr->sbuffer);
if (bytes_received < 0)
break;
 
connptr->content_length.server -= bytes_received;
if (connptr->content_length.server == 0)
break;
}
if (FD_ISSET(connptr->client_fd, &rset)
&& read_buffer(connptr->client_fd, connptr->cbuffer) < 0) {
break;
}
if (FD_ISSET(connptr->server_fd, &wset)
&& write_buffer(connptr->server_fd, connptr->cbuffer) < 0) {
break;
}
if (FD_ISSET(connptr->client_fd, &wset)
&& write_buffer(connptr->client_fd, connptr->sbuffer) < 0) {
break;
}
}
 
 
// Here the server has closed the connection... write the
// remainder to the client and then exit.
 
socket_blocking(connptr->client_fd);
while (buffer_size(connptr->sbuffer) > 0) {
if (write_buffer(connptr->client_fd, connptr->sbuffer) < 0)
break;
}
shutdown(connptr->client_fd, SHUT_WR);
 
 
// Try to send any remaining data to the server if we can.
socket_blocking(connptr->server_fd);
while (buffer_size(connptr->cbuffer) > 0) {
if (write_buffer(connptr->server_fd, connptr->cbuffer) < 0)
break;
}
*/
 
return TRUE;
}
 
void process_request(gpointer data, gpointer user_data)
{
int request = *((int*)user_data);
 
GString* string = get_request(request);
 
http_proxy_print("Request gotten: %s", string->str);
}
 
gpointer http_proxy_run(gpointer data)
{
http_proxy* proxy = data;
 
GError* error = NULL;
 
proxy->pool = g_thread_pool_new(process_request, proxy, proxy->max_threads, FALSE, &error);
 
gboolean success = TRUE;
 
while(success)
{
struct sockaddr_in address;
socklen_t address_size = sizeof(struct sockaddr_in);
int request = accept(the_proxy.in_socket, (struct sockaddr*)&address, &address_size);
 
success = (request == -1);
 
if(!success)
{
switch(errno)
{
case EAGAIN: //EWOULDBLOCK
{
//The socket is marked non-blocking and no connections are present to be accepted.
success = TRUE;
continue;
}
case EBADF:
{
http_proxy_printerr("The descriptor is invalid.");
break;
}
case ECONNABORTED:
{
http_proxy_printerr("Aproxy connection has been aborted.");
break;
}
case EINTR:
{
http_proxy_printerr("The system call was interrupted by a signal that was caught before a valid connection arrived.");
break;
}
case EINVAL:
{
http_proxy_printerr("Socket is not listening for connections, or addrlen is invalid (e.g., is negative).");
break;
}
case EMFILE:
{
http_proxy_printerr("The per-process limit of open file descriptors has been reached.");
break;
}
case ENFILE:
{
http_proxy_printerr("The system limit on the total number of open files has been reached.");
break;
}
case ENOTSOCK:
{
http_proxy_printerr("The descriptor references a file, not a socket.");
break;
}
case EOPNOTSUPP:
{
http_proxy_printerr("The referenced socket is not of type SOCK_STREAM.");
break;
}
case EFAULT:
{
http_proxy_printerr("The addr argument is not in a writable part of the user address space.");
break;
}
case ENOBUFS:
case ENOMEM:
{
http_proxy_printerr("Not enough free memory. This often means that the memory allocation is limited by the socket buffer limits, not by the system memory.");
break;
}
case EPROTO:
{
http_proxy_printerr("Protocol error.");
break;
}
case EPERM:
{
http_proxy_printerr("Firewall rules forbid connection.");
break;
}
default:
{
http_proxy_printerr("Unrecognized error when trying to accept.");
break;
}
}
}
 
if(success)
{
GError* error = NULL;
g_thread_pool_push(proxy->pool, &request, &error);
 
if(error)
{
http_proxy_printerr(error->message);
success = FALSE;
}
}
}
 
g_thread_pool_free(the_proxy.pool, TRUE, TRUE);
 
http_proxy_print("Stopped listening");
return NULL;
}
 
gboolean http_proxy_init()
{
//AF_INET??
the_proxy.in_socket = socket(PF_INET, SOCK_STREAM, 0);
//fd_unblock(the_proxy.in_socket);
 
if(!check_socket_valid(the_proxy.in_socket))
return FALSE;
 
struct sockaddr_in in_address;
  in_address.sin_family = AF_INET;
  in_address.sin_addr.s_addr = INADDR_ANY;
  in_address.sin_port = the_proxy.listening_port;
 
  gboolean success = FALSE;
 
  while(!success)
  {
  int result = bind(the_proxy.in_socket, (struct sockaddr*)&in_address, sizeof(struct sockaddr_in));
   success = (result != -1);
 
  if(!success)
  {
switch(errno)
{
case EACCES: //The address is protected, and the user is not the superuser.
case EADDRINUSE: //The given address is already in use
break; //ignore
case EINVAL:
{
success = TRUE; //The socket is already bound to an address
break;
}
default:
{
check_bind_valid(the_proxy.in_socket, result);
break;
}
}
}
 
if(success)
{
result = listen(the_proxy.in_socket, MAX_LISTENING_BUFFER);
success = (result != -1);
 
if(!success)
  {
  switch(errno)
{
case EADDRINUSE: //Another socket is already listening on the same port.
break; //ignore
default:
{
check_listen_valid(result);
break;
}
}
  }
}
 
  if(!success)
  {
//cannot try any other port
  if(the_proxy.listening_port == G_MAXUINT16)
  return FALSE;
 
  the_proxy.listening_port++;
  in_address.sin_port = the_proxy.listening_port;
  }
  }
 
  http_proxy_print("Starting up on Port %u", the_proxy.listening_port);
 
  GError* error = NULL;
 
  if(!g_thread_supported())
  g_thread_init(NULL);
 
  the_proxy.run_thread = g_thread_create(http_proxy_run, &the_proxy, FALSE, &error);
 
  if(error)
  {
  http_proxy_printerr(error->message);
  return FALSE;
  }
 
  return TRUE;
}
 
gboolean http_proxy_set_requests(size_t request_count)
{
GError* error = NULL;
g_thread_pool_set_max_threads(the_proxy.pool, request_count, &error);
 
if(error)
{
http_proxy_printerr(error->message);
return FALSE;
}
 
return TRUE;
}
 
guint get_peer_address(int fd)
{
struct sockaddr_in address;
guint address_size;
 
gboolean success = getpeername(fd, (struct sockaddr *)&address, &address_size) != -1;
 
if(!success)
{
switch(errno)
{
case EBADF:
{
     http_proxy_printerr("The socket argument is not a valid file descriptor.");
     break;
}
case EFAULT:
{
http_proxy_printerr("The address or address_len parameter can not be accessed or written.");
     break;
}
case EINVAL:
{
http_proxy_printerr("The socket has been shut down.");
break;
}
case ENOTCONN:
{
http_proxy_printerr("The socket is not connected or otherwise has not had the peer prespecified.");
break;
}
case ENOTSOCK:
{
http_proxy_printerr("The socket argument does not refer to a socket.");
break;
}
case EOPNOTSUPP:
{
     http_proxy_printerr("The operation is not supported for the socket protocol.");
break;
}
case ENOBUFS:
{
http_proxy_printerr("Insufficient resources were available in the system to complete the call.");
break;
}
case ENOSR:
{
http_proxy_printerr("There were insufficient STREAMS resources available for the operation to complete.");
break;
}
default:
{
http_proxy_printerr("Unrecognized error '%d' when retrieving peer name.", errno);
break;
}
}
}
 
if(success)
return address.sin_addr.s_addr;
 
return 0;
}
 
void http_proxy_destroy()
{
close(the_proxy.in_socket);
close(the_proxy.out_socket);
}